From: Johan Herland Date: Sun, 30 May 2010 13:37:17 +0000 (+0200) Subject: diff.c: Ensure "index $from..$to" line contains unambiguous SHA1s X-Git-Tag: v1.7.2-rc0~51^2 X-Git-Url: https://git.tokkee.org/?a=commitdiff_plain;h=3e5a188f1d5b48dcc0bc73ad520925cdb846dfaf;p=git.git 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 Signed-off-by: Junio C Hamano --- diff --git a/diff.c b/diff.c index 494f5601e..1aefa6637 100644 --- a/diff.c +++ b/diff.c @@ -2419,9 +2419,9 @@ static void fill_metainfo(struct strbuf *msg, (!fill_mmfile(&mf, two) && diff_filespec_is_binary(two))) abbrev = 40; } - strbuf_addf(msg, "index %.*s..%.*s", - abbrev, sha1_to_hex(one->sha1), - abbrev, sha1_to_hex(two->sha1)); + strbuf_addf(msg, "index %s..", + find_unique_abbrev(one->sha1, abbrev)); + strbuf_addstr(msg, find_unique_abbrev(two->sha1, abbrev)); if (one->mode == two->mode) strbuf_addf(msg, " %06o", one->mode); strbuf_addch(msg, '\n'); diff --git a/t/t4044-diff-index-unique-abbrev.sh b/t/t4044-diff-index-unique-abbrev.sh new file mode 100755 index 000000000..d5ce72be6 --- /dev/null +++ b/t/t4044-diff-index-unique-abbrev.sh @@ -0,0 +1,35 @@ +#!/bin/sh + +test_description='test unique sha1 abbreviation on "index from..to" line' +. ./test-lib.sh + +cat >expect_initial <expect_update < foo && + git add foo && + git commit -m "initial" && + git cat-file -p HEAD: > actual && + test_cmp expect_initial actual && + echo 11742 > foo && + git commit -a -m "update" && + git cat-file -p HEAD: > actual && + test_cmp expect_update actual +' + +cat >expect < actual && + test_cmp expect actual +' + +test_done