do not use full path in diff-index Currently, diff_index passes the full relative path from the repository root to the callback. In case of an addition, it passes the tree entry instead of the index entry. This change fixes the path used for addition, and it passes only the basename of the path. This mimics the current behavior of git_tree_diff.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
diff --git a/src/tree.c b/src/tree.c
index 50a9afa..8bc17d9 100644
--- a/src/tree.c
+++ b/src/tree.c
@@ -959,15 +959,22 @@ static int cmp_tentry_ientry(git_tree_entry *tentry, git_index_entry *ientry)
return git_oid_cmp(&tentry->oid, &ientry->oid);
}
-static void make_tentry(git_tree_entry *tentry, git_index_entry *ientry, git_buf *buf)
+static void make_tentry(git_tree_entry *tentry, git_index_entry *ientry)
{
+ char *last_slash;
+
memset(tentry, 0x0, sizeof(git_tree_entry));
tentry->attr = ientry->mode;
+
+ last_slash = strrchr(ientry->path, '/');
+ if (last_slash)
+ last_slash++;
+ else
+ last_slash = ientry->path;
+ tentry->filename = last_slash;
+
git_oid_cpy(&tentry->oid, &ientry->oid);
- if (buf != NULL) {
- tentry->filename = buf->ptr;
- tentry->filename_len = buf->size;
- }
+ tentry->filename_len = strlen(tentry->filename);
}
static int diff_index_cb(const char *root, git_tree_entry *tentry, void *data)
@@ -991,25 +998,24 @@ static int diff_index_cb(const char *root, git_tree_entry *tentry, void *data)
/* Like with 'git diff-index', the index is the right side*/
cmp = strcmp(git_buf_cstr(&fn_buf), ientry->path);
+ git_buf_free(&fn_buf);
if (cmp == 0) {
cbdata->i++;
if (!cmp_tentry_ientry(tentry, ientry))
goto exit;
/* modification */
- make_tentry(&fake_entry, ientry, &fn_buf);
+ make_tentry(&fake_entry, ientry);
if ((error = signal_modification(tentry, &fake_entry, cbdata->cb, cbdata->data)) < 0)
goto exit;
} else if (cmp < 0) {
/* deletion */
memcpy(&fake_entry, tentry, sizeof(git_tree_entry));
- fake_entry.filename = fn_buf.ptr;
- fake_entry.filename_len = fn_buf.size;
if ((error = signal_deletion(tentry, cbdata->cb, cbdata->data)) < 0)
goto exit;
} else {
/* addition */
cbdata->i++;
- make_tentry(&fake_entry, ientry, &fn_buf);
+ make_tentry(&fake_entry, ientry);
if ((error = signal_addition(&fake_entry, cbdata->cb, cbdata->data)) < 0)
goto exit;
/*
@@ -1022,7 +1028,6 @@ static int diff_index_cb(const char *root, git_tree_entry *tentry, void *data)
}
exit:
- git_buf_free(&fn_buf);
return error;
}