mergetool: fallback to tool when guitool unavailable
authorDenton Liu <liu.denton@gmail.com>
Mon, 29 Apr 2019 06:21:14 +0000 (02:21 -0400)
committerJunio C Hamano <gitster@pobox.com>
Mon, 13 May 2019 14:11:59 +0000 (23:11 +0900)
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, when called with `--gui`, were to use
`get_configured_mergetool` in a future patch, it would also get the
fallback behavior in the following precedence:

1. diff.guitool
2. merge.guitool
3. diff.tool
4. merge.tool

The behavior for when difftool or mergetool are called without `--gui`
should be identical with or without this patch.

Note that the search loop could be written as

sections="merge"
keys="tool"
if diff_mode
then
sections="diff $sections"
fi
if gui_mode
then
keys="guitool $keys"
fi

merge_tool=$(
IFS=' '
for key in $keys
do
for section in $sections
do
selected=$(git config $section.$key)
if test -n "$selected"
then
echo "$selected"
return
fi
done
done)

which would make adding a mode in the future much easier. However,
adding a new mode will likely never happen as it is highly discouraged
so, as a result, it is written in its current form so that it is more
readable for future readers.

Signed-off-by: Denton Liu <liu.denton@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
Documentation/git-mergetool.txt
git-mergetool--lib.sh
t/t7610-mergetool.sh
index 0c7975a0507a35f9624516f0844ad11cd5141d50..6b14702e78498ee45f67e0084bea429d484b28cf 100644 (file)
@@ -83,7 +83,9 @@ success of the resolution after the custom tool has exited.
 --gui::
        When 'git-mergetool' is invoked with the `-g` or `--gui` option
        the default merge tool will be read from the configured
-       `merge.guitool` variable instead of `merge.tool`.
+       `merge.guitool` variable instead of `merge.tool`. If
+       `merge.guitool` is not set, we will fallback to the tool
+       configured under `merge.tool`.
 
 --no-gui::
        This overrides a previous `-g` or `--gui` setting and reads the
index 4ca170c8a70ed640eeae8e3ed8676c69614f41f4..696eb491609ca08a037e04b24cd01384f74d525d 100644 (file)
@@ -354,19 +354,36 @@ guess_merge_tool () {
 }
 
 get_configured_merge_tool () {
-       if gui_mode
-       then
-               gui_prefix=gui
-       fi
-
-       # Diff mode first tries diff.(gui)tool and falls back to merge.(gui)tool.
-       # Merge mode only checks merge.(gui)tool
+       keys=
        if diff_mode
        then
-               merge_tool=$(git config diff.${gui_prefix}tool || git config merge.${gui_prefix}tool)
+               if gui_mode
+               then
+                       keys="diff.guitool merge.guitool diff.tool merge.tool"
+               else
+                       keys="diff.tool merge.tool"
+               fi
        else
-               merge_tool=$(git config merge.${gui_prefix}tool)
+               if gui_mode
+               then
+                       keys="merge.guitool merge.tool"
+               else
+                       keys="merge.tool"
+               fi
        fi
+
+       merge_tool=$(
+               IFS=' '
+               for key in $keys
+               do
+                       selected=$(git config $key)
+                       if test -n "$selected"
+                       then
+                               echo "$selected"
+                               return
+                       fi
+               done)
+
        if test -n "$merge_tool" && ! valid_tool "$merge_tool"
        then
                echo >&2 "git config option $TOOL_MODE.${gui_prefix}tool set to unknown tool: $merge_tool"
index dad607e1864b472a2e2ed9bb3821b33ddc795d93..5b61c10a9c5402bfca8473f663076f9062a2e2a4 100755 (executable)
@@ -167,6 +167,25 @@ test_expect_success 'gui mergetool' '
        git commit -m "branch1 resolved with mergetool"
 '
 
+test_expect_success 'gui mergetool without merge.guitool set falls back to merge.tool' '
+       test_when_finished "git reset --hard" &&
+       git checkout -b test$test_count branch1 &&
+       git submodule update -N &&
+       test_must_fail git merge master &&
+       ( yes "" | git mergetool --gui both ) &&
+       ( yes "" | git mergetool -g file1 file1 ) &&
+       ( yes "" | git mergetool --gui file2 "spaced name" ) &&
+       ( yes "" | git mergetool --gui subdir/file3 ) &&
+       ( yes "d" | git mergetool --gui file11 ) &&
+       ( yes "d" | git mergetool --gui file12 ) &&
+       ( yes "l" | git mergetool --gui submod ) &&
+       test "$(cat file1)" = "master updated" &&
+       test "$(cat file2)" = "master new" &&
+       test "$(cat subdir/file3)" = "master new sub" &&
+       test "$(cat submod/bar)" = "branch1 submodule" &&
+       git commit -m "branch1 resolved with mergetool"
+'
+
 test_expect_success 'mergetool crlf' '
        test_when_finished "git reset --hard" &&
        # This test_config line must go after the above reset line so that