Skip to content

Commit 9a41735

Browse files
rscharfegitster
authored andcommitted
diff: report modified binary files as changes in builtin_diff()
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 1aaf69e (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. d7b97b7 (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 <[email protected]> Signed-off-by: René Scharfe <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 1159185 commit 9a41735

File tree

2 files changed

+9
-0
lines changed

2 files changed

+9
-0
lines changed

diff.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3673,6 +3673,7 @@ static void builtin_diff(const char *name_a,
36733673
emit_diff_symbol(o, DIFF_SYMBOL_BINARY_FILES,
36743674
sb.buf, sb.len, 0);
36753675
strbuf_release(&sb);
3676+
o->found_changes = 1;
36763677
goto free_ab_and_return;
36773678
}
36783679
if (fill_mmfile(o->repo, &mf1, one) < 0 ||

t/t4017-diff-retval.sh

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,14 @@ test_expect_success 'option errors are not confused by --exit-code' '
145145

146146
for option in --exit-code --quiet
147147
do
148+
test_expect_success "git diff $option returns 1 for changed binary file" "
149+
test_when_finished 'rm -f .gitattributes' &&
150+
git reset --hard &&
151+
echo a binary >.gitattributes &&
152+
echo 2 >>a &&
153+
test_expect_code 1 git diff $option
154+
"
155+
148156
test_expect_success "git diff $option returns 1 for copied file" "
149157
git reset --hard &&
150158
cp a copy &&

0 commit comments

Comments
 (0)