Skip to content

Commit 296c6bb

Browse files
chriscoolgitster
authored andcommitted
diff: fix "git show -C -C" output when renaming a binary file
A bug was introduced in 3e97c7c (No diff -b/-w output for all-whitespace changes, Nov 19 2009) that made the lines: diff --git a/bar b/sub/bar similarity index 100% rename from bar rename to sub/bar disappear from "git show -C -C" output when file bar is a binary file. Signed-off-by: Christian Couder <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent dfea790 commit 296c6bb

File tree

3 files changed

+99
-6
lines changed

3 files changed

+99
-6
lines changed

diff.c

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1596,6 +1596,7 @@ static void builtin_diff(const char *name_a,
15961596
struct diff_filespec *one,
15971597
struct diff_filespec *two,
15981598
const char *xfrm_msg,
1599+
int must_show_header,
15991600
struct diff_options *o,
16001601
int complete_rewrite)
16011602
{
@@ -1647,16 +1648,19 @@ static void builtin_diff(const char *name_a,
16471648
strbuf_addf(&header, "%snew file mode %06o%s\n", set, two->mode, reset);
16481649
if (xfrm_msg && xfrm_msg[0])
16491650
strbuf_addf(&header, "%s%s%s\n", set, xfrm_msg, reset);
1651+
must_show_header = 1;
16501652
}
16511653
else if (lbl[1][0] == '/') {
16521654
strbuf_addf(&header, "%sdeleted file mode %06o%s\n", set, one->mode, reset);
16531655
if (xfrm_msg && xfrm_msg[0])
16541656
strbuf_addf(&header, "%s%s%s\n", set, xfrm_msg, reset);
1657+
must_show_header = 1;
16551658
}
16561659
else {
16571660
if (one->mode != two->mode) {
16581661
strbuf_addf(&header, "%sold mode %06o%s\n", set, one->mode, reset);
16591662
strbuf_addf(&header, "%snew mode %06o%s\n", set, two->mode, reset);
1663+
must_show_header = 1;
16601664
}
16611665
if (xfrm_msg && xfrm_msg[0])
16621666
strbuf_addf(&header, "%s%s%s\n", set, xfrm_msg, reset);
@@ -1687,8 +1691,11 @@ static void builtin_diff(const char *name_a,
16871691
(diff_filespec_is_binary(two) && !textconv_two) )) {
16881692
/* Quite common confusing case */
16891693
if (mf1.size == mf2.size &&
1690-
!memcmp(mf1.ptr, mf2.ptr, mf1.size))
1694+
!memcmp(mf1.ptr, mf2.ptr, mf1.size)) {
1695+
if (must_show_header)
1696+
fprintf(o->file, "%s", header.buf);
16911697
goto free_ab_and_return;
1698+
}
16921699
fprintf(o->file, "%s", header.buf);
16931700
strbuf_reset(&header);
16941701
if (DIFF_OPT_TST(o, BINARY))
@@ -1706,7 +1713,7 @@ static void builtin_diff(const char *name_a,
17061713
struct emit_callback ecbdata;
17071714
const struct userdiff_funcname *pe;
17081715

1709-
if (!DIFF_XDL_TST(o, WHITESPACE_FLAGS)) {
1716+
if (!DIFF_XDL_TST(o, WHITESPACE_FLAGS) || must_show_header) {
17101717
fprintf(o->file, "%s", header.buf);
17111718
strbuf_reset(&header);
17121719
}
@@ -2315,8 +2322,10 @@ static void fill_metainfo(struct strbuf *msg,
23152322
struct diff_filespec *one,
23162323
struct diff_filespec *two,
23172324
struct diff_options *o,
2318-
struct diff_filepair *p)
2325+
struct diff_filepair *p,
2326+
int *must_show_header)
23192327
{
2328+
*must_show_header = 1;
23202329
strbuf_init(msg, PATH_MAX * 2 + 300);
23212330
switch (p->status) {
23222331
case DIFF_STATUS_COPIED:
@@ -2344,7 +2353,7 @@ static void fill_metainfo(struct strbuf *msg,
23442353
/* fallthru */
23452354
default:
23462355
/* nothing */
2347-
;
2356+
*must_show_header = 0;
23482357
}
23492358
if (one && two && hashcmp(one->sha1, two->sha1)) {
23502359
int abbrev = DIFF_OPT_TST(o, FULL_INDEX) ? 40 : DEFAULT_ABBREV;
@@ -2378,9 +2387,10 @@ static void run_diff_cmd(const char *pgm,
23782387
{
23792388
const char *xfrm_msg = NULL;
23802389
int complete_rewrite = (p->status == DIFF_STATUS_MODIFIED) && p->score;
2390+
int must_show_header = 0;
23812391

23822392
if (msg) {
2383-
fill_metainfo(msg, name, other, one, two, o, p);
2393+
fill_metainfo(msg, name, other, one, two, o, p, &must_show_header);
23842394
xfrm_msg = msg->len ? msg->buf : NULL;
23852395
}
23862396

@@ -2399,7 +2409,8 @@ static void run_diff_cmd(const char *pgm,
23992409
}
24002410
if (one && two)
24012411
builtin_diff(name, other ? other : name,
2402-
one, two, xfrm_msg, o, complete_rewrite);
2412+
one, two, xfrm_msg, must_show_header,
2413+
o, complete_rewrite);
24032414
else
24042415
fprintf(o->file, "* Unmerged path %s\n", name);
24052416
}

t/t4015-diff-whitespace.sh

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -396,6 +396,43 @@ test_expect_success 'whitespace-only changes not reported' '
396396
test_cmp expect actual
397397
'
398398

399+
cat <<EOF >expect
400+
diff --git a/x b/z
401+
similarity index NUM%
402+
rename from x
403+
rename to z
404+
index 380c32a..a97b785 100644
405+
EOF
406+
test_expect_success 'whitespace-only changes reported across renames' '
407+
git reset --hard &&
408+
for i in 1 2 3 4 5 6 7 8 9; do echo "$i$i$i$i$i$i"; done >x &&
409+
git add x &&
410+
git commit -m "base" &&
411+
sed -e "5s/^/ /" x >z &&
412+
git rm x &&
413+
git add z &&
414+
git diff -w -M --cached |
415+
sed -e "/^similarity index /s/[0-9][0-9]*/NUM/" >actual &&
416+
test_cmp expect actual
417+
'
418+
419+
cat >expected <<\EOF
420+
diff --git a/empty b/void
421+
similarity index 100%
422+
rename from empty
423+
rename to void
424+
EOF
425+
426+
test_expect_success 'rename empty' '
427+
git reset --hard &&
428+
>empty &&
429+
git add empty &&
430+
git commit -m empty &&
431+
git mv empty void &&
432+
git diff -w --cached -M >current &&
433+
test_cmp expected current
434+
'
435+
399436
test_expect_success 'combined diff with autocrlf conversion' '
400437
401438
git reset --hard &&

t/t4043-diff-rename-binary.sh

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
#!/bin/sh
2+
#
3+
# Copyright (c) 2010 Jakub Narebski, Christian Couder
4+
#
5+
6+
test_description='Move a binary file'
7+
8+
. ./test-lib.sh
9+
10+
11+
test_expect_success 'prepare repository' '
12+
git init &&
13+
echo foo > foo &&
14+
echo "barQ" | q_to_nul > bar &&
15+
git add . &&
16+
git commit -m "Initial commit"
17+
'
18+
19+
test_expect_success 'move the files into a "sub" directory' '
20+
mkdir sub &&
21+
git mv bar foo sub/ &&
22+
git commit -m "Moved to sub/"
23+
'
24+
25+
cat > expected <<\EOF
26+
bar => sub/bar | Bin 5 -> 5 bytes
27+
foo => sub/foo | 0
28+
2 files changed, 0 insertions(+), 0 deletions(-)
29+
30+
diff --git a/bar b/sub/bar
31+
similarity index 100%
32+
rename from bar
33+
rename to sub/bar
34+
diff --git a/foo b/sub/foo
35+
similarity index 100%
36+
rename from foo
37+
rename to sub/foo
38+
EOF
39+
40+
test_expect_success 'git show -C -C report renames' '
41+
git show -C -C --raw --binary --stat | tail -n 12 > current &&
42+
test_cmp expected current
43+
'
44+
45+
test_done

0 commit comments

Comments
 (0)