Skip to content

Commit 5977744

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

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
@@ -1837,6 +1837,7 @@ static void builtin_diff(const char *name_a,
18371837
struct diff_filespec *one,
18381838
struct diff_filespec *two,
18391839
const char *xfrm_msg,
1840+
int must_show_header,
18401841
struct diff_options *o,
18411842
int complete_rewrite)
18421843
{
@@ -1896,16 +1897,19 @@ static void builtin_diff(const char *name_a,
18961897
strbuf_addf(&header, "%s%snew file mode %06o%s\n", line_prefix, set, two->mode, reset);
18971898
if (xfrm_msg)
18981899
strbuf_addstr(&header, xfrm_msg);
1900+
must_show_header = 1;
18991901
}
19001902
else if (lbl[1][0] == '/') {
19011903
strbuf_addf(&header, "%s%sdeleted file mode %06o%s\n", line_prefix, set, one->mode, reset);
19021904
if (xfrm_msg)
19031905
strbuf_addstr(&header, xfrm_msg);
1906+
must_show_header = 1;
19041907
}
19051908
else {
19061909
if (one->mode != two->mode) {
19071910
strbuf_addf(&header, "%s%sold mode %06o%s\n", line_prefix, set, one->mode, reset);
19081911
strbuf_addf(&header, "%s%snew mode %06o%s\n", line_prefix, set, two->mode, reset);
1912+
must_show_header = 1;
19091913
}
19101914
if (xfrm_msg)
19111915
strbuf_addstr(&header, xfrm_msg);
@@ -1935,8 +1939,11 @@ static void builtin_diff(const char *name_a,
19351939
die("unable to read files to diff");
19361940
/* Quite common confusing case */
19371941
if (mf1.size == mf2.size &&
1938-
!memcmp(mf1.ptr, mf2.ptr, mf1.size))
1942+
!memcmp(mf1.ptr, mf2.ptr, mf1.size)) {
1943+
if (must_show_header)
1944+
fprintf(o->file, "%s", header.buf);
19391945
goto free_ab_and_return;
1946+
}
19401947
fprintf(o->file, "%s", header.buf);
19411948
strbuf_reset(&header);
19421949
if (DIFF_OPT_TST(o, BINARY))
@@ -1954,7 +1961,7 @@ static void builtin_diff(const char *name_a,
19541961
struct emit_callback ecbdata;
19551962
const struct userdiff_funcname *pe;
19561963

1957-
if (!DIFF_XDL_TST(o, WHITESPACE_FLAGS)) {
1964+
if (!DIFF_XDL_TST(o, WHITESPACE_FLAGS) || must_show_header) {
19581965
fprintf(o->file, "%s", header.buf);
19591966
strbuf_reset(&header);
19601967
}
@@ -2568,18 +2575,19 @@ static void fill_metainfo(struct strbuf *msg,
25682575
struct diff_filespec *two,
25692576
struct diff_options *o,
25702577
struct diff_filepair *p,
2578+
int *must_show_header,
25712579
int use_color)
25722580
{
25732581
const char *set = diff_get_color(use_color, DIFF_METAINFO);
25742582
const char *reset = diff_get_color(use_color, DIFF_RESET);
25752583
struct strbuf *msgbuf;
25762584
char *line_prefix = "";
25772585

2586+
*must_show_header = 1;
25782587
if (o->output_prefix) {
25792588
msgbuf = o->output_prefix(o, o->output_prefix_data);
25802589
line_prefix = msgbuf->buf;
25812590
}
2582-
25832591
strbuf_init(msg, PATH_MAX * 2 + 300);
25842592
switch (p->status) {
25852593
case DIFF_STATUS_COPIED:
@@ -2613,7 +2621,7 @@ static void fill_metainfo(struct strbuf *msg,
26132621
/* fallthru */
26142622
default:
26152623
/* nothing */
2616-
;
2624+
*must_show_header = 0;
26172625
}
26182626
if (one && two && hashcmp(one->sha1, two->sha1)) {
26192627
int abbrev = DIFF_OPT_TST(o, FULL_INDEX) ? 40 : DEFAULT_ABBREV;
@@ -2646,6 +2654,7 @@ static void run_diff_cmd(const char *pgm,
26462654
{
26472655
const char *xfrm_msg = NULL;
26482656
int complete_rewrite = (p->status == DIFF_STATUS_MODIFIED) && p->score;
2657+
int must_show_header = 0;
26492658

26502659
if (!DIFF_OPT_TST(o, ALLOW_EXTERNAL))
26512660
pgm = NULL;
@@ -2661,6 +2670,7 @@ static void run_diff_cmd(const char *pgm,
26612670
* external diff driver
26622671
*/
26632672
fill_metainfo(msg, name, other, one, two, o, p,
2673+
&must_show_header,
26642674
DIFF_OPT_TST(o, COLOR_DIFF) && !pgm);
26652675
xfrm_msg = msg->len ? msg->buf : NULL;
26662676
}
@@ -2672,7 +2682,8 @@ static void run_diff_cmd(const char *pgm,
26722682
}
26732683
if (one && two)
26742684
builtin_diff(name, other ? other : name,
2675-
one, two, xfrm_msg, o, complete_rewrite);
2685+
one, two, xfrm_msg, must_show_header,
2686+
o, complete_rewrite);
26762687
else
26772688
fprintf(o->file, "* Unmerged path %s\n", name);
26782689
}

t/t4015-diff-whitespace.sh

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -438,6 +438,43 @@ test_expect_success 'whitespace-only changes not reported' '
438438
test_cmp expect actual
439439
'
440440

441+
cat <<EOF >expect
442+
diff --git a/x b/z
443+
similarity index NUM%
444+
rename from x
445+
rename to z
446+
index 380c32a..a97b785 100644
447+
EOF
448+
test_expect_success 'whitespace-only changes reported across renames' '
449+
git reset --hard &&
450+
for i in 1 2 3 4 5 6 7 8 9; do echo "$i$i$i$i$i$i"; done >x &&
451+
git add x &&
452+
git commit -m "base" &&
453+
sed -e "5s/^/ /" x >z &&
454+
git rm x &&
455+
git add z &&
456+
git diff -w -M --cached |
457+
sed -e "/^similarity index /s/[0-9][0-9]*/NUM/" >actual &&
458+
test_cmp expect actual
459+
'
460+
461+
cat >expected <<\EOF
462+
diff --git a/empty b/void
463+
similarity index 100%
464+
rename from empty
465+
rename to void
466+
EOF
467+
468+
test_expect_success 'rename empty' '
469+
git reset --hard &&
470+
>empty &&
471+
git add empty &&
472+
git commit -m empty &&
473+
git mv empty void &&
474+
git diff -w --cached -M >current &&
475+
test_cmp expected current
476+
'
477+
441478
test_expect_success 'combined diff with autocrlf conversion' '
442479
443480
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)