Skip to contentSkip to conversation

difftool--helper: exit when reading a prompt answer fails

PATCH In review Johannes Sixt 4 participants 9 messages Jun 15, 2016 Documentation

lore
Share & tools
lore
#0 lore raw

An attempt to quit difftool by hitting Ctrl-D (EOF) at its prompt does not quit it, but is treated as if 'yes' was answered to the prompt and all following prompts, which is contrary to the user's intent. Fix the error check.

---
Found while reviewing your latest patch.

I chose the 'foo || return' idiom for the error check, but left the 'if' for the interesting check, because I feel it is more readable than 'test ... && return'.

-- Hannes

 git-difftool--helper.sh | 3 ++-
 t/t7800-difftool.sh     | 8 ++++++++
 2 files changed, 10 insertions(+), 1 deletion(-)
git-difftool--helper.sh +2 -1
index 7ef36b9..aca0413 100755
@@ -49,7 +49,8 @@ launch_merge_tool () {
 		else
 			printf "Launch '%s' [Y/n]: " "$merge_tool"
 		fi
-		if read ans && test "$ans" = n
+		read ans || return
+		if test "$ans" = n
 		then
 			return
 		fi
t/t7800-difftool.sh +8 -0
index dc30a51..9cf5dc9 100755
@@ -301,6 +301,14 @@ test_expect_success PERL 'say no to the second file' '
 	! grep br2 output
 '
 
+test_expect_success PERL 'ending prompt input with EOF' '
+	git difftool -x cat branch </dev/null >output &&
+	! grep master output &&
+	! grep branch output &&
+	! grep m2 output &&
+	! grep br2 output
+'
+
 test_expect_success PERL 'difftool --tool-help' '
 	git difftool --tool-help >output &&
 	grep tool output
Signature · 1 line
2.0.0.12.gbcf935e
#1 lore raw

PATCH v3 1/2 t7800-difftool: use "test_must_fail grep" instead of "! grep"

---
This patch is new since v2.

This series now depends on "difftool--helper: exit when reading a prompt answer fails".

 t/t7800-difftool.sh | 16 ++++++++--------
 1 file changed, 8 insertions(+), 8 deletions(-)
t/t7800-difftool.sh +8 -8
index 9cf5dc9..b5b7f62 100755
@@ -288,8 +288,8 @@ test_expect_success PERL 'say no to the first file' '
 	git difftool -x cat branch <input >output &&
 	grep m2 output &&
 	grep br2 output &&
-	! grep master output &&
-	! grep branch output
+	test_must_fail grep master output &&
+	test_must_fail grep branch output
 '
 
 test_expect_success PERL 'say no to the second file' '
@@ -297,16 +297,16 @@ test_expect_success PERL 'say no to the second file' '
 	git difftool -x cat branch <input >output &&
 	grep master output &&
 	grep branch output &&
-	! grep m2 output &&
-	! grep br2 output
+	test_must_fail grep m2 output &&
+	test_must_fail grep br2 output
 '
 
 test_expect_success PERL 'ending prompt input with EOF' '
 	git difftool -x cat branch </dev/null >output &&
-	! grep master output &&
-	! grep branch output &&
-	! grep m2 output &&
-	! grep br2 output
+	test_must_fail grep master output &&
+	test_must_fail grep branch output &&
+	test_must_fail grep m2 output &&
+	test_must_fail grep br2 output
 '
 
 test_expect_success PERL 'difftool --tool-help' '
Signature · 1 line
2.1.2.557.g1025af0
#2 lore raw

PATCH v3 2/2 difftool: add support for --trust-exit-code

Teach difftool to exit when a diff tool returns a non-zero exit code when either --trust-exit-code is specified or difftool.trustExitCode is true.

Forward exit codes from invoked diff tools to the caller when --trust-exit-code is used.

---
Changes since v2:

This series now depends on "difftool--helper: exit when reading a prompt answer fails".

A missing "git reset -- for-diff" was missing from the test.

Setting and testing of the status variable was moved to be before "shift 7" to make things more cohesive; status setting was previously buried inside the launch_merge_tool function, which made it harder to follow.

 Documentation/git-difftool.txt | 14 ++++++++++++++
 git-difftool--helper.sh        |  6 ++++++
 git-difftool.perl              | 12 ++++++++++++
 t/t7800-difftool.sh            | 44 ++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 76 insertions(+)
Documentation/git-difftool.txt +14 -0
index 11887e6..333cf6f 100644
@@ -91,6 +91,15 @@ instead.  `--no-symlinks` is the default on Windows.
 	the default diff tool will be read from the configured
 	`diff.guitool` variable instead of `diff.tool`.
 
+--[no-]trust-exit-code::
+	'git-difftool' invokes a diff tool individually on each file.
+	Errors reported by the diff tool are ignored by default.
+	Use `--trust-exit-code` to make 'git-difftool' exit when an
+	invoked diff tool returns a non-zero exit code.
++
+'git-difftool' will forward the exit code of the invoked tool when
+'--trust-exit-code' is used.
+
 See linkgit:git-diff[1] for the full list of supported options.
 
 CONFIG VARIABLES
@@ -116,6 +125,11 @@ See the `--tool=<tool>` option above for more details.
 difftool.prompt::
 	Prompt before each invocation of the diff tool.
 
+difftool.trustExitCode::
+	Exit difftool if the invoked diff tool returns a non-zero exit status.
++
+See the `--trust-exit-code` option above for more details.
+
 SEE ALSO
 --------
 linkgit:git-diff[1]::
git-difftool--helper.sh +6 -0
index aca0413..d4fb6df 100755
@@ -85,6 +85,12 @@ else
 	while test $# -gt 6
 	do
 		launch_merge_tool "$1" "$2" "$5"
+		status=$?
+		if test "$status" != 0 &&
+			test "$GIT_DIFFTOOL_TRUST_EXIT_CODE" = true
+		then
+			exit $status
+		fi
 		shift 7
 	done
 fi
git-difftool.perl +12 -0
index 598fcc2..7df7c8a 100755
@@ -342,6 +342,7 @@ sub main
 		symlinks => $^O ne 'cygwin' &&
 				$^O ne 'MSWin32' && $^O ne 'msys',
 		tool_help => undef,
+		trust_exit_code => undef,
 	);
 	GetOptions('g|gui!' => \$opts{gui},
 		'd|dir-diff' => \$opts{dirdiff},
@@ -352,6 +353,8 @@ sub main
 		'no-symlinks' => sub { $opts{symlinks} = 0; },
 		't|tool:s' => \$opts{difftool_cmd},
 		'tool-help' => \$opts{tool_help},
+		'trust-exit-code' => \$opts{trust_exit_code},
+		'no-trust-exit-code' => sub { $opts{trust_exit_code} = 0; },
 		'x|extcmd:s' => \$opts{extcmd});
 
 	if (defined($opts{help})) {
@@ -383,6 +386,15 @@ sub main
 		}
 	}
 
