]> git.ipfire.org Git - thirdparty/git.git/commitdiff
diff: report modified binary files as changes in builtin_diff()
authorRené Scharfe <l.s.r@web.de>
Sat, 21 Sep 2024 15:09:54 +0000 (17:09 +0200)
committerJunio C Hamano <gitster@pobox.com>
Mon, 23 Sep 2024 16:41:07 +0000 (09:41 -0700)
The diff machinery has two ways to detect changes to set the exit code:
Just comparing hashes and comparing blob contents.  The latter is needed
if certain changes have to be ignored, e.g. with --ignore-space-change
or --ignore-matching-lines.  It's enabled by the diff_options flag
diff_from_contents.

The code for handling binary files added by 1aaf69e669 (diff: shortcut
for diff'ing two binary SHA-1 objects, 2014-08-16) always uses a quick
hash-only comparison, even if the slow way is taken.  We need it to
report a hash difference as a change for the purpose of setting the
exit code, though, but it never did.  Fix that.

d7b97b7185 (diff: let external diffs report that changes are
uninteresting, 2024-06-09) set diff_from_contents if external diff
programs are allowed.  This is the default e.g. for git diff, and so
that change exposed the inconsistency much more widely.

Reported-by: Kohei Shibata <shiba200712@gmail.com>
Signed-off-by: René Scharfe <l.s.r@web.de>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
diff.c
t/t4017-diff-retval.sh

diff --git a/diff.c b/diff.c
index a83409944b0243c917a8bba287f979fec0ba3d78..6da7bfd0c6856fa42c3870f31a33a474b57b987d 100644 (file)
--- a/diff.c
+++ b/diff.c
@@ -3673,6 +3673,7 @@ static void builtin_diff(const char *name_a,
                        emit_diff_symbol(o, DIFF_SYMBOL_BINARY_FILES,
                                         sb.buf, sb.len, 0);
                        strbuf_release(&sb);
+                       o->found_changes = 1;
                        goto free_ab_and_return;
                }
                if (fill_mmfile(o->repo, &mf1, one) < 0 ||
index d644310e22a5d17b7ba0b57d6bf308e777be1882..1cea73ef5a31a5c6d316f8bb51fd87e478a7a87d 100755 (executable)
@@ -145,6 +145,14 @@ test_expect_success 'option errors are not confused by --exit-code' '
 
 for option in --exit-code --quiet
 do
+       test_expect_success "git diff $option returns 1 for changed binary file" "
+               test_when_finished 'rm -f .gitattributes' &&
+               git reset --hard &&
+               echo a binary >.gitattributes &&
+               echo 2 >>a &&
+               test_expect_code 1 git diff $option
+       "
+
        test_expect_success "git diff $option returns 1 for copied file" "
                git reset --hard &&
                cp a copy &&