I J
I J

Reputation: 57

Is there a way to stop `git diff-tool` when return code is non-zero from the diff-tool is non-zero

I am aware of the git config option difftool.prompt true

However, I am wondering if there is a way to stop diffing when return code is non-zero. I am using vim as diff-tool and if I exit using :cq it will return non-zero but the default settings of git do not stop diffing the remaining files when it gets non-zero return code.

I guess this makes sense since some tools will return non-zero code when files differ and most people will not want to stop at this point, however, in my case it will return non-zero only if I vim encounter an error or if I asked it to do and in these cases I will always want git diff-tool to stop.

So is there a way to change the default setting to stop when received a non-zero return code?

Upvotes: 2

Views: 412

Answers (2)

VonC
VonC

Reputation: 1324228

difftool.trustExitCode will also work with git difftool --dir-diff now.

With Git 2.45 (Q2 2024), batch 3, "git difftool --dir-diff"(man) learned to honor the --trust-exit-code option; it used to always exit with 0 and signalled success.

See commit eb84c8b (20 Feb 2024) by Patrick Steinhardt (pks-t).
(Merged by Junio C Hamano -- gitster -- in commit a87469c, 01 Mar 2024)

git-difftool--helper: honor --trust-exit-code with --dir-diff

Reported-by: Jean-Rémy Falleri
Signed-off-by: Patrick Steinhardt

The --trust-exit-code option for git-diff-tool(1) was introduced via 2b52123 ("difftool: add support for --trust-exit-code", 2014-10-26, Git v2.2.0-rc0 -- merge).
When set, it makes us return the exit code of the invoked diff tool when diffing multiple files.
This patch didn't change the code path where --dir-diff was passed because we already returned the exit code of the diff tool unconditionally in that case.

This was changed a month later via c41d3fe ("difftool--helper: add explicit exit statement", 2014-11-20, Git v2.3.0-rc0 -- merge listed in batch #2), where an explicit exit 0 was added to the end of git-difftool--helper.sh.
While the stated intent of that commit was merely a cleanup, it had the consequence that we now to ignore the exit code of the diff tool when --dir-diff was set.
This change in behaviour is thus very likely an unintended side effect of this patch.

Now there are two ways to fix this:

  • We can either restore the original behaviour, which unconditionally returned the exit code of the diffing tool when --dir-diff is passed.
  • Or we can make the --dir-diff case respect the --trust-exit-code flag.

The fact that we have been ignoring exit codes for 7 years by now makes me rather lean towards the latter option.
Furthermore, respecting the flag in one case but not the other would needlessly make the user interface more complex.

Fix the bug so that we also honor --trust-exit-code for dir diffs and adjust the documentation accordingly.

Upvotes: 1

phd
phd

Reputation: 94473

Once:

git difftool --trust-exit-code

Permanet:

git config difftool.trustExitCode true

See the docs: 1, 2.

Upvotes: 3

Related Questions