+	if (!defined $opts{trust_exit_code}) {
+		$opts{trust_exit_code} = Git::config_bool('difftool.trustExitCode');
+	}
+	if ($opts{trust_exit_code}) {
+		$ENV{GIT_DIFFTOOL_TRUST_EXIT_CODE} = 'true';
+	} else {
+		$ENV{GIT_DIFFTOOL_TRUST_EXIT_CODE} = 'false';
+	}
+
 	# In directory diff mode, 'git-difftool--helper' is called once
 	# to compare the a/b directories.  In file diff mode, 'git diff'
 	# will invoke a separate instance of 'git-difftool--helper' for
t/t7800-difftool.sh +44 -0

50 lines not on this page · raw message

Signature · 1 line
2.1.2.557.g1025af0
#3 lore raw

Re: [PATCH v3 2/2] difftool: add support for --trust-exit-code

Review

Quoted 4 lines · David Aguilar
(quoted diff) +4 -0
+write_script .git/fail-right-file <<\EOF
+echo "$2"
+exit 1
+EOF

This should be inside the next one, no?

Review

Quoted 10 lines
(quoted diff) +10 -0
+test_expect_success PERL 'difftool stops on error with --trust-exit-code' '
+	>for-diff &&
+	git add for-diff &&
+	echo file>expect &&
+	test_must_fail git difftool -y --trust-exit-code \
+		--extcmd .git/fail-right-file branch >actual &&
+	test_cmp expect actual &&
+	git reset -- for-diff &&
+	rm -f for-diff .git/fail-right-file
+'

In other words, this squashed in.

 t/t7800-difftool.sh | 17 ++++++++---------
 1 file changed, 8 insertions(+), 9 deletions(-)
t/t7800-difftool.sh +8 -9
index 4b2f611..69bde7a 100755
@@ -104,20 +104,19 @@ test_expect_success PERL 'difftool ignores exit code with --no-trust-exit-code'
 	git difftool -y --no-trust-exit-code -t error branch
 '
 
-write_script .git/fail-right-file <<\EOF
-echo "$2"
-exit 1
-EOF
-
 test_expect_success PERL 'difftool stops on error with --trust-exit-code' '
+	test_when_finished "rm -f for-diff .git/fail-right-file" &&
+	test_when_finished "git reset -- for-diff" &&
+	write_script .git/fail-right-file <<-\EOF &&
+	echo "$2"
+	exit 1
+	EOF
 	>for-diff &&
 	git add for-diff &&
