diff mbox series

[3/5] git-mergetool--lib.sh: add error message in 'setup_user_tool'

Message ID 79c3a6ffe8f2872755f76340e2d5ae1a94885456.1731459128.git.gitgitgadget@gmail.com (mailing list archive)
State New
Headers show
Series git-mergetool: improve error code paths and messages | expand

Commit Message

Philippe Blain Nov. 13, 2024, 12:52 a.m. UTC
From: Philippe Blain <levraiphilippeblain@gmail.com>

In git-mergetool--lib.sh::setup_tool, we check if the given tool is a
known builtin tool, a known variant, or a user-defined tool by calling
setup_user_tool, and we return with the exit code from setup_user_tool
if it was called. setup_user_tool checks if {diff,merge}tool.$tool.cmd
is set and quietly returns with an error if not.

This leads to the following invocation quietly failing:

	git mergetool --tool=unknown

which is not very user-friendly. Adjust setup_user_tool to output an
error message before returning if {diff,merge}tool.$tool.cmd is not set.

Adjust the second call to setup_user_tool in setup_tool to silence this
new error, as this call is only meant to allow users to redefine 'cmd'
for a builtin tool; it is not an error if they have not done so (which
is why we do not check the return status of this call).

Note that this behaviour of quietly failing is a regression dating back
to de8dafbada (mergetool: break setup_tool out into separate
initialization function, 2021-02-09), as before this commit an unknown
mergetool would be diagnosed in get_merge_tool_path when called from
run_merge_tool.

Signed-off-by: Philippe Blain <levraiphilippeblain@gmail.com>
---
 git-mergetool--lib.sh | 10 +++++++---
 t/t7610-mergetool.sh  |  8 ++++++++
 2 files changed, 15 insertions(+), 3 deletions(-)

Comments

Junio C Hamano Nov. 13, 2024, 1:48 a.m. UTC | #1
"Philippe Blain via GitGitGadget" <gitgitgadget@gmail.com> writes:

>  valid_tool () {
> -	setup_tool "$1" && return 0
> +	setup_tool "$1" 2>/dev/null && return 0
>  	cmd=$(get_merge_tool_cmd "$1")
>  	test -n "$cmd"
>  }

As we are checking if a tool is valid, it is normal for setup_tool
to fail when we are checking is not valid (aka "fails to get set
up").  There is no need to show an error message for such a failure,
as the callers of valid_tool would do so if they wish.  OK.

>  setup_user_tool () {
>  	merge_tool_cmd=$(get_merge_tool_cmd "$tool")
> -	test -n "$merge_tool_cmd" || return 1
> +	if test -z "$merge_tool_cmd"
> +	then
> +		echo >&2 "error: ${TOOL_MODE}tool.$tool.cmd not set for tool '$tool'"
> +		return 1
> +	fi

There are only two callers of setup_user_tool, and one of them
squelches this message by sending it to /dev/null.  The error
message composed here does not use anything that is unique to the
function (in other words, $tool and ${TOOL_MODE} are available to
its callers).

I wonder if it is a better design to leave this one as-is, and
instead show the error message from the other caller of
setup_user_tool that does not squelch the message?  Are we planning
to add more callers of this function that want to show the same
message?

>  	diff_cmd () {
>  		( eval $merge_tool_cmd )
> @@ -255,7 +259,7 @@ setup_tool () {
>  
>  	# Now let the user override the default command for the tool.  If
>  	# they have not done so then this will return 1 which we ignore.
> -	setup_user_tool
> +	setup_user_tool 2>/dev/null

If we did that, then this change can be dropped.  Instead, a few
lines above this hunk, we can give the error message ourselves from
this setup_tool function.

>  	if ! list_tool_variants | grep -q "^$tool$"
>  	then
> diff --git a/t/t7610-mergetool.sh b/t/t7610-mergetool.sh
> index 22b3a85b3e9..82a88107850 100755
> --- a/t/t7610-mergetool.sh
> +++ b/t/t7610-mergetool.sh
> @@ -898,4 +898,12 @@ test_expect_success 'mergetool with guiDefault' '
>  	git commit -m "branch1 resolved with mergetool"
>  '
>  
> +test_expect_success 'mergetool with non-existent tool' '
> +	test_when_finished "git reset --hard" &&
> +	git checkout -b test$test_count branch1 &&
> +	test_must_fail git merge main &&
> +	yes "" | test_must_fail git mergetool --tool=absent >out 2>&1 &&
> +	test_grep -i "not set for tool" out
> +'

Why "-i"?  I do not offhand see the reason why we want to be loose
here.

The "${TOOL_MODE}tool" part may also want to be verified, perhaps,
which was related to the topic of the fix in [2/5]?
diff mbox series

Patch

diff --git a/git-mergetool--lib.sh b/git-mergetool--lib.sh
index 269a60ea44c..f4786afc63f 100644
--- a/git-mergetool--lib.sh
+++ b/git-mergetool--lib.sh
@@ -159,14 +159,18 @@  check_unchanged () {
 }
 
 valid_tool () {
-	setup_tool "$1" && return 0
+	setup_tool "$1" 2>/dev/null && return 0
 	cmd=$(get_merge_tool_cmd "$1")
 	test -n "$cmd"
 }
 
 setup_user_tool () {
 	merge_tool_cmd=$(get_merge_tool_cmd "$tool")
-	test -n "$merge_tool_cmd" || return 1
+	if test -z "$merge_tool_cmd"
+	then
+		echo >&2 "error: ${TOOL_MODE}tool.$tool.cmd not set for tool '$tool'"
+		return 1
+	fi
 
 	diff_cmd () {
 		( eval $merge_tool_cmd )
@@ -255,7 +259,7 @@  setup_tool () {
 
 	# Now let the user override the default command for the tool.  If
 	# they have not done so then this will return 1 which we ignore.
-	setup_user_tool
+	setup_user_tool 2>/dev/null
 
 	if ! list_tool_variants | grep -q "^$tool$"
 	then
diff --git a/t/t7610-mergetool.sh b/t/t7610-mergetool.sh
index 22b3a85b3e9..82a88107850 100755
--- a/t/t7610-mergetool.sh
+++ b/t/t7610-mergetool.sh
@@ -898,4 +898,12 @@  test_expect_success 'mergetool with guiDefault' '
 	git commit -m "branch1 resolved with mergetool"
 '
 
+test_expect_success 'mergetool with non-existent tool' '
+	test_when_finished "git reset --hard" &&
+	git checkout -b test$test_count branch1 &&
+	test_must_fail git merge main &&
+	yes "" | test_must_fail git mergetool --tool=absent >out 2>&1 &&
+	test_grep -i "not set for tool" out
+'
+
 test_done