Skip to content

Commit 762cccf

Browse files
committed
Merge branch 'dl/difftool-mergetool' into pu
* dl/difftool-mergetool: difftool: fallback on merge.guitool difftool: make --gui, --tool and --extcmd mutually exclusive mergetool: fallback to tool when guitool unavailable mergetool: use get_merge_tool function t7610: add mergetool --gui tests t7610: unsuppress output
2 parents 2604116 + 73c920e commit 762cccf

File tree

9 files changed

+167
-91
lines changed

9 files changed

+167
-91
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: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -351,20 +351,29 @@ guess_merge_tool () {
351351
}
352352

353353
get_configured_merge_tool () {
354-
# If first argument is true, find the guitool instead
355-
if test "$1" = true
354+
is_gui="$1"
355+
sections="merge"
356+
keys="tool"
357+
358+
if diff_mode
356359
then
357-
gui_prefix=gui
360+
sections="diff $sections"
358361
fi
359362

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

406415
get_merge_tool () {
416+
not_guessed=true
407417
# Check if a merge tool has been configured
408-
merge_tool=$(get_configured_merge_tool)
418+
merge_tool=$(get_configured_merge_tool $GIT_MERGETOOL_GUI)
409419
# Try to guess an appropriate merge tool if no tool has been set.
410420
if test -z "$merge_tool"
411421
then
412422
merge_tool=$(guess_merge_tool) || exit
423+
not_guessed=false
413424
fi
414425
echo "$merge_tool"
426+
test "$not_guessed" = true
415427
}
416428

417429
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)