-	echo file>expect &&
+	echo file >expect &&
 	test_must_fail git difftool -y --trust-exit-code \
 		--extcmd .git/fail-right-file branch >actual &&
-	test_cmp expect actual &&
-	git reset -- for-diff &&
-	rm -f for-diff .git/fail-right-file
+	test_cmp expect actual
 '
 
 test_expect_success PERL 'difftool honors --gui' '
#4 lore raw

Re: [PATCH v3 2/2] difftool: add support for --trust-exit-code

Quoted 9 lines · Junio C Hamano

t/t7800-difftool.sh test_expect_success PERL 'difftool ignores exit code with --no-trust-exit-code' · line 113

Quoted 4 lines · David Aguilar
t/t7800-difftool.sh +4 -0
+write_script .git/fail-right-file <<\EOF
+echo "$2"
+exit 1
+EOF

This should be inside the next one, no?

Quoted 12 lines

Review

Quoted 10 lines
(quoted diff) +10 -0
+test_expect_success PERL 'difftool stops on error with --trust-exit-code' '
+	>for-diff &&
+	git add for-diff &&
+	echo file>expect &&
+	test_must_fail git difftool -y --trust-exit-code \
+		--extcmd .git/fail-right-file branch >actual &&
+	test_cmp expect actual &&
+	git reset -- for-diff &&
+	rm -f for-diff .git/fail-right-file
+'

In other words, this squashed in.

Yes, please. And drop patch 1/2, of course.
Thanks for the clarification.

Quoted 37 lines
 t/t7800-difftool.sh | 17 ++++++++---------
 1 file changed, 8 insertions(+), 9 deletions(-)
t/t7800-difftool.sh +8 -9
index 4b2f611..69bde7a 100755
@@ -104,20 +104,19 @@ test_expect_success PERL 'difftool ignores exit code with --no-trust-exit-code'
 	git difftool -y --no-trust-exit-code -t error branch
 '
 
-write_script .git/fail-right-file <<\EOF
-echo "$2"
-exit 1
-EOF
-
 test_expect_success PERL 'difftool stops on error with --trust-exit-code' '
+	test_when_finished "rm -f for-diff .git/fail-right-file" &&
+	test_when_finished "git reset -- for-diff" &&
+	write_script .git/fail-right-file <<-\EOF &&
+	echo "$2"
+	exit 1
+	EOF
 	>for-diff &&
 	git add for-diff &&
-	echo file>expect &&
+	echo file >expect &&
 	test_must_fail git difftool -y --trust-exit-code \
 		--extcmd .git/fail-right-file branch >actual &&
-	test_cmp expect actual &&
-	git reset -- for-diff &&
-	rm -f for-diff .git/fail-right-file
+	test_cmp expect actual
 '
 
 test_expect_success PERL 'difftool honors --gui' '
Signature · 1 line
David
#5 lore raw
In reply to Johannes Sixt
Quoted 8 lines · Johannes Sixt

An attempt to quit difftool by hitting Ctrl-D (EOF) at its prompt does not quit it, but is treated as if 'yes' was answered to the prompt and all following prompts, which is contrary to the user's intent. Fix the error check.

---
Found while reviewing your latest patch.

Thanks for the careful review.
I have one small question about the test below.

I'll base the reroll of my patch on top of this one so that I'm including your new test while developing.

Junio, can you please queue this patch before my upcoming v3 patch when I reroll?

I also noticed that I forgot to "git reset --hard" after doing "git add" in one of the tests I introduced so I'll be adjusting that as well in v3.

Quoted 29 lines

I chose the 'foo || return' idiom for the error check, but left the 'if' for the interesting check, because I feel it is more readable than 'test ... && return'.

-- Hannes

 git-difftool--helper.sh | 3 ++-
 t/t7800-difftool.sh     | 8 ++++++++
 2 files changed, 10 insertions(+), 1 deletion(-)
