diff: report dirty submodules 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 slower mode as never considered submodules (and subrepos) as changes with --submodule=diff or --submodule=log, which is inconsistent with --submodule=short (the default). Fix it. 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: David Hull <david.hull@friendbuy.com> Signed-off-by: René Scharfe <l.s.r@web.de> Signed-off-by: Junio C Hamano <gitster@pobox.com>

René Scharfe committed Sep 8, 2024 at 09:08 UTC 11591850ddd5e65d3d0aab22c0a7131b1eb1d6f0
2 files changed +23
diff.c
+2
@@ -3565,6 +3565,7 @@ static void builtin_diff(const char *name_a,
3565 show_submodule_diff_summary(o, one->path ? one->path : two->path,
3566 &one->oid, &two->oid,
3567 two->dirty_submodule);
3568 + o->found_changes = 1;
3569 return;
3570 } else if (o->submodule_format == DIFF_SUBMODULE_INLINE_DIFF &&
3571 (!one->mode || S_ISGITLINK(one->mode)) &&
@@ -3573,6 +3574,7 @@ static void builtin_diff(const char *name_a,
3574 show_submodule_inline_diff(o, one->path ? one->path : two->path,
3575 &one->oid, &two->oid,
3576 two->dirty_submodule);
3577 + o->found_changes = 1;
3578 return;
3579 }
3580
t/t4017-diff-retval.sh
+21
@@ -159,4 +159,25 @@ do
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 +
183 test_done