Skip to content

Commit 158b06c

Browse files
peffgitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent d04ec74 commit 158b06c

File tree

2 files changed

+8
-3
lines changed

2 files changed

+8
-3
lines changed

builtin/diff.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,11 @@
2323
static const char builtin_diff_usage[] =
2424
"git diff [<options>] [<commit> [<commit>]] [--] [<path>...]";
2525

26+
static const char *blob_path(struct object_array_entry *entry)
27+
{
28+
return entry->path ? entry->path : entry->name;
29+
}
30+
2631
static void stuff_change(struct diff_options *opt,
2732
unsigned old_mode, unsigned new_mode,
2833
const struct object_id *old_oid,
@@ -110,7 +115,7 @@ static int builtin_diff_blobs(struct rev_info *revs,
110115
blob[0]->mode, blob[1]->mode,
111116
&blob[0]->item->oid, &blob[1]->item->oid,
112117
1, 1,
113-
blob[0]->name, blob[1]->name);
118+
blob_path(blob[0]), blob_path(blob[1]));
114119
diffcore_std(&revs->diffopt);
115120
diff_flush(&revs->diffopt);
116121
return 0;

t/t4063-diff-blobs.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ test_expect_success 'diff by tree:path (run)' '
5555
test_expect_success 'index of tree:path diff' '
5656
check_index $sha1_one $sha1_two
5757
'
58-
test_expect_failure 'tree:path diff uses filenames as paths' '
58+
test_expect_success 'tree:path diff uses filenames as paths' '
5959
check_paths one two
6060
'
6161
test_expect_success 'tree:path diff shows mode change' '
@@ -68,7 +68,7 @@ test_expect_success 'diff by ranged tree:path' '
6868
test_expect_success 'index of ranged tree:path diff' '
6969
check_index $sha1_one $sha1_two
7070
'
71-
test_expect_failure 'ranged tree:path diff uses filenames as paths' '
71+
test_expect_success 'ranged tree:path diff uses filenames as paths' '
7272
check_paths one two
7373
'
7474
test_expect_success 'ranged tree:path diff shows mode change' '

0 commit comments

Comments
 (0)