Skip to content

Commit e18872b

Browse files
keszybzgitster
authored andcommitted
diff --stat: report mode-only changes for binary files like text files
Mode-only changes to binary files without content change were reported as if they were rewritten, but text files in the same situation were reported as "unchanged". Let's treat binary files like text files here, and simply say that they are unchanged. Output of --shortstat is modified in the same way. Reported-by: Martin Mareš <[email protected]> Signed-off-by: Zbigniew Jędrzejewski-Szmek <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 4434e6b commit e18872b

File tree

2 files changed

+24
-22
lines changed

2 files changed

+24
-22
lines changed

diff.c

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1583,8 +1583,12 @@ static void show_stats(struct diffstat_t *data, struct diff_options *options)
15831583
if (data->files[i]->is_binary) {
15841584
fprintf(options->file, "%s", line_prefix);
15851585
show_name(options->file, prefix, name, len);
1586-
fprintf(options->file, " Bin ");
1587-
fprintf(options->file, "%s%"PRIuMAX"%s",
1586+
fprintf(options->file, " Bin");
1587+
if (!added && !deleted) {
1588+
putc('\n', options->file);
1589+
continue;
1590+
}
1591+
fprintf(options->file, " %s%"PRIuMAX"%s",
15881592
del_c, deleted, reset);
15891593
fprintf(options->file, " -> ");
15901594
fprintf(options->file, "%s%"PRIuMAX"%s",
@@ -1657,17 +1661,16 @@ static void show_shortstats(struct diffstat_t *data, struct diff_options *option
16571661
return;
16581662

16591663
for (i = 0; i < data->nr; i++) {
1660-
if (!data->files[i]->is_binary &&
1661-
!data->files[i]->is_unmerged) {
1662-
int added = data->files[i]->added;
1663-
int deleted= data->files[i]->deleted;
1664-
if (!data->files[i]->is_renamed &&
1665-
(added + deleted == 0)) {
1666-
total_files--;
1667-
} else {
1668-
adds += added;
1669-
dels += deleted;
1670-
}
1664+
int added = data->files[i]->added;
1665+
int deleted= data->files[i]->deleted;
1666+
1667+
if (data->files[i]->is_unmerged)
1668+
continue;
1669+
if (!data->files[i]->is_renamed && (added + deleted == 0)) {
1670+
total_files--;
1671+
} else {
1672+
adds += added;
1673+
dels += deleted;
16711674
}
16721675
}
16731676
if (options->output_prefix) {
@@ -2377,8 +2380,13 @@ static void builtin_diffstat(const char *name_a, const char *name_b,
23772380

23782381
if (diff_filespec_is_binary(one) || diff_filespec_is_binary(two)) {
23792382
data->is_binary = 1;
2380-
data->added = diff_filespec_size(two);
2381-
data->deleted = diff_filespec_size(one);
2383+
if (!hashcmp(one->sha1, two->sha1)) {
2384+
data->added = 0;
2385+
data->deleted = 0;
2386+
} else {
2387+
data->added = diff_filespec_size(two);
2388+
data->deleted = diff_filespec_size(one);
2389+
}
23822390
}
23832391

23842392
else if (complete_rewrite) {

t/t4006-diff-mode.sh

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -46,18 +46,12 @@ test_expect_success '--shortstat output after text chmod' '
4646

4747
test_expect_success '--stat output after binary chmod' '
4848
test_chmod +x binbin &&
49-
cat >expect <<-EOF &&
50-
binbin | Bin 1024 -> 1024 bytes
51-
1 file changed, 0 insertions(+), 0 deletions(-)
52-
EOF
49+
echo " 0 files changed" >expect &&
5350
git diff HEAD --stat >actual &&
5451
test_cmp expect actual
5552
'
5653

5754
test_expect_success '--shortstat output after binary chmod' '
58-
cat >expect <<-EOF &&
59-
1 file changed, 0 insertions(+), 0 deletions(-)
60-
EOF
6155
git diff HEAD --shortstat >actual &&
6256
test_cmp expect actual
6357
'

0 commit comments

Comments
 (0)