aboutsummaryrefslogtreecommitdiffstats
path: root/t/t4020-diff-external.sh
diff options
context:
space:
mode:
authorRené Scharfe <l.s.r@web.de>2024-06-09 09:41:44 +0200
committerJunio C Hamano <gitster@pobox.com>2024-06-10 09:20:46 -0700
commitd7b97b7185521e3b9364b3abc6553df2480da173 (patch)
tree534350cc10136e821f9713da1f2e9ec32c4ac9b1 /t/t4020-diff-external.sh
parentuserdiff: add and use struct external_diff (diff)
downloadgit-d7b97b7185521e3b9364b3abc6553df2480da173.tar.gz
git-d7b97b7185521e3b9364b3abc6553df2480da173.zip
diff: let external diffs report that changes are uninteresting
The options --exit-code and --quiet instruct git diff to indicate whether it found any significant changes by exiting with code 1 if it did and 0 if there were none. Currently this doesn't work if external diff programs are involved, as we have no way to learn what they found. Add that ability in the form of the new configuration options diff.trustExitCode and diff.<driver>.trustExitCode and the environment variable GIT_EXTERNAL_DIFF_TRUST_EXIT_CODE. They pair with the config options diff.external and diff.<driver>.command and the environment variable GIT_EXTERNAL_DIFF, respectively. The new options are off by default, keeping the old behavior. Enabling them indicates that the external diff returns exit code 1 if it finds significant changes and 0 if it doesn't, like diff(1). The name of the new options is taken from the git difftool and mergetool options of similar purpose. (There they enable passing on the exit code of a diff tool and to infer whether a merge done by a merge tool is successful.) The new feature sets the diff flag diff_from_contents in diff_setup_done() if we need the exit code and are allowed to call external diffs. This disables the optimization that avoids calling the program with --quiet. Add it back by skipping the call if the external diff is not able to report empty diffs. We can only do that check after evaluating the file-specific attributes in run_external_diff(). If we do run the external diff with --quiet, send its output to /dev/null. I considered checking the output of the external diff to check whether its empty. It was added as 11be65cfa4 (diff: fix --exit-code with external diff, 2024-05-05) and quickly reverted, as it does not work with external diffs that do not write to stdout. There's no reason why a graphical diff tool would even need to write anything there at all. I also considered using a non-zero exit code for empty diffs, which could be done without adding new configuration options. We'd need to disable the optimization that allows git diff --quiet to skip calling external diffs, though -- that might be quite surprising if graphical diff programs are involved. And assigning the opposite meaning of the exit codes compared to diff(1) and git diff --exit-code to the external diff can cause unnecessary confusion. Suggested-by: Phillip Wood <phillip.wood123@gmail.com> Signed-off-by: René Scharfe <l.s.r@web.de> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Diffstat (limited to 't/t4020-diff-external.sh')
-rwxr-xr-xt/t4020-diff-external.sh33
1 files changed, 23 insertions, 10 deletions
diff --git a/t/t4020-diff-external.sh b/t/t4020-diff-external.sh
index 4070523cdb..3baa52a9bf 100755
--- a/t/t4020-diff-external.sh
+++ b/t/t4020-diff-external.sh
@@ -177,15 +177,17 @@ check_external_diff () {
expect_out=$2
expect_err=$3
command_code=$4
- shift 4
+ trust_exit_code=$5
+ shift 5
options="$@"
command="echo output; exit $command_code;"
- desc="external diff '$command'"
+ desc="external diff '$command' with trustExitCode=$trust_exit_code"
with_options="${options:+ with }$options"
test_expect_success "$desc via attribute$with_options" "
test_config diff.foo.command \"$command\" &&
+ test_config diff.foo.trustExitCode $trust_exit_code &&
echo \"file diff=foo\" >.gitattributes &&
test_expect_code $expect_code git diff $options >out 2>err &&
test_cmp $expect_out out &&
@@ -194,6 +196,7 @@ check_external_diff () {
test_expect_success "$desc via diff.external$with_options" "
test_config diff.external \"$command\" &&
+ test_config diff.trustExitCode $trust_exit_code &&
>.gitattributes &&
test_expect_code $expect_code git diff $options >out 2>err &&
test_cmp $expect_out out &&
@@ -204,6 +207,7 @@ check_external_diff () {
>.gitattributes &&
test_expect_code $expect_code env \
GIT_EXTERNAL_DIFF=\"$command\" \
+ GIT_EXTERNAL_DIFF_TRUST_EXIT_CODE=$trust_exit_code \
git diff $options >out 2>err &&
test_cmp $expect_out out &&
test_cmp $expect_err err
@@ -216,14 +220,23 @@ test_expect_success 'setup output files' '
echo "fatal: external diff died, stopping at file" >error
'
-check_external_diff 0 output empty 0
-check_external_diff 128 output error 1
-
-check_external_diff 1 output empty 0 --exit-code
-check_external_diff 128 output error 1 --exit-code
-
-check_external_diff 1 empty empty 0 --quiet
-check_external_diff 1 empty empty 1 --quiet # we don't even call the program
+check_external_diff 0 output empty 0 off
+check_external_diff 128 output error 1 off
+check_external_diff 0 output empty 0 on
+check_external_diff 0 output empty 1 on
+check_external_diff 128 output error 2 on
+
+check_external_diff 1 output empty 0 off --exit-code
+check_external_diff 128 output error 1 off --exit-code
+check_external_diff 0 output empty 0 on --exit-code
+check_external_diff 1 output empty 1 on --exit-code
+check_external_diff 128 output error 2 on --exit-code
+
+check_external_diff 1 empty empty 0 off --quiet
+check_external_diff 1 empty empty 1 off --quiet # we don't even call the program
+check_external_diff 0 empty empty 0 on --quiet
+check_external_diff 1 empty empty 1 on --quiet
+check_external_diff 128 empty error 2 on --quiet
echo NULZbetweenZwords | perl -pe 'y/Z/\000/' > file