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š <mj@ucw.cz> Signed-off-by: Zbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Zbigniew Jędrzejewski-Szmek committed May 1, 2012 at 19:10 UTC e18872b2f0fba4cb860c9350bb8b8d8680dfc83b
2 files changed +24 -22
diff.c
+23 -15
@@ -1583,8 +1583,12 @@ static void show_stats(struct diffstat_t *data, struct diff_options *options)
1583 if (data->files[i]->is_binary) {
1584 fprintf(options->file, "%s", line_prefix);
1585 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",
1592 del_c, deleted, reset);
1593 fprintf(options->file, " -> ");
1594 fprintf(options->file, "%s%"PRIuMAX"%s",
@@ -1657,17 +1661,16 @@ static void show_shortstats(struct diffstat_t *data, struct diff_options *option
1661 return;
1662
1663 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;
1674 }
1675 }
1676 if (options->output_prefix) {
@@ -2377,8 +2380,13 @@ static void builtin_diffstat(const char *name_a, const char *name_b,
2380
2381 if (diff_filespec_is_binary(one) || diff_filespec_is_binary(two)) {
2382 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 + }
2390 }
2391
2392 else if (complete_rewrite) {
t/t4006-diff-mode.sh
+1 -7
@@ -46,18 +46,12 @@ test_expect_success '--shortstat output after text chmod' '
46
47 test_expect_success '--stat output after binary chmod' '
48 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 &&
50 git diff HEAD --stat >actual &&
51 test_cmp expect actual
52 '
53
54 test_expect_success '--shortstat output after binary chmod' '
58 - cat >expect <<-EOF &&
59 - 1 file changed, 0 insertions(+), 0 deletions(-)
60 - EOF
55 git diff HEAD --shortstat >actual &&
56 test_cmp expect actual
57 '