git@vger.kernel.org mailing list mirror (one of many)
 help / color / mirror / code / Atom feed
From: "René Scharfe" <l.s.r@web.de>
To: German Lashevich <german.lashevich@gmail.com>, git@vger.kernel.org
Subject: Re: Possible git-diff bug when using exit-code with diff filters
Date: Sun, 21 Apr 2024 12:42:18 +0200	[thread overview]
Message-ID: <82561c70-ec33-41bf-b036-52310ffc1926@web.de> (raw)
In-Reply-To: <CACDhgro3KXD0O9ZdE1q46jmXE0O=vf-Z+ZX50WMqmRHAeowGAA@mail.gmail.com>

Am 20.04.24 um 03:13 schrieb German Lashevich:
>> What did you do before the bug happened? (Steps to reproduce your issue)
>
> I configured a diff filter via gitattributes to use a custom script that,
> sometimes, can change the files being compared in a way that there are no
> differences between them.
> Then I used `git diff --exit-code` on the changed file.
>
>> What did you expect to happen? (Expected behavior)
>
> I expected `git diff --exit-code` to return 0, since there are no differences
> between the files after the filter is applied.
>
>> What happened instead? (Actual behavior)
>
> `git diff --exit-code` correctly produces no output, but returns 1.
>
>> What's different between what you expected and what actually happened?
>
> The difference is that `git diff --exit-code`, instead of returning 0, returns
> 1 even when there is no output.
>
>> Anything else you want to add:
>
> I have prepared a repository with a test case that reproduces the issue.
> You can find it at https://github.com/Zebradil/git-diff-exit-code-bug-repro
> The Readme file in the repository contains instructions on how to reproduce
> the issue.

You can more easily reproduce it by setting the environment variable
GIT_EXTERNAL_DIFF or the configuration setting diff.external -- no
attributes needed.

The output of external diffs does not influence the exit code currently.
We don't even have a way to find out if there was any output.

The patch below pipes it through a buffer and sets found_changes only if
something was written.  It fixes the error code for external diffs
producing no output, but breaks several am, pull and merge tests (t4150,
t4151, t5520, t6424, t6430, t7611).  So diff_from_contents doesn't work
quite as I expected.

And slurping in the whole output of external diffs is inefficient, of
course.  That could be fixed later by using a small buffer and writing
directly as we read.

René


diff --git a/diff.c b/diff.c
index 108c187577..76631644e5 100644
--- a/diff.c
+++ b/diff.c
@@ -40,6 +40,7 @@
 #include "setup.h"
 #include "strmap.h"
 #include "ws.h"
+#include "write-or-die.h"

 #ifdef NO_FAST_WORKING_DIRECTORY
 #define FAST_WORKING_DIRECTORY 0
@@ -4404,8 +4405,18 @@ static void run_external_diff(const char *pgm,
 	diff_free_filespec_data(one);
 	diff_free_filespec_data(two);
 	cmd.use_shell = 1;
-	if (run_command(&cmd))
-		die(_("external diff died, stopping at %s"), name);
+	if (o->flags.exit_with_status) {
+		struct strbuf out = STRBUF_INIT;
+		if (capture_command(&cmd, &out, 0))
+			die(_("external diff died, stopping at %s"), name);
+		if (out.len)
+			o->found_changes = 1;
+		write_or_die(1, out.buf, out.len);
+		strbuf_release(&out);
+	} else {
+		if (run_command(&cmd))
+			die(_("external diff died, stopping at %s"), name);
+	}

 	remove_tempfile();
 }
@@ -4851,6 +4862,7 @@ void diff_setup_done(struct diff_options *options)
 	 */

 	if ((options->xdl_opts & XDF_WHITESPACE_FLAGS) ||
+	    options->flags.exit_with_status ||
 	    options->ignore_regex_nr)
 		options->flags.diff_from_contents = 1;
 	else
diff --git a/t/t4020-diff-external.sh b/t/t4020-diff-external.sh
index fdd865f7c3..26430ca66b 100755
--- a/t/t4020-diff-external.sh
+++ b/t/t4020-diff-external.sh
@@ -172,6 +172,14 @@ test_expect_success 'no diff with -diff' '
 	grep Binary out
 '

+test_expect_success 'diff.external and --exit-code with output' '
+	test_expect_code 1 git -c diff.external=echo diff --exit-code
+'
+
+test_expect_success 'diff.external and --exit-code without output' '
+	git -c diff.external=true diff --exit-code
+'
+
 echo NULZbetweenZwords | perl -pe 'y/Z/\000/' > file

 test_expect_success 'force diff with "diff"' '



  reply	other threads:[~2024-04-21 10:42 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-04-20  1:13 Possible git-diff bug when using exit-code with diff filters German Lashevich
2024-04-21 10:42 ` René Scharfe [this message]
2024-04-21 18:17   ` Junio C Hamano
2024-04-21 18:32     ` rsbecker
2024-04-21 19:09       ` Junio C Hamano
2024-04-21 20:18         ` rsbecker
2024-05-05 10:19     ` René Scharfe
2024-05-06 17:22       ` Junio C Hamano
2024-05-05 10:19   ` [PATCH 1/2] diff: report unmerged paths as changes in run_diff_cmd() René Scharfe
2024-05-05 10:20   ` [PATCH 2/2] diff: fix --exit-code with external diff René Scharfe
2024-05-05 15:25     ` Phillip Wood
2024-05-06 17:31       ` Junio C Hamano
2024-05-06 18:23       ` René Scharfe
2024-05-08 15:25         ` phillip.wood123
2024-05-11 20:32           ` René Scharfe
2024-05-12  9:38             ` René Scharfe

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: http://vger.kernel.org/majordomo-info.html

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=82561c70-ec33-41bf-b036-52310ffc1926@web.de \
    --to=l.s.r@web.de \
    --cc=german.lashevich@gmail.com \
    --cc=git@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://80x24.org/mirrors/git.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).