From 42f5ba5bb6648c16a3c90a0110fbdb430e590a1b Mon Sep 17 00:00:00 2001 From: Jeff King Date: Fri, 19 May 2017 08:57:30 -0400 Subject: diff: pass whole pending entry in blobinfo When diffing blobs directly, git-diff picks the blobs out of the rev_info's pending array and copies the relevant bits to a custom "struct blobinfo". But the pending array entry already has all of this information (and more, which we'll use in future patches). Let's just pass the original entry instead. In practice, these two blobs are probably adjacent in the revs->pending array, and we could just pass the whole array. But the current code is careful to pick each blob out separately and put it into another array, so we'll continue to do so and make our own array-of-pointers. Signed-off-by: Jeff King Signed-off-by: Junio C Hamano --- builtin/diff.c | 38 +++++++++++++++----------------------- 1 file changed, 15 insertions(+), 23 deletions(-) (limited to 'builtin/diff.c') diff --git a/builtin/diff.c b/builtin/diff.c index d184aafab9..8b276ae575 100644 --- a/builtin/diff.c +++ b/builtin/diff.c @@ -20,12 +20,6 @@ #define DIFF_NO_INDEX_EXPLICIT 1 #define DIFF_NO_INDEX_IMPLICIT 2 -struct blobinfo { - struct object_id oid; - const char *name; - unsigned mode; -}; - static const char builtin_diff_usage[] = "git diff [] [ []] [--] [...]"; @@ -65,7 +59,7 @@ static void stuff_change(struct diff_options *opt, static int builtin_diff_b_f(struct rev_info *revs, int argc, const char **argv, - struct blobinfo *blob) + struct object_array_entry **blob) { /* Blob vs file in the working tree*/ struct stat st; @@ -84,12 +78,12 @@ static int builtin_diff_b_f(struct rev_info *revs, diff_set_mnemonic_prefix(&revs->diffopt, "o/", "w/"); - if (blob[0].mode == S_IFINVALID) - blob[0].mode = canon_mode(st.st_mode); + if (blob[0]->mode == S_IFINVALID) + blob[0]->mode = canon_mode(st.st_mode); stuff_change(&revs->diffopt, - blob[0].mode, canon_mode(st.st_mode), - &blob[0].oid, &null_oid, + blob[0]->mode, canon_mode(st.st_mode), + &blob[0]->item->oid, &null_oid, 1, 0, path, path); diffcore_std(&revs->diffopt); @@ -99,24 +93,24 @@ static int builtin_diff_b_f(struct rev_info *revs, static int builtin_diff_blobs(struct rev_info *revs, int argc, const char **argv, - struct blobinfo *blob) + struct object_array_entry **blob) { unsigned mode = canon_mode(S_IFREG | 0644); if (argc > 1) usage(builtin_diff_usage); - if (blob[0].mode == S_IFINVALID) - blob[0].mode = mode; + if (blob[0]->mode == S_IFINVALID) + blob[0]->mode = mode; - if (blob[1].mode == S_IFINVALID) - blob[1].mode = mode; + if (blob[1]->mode == S_IFINVALID) + blob[1]->mode = mode; stuff_change(&revs->diffopt, - blob[0].mode, blob[1].mode, - &blob[0].oid, &blob[1].oid, + blob[0]->mode, blob[1]->mode, + &blob[0]->item->oid, &blob[1]->item->oid, 1, 1, - blob[0].name, blob[1].name); + blob[0]->name, blob[1]->name); diffcore_std(&revs->diffopt); diff_flush(&revs->diffopt); return 0; @@ -259,7 +253,7 @@ int cmd_diff(int argc, const char **argv, const char *prefix) struct rev_info rev; struct object_array ent = OBJECT_ARRAY_INIT; int blobs = 0, paths = 0; - struct blobinfo blob[2]; + struct object_array_entry *blob[2]; int nongit = 0, no_index = 0; int result = 0; @@ -408,9 +402,7 @@ int cmd_diff(int argc, const char **argv, const char *prefix) } else if (obj->type == OBJ_BLOB) { if (2 <= blobs) die(_("more than two blobs given: '%s'"), name); - hashcpy(blob[blobs].oid.hash, obj->oid.hash); - blob[blobs].name = name; - blob[blobs].mode = entry->mode; + blob[blobs] = entry; blobs++; } else { -- cgit v1.2.3 From d04ec74b17138733463c0ca1024fdbb42be6096a Mon Sep 17 00:00:00 2001 From: Jeff King Date: Fri, 19 May 2017 08:58:05 -0400 Subject: diff: use the word "path" instead of "name" for blobs The stuff_change() function makes diff_filespecs out of blobs. The term we generally use for filespecs is "path", not "name", so let's be consistent here. That will make things less confusing when the next patch starts caring about the path/name distinction inside the pending object array. Signed-off-by: Jeff King Signed-off-by: Junio C Hamano --- builtin/diff.c | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) (limited to 'builtin/diff.c') diff --git a/builtin/diff.c b/builtin/diff.c index 8b276ae575..4c0811d6fc 100644 --- a/builtin/diff.c +++ b/builtin/diff.c @@ -29,8 +29,8 @@ static void stuff_change(struct diff_options *opt, const struct object_id *new_oid, int old_oid_valid, int new_oid_valid, - const char *old_name, - const char *new_name) + const char *old_path, + const char *new_path) { struct diff_filespec *one, *two; @@ -41,16 +41,16 @@ static void stuff_change(struct diff_options *opt, if (DIFF_OPT_TST(opt, REVERSE_DIFF)) { SWAP(old_mode, new_mode); SWAP(old_oid, new_oid); - SWAP(old_name, new_name); + SWAP(old_path, new_path); } if (opt->prefix && - (strncmp(old_name, opt->prefix, opt->prefix_length) || - strncmp(new_name, opt->prefix, opt->prefix_length))) + (strncmp(old_path, opt->prefix, opt->prefix_length) || + strncmp(new_path, opt->prefix, opt->prefix_length))) return; - one = alloc_filespec(old_name); - two = alloc_filespec(new_name); + one = alloc_filespec(old_path); + two = alloc_filespec(new_path); fill_filespec(one, old_oid->hash, old_oid_valid, old_mode); fill_filespec(two, new_oid->hash, new_oid_valid, new_mode); -- cgit v1.2.3 From 158b06caee5f9ea2987f444b8e49bd2d678b187d Mon Sep 17 00:00:00 2001 From: Jeff King Date: Fri, 19 May 2017 08:59:15 -0400 Subject: diff: use pending "path" if it is available There's a subtle distinction between "name" and "path" for a blob that we resolve: the name is what the user told us on the command line, and the path is what we traversed when finding the blob within a tree (if we did so). When we diff blobs directly, we use "name", but "path" is more likely to be useful to the user (it will find the correct .gitattributes, and give them a saner diff header). We still have to fall back to using the name for some cases (i.e., any blob reference that isn't of the form tree:path). That's the best we can do in such a case. Signed-off-by: Jeff King Signed-off-by: Junio C Hamano --- builtin/diff.c | 7 ++++++- t/t4063-diff-blobs.sh | 4 ++-- 2 files changed, 8 insertions(+), 3 deletions(-) (limited to 'builtin/diff.c') diff --git a/builtin/diff.c b/builtin/diff.c index 4c0811d6fc..1a1149eed4 100644 --- a/builtin/diff.c +++ b/builtin/diff.c @@ -23,6 +23,11 @@ static const char builtin_diff_usage[] = "git diff [] [ []] [--] [...]"; +static const char *blob_path(struct object_array_entry *entry) +{ + return entry->path ? entry->path : entry->name; +} + static void stuff_change(struct diff_options *opt, unsigned old_mode, unsigned new_mode, const struct object_id *old_oid, @@ -110,7 +115,7 @@ static int builtin_diff_blobs(struct rev_info *revs, blob[0]->mode, blob[1]->mode, &blob[0]->item->oid, &blob[1]->item->oid, 1, 1, - blob[0]->name, blob[1]->name); + blob_path(blob[0]), blob_path(blob[1])); diffcore_std(&revs->diffopt); diff_flush(&revs->diffopt); return 0; diff --git a/t/t4063-diff-blobs.sh b/t/t4063-diff-blobs.sh index df9c35b2dd..80ce033ab6 100755 --- a/t/t4063-diff-blobs.sh +++ b/t/t4063-diff-blobs.sh @@ -55,7 +55,7 @@ test_expect_success 'diff by tree:path (run)' ' test_expect_success 'index of tree:path diff' ' check_index $sha1_one $sha1_two ' -test_expect_failure 'tree:path diff uses filenames as paths' ' +test_expect_success 'tree:path diff uses filenames as paths' ' check_paths one two ' test_expect_success 'tree:path diff shows mode change' ' @@ -68,7 +68,7 @@ test_expect_success 'diff by ranged tree:path' ' test_expect_success 'index of ranged tree:path diff' ' check_index $sha1_one $sha1_two ' -test_expect_failure 'ranged tree:path diff uses filenames as paths' ' +test_expect_success 'ranged tree:path diff uses filenames as paths' ' check_paths one two ' test_expect_success 'ranged tree:path diff shows mode change' ' -- cgit v1.2.3 From 30d005c02014680403b5d35ef274047ab91fa5bd Mon Sep 17 00:00:00 2001 From: Jeff King Date: Fri, 19 May 2017 08:59:34 -0400 Subject: diff: use blob path for blob/file diffs When we diff a blob against a working tree file like: git diff HEAD:Makefile Makefile we always use the working tree filename for both sides of the diff. In most cases that's fine, as the two would be the same anyway, as above. And until recently, we used the "name" for the blob, not the path, which would have the messy "HEAD:" on the beginning. But when they don't match, like: git diff HEAD:old_path new_path it makes sense to show both names. This patch uses the blob's path field if it's available, and otherwise falls back to using the filename (in preference to the blob's name, which is likely to be garbage like a raw sha1). Signed-off-by: Jeff King Signed-off-by: Junio C Hamano --- builtin/diff.c | 3 ++- t/t4063-diff-blobs.sh | 7 ++++++- 2 files changed, 8 insertions(+), 2 deletions(-) (limited to 'builtin/diff.c') diff --git a/builtin/diff.c b/builtin/diff.c index 1a1149eed4..5e7c6428c9 100644 --- a/builtin/diff.c +++ b/builtin/diff.c @@ -90,7 +90,8 @@ static int builtin_diff_b_f(struct rev_info *revs, blob[0]->mode, canon_mode(st.st_mode), &blob[0]->item->oid, &null_oid, 1, 0, - path, path); + blob[0]->path ? blob[0]->path : path, + path); diffcore_std(&revs->diffopt); diff_flush(&revs->diffopt); return 0; diff --git a/t/t4063-diff-blobs.sh b/t/t4063-diff-blobs.sh index 80ce033ab6..bc69e26c52 100755 --- a/t/t4063-diff-blobs.sh +++ b/t/t4063-diff-blobs.sh @@ -81,11 +81,16 @@ test_expect_success 'diff blob against file' ' test_expect_success 'index of blob-file diff' ' check_index $sha1_one $sha1_two ' -test_expect_failure 'blob-file diff uses filename as paths' ' +test_expect_success 'blob-file diff uses filename as paths' ' check_paths one two ' test_expect_success FILEMODE 'blob-file diff shows mode change' ' check_mode 100644 100755 ' +test_expect_success 'blob-file diff prefers filename to sha1' ' + run_diff $sha1_one two && + check_paths two two +' + test_done -- cgit v1.2.3