Skip to content

Commit

Permalink
Merge branch 'cc/maint-diff-CC-binary'
Browse files Browse the repository at this point in the history
* cc/maint-diff-CC-binary:
  diff: fix "git show -C -C" output when renaming a binary file

Conflicts:
	diff.c
  • Loading branch information
Junio C Hamano committed Jun 18, 2010
2 parents 98ad90f + 296c6bb commit 5977744
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 5 deletions.
21 changes: 16 additions & 5 deletions diff.c
Original file line number Diff line number Diff line change
Expand Up @@ -1837,6 +1837,7 @@ static void builtin_diff(const char *name_a,
struct diff_filespec *one,
struct diff_filespec *two,
const char *xfrm_msg,
int must_show_header,
struct diff_options *o,
int complete_rewrite)
{
Expand Down Expand Up @@ -1896,16 +1897,19 @@ static void builtin_diff(const char *name_a,
strbuf_addf(&header, "%s%snew file mode %06o%s\n", line_prefix, set, two->mode, reset);
if (xfrm_msg)
strbuf_addstr(&header, xfrm_msg);
must_show_header = 1;
}
else if (lbl[1][0] == '/') {
strbuf_addf(&header, "%s%sdeleted file mode %06o%s\n", line_prefix, set, one->mode, reset);
if (xfrm_msg)
strbuf_addstr(&header, xfrm_msg);
must_show_header = 1;
}
else {
if (one->mode != two->mode) {
strbuf_addf(&header, "%s%sold mode %06o%s\n", line_prefix, set, one->mode, reset);
strbuf_addf(&header, "%s%snew mode %06o%s\n", line_prefix, set, two->mode, reset);
must_show_header = 1;
}
if (xfrm_msg)
strbuf_addstr(&header, xfrm_msg);
Expand Down Expand Up @@ -1935,8 +1939,11 @@ static void builtin_diff(const char *name_a,
die("unable to read files to diff");
/* Quite common confusing case */
if (mf1.size == mf2.size &&
!memcmp(mf1.ptr, mf2.ptr, mf1.size))
!memcmp(mf1.ptr, mf2.ptr, mf1.size)) {
if (must_show_header)
fprintf(o->file, "%s", header.buf);
goto free_ab_and_return;
}
fprintf(o->file, "%s", header.buf);
strbuf_reset(&header);
if (DIFF_OPT_TST(o, BINARY))
Expand All @@ -1954,7 +1961,7 @@ static void builtin_diff(const char *name_a,
struct emit_callback ecbdata;
const struct userdiff_funcname *pe;

if (!DIFF_XDL_TST(o, WHITESPACE_FLAGS)) {
if (!DIFF_XDL_TST(o, WHITESPACE_FLAGS) || must_show_header) {
fprintf(o->file, "%s", header.buf);
strbuf_reset(&header);
}
Expand Down Expand Up @@ -2568,18 +2575,19 @@ static void fill_metainfo(struct strbuf *msg,
struct diff_filespec *two,
struct diff_options *o,
struct diff_filepair *p,
int *must_show_header,
int use_color)
{
const char *set = diff_get_color(use_color, DIFF_METAINFO);
const char *reset = diff_get_color(use_color, DIFF_RESET);
struct strbuf *msgbuf;
char *line_prefix = "";

*must_show_header = 1;
if (o->output_prefix) {
msgbuf = o->output_prefix(o, o->output_prefix_data);
line_prefix = msgbuf->buf;
}

strbuf_init(msg, PATH_MAX * 2 + 300);
switch (p->status) {
case DIFF_STATUS_COPIED:
Expand Down Expand Up @@ -2613,7 +2621,7 @@ static void fill_metainfo(struct strbuf *msg,
/* fallthru */
default:
/* nothing */
;
*must_show_header = 0;
}
if (one && two && hashcmp(one->sha1, two->sha1)) {
int abbrev = DIFF_OPT_TST(o, FULL_INDEX) ? 40 : DEFAULT_ABBREV;
Expand Down Expand Up @@ -2646,6 +2654,7 @@ static void run_diff_cmd(const char *pgm,
{
const char *xfrm_msg = NULL;
int complete_rewrite = (p->status == DIFF_STATUS_MODIFIED) && p->score;
int must_show_header = 0;

if (!DIFF_OPT_TST(o, ALLOW_EXTERNAL))
pgm = NULL;
Expand All @@ -2661,6 +2670,7 @@ static void run_diff_cmd(const char *pgm,
* external diff driver
*/
fill_metainfo(msg, name, other, one, two, o, p,
&must_show_header,
DIFF_OPT_TST(o, COLOR_DIFF) && !pgm);
xfrm_msg = msg->len ? msg->buf : NULL;
}
Expand All @@ -2672,7 +2682,8 @@ static void run_diff_cmd(const char *pgm,
}
if (one && two)
builtin_diff(name, other ? other : name,
one, two, xfrm_msg, o, complete_rewrite);
one, two, xfrm_msg, must_show_header,
o, complete_rewrite);
else
fprintf(o->file, "* Unmerged path %s\n", name);
}
Expand Down
37 changes: 37 additions & 0 deletions t/t4015-diff-whitespace.sh
Original file line number Diff line number Diff line change
Expand Up @@ -438,6 +438,43 @@ test_expect_success 'whitespace-only changes not reported' '
test_cmp expect actual
'

cat <<EOF >expect
diff --git a/x b/z
similarity index NUM%
rename from x
rename to z
index 380c32a..a97b785 100644
EOF
test_expect_success 'whitespace-only changes reported across renames' '
git reset --hard &&
for i in 1 2 3 4 5 6 7 8 9; do echo "$i$i$i$i$i$i"; done >x &&
git add x &&
git commit -m "base" &&
sed -e "5s/^/ /" x >z &&
git rm x &&
git add z &&
git diff -w -M --cached |
sed -e "/^similarity index /s/[0-9][0-9]*/NUM/" >actual &&
test_cmp expect actual
'

cat >expected <<\EOF
diff --git a/empty b/void
similarity index 100%
rename from empty
rename to void
EOF

test_expect_success 'rename empty' '
git reset --hard &&
>empty &&
git add empty &&
git commit -m empty &&
git mv empty void &&
git diff -w --cached -M >current &&
test_cmp expected current
'

test_expect_success 'combined diff with autocrlf conversion' '
git reset --hard &&
Expand Down
45 changes: 45 additions & 0 deletions t/t4043-diff-rename-binary.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#!/bin/sh
#
# Copyright (c) 2010 Jakub Narebski, Christian Couder
#

test_description='Move a binary file'

. ./test-lib.sh


test_expect_success 'prepare repository' '
git init &&
echo foo > foo &&
echo "barQ" | q_to_nul > bar &&
git add . &&
git commit -m "Initial commit"
'

test_expect_success 'move the files into a "sub" directory' '
mkdir sub &&
git mv bar foo sub/ &&
git commit -m "Moved to sub/"
'

cat > expected <<\EOF
bar => sub/bar | Bin 5 -> 5 bytes
foo => sub/foo | 0
2 files changed, 0 insertions(+), 0 deletions(-)
diff --git a/bar b/sub/bar
similarity index 100%
rename from bar
rename to sub/bar
diff --git a/foo b/sub/foo
similarity index 100%
rename from foo
rename to sub/foo
EOF

test_expect_success 'git show -C -C report renames' '
git show -C -C --raw --binary --stat | tail -n 12 > current &&
test_cmp expected current
'

test_done

0 comments on commit 5977744

Please sign in to comment.