Skip to content
This repository was archived by the owner on Nov 9, 2017. It is now read-only.

Commit fc1320b

Browse files
committed
Merge branch 'zj/diff-empty-chmod'
"git diff --stat" used to fully count a binary file with modified execution bits whose contents is unmodified, which was not right. By Zbigniew Jędrzejewski-Szmek (4) and Johannes Sixt (1) * zj/diff-empty-chmod: t4006: Windows do not have /dev/zero diff --stat: do not run diff on indentical files diff --stat: report mode-only changes for binary files like text files tests: check --[short]stat output after chmod test: modernize style of t4006 Conflicts: diff.c
2 parents 43d1e41 + 9380aed commit fc1320b

File tree

2 files changed

+74
-34
lines changed

2 files changed

+74
-34
lines changed

diff.c

Lines changed: 27 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1614,8 +1614,12 @@ static void show_stats(struct diffstat_t *data, struct diff_options *options)
16141614
if (data->files[i]->is_binary) {
16151615
fprintf(options->file, "%s", line_prefix);
16161616
show_name(options->file, prefix, name, len);
1617-
fprintf(options->file, " %*s ", number_width, "Bin");
1618-
fprintf(options->file, "%s%"PRIuMAX"%s",
1617+
fprintf(options->file, " %*s", number_width, "Bin");
1618+
if (!added && !deleted) {
1619+
putc('\n', options->file);
1620+
continue;
1621+
}
1622+
fprintf(options->file, " %s%"PRIuMAX"%s",
16191623
del_c, deleted, reset);
16201624
fprintf(options->file, " -> ");
16211625
fprintf(options->file, "%s%"PRIuMAX"%s",
@@ -1689,17 +1693,16 @@ static void show_shortstats(struct diffstat_t *data, struct diff_options *option
16891693
return;
16901694

16911695
for (i = 0; i < data->nr; i++) {
1692-
if (!data->files[i]->is_binary &&
1693-
!data->files[i]->is_unmerged) {
1694-
int added = data->files[i]->added;
1695-
int deleted= data->files[i]->deleted;
1696-
if (!data->files[i]->is_renamed &&
1697-
(added + deleted == 0)) {
1698-
total_files--;
1699-
} else {
1700-
adds += added;
1701-
dels += deleted;
1702-
}
1696+
int added = data->files[i]->added;
1697+
int deleted= data->files[i]->deleted;
1698+
1699+
if (data->files[i]->is_unmerged)
1700+
continue;
1701+
if (!data->files[i]->is_renamed && (added + deleted == 0)) {
1702+
total_files--;
1703+
} else {
1704+
adds += added;
1705+
dels += deleted;
17031706
}
17041707
}
17051708
if (options->output_prefix) {
@@ -2399,6 +2402,7 @@ static void builtin_diffstat(const char *name_a, const char *name_b,
23992402
{
24002403
mmfile_t mf1, mf2;
24012404
struct diffstat_file *data;
2405+
int same_contents;
24022406

24032407
data = diffstat_add(diffstat, name_a, name_b);
24042408

@@ -2407,10 +2411,17 @@ static void builtin_diffstat(const char *name_a, const char *name_b,
24072411
return;
24082412
}
24092413

2414+
same_contents = !hashcmp(one->sha1, two->sha1);
2415+
24102416
if (diff_filespec_is_binary(one) || diff_filespec_is_binary(two)) {
24112417
data->is_binary = 1;
2412-
data->added = diff_filespec_size(two);
2413-
data->deleted = diff_filespec_size(one);
2418+
if (same_contents) {
2419+
data->added = 0;
2420+
data->deleted = 0;
2421+
} else {
2422+
data->added = diff_filespec_size(two);
2423+
data->deleted = diff_filespec_size(one);
2424+
}
24142425
}
24152426

24162427
else if (complete_rewrite) {
@@ -2420,7 +2431,7 @@ static void builtin_diffstat(const char *name_a, const char *name_b,
24202431
data->added = count_lines(two->data, two->size);
24212432
}
24222433

2423-
else {
2434+
else if (!same_contents) {
24242435
/* Crazy xdl interfaces.. */
24252436
xpparam_t xpp;
24262437
xdemitconf_t xecfg;

t/t4006-diff-mode.sh

Lines changed: 47 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,23 +8,52 @@ test_description='Test mode change diffs.
88
'
99
. ./test-lib.sh
1010

11-
test_expect_success \
12-
'setup' \
13-
'echo frotz >rezrov &&
14-
git update-index --add rezrov &&
15-
tree=`git write-tree` &&
16-
echo $tree'
17-
18-
test_expect_success \
19-
'chmod' \
20-
'test_chmod +x rezrov &&
21-
git diff-index $tree >current'
22-
23-
sed -e 's/\(:100644 100755\) \('"$_x40"'\) \2 /\1 X X /' <current >check
24-
echo ":100644 100755 X X M rezrov" >expected
25-
26-
test_expect_success \
27-
'verify' \
28-
'test_cmp expected check'
11+
sed_script='s/\(:100644 100755\) \('"$_x40"'\) \2 /\1 X X /'
12+
13+
test_expect_success 'setup' '
14+
echo frotz >rezrov &&
15+
git update-index --add rezrov &&
16+
tree=`git write-tree` &&
17+
echo $tree
18+
'
19+
20+
test_expect_success 'chmod' '
21+
test_chmod +x rezrov &&
22+
git diff-index $tree >current &&
23+
sed -e "$sed_script" <current >check &&
24+
echo ":100644 100755 X X M rezrov" >expected &&
25+
test_cmp expected check
26+
'
27+
28+
test_expect_success 'prepare binary file' '
29+
git commit -m rezrov &&
30+
printf "\00\01\02\03\04\05\06" >binbin &&
31+
git add binbin &&
32+
git commit -m binbin
33+
'
34+
35+
test_expect_success '--stat output after text chmod' '
36+
test_chmod -x rezrov &&
37+
echo " 0 files changed" >expect &&
38+
git diff HEAD --stat >actual &&
39+
test_cmp expect actual
40+
'
41+
42+
test_expect_success '--shortstat output after text chmod' '
43+
git diff HEAD --shortstat >actual &&
44+
test_cmp expect actual
45+
'
46+
47+
test_expect_success '--stat output after binary chmod' '
48+
test_chmod +x binbin &&
49+
echo " 0 files changed" >expect &&
50+
git diff HEAD --stat >actual &&
51+
test_cmp expect actual
52+
'
53+
54+
test_expect_success '--shortstat output after binary chmod' '
55+
git diff HEAD --shortstat >actual &&
56+
test_cmp expect actual
57+
'
2958

3059
test_done

0 commit comments

Comments
 (0)