From ec57a821a3bacdaaaf5eeba55a124bf1a502a13f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Zbigniew=20J=C4=99drzejewski-Szmek?= Date: Tue, 1 May 2012 19:10:12 +0200 Subject: [PATCH 1/5] test: modernize style of t4006 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Zbigniew Jędrzejewski-Szmek Signed-off-by: Junio C Hamano --- t/t4006-diff-mode.sh | 30 ++++++++++++++---------------- 1 file changed, 14 insertions(+), 16 deletions(-) diff --git a/t/t4006-diff-mode.sh b/t/t4006-diff-mode.sh index ff8c2f7532..b85f622e7c 100755 --- a/t/t4006-diff-mode.sh +++ b/t/t4006-diff-mode.sh @@ -8,23 +8,21 @@ test_description='Test mode change diffs. ' . ./test-lib.sh -test_expect_success \ - 'setup' \ - 'echo frotz >rezrov && - git update-index --add rezrov && - tree=`git write-tree` && - echo $tree' +sed_script='s/\(:100644 100755\) \('"$_x40"'\) \2 /\1 X X /' -test_expect_success \ - 'chmod' \ - 'test_chmod +x rezrov && - git diff-index $tree >current' +test_expect_success 'setup' ' + echo frotz >rezrov && + git update-index --add rezrov && + tree=`git write-tree` && + echo $tree +' -sed -e 's/\(:100644 100755\) \('"$_x40"'\) \2 /\1 X X /' check -echo ":100644 100755 X X M rezrov" >expected - -test_expect_success \ - 'verify' \ - 'test_cmp expected check' +test_expect_success 'chmod' ' + test_chmod +x rezrov && + git diff-index $tree >current && + sed -e "$sed_script" check && + echo ":100644 100755 X X M rezrov" >expected && + test_cmp expected check +' test_done From 4434e6ba6c2de994bd28892abe9fb3bdf1641560 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Zbigniew=20J=C4=99drzejewski-Szmek?= Date: Tue, 1 May 2012 19:10:13 +0200 Subject: [PATCH 2/5] tests: check --[short]stat output after chmod MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Add a test to check 'diff --stat' output with a text file after chmod, and the same for a binary file. This demonstrates that text and binary files are treated differently, which can be misleading. While at it, add tests to check --shortstat output, too. Reported-by: Martin Mareš Signed-off-by: Zbigniew Jędrzejewski-Szmek Signed-off-by: Junio C Hamano --- t/t4006-diff-mode.sh | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/t/t4006-diff-mode.sh b/t/t4006-diff-mode.sh index b85f622e7c..392dfeff57 100755 --- a/t/t4006-diff-mode.sh +++ b/t/t4006-diff-mode.sh @@ -25,4 +25,41 @@ test_expect_success 'chmod' ' test_cmp expected check ' +test_expect_success 'prepare binary file' ' + git commit -m rezrov && + dd if=/dev/zero of=binbin bs=1024 count=1 && + git add binbin && + git commit -m binbin +' + +test_expect_success '--stat output after text chmod' ' + test_chmod -x rezrov && + echo " 0 files changed" >expect && + git diff HEAD --stat >actual && + test_cmp expect actual +' + +test_expect_success '--shortstat output after text chmod' ' + git diff HEAD --shortstat >actual && + test_cmp expect actual +' + +test_expect_success '--stat output after binary chmod' ' + test_chmod +x binbin && + cat >expect <<-EOF && + binbin | Bin 1024 -> 1024 bytes + 1 file changed, 0 insertions(+), 0 deletions(-) + EOF + git diff HEAD --stat >actual && + test_cmp expect actual +' + +test_expect_success '--shortstat output after binary chmod' ' + cat >expect <<-EOF && + 1 file changed, 0 insertions(+), 0 deletions(-) + EOF + git diff HEAD --shortstat >actual && + test_cmp expect actual +' + test_done From e18872b2f0fba4cb860c9350bb8b8d8680dfc83b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Zbigniew=20J=C4=99drzejewski-Szmek?= Date: Tue, 1 May 2012 19:10:14 +0200 Subject: [PATCH 3/5] diff --stat: report mode-only changes for binary files like text files MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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š Signed-off-by: Zbigniew Jędrzejewski-Szmek Signed-off-by: Junio C Hamano --- diff.c | 38 +++++++++++++++++++++++--------------- t/t4006-diff-mode.sh | 8 +------- 2 files changed, 24 insertions(+), 22 deletions(-) diff --git a/diff.c b/diff.c index 22288b0106..e61ccd1123 100644 --- a/diff.c +++ b/diff.c @@ -1583,8 +1583,12 @@ static void show_stats(struct diffstat_t *data, struct diff_options *options) if (data->files[i]->is_binary) { fprintf(options->file, "%s", line_prefix); show_name(options->file, prefix, name, len); - fprintf(options->file, " Bin "); - fprintf(options->file, "%s%"PRIuMAX"%s", + fprintf(options->file, " Bin"); + if (!added && !deleted) { + putc('\n', options->file); + continue; + } + fprintf(options->file, " %s%"PRIuMAX"%s", del_c, deleted, reset); fprintf(options->file, " -> "); fprintf(options->file, "%s%"PRIuMAX"%s", @@ -1657,17 +1661,16 @@ static void show_shortstats(struct diffstat_t *data, struct diff_options *option return; for (i = 0; i < data->nr; i++) { - if (!data->files[i]->is_binary && - !data->files[i]->is_unmerged) { - int added = data->files[i]->added; - int deleted= data->files[i]->deleted; - if (!data->files[i]->is_renamed && - (added + deleted == 0)) { - total_files--; - } else { - adds += added; - dels += deleted; - } + int added = data->files[i]->added; + int deleted= data->files[i]->deleted; + + if (data->files[i]->is_unmerged) + continue; + if (!data->files[i]->is_renamed && (added + deleted == 0)) { + total_files--; + } else { + adds += added; + dels += deleted; } } if (options->output_prefix) { @@ -2377,8 +2380,13 @@ static void builtin_diffstat(const char *name_a, const char *name_b, if (diff_filespec_is_binary(one) || diff_filespec_is_binary(two)) { data->is_binary = 1; - data->added = diff_filespec_size(two); - data->deleted = diff_filespec_size(one); + if (!hashcmp(one->sha1, two->sha1)) { + data->added = 0; + data->deleted = 0; + } else { + data->added = diff_filespec_size(two); + data->deleted = diff_filespec_size(one); + } } else if (complete_rewrite) { diff --git a/t/t4006-diff-mode.sh b/t/t4006-diff-mode.sh index 392dfeff57..693bfc4a52 100755 --- a/t/t4006-diff-mode.sh +++ b/t/t4006-diff-mode.sh @@ -46,18 +46,12 @@ test_expect_success '--shortstat output after text chmod' ' test_expect_success '--stat output after binary chmod' ' test_chmod +x binbin && - cat >expect <<-EOF && - binbin | Bin 1024 -> 1024 bytes - 1 file changed, 0 insertions(+), 0 deletions(-) - EOF + echo " 0 files changed" >expect && git diff HEAD --stat >actual && test_cmp expect actual ' test_expect_success '--shortstat output after binary chmod' ' - cat >expect <<-EOF && - 1 file changed, 0 insertions(+), 0 deletions(-) - EOF git diff HEAD --shortstat >actual && test_cmp expect actual ' From 352ca4e105e6fb37cad97b80f2640d28b133ca9d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Zbigniew=20J=C4=99drzejewski-Szmek?= Date: Tue, 1 May 2012 19:10:15 +0200 Subject: [PATCH 4/5] diff --stat: do not run diff on indentical files MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit If two objects are known to be equal, there is no point running the diff. Signed-off-by: Zbigniew Jędrzejewski-Szmek Signed-off-by: Junio C Hamano --- diff.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/diff.c b/diff.c index e61ccd1123..8e61f4bf85 100644 --- a/diff.c +++ b/diff.c @@ -2370,6 +2370,7 @@ static void builtin_diffstat(const char *name_a, const char *name_b, { mmfile_t mf1, mf2; struct diffstat_file *data; + int same_contents; data = diffstat_add(diffstat, name_a, name_b); @@ -2378,9 +2379,11 @@ static void builtin_diffstat(const char *name_a, const char *name_b, return; } + same_contents = !hashcmp(one->sha1, two->sha1); + if (diff_filespec_is_binary(one) || diff_filespec_is_binary(two)) { data->is_binary = 1; - if (!hashcmp(one->sha1, two->sha1)) { + if (same_contents) { data->added = 0; data->deleted = 0; } else { @@ -2396,7 +2399,7 @@ static void builtin_diffstat(const char *name_a, const char *name_b, data->added = count_lines(two->data, two->size); } - else { + else if (!same_contents) { /* Crazy xdl interfaces.. */ xpparam_t xpp; xdemitconf_t xecfg; From 9380aed286df31dcb31fd6ae467b69471c8cf148 Mon Sep 17 00:00:00 2001 From: Johannes Sixt Date: Wed, 2 May 2012 09:36:44 +0200 Subject: [PATCH 5/5] t4006: Windows do not have /dev/zero We only need to have a file with _some_ binary contents; be nice to our Windows friends and avoid using /dev/zero Signed-off-by: Johannes Sixt Signed-off-by: Junio C Hamano --- t/t4006-diff-mode.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/t/t4006-diff-mode.sh b/t/t4006-diff-mode.sh index 693bfc4a52..7a3e1f9a24 100755 --- a/t/t4006-diff-mode.sh +++ b/t/t4006-diff-mode.sh @@ -27,7 +27,7 @@ test_expect_success 'chmod' ' test_expect_success 'prepare binary file' ' git commit -m rezrov && - dd if=/dev/zero of=binbin bs=1024 count=1 && + printf "\00\01\02\03\04\05\06" >binbin && git add binbin && git commit -m binbin '