git-difftool--helper.sh +2 -1
index 7ef36b9..aca0413 100755
@@ -49,7 +49,8 @@ launch_merge_tool () {
 		else
 			printf "Launch '%s' [Y/n]: " "$merge_tool"
 		fi
-		if read ans && test "$ans" = n
+		read ans || return
+		if test "$ans" = n
 		then
 			return
 		fi
t/t7800-difftool.sh +0 -0
index dc30a51..9cf5dc9 100755

t/t7800-difftool.sh test_expect_success PERL 'say no to the second file' ' · line 310

Quoted 10 lines
t/t7800-difftool.sh +7 -0
@@ -301,6 +301,14 @@ test_expect_success PERL 'say no to the second file' '
 	! grep br2 output
 '
 
+test_expect_success PERL 'ending prompt input with EOF' '
+	git difftool -x cat branch </dev/null >output &&
+	! grep master output &&
+	! grep branch output &&
+	! grep m2 output &&
+	! grep br2 output
+'

Should we use "test_must_fail grep ..." instead of "! grep ..." here?

Signature · 1 line
David
#6 lore raw
Quoted 13 lines · David Aguilar
Quoted 8 lines · Johannes Sixt

An attempt to quit difftool by hitting Ctrl-D (EOF) at its prompt does not quit it, but is treated as if 'yes' was answered to the prompt and all following prompts, which is contrary to the user's intent. Fix the error check.

---
Found while reviewing your latest patch.

Thanks for the careful review.
I have one small question about the test below.

[snip]

Quoted 8 lines
Quoted 8 lines
t/t7800-difftool.sh +0 -0
index dc30a51..9cf5dc9 100755
@@ -301,6 +301,14 @@ test_expect_success PERL 'say no to the second file' '
 	! grep br2 output
 '
 
Quoted 9 lines

t/t7800-difftool.sh test_expect_success PERL 'say no to the second file' ' · line 310

Quoted 7 lines
t/t7800-difftool.sh +7 -0
+test_expect_success PERL 'ending prompt input with EOF' '
+	git difftool -x cat branch </dev/null >output &&
+	! grep master output &&
+	! grep branch output &&
+	! grep m2 output &&
+	! grep br2 output
+'

Should we use "test_must_fail grep ..." instead of "! grep ..." here?

Nevermind, this is good as-is.
Using "! grep" is consistent with the rest of the tests in t7800.

What I'll do is add a follow-up patch in my upcoming reroll that swaps all the "! grep" lines to "test_must_fail grep" in one step.

Signature · 1 line
David
#7 lore raw

David Aguilar schrieb am 27.10.2014 um 02:10:

Quoted 36 lines · David Aguilar
Quoted 13 lines · Johannes Sixt
Quoted 8 lines

An attempt to quit difftool by hitting Ctrl-D (EOF) at its prompt does not quit it, but is treated as if 'yes' was answered to the prompt and all following prompts, which is contrary to the user's intent. Fix the error check.

---
Found while reviewing your latest patch.

Thanks for the careful review.
I have one small question about the test below.

[snip]

Quoted 8 lines
Quoted 8 lines
t/t7800-difftool.sh +0 -0
index dc30a51..9cf5dc9 100755
@@ -301,6 +301,14 @@ test_expect_success PERL 'say no to the second file' '
 	! grep br2 output
 '
 
Quoted 9 lines

t/t7800-difftool.sh test_expect_success PERL 'say no to the second file' ' · line 310

Quoted 7 lines
t/t7800-difftool.sh +7 -0
+test_expect_success PERL 'ending prompt input with EOF' '
+	git difftool -x cat branch </dev/null >output &&
+	! grep master output &&
+	! grep branch output &&
+	! grep m2 output &&
+	! grep br2 output
+'

Should we use "test_must_fail grep ..." instead of "! grep ..." here?

Quoted 6 lines

Nevermind, this is good as-is.
Using "! grep" is consistent with the rest of the tests in t7800.

What I'll do is add a follow-up patch in my upcoming reroll that swaps all the "! grep" lines to "test_must_fail grep" in one step.

Don't do that ;)

test_must_fail is meant for testing (git) commands such that a "failure return code" is marked as "success", whereas a failure to run the command is still capturd and marked as a failed test.

For non-git commands like grep sed etc. which we do not perform tests *on* (but only *with*), the simple negator "!" is fine and preferable.

Michael who has sinned in the past, but repented

#8 lore raw
In reply to David Aguilar
Quoted 8 lines · David Aguilar
Quoted 8 lines
t/t7800-difftool.sh +0 -0
index dc30a51..9cf5dc9 100755
@@ -301,6 +301,14 @@ test_expect_success PERL 'say no to the second file' '
 	! grep br2 output
 '
 
Quoted 9 lines

t/t7800-difftool.sh test_expect_success PERL 'say no to the second file' ' · line 310

Quoted 7 lines
t/t7800-difftool.sh +7 -0
+test_expect_success PERL 'ending prompt input with EOF' '
+	git difftool -x cat branch </dev/null >output &&
+	! grep master output &&
+	! grep branch output &&
+	! grep m2 output &&
+	! grep br2 output
+'

Should we use "test_must_fail grep ..." instead of "! grep ..." here?

NO. We do not expect system-supplied "grep" to dump core and declare it as a test failure. test_must_fail is for catching an expected non-zero status exit from git commands, i.e. when we expect our binary to correctly notice some condition and report that fact with non-zero exit status, we do not want to mistake the binary segfaulting as working correctly.