Skip to content

Commit 3e5a188

Browse files
jherlandgitster
authored andcommitted
diff.c: Ensure "index $from..$to" line contains unambiguous SHA1s
In the metainfo section of git diffs there's an "index" line providing abbreviated (unless --full-index is used) blob SHA1s from the pre-/post-images used to generate the diff. These provide hints that can be used to reconstruct a 3-way merge when applying the patch (see the --3way option to 'git am' for more details). In order for this to work, however, the blob SHA1s must not be abbreviated into ambiguity. This patch eliminates the possible ambiguity by using find_unique_abbrev() to produce the abbreviated SHA1s (instead of blind abbreviation by way of "%.*s"). A testcase verifying the fix is also included. Signed-off-by: Johan Herland <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 81fa024 commit 3e5a188

File tree

2 files changed

+38
-3
lines changed

2 files changed

+38
-3
lines changed

diff.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2419,9 +2419,9 @@ static void fill_metainfo(struct strbuf *msg,
24192419
(!fill_mmfile(&mf, two) && diff_filespec_is_binary(two)))
24202420
abbrev = 40;
24212421
}
2422-
strbuf_addf(msg, "index %.*s..%.*s",
2423-
abbrev, sha1_to_hex(one->sha1),
2424-
abbrev, sha1_to_hex(two->sha1));
2422+
strbuf_addf(msg, "index %s..",
2423+
find_unique_abbrev(one->sha1, abbrev));
2424+
strbuf_addstr(msg, find_unique_abbrev(two->sha1, abbrev));
24252425
if (one->mode == two->mode)
24262426
strbuf_addf(msg, " %06o", one->mode);
24272427
strbuf_addch(msg, '\n');

t/t4044-diff-index-unique-abbrev.sh

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#!/bin/sh
2+
3+
test_description='test unique sha1 abbreviation on "index from..to" line'
4+
. ./test-lib.sh
5+
6+
cat >expect_initial <<EOF
7+
100644 blob 51d2738463ea4ca66f8691c91e33ce64b7d41bb1 foo
8+
EOF
9+
10+
cat >expect_update <<EOF
11+
100644 blob 51d2738efb4ad8a1e40bed839ab8e116f0a15e47 foo
12+
EOF
13+
14+
test_expect_success 'setup' '
15+
echo 4827 > foo &&
16+
git add foo &&
17+
git commit -m "initial" &&
18+
git cat-file -p HEAD: > actual &&
19+
test_cmp expect_initial actual &&
20+
echo 11742 > foo &&
21+
git commit -a -m "update" &&
22+
git cat-file -p HEAD: > actual &&
23+
test_cmp expect_update actual
24+
'
25+
26+
cat >expect <<EOF
27+
index 51d27384..51d2738e 100644
28+
EOF
29+
30+
test_expect_success 'diff does not produce ambiguous index line' '
31+
git diff HEAD^..HEAD | grep index > actual &&
32+
test_cmp expect actual
33+
'
34+
35+
test_done

0 commit comments

Comments
 (0)