Skip to content

Commit 85ac27e

Browse files
committed
Merge branch 'dl/difftool-mergetool'
Update "git difftool" and "git mergetool" so that the combinations of {diff,merge}.{tool,guitool} configuration variables serve as fallback settings of each other in a sensible order. * dl/difftool-mergetool: difftool: fallback on merge.guitool difftool: make --gui, --tool and --extcmd mutually exclusive mergetool: fallback to tool when guitool unavailable mergetool--lib: create gui_mode function mergetool: use get_merge_tool function t7610: add mergetool --gui tests t7610: unsuppress output
2 parents fed9391 + 6c22d71 commit 85ac27e

9 files changed

+180
-93
lines changed

Documentation/git-difftool.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,9 @@ instead. `--no-symlinks` is the default on Windows.
9090
When 'git-difftool' is invoked with the `-g` or `--gui` option
9191
the default diff tool will be read from the configured
9292
`diff.guitool` variable instead of `diff.tool`. The `--no-gui`
93-
option can be used to override this setting.
93+
option can be used to override this setting. If `diff.guitool`
94+
is not set, we will fallback in the order of `merge.guitool`,
95+
`diff.tool`, `merge.tool` until a tool is found.
9496

9597
--[no-]trust-exit-code::
9698
'git-difftool' invokes a diff tool individually on each file.

Documentation/git-mergetool--lib.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,9 @@ to define the operation mode for the functions listed below.
2828
FUNCTIONS
2929
---------
3030
get_merge_tool::
31-
returns a merge tool.
31+
returns a merge tool. the return code is 1 if we returned a guessed
32+
merge tool, else 0. '$GIT_MERGETOOL_GUI' may be set to 'true' to
33+
search for the appropriate guitool.
3234

3335
get_merge_tool_cmd::
3436
returns the custom command for a merge tool.

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

builtin/difftool.c

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
#include "object-store.h"
2525
#include "dir.h"
2626

27-
static char *diff_gui_tool;
2827
static int trust_exit_code;
2928

3029
static const char *const builtin_difftool_usage[] = {
@@ -34,11 +33,6 @@ static const char *const builtin_difftool_usage[] = {
3433

3534
static int difftool_config(const char *var, const char *value, void *cb)
3635
{
37-
if (!strcmp(var, "diff.guitool")) {
38-
diff_gui_tool = xstrdup(value);
39-
return 0;
40-
}
41-
4236
if (!strcmp(var, "difftool.trustexitcode")) {
4337
trust_exit_code = git_config_bool(var, value);
4438
return 0;
@@ -735,8 +729,11 @@ int cmd_difftool(int argc, const char **argv, const char *prefix)
735729
setenv(GIT_WORK_TREE_ENVIRONMENT, absolute_path(get_git_work_tree()), 1);
736730
}
737731

738-
if (use_gui_tool && diff_gui_tool && *diff_gui_tool)
739-
setenv("GIT_DIFF_TOOL", diff_gui_tool, 1);
732+
if (use_gui_tool + !!difftool_cmd + !!extcmd > 1)
733+
die(_("--gui, --tool and --extcmd are mutually exclusive"));
734+
735+
if (use_gui_tool)
736+
setenv("GIT_MERGETOOL_GUI", "true", 1);
740737
else if (difftool_cmd) {
741738
if (*difftool_cmd)
742739
setenv("GIT_DIFF_TOOL", difftool_cmd, 1);

git-difftool--helper.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ then
7171
then
7272
merge_tool="$GIT_DIFF_TOOL"
7373
else
74-
merge_tool="$(get_merge_tool)" || exit
74+
merge_tool="$(get_merge_tool)"
7575
fi
7676
fi
7777

git-mergetool--lib.sh

Lines changed: 35 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -80,14 +80,18 @@ show_tool_names () {
8080
}
8181
}
8282

83-
diff_mode() {
83+
diff_mode () {
8484
test "$TOOL_MODE" = diff
8585
}
8686

87-
merge_mode() {
87+
merge_mode () {
8888
test "$TOOL_MODE" = merge
8989
}
9090

91+
gui_mode () {
92+
test "$GIT_MERGETOOL_GUI" = true
93+
}
94+
9195
translate_merge_tool_path () {
9296
echo "$1"
9397
}
@@ -351,20 +355,36 @@ guess_merge_tool () {
351355
}
352356

353357
get_configured_merge_tool () {
354-
# If first argument is true, find the guitool instead
355-
if test "$1" = true
356-
then
357-
gui_prefix=gui
358-
fi
359-
360-
# Diff mode first tries diff.(gui)tool and falls back to merge.(gui)tool.
361-
# Merge mode only checks merge.(gui)tool
358+
keys=
362359
if diff_mode
363360
then
364-
merge_tool=$(git config diff.${gui_prefix}tool || git config merge.${gui_prefix}tool)
361+
if gui_mode
362+
then
363+
keys="diff.guitool merge.guitool diff.tool merge.tool"
364+
else
365+
keys="diff.tool merge.tool"
366+
fi
365367
else
366-
merge_tool=$(git config merge.${gui_prefix}tool)
368+
if gui_mode
369+
then
370+
keys="merge.guitool merge.tool"
371+
else
372+
keys="merge.tool"
373+
fi
367374
fi
375+
376+
merge_tool=$(
377+
IFS=' '
378+
for key in $keys
379+
do
380+
selected=$(git config $key)
381+
if test -n "$selected"
382+
then
383+
echo "$selected"
384+
return
385+
fi
386+
done)
387+
368388
if test -n "$merge_tool" && ! valid_tool "$merge_tool"
369389
then
370390
echo >&2 "git config option $TOOL_MODE.${gui_prefix}tool set to unknown tool: $merge_tool"
@@ -404,14 +424,17 @@ get_merge_tool_path () {
404424
}
405425
406426
get_merge_tool () {
427+
is_guessed=false
407428
# Check if a merge tool has been configured
408429
merge_tool=$(get_configured_merge_tool)
409430
# Try to guess an appropriate merge tool if no tool has been set.
410431
if test -z "$merge_tool"
411432
then
412433
merge_tool=$(guess_merge_tool) || exit
434+
is_guessed=true
413435
fi
414436
echo "$merge_tool"
437+
test "$is_guessed" = false
415438
}
416439
417440
mergetool_find_win32_cmd () {

git-mergetool.sh

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -389,7 +389,7 @@ print_noop_and_exit () {
389389

390390
main () {
391391
prompt=$(git config --bool mergetool.prompt)
392-
gui_tool=false
392+
GIT_MERGETOOL_GUI=false
393393
guessed_merge_tool=false
394394
orderfile=
395395

@@ -416,10 +416,10 @@ main () {
416416
esac
417417
;;
418418
--no-gui)
419-
gui_tool=false
419+
GIT_MERGETOOL_GUI=false
420420
;;
421421
-g|--gui)
422-
gui_tool=true
422+
GIT_MERGETOOL_GUI=true
423423
;;
424424
-y|--no-prompt)
425425
prompt=false
@@ -449,12 +449,8 @@ main () {
449449

450450
if test -z "$merge_tool"
451451
then
452-
# Check if a merge tool has been configured
453-
merge_tool=$(get_configured_merge_tool $gui_tool)
454-
# Try to guess an appropriate merge tool if no tool has been set.
455-
if test -z "$merge_tool"
452+
if ! merge_tool=$(get_merge_tool)
456453
then
457-
merge_tool=$(guess_merge_tool) || exit
458454
guessed_merge_tool=true
459455
fi
460456
fi

0 commit comments

Comments
 (0)