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 <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Jeff King committed
May 19, 2017 at 08:59 UTC
158b06caee5f9ea2987f444b8e49bd2d678b187d
2 files changed
+8
-3
builtin/diff.c
+6
-1
@@ -23,6 +23,11 @@
23
static const char builtin_diff_usage[] =
24
"git diff [<options>] [<commit> [<commit>]] [--] [<path>...]";
25
26
+static const char *blob_path(struct object_array_entry *entry)
27
+{
28
+ return entry->path ? entry->path : entry->name;
29
+}
30
+
31
static void stuff_change(struct diff_options *opt,
32
unsigned old_mode, unsigned new_mode,
33
const struct object_id *old_oid,
@@ -110,7 +115,7 @@ static int builtin_diff_blobs(struct rev_info *revs,
115
blob[0]->mode, blob[1]->mode,
116
&blob[0]->item->oid, &blob[1]->item->oid,
117
1, 1,
113
- blob[0]->name, blob[1]->name);
118
+ blob_path(blob[0]), blob_path(blob[1]));
119
diffcore_std(&revs->diffopt);
120
diff_flush(&revs->diffopt);
121
return 0;
t/t4063-diff-blobs.sh
+2
-2
@@ -55,7 +55,7 @@ test_expect_success 'diff by tree:path (run)' '
55
test_expect_success 'index of tree:path diff' '
56
check_index $sha1_one $sha1_two
57
'
58
-test_expect_failure 'tree:path diff uses filenames as paths' '
58
+test_expect_success 'tree:path diff uses filenames as paths' '
59
check_paths one two
60
'
61
test_expect_success 'tree:path diff shows mode change' '
@@ -68,7 +68,7 @@ test_expect_success 'diff by ranged tree:path' '
68
test_expect_success 'index of ranged tree:path diff' '
69
check_index $sha1_one $sha1_two
70
'
71
-test_expect_failure 'ranged tree:path diff uses filenames as paths' '
71
+test_expect_success 'ranged tree:path diff uses filenames as paths' '
72
check_paths one two
73
'
74
test_expect_success 'ranged tree:path diff shows mode change' '