Skip to content

Commit 3c65689

Browse files
committed
Merge branch 'cc/maint-diff-CC-binary' into maint
* cc/maint-diff-CC-binary: diff: fix "git show -C -C" output when renaming a binary file Conflicts: diff.c
2 parents a8c4d92 + 296c6bb commit 3c65689

File tree

3 files changed

+98
-5
lines changed

3 files changed

+98
-5
lines changed

diff.c

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1600,6 +1600,7 @@ static void builtin_diff(const char *name_a,
16001600
struct diff_filespec *one,
16011601
struct diff_filespec *two,
16021602
const char *xfrm_msg,
1603+
int must_show_header,
16031604
struct diff_options *o,
16041605
int complete_rewrite)
16051606
{
@@ -1651,16 +1652,19 @@ static void builtin_diff(const char *name_a,
16511652
strbuf_addf(&header, "%snew file mode %06o%s\n", set, two->mode, reset);
16521653
if (xfrm_msg)
16531654
strbuf_addstr(&header, xfrm_msg);
1655+
must_show_header = 1;
16541656
}
16551657
else if (lbl[1][0] == '/') {
16561658
strbuf_addf(&header, "%sdeleted file mode %06o%s\n", set, one->mode, reset);
16571659
if (xfrm_msg)
16581660
strbuf_addstr(&header, xfrm_msg);
1661+
must_show_header = 1;
16591662
}
16601663
else {
16611664
if (one->mode != two->mode) {
16621665
strbuf_addf(&header, "%sold mode %06o%s\n", set, one->mode, reset);
16631666
strbuf_addf(&header, "%snew mode %06o%s\n", set, two->mode, reset);
1667+
must_show_header = 1;
16641668
}
16651669
if (xfrm_msg)
16661670
strbuf_addstr(&header, xfrm_msg);
@@ -1691,8 +1695,11 @@ static void builtin_diff(const char *name_a,
16911695
(diff_filespec_is_binary(two) && !textconv_two) )) {
16921696
/* Quite common confusing case */
16931697
if (mf1.size == mf2.size &&
1694-
!memcmp(mf1.ptr, mf2.ptr, mf1.size))
1698+
!memcmp(mf1.ptr, mf2.ptr, mf1.size)) {
1699+
if (must_show_header)
1700+
fprintf(o->file, "%s", header.buf);
16951701
goto free_ab_and_return;
1702+
}
16961703
fprintf(o->file, "%s", header.buf);
16971704
strbuf_reset(&header);
16981705
if (DIFF_OPT_TST(o, BINARY))
@@ -1710,7 +1717,7 @@ static void builtin_diff(const char *name_a,
17101717
struct emit_callback ecbdata;
17111718
const struct userdiff_funcname *pe;
17121719

1713-
if (!DIFF_XDL_TST(o, WHITESPACE_FLAGS)) {
1720+
if (!DIFF_XDL_TST(o, WHITESPACE_FLAGS) || must_show_header) {
17141721
fprintf(o->file, "%s", header.buf);
17151722
strbuf_reset(&header);
17161723
}
@@ -2320,11 +2327,13 @@ static void fill_metainfo(struct strbuf *msg,
23202327
struct diff_filespec *two,
23212328
struct diff_options *o,
23222329
struct diff_filepair *p,
2330+
int *must_show_header,
23232331
int use_color)
23242332
{
23252333
const char *set = diff_get_color(use_color, DIFF_METAINFO);
23262334
const char *reset = diff_get_color(use_color, DIFF_RESET);
23272335

2336+
*must_show_header = 1;
23282337
strbuf_init(msg, PATH_MAX * 2 + 300);
23292338
switch (p->status) {
23302339
case DIFF_STATUS_COPIED:
@@ -2353,8 +2362,7 @@ static void fill_metainfo(struct strbuf *msg,
23532362
}
23542363
/* fallthru */
23552364
default:
2356-
/* nothing */
2357-
;
2365+
*must_show_header = 0;
23582366
}
23592367
if (one && two && hashcmp(one->sha1, two->sha1)) {
23602368
int abbrev = DIFF_OPT_TST(o, FULL_INDEX) ? 40 : DEFAULT_ABBREV;
@@ -2386,6 +2394,7 @@ static void run_diff_cmd(const char *pgm,
23862394
{
23872395
const char *xfrm_msg = NULL;
23882396
int complete_rewrite = (p->status == DIFF_STATUS_MODIFIED) && p->score;
2397+
int must_show_header = 0;
23892398

23902399
if (!DIFF_OPT_TST(o, ALLOW_EXTERNAL))
23912400
pgm = NULL;
@@ -2401,6 +2410,7 @@ static void run_diff_cmd(const char *pgm,
24012410
* external diff driver
24022411
*/
24032412
fill_metainfo(msg, name, other, one, two, o, p,
2413+
&must_show_header,
24042414
DIFF_OPT_TST(o, COLOR_DIFF) && !pgm);
24052415
xfrm_msg = msg->len ? msg->buf : NULL;
24062416
}
@@ -2412,7 +2422,8 @@ static void run_diff_cmd(const char *pgm,
24122422
}
24132423
if (one && two)
24142424
builtin_diff(name, other ? other : name,
2415-
one, two, xfrm_msg, o, complete_rewrite);
2425+
one, two, xfrm_msg, must_show_header,
2426+
o, complete_rewrite);
24162427
else
24172428
fprintf(o->file, "* Unmerged path %s\n", name);
24182429
}

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)