Skip to content

Commit 87cf960

Browse files
rscharfegitster
authored andcommitted
diff: report copies and renames as changes in run_diff_cmd()
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 slower mode has never considered copies and renames to be changes, which is inconsistent with the quicker one. Fix it. Even if we ignore the file contents (because it's empty or contains only ignored lines), there's still the meta data change of adding or changing a filename, so we need to report it in the exit code. 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: Jorge Luis Martinez Gomez <[email protected]> Signed-off-by: René Scharfe <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 39bf06a commit 87cf960

File tree

2 files changed

+19
-0
lines changed

2 files changed

+19
-0
lines changed

diff.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4587,6 +4587,9 @@ static void run_diff_cmd(const struct external_diff *pgm,
45874587
builtin_diff(name, other ? other : name,
45884588
one, two, xfrm_msg, must_show_header,
45894589
o, complete_rewrite);
4590+
if (p->status == DIFF_STATUS_COPIED ||
4591+
p->status == DIFF_STATUS_RENAMED)
4592+
o->found_changes = 1;
45904593
} else {
45914594
fprintf(o->file, "* Unmerged path %s\n", name);
45924595
o->found_changes = 1;

t/t4017-diff-retval.sh

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,4 +143,20 @@ test_expect_success 'option errors are not confused by --exit-code' '
143143
grep '^usage:' err
144144
'
145145

146+
for option in --exit-code --quiet
147+
do
148+
test_expect_success "git diff $option returns 1 for copied file" "
149+
git reset --hard &&
150+
cp a copy &&
151+
git add copy &&
152+
test_expect_code 1 git diff $option --cached --find-copies-harder
153+
"
154+
155+
test_expect_success "git diff $option returns 1 for renamed file" "
156+
git reset --hard &&
157+
git mv a renamed &&
158+
test_expect_code 1 git diff $option --cached
159+
"
160+
done
161+
146162
test_done

0 commit comments

Comments
 (0)