Skip to content

Commit 6d9c2d4

Browse files
committed
Merge branch 'pb/mergetool-errors' into seen
End-user experience of "git mergetool" when the command errors out has been improved. * pb/mergetool-errors: git-difftool--helper.sh: exit upon initialize_merge_tool errors git-mergetool--lib.sh: add error message for unknown tool variant git-mergetool--lib.sh: add error message in 'setup_user_tool' git-mergetool--lib.sh: use TOOL_MODE when erroring about unknown tool completion: complete '--tool-help' in 'git mergetool'
2 parents f872605 + cf5a972 commit 6d9c2d4

File tree

4 files changed

+20
-11
lines changed

4 files changed

+20
-11
lines changed

contrib/completion/git-completion.bash

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2331,7 +2331,7 @@ _git_mergetool ()
23312331
return
23322332
;;
23332333
--*)
2334-
__gitcomp "--tool= --prompt --no-prompt --gui --no-gui"
2334+
__gitcomp "--tool= --tool-help --prompt --no-prompt --gui --no-gui"
23352335
return
23362336
;;
23372337
esac

git-difftool--helper.sh

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,7 @@ launch_merge_tool () {
6161
export BASE
6262
eval $GIT_DIFFTOOL_EXTCMD '"$LOCAL"' '"$REMOTE"'
6363
else
64-
initialize_merge_tool "$merge_tool"
65-
# ignore the error from the above --- run_merge_tool
66-
# will diagnose unusable tool by itself
64+
initialize_merge_tool "$merge_tool" || exit 1
6765
run_merge_tool "$merge_tool"
6866
fi
6967
}
@@ -87,9 +85,7 @@ if test -n "$GIT_DIFFTOOL_DIRDIFF"
8785
then
8886
LOCAL="$1"
8987
REMOTE="$2"
90-
initialize_merge_tool "$merge_tool"
91-
# ignore the error from the above --- run_merge_tool
92-
# will diagnose unusable tool by itself
88+
initialize_merge_tool "$merge_tool" || exit 1
9389
run_merge_tool "$merge_tool" false
9490

9591
status=$?

git-mergetool--lib.sh

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -159,14 +159,18 @@ check_unchanged () {
159159
}
160160

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

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

171175
diff_cmd () {
172176
( eval $merge_tool_cmd )
@@ -255,10 +259,11 @@ setup_tool () {
255259

256260
# Now let the user override the default command for the tool. If
257261
# they have not done so then this will return 1 which we ignore.
258-
setup_user_tool
262+
setup_user_tool 2>/dev/null
259263

260264
if ! list_tool_variants | grep -q "^$tool$"
261265
then
266+
echo "error: unknown ${tool%[0-9]} variant '$tool'" >&2
262267
return 1
263268
fi
264269

@@ -474,7 +479,7 @@ get_merge_tool_path () {
474479
merge_tool="$1"
475480
if ! valid_tool "$merge_tool"
476481
then
477-
echo >&2 "Unknown merge tool $merge_tool"
482+
echo >&2 "Unknown $TOOL_MODE tool $merge_tool"
478483
exit 1
479484
fi
480485
if diff_mode

t/t7610-mergetool.sh

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -898,4 +898,12 @@ test_expect_success 'mergetool with guiDefault' '
898898
git commit -m "branch1 resolved with mergetool"
899899
'
900900

901+
test_expect_success 'mergetool with non-existent tool' '
902+
test_when_finished "git reset --hard" &&
903+
git checkout -b test$test_count branch1 &&
904+
test_must_fail git merge main &&
905+
yes "" | test_must_fail git mergetool --tool=absent >out 2>&1 &&
906+
test_grep -i "not set for tool" out
907+
'
908+
901909
test_done

0 commit comments

Comments
 (0)