]> git.ipfire.org Git - thirdparty/git.git/commitdiff
diff: restore redirection to /dev/null for diff_from_contents
authorJeff King <peff@peff.net>
Fri, 17 Oct 2025 08:36:41 +0000 (04:36 -0400)
committerJunio C Hamano <gitster@pobox.com>
Fri, 17 Oct 2025 18:41:50 +0000 (11:41 -0700)
In --quiet mode, since we produce only an exit code for "something was
changed" and no actual output, we can often get by with just a
tree-level diff. However, certain options require us to actually look at
the file contents (e.g., if we are ignoring whitespace changes). We have
a flag "diff_from_contents" for that, and if it is set we call
diff_flush() on each path.

To avoid producing any output (since we were asked to be --quiet), we
traditionally just redirected the output to /dev/null. That changed in
b55e6d36eb (diff: ensure consistent diff behavior with ignore options,
2025-08-08), which replaced that with a "dry_run" flag. In theory, with
dry_run set, we should produce no output. But it carries a risk of
regression: if we forget to respect dry_run in any of the output paths,
we'll accidentally produce output.

And indeed, there is at least one such regression in that commit, as it
covered only the case where we actually call into xdiff, and not
creation or deletion diffs, where we manually generate the headers. We
even test this case in t4035, but only with diff-tree, which does not
show the bug by default because it does not require diff_from_contents.
But git-diff does, because it allows external diff programs by default
(so we must dig into each diff filepair to decide if it requires running
an external diff that may declare two distinct blobs to actually be the
same).

We should fix all of those code paths to respect dry_run correctly, but
in the meantime we can protect ourselves more fully by restoring the
redirection to /dev/null. This gives us an extra layer of protection
against regressions dues to other code paths we've missed.

Though the original issue was reported with "git diff" (and due to its
default of --ext-diff), I've used "diff-tree -w" in the new test. It
triggers the same issue, but I think the fact that "-w" implies
diff_from_contents is a bit more obvious, and fits in with the rest of
t4035.

Reported-by: Jake Zimmerman <jake@zimmerman.io>
Signed-off-by: Jeff King <peff@peff.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
diff.c
t/t4035-diff-quiet.sh

diff --git a/diff.c b/diff.c
index cb04a9a6f26f5cbf163e5b459af6a27572464c1e..9b8d658b9ebb8e92f4e4b4c8c177529f47a22c92 100644 (file)
--- a/diff.c
+++ b/diff.c
@@ -6876,6 +6876,15 @@ void diff_flush(struct diff_options *options)
        if (output_format & DIFF_FORMAT_NO_OUTPUT &&
            options->flags.exit_with_status &&
            options->flags.diff_from_contents) {
+               /*
+                * run diff_flush_patch for the exit status. setting
+                * options->file to /dev/null should be safe, because we
+                * aren't supposed to produce any output anyway.
+                */
+               diff_free_file(options);
+               options->file = xfopen("/dev/null", "w");
+               options->close_file = 1;
+               options->color_moved = 0;
                for (i = 0; i < q->nr; i++) {
                        struct diff_filepair *p = q->queue[i];
                        if (check_pair_status(p))
index 0352bf81a90a38adf14fb7a980c98600e1f650b2..35eaf0855f86a55b23ccca1ec40cd406b48245b1 100755 (executable)
@@ -50,6 +50,10 @@ test_expect_success 'git diff-tree HEAD HEAD' '
        test_expect_code 0 git diff-tree --quiet HEAD HEAD >cnt &&
        test_line_count = 0 cnt
 '
+test_expect_success 'git diff-tree -w HEAD^ HEAD' '
+       test_expect_code 1 git diff-tree --quiet -w HEAD^ HEAD >cnt &&
+       test_line_count = 0 cnt
+'
 test_expect_success 'git diff-files' '
        test_expect_code 0 git diff-files --quiet >cnt &&
        test_line_count = 0 cnt