Skip to content

Commit 7dc6ca6

Browse files
Denton-Lgitster
authored andcommitted
mergetool: fallback to tool when guitool unavailable
In git-difftool, if the tool is called with --gui but `diff.guitool` is not set, it falls back to `diff.tool`. Make git-mergetool also fallback from `merge.guitool` to `merge.tool` if the former is undefined. If git-difftool were to use `get_configured_mergetool`, it would also get the fallback behaviour in the following precedence: 1. diff.guitool 2. merge.guitool 3. diff.tool 4. merge.tool Signed-off-by: Denton Liu <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent db7e694 commit 7dc6ca6

File tree

3 files changed

+40
-10
lines changed

3 files changed

+40
-10
lines changed

Documentation/git-mergetool.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,9 @@ success of the resolution after the custom tool has exited.
8383
--gui::
8484
When 'git-mergetool' is invoked with the `-g` or `--gui` option
8585
the default merge tool will be read from the configured
86-
`merge.guitool` variable instead of `merge.tool`.
86+
`merge.guitool` variable instead of `merge.tool`. If
87+
`merge.guitool` is not set, we will fallback to the tool
88+
configured under `merge.tool`.
8789

8890
--no-gui::
8991
This overrides a previous `-g` or `--gui` setting and reads the

git-mergetool--lib.sh

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -350,20 +350,29 @@ guess_merge_tool () {
350350
}
351351

352352
get_configured_merge_tool () {
353-
# If first argument is true, find the guitool instead
354-
if test "$1" = true
353+
is_gui="$1"
354+
sections="merge"
355+
keys="tool"
356+
357+
if diff_mode
355358
then
356-
gui_prefix=gui
359+
sections="diff $sections"
357360
fi
358361

359-
# Diff mode first tries diff.(gui)tool and falls back to merge.(gui)tool.
360-
# Merge mode only checks merge.(gui)tool
361-
if diff_mode
362+
if "$is_gui" = true
362363
then
363-
merge_tool=$(git config diff.${gui_prefix}tool || git config merge.${gui_prefix}tool)
364-
else
365-
merge_tool=$(git config merge.${gui_prefix}tool)
364+
keys="guitool $keys"
366365
fi
366+
367+
IFS=' '
368+
for key in $keys
369+
do
370+
for section in $sections
371+
do
372+
merge_tool=$(git config $section.$key) && break 2
373+
done
374+
done
375+
367376
if test -n "$merge_tool" && ! valid_tool "$merge_tool"
368377
then
369378
echo >&2 "git config option $TOOL_MODE.${gui_prefix}tool set to unknown tool: $merge_tool"

t/t7610-mergetool.sh

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,25 @@ test_expect_success 'gui mergetool' '
167167
git commit -m "branch1 resolved with mergetool"
168168
'
169169

170+
test_expect_success 'gui mergetool without merge.guitool set falls back to merge.tool' '
171+
test_when_finished "git reset --hard" &&
172+
git checkout -b test$test_count branch1 &&
173+
git submodule update -N &&
174+
test_must_fail git merge master &&
175+
( yes "" | git mergetool --gui both ) &&
176+
( yes "" | git mergetool -g file1 file1 ) &&
177+
( yes "" | git mergetool --gui file2 "spaced name" ) &&
178+
( yes "" | git mergetool --gui subdir/file3 ) &&
179+
( yes "d" | git mergetool --gui file11 ) &&
180+
( yes "d" | git mergetool --gui file12 ) &&
181+
( yes "l" | git mergetool --gui submod ) &&
182+
test "$(cat file1)" = "master updated" &&
183+
test "$(cat file2)" = "master new" &&
184+
test "$(cat subdir/file3)" = "master new sub" &&
185+
test "$(cat submod/bar)" = "branch1 submodule" &&
186+
git commit -m "branch1 resolved with mergetool"
187+
'
188+
170189
test_expect_success 'mergetool crlf' '
171190
test_when_finished "git reset --hard" &&
172191
# This test_config line must go after the above reset line so that

0 commit comments

Comments
 (0)