Skip to content

Commit 7794e09

Browse files
committed
Merge branch 'rs/diff-exit-code-fix' into maint-2.46
In a few corner cases "git diff --exit-code" failed to report "changes" (e.g., renamed without any content change), which has been corrected. * rs/diff-exit-code-fix: diff: report dirty submodules as changes in builtin_diff() diff: report copies and renames as changes in run_diff_cmd()
2 parents e29e5cf + 1159185 commit 7794e09

File tree

2 files changed

+42
-0
lines changed

2 files changed

+42
-0
lines changed

diff.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3565,6 +3565,7 @@ static void builtin_diff(const char *name_a,
35653565
show_submodule_diff_summary(o, one->path ? one->path : two->path,
35663566
&one->oid, &two->oid,
35673567
two->dirty_submodule);
3568+
o->found_changes = 1;
35683569
return;
35693570
} else if (o->submodule_format == DIFF_SUBMODULE_INLINE_DIFF &&
35703571
(!one->mode || S_ISGITLINK(one->mode)) &&
@@ -3573,6 +3574,7 @@ static void builtin_diff(const char *name_a,
35733574
show_submodule_inline_diff(o, one->path ? one->path : two->path,
35743575
&one->oid, &two->oid,
35753576
two->dirty_submodule);
3577+
o->found_changes = 1;
35763578
return;
35773579
}
35783580

@@ -4587,6 +4589,9 @@ static void run_diff_cmd(const struct external_diff *pgm,
45874589
builtin_diff(name, other ? other : name,
45884590
one, two, xfrm_msg, must_show_header,
45894591
o, complete_rewrite);
4592+
if (p->status == DIFF_STATUS_COPIED ||
4593+
p->status == DIFF_STATUS_RENAMED)
4594+
o->found_changes = 1;
45904595
} else {
45914596
fprintf(o->file, "* Unmerged path %s\n", name);
45924597
o->found_changes = 1;

t/t4017-diff-retval.sh

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,4 +143,41 @@ 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+
162+
test_expect_success 'setup dirty subrepo' '
163+
git reset --hard &&
164+
test_create_repo subrepo &&
165+
test_commit -C subrepo subrepo-file &&
166+
test_tick &&
167+
git add subrepo &&
168+
git commit -m subrepo &&
169+
test_commit -C subrepo another-subrepo-file
170+
'
171+
172+
for option in --exit-code --quiet
173+
do
174+
for submodule_format in diff log short
175+
do
176+
opts="$option --submodule=$submodule_format" &&
177+
test_expect_success "git diff $opts returns 1 for dirty subrepo" "
178+
test_expect_code 1 git diff $opts
179+
"
180+
done
181+
done
182+
146183
test_done

0 commit comments

Comments
 (0)