Skip to contentSkip to conversation

Quit git difftool on error code

PATCH New Adri Farr 3 participants 4 messages Jun 15, 2016 Documentation

lore
Share & tools
lore

Adri Farr

author
#0 lore raw

Hello!,

Pardon my ignorance, but I haven't found the solution anywhere. Is there a way to make git difftool quit when it receives an error code? I have vim as my difftool, and if I quit with an error (:cq) when there are pending files to be diffed, git difftool doesn't quit and keeps pushing me files to be diffed until there are no more pending files. What is the best way to get out of it?

Thank you

#1 lore raw

PATCH 1/1 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.

---
 Documentation/git-difftool.txt | 14 ++++++++++++++
 git-difftool--helper.sh        |  7 +++++++
 git-difftool.perl              | 12 ++++++++++++
 t/t7800-difftool.sh            | 43 ++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 76 insertions(+)
Documentation/git-difftool.txt +14 -0
index 11887e6..dc843d3 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 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 +7 -0
index 7ef36b9..fdbd768 100755
@@ -62,6 +62,8 @@ launch_merge_tool () {
 	else
 		run_merge_tool "$merge_tool"
 	fi
+	status=$?
+	return $status
 }
 
 if ! use_ext_cmd
@@ -85,5 +87,10 @@ else
 	do
 		launch_merge_tool "$1" "$2" "$5"
 		shift 7
+		if test "$status" != 0 &&
+			test "$GIT_DIFFTOOL_TRUST_EXIT_CODE" = true
+		then
+			exit $status
+		fi
 	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 +43 -0

49 lines not on this page · raw message

Signature · 1 line
2.1.2.556.g2136b5c
#2 lore raw

PATCH v2 1/1 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 v1:

This fixes typos in the documentation (s/an an/an/) and tests.

I'll wait until tomorrow before sending follow-ups in case anyone spots any other issues.

 Documentation/git-difftool.txt | 14 ++++++++++++++
 git-difftool--helper.sh        |  7 +++++++
 git-difftool.perl              | 12 ++++++++++++
 t/t7800-difftool.sh            | 43 ++++++++++++++++++++++++++++++++++++++++++
 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 +7 -0
index 7ef36b9..fdbd768 100755
@@ -62,6 +62,8 @@ launch_merge_tool () {
 	else
 		run_merge_tool "$merge_tool"
 	fi
+	status=$?
+	return $status
 }
 
 if ! use_ext_cmd
@@ -85,5 +87,10 @@ else
 	do
 		launch_merge_tool "$1" "$2" "$5"
 		shift 7
+		if test "$status" != 0 &&
+			test "$GIT_DIFFTOOL_TRUST_EXIT_CODE" = true
+		then
+			exit $status
+		fi
 	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 +43 -0

49 lines not on this page · raw message

Signature · 1 line
2.1.2.556.g2136b5c
#3 lore raw

Re: [PATCH v2] difftool: add support for --trust-exit-code

Am 26.10.2014 um 02:38 schrieb David Aguilar:

Quoted 14 lines
git-difftool--helper.sh +2 -0
index 7ef36b9..fdbd768 100755
@@ -62,6 +62,8 @@ launch_merge_tool () {
 	else
 		run_merge_tool "$merge_tool"
 	fi
+	status=$?
+	return $status
 }
 
 if ! use_ext_cmd

git-difftool--helper.sh else · line 94

Quoted 8 lines
git-difftool--helper.sh +5 -0
@@ -85,5 +87,10 @@ else
 	do
 		launch_merge_tool "$1" "$2" "$5"
 		shift 7
+		if test "$status" != 0 &&
+			test "$GIT_DIFFTOOL_TRUST_EXIT_CODE" = true
+		then
+			exit $status
+		fi

If you insert this new code block before the 'shift 7', you can set status here and do not have to hide it in the helper function.

git-difftool--helper.sh else · line 96

Quoted 2 lines
git-difftool--helper.sh +0 -0
 	done
 fi

-- Hannes