Is Git's auto-detection scripted or is it within some Git executable?
Asked Answered
C

3

1

This question is based on VonC's comment at the thread.

Is Git's auto-detection for difftool or mergetool scripted or is it within some Git executable?

Canonize answered 30/6, 2009 at 14:50 Comment(1)
Jakub Narębski just find the right section in the Git script: See my completed answer.Kilgore
G
4

It's scripted in git-mergetool. I found this at line 344 of my copy.

if test -z "$merge_tool"; then
    merge_tool=`git config merge.tool`
    if test -n "$merge_tool" && ! valid_tool "$merge_tool"; then
        echo >&2 "git config option merge.tool set to unknown tool: $merge_tool"
        echo >&2 "Resetting to default..."
        unset merge_tool
    fi
fi

if test -z "$merge_tool" ; then
    if test -n "$DISPLAY"; then
        merge_tool_candidates="kdiff3 tkdiff xxdiff meld gvimdiff"
        if test -n "$GNOME_DESKTOP_SESSION_ID" ; then
            merge_tool_candidates="meld $merge_tool_candidates"
        fi
        if test "$KDE_FULL_SESSION" = "true"; then
            merge_tool_candidates="kdiff3 $merge_tool_candidates"
        fi
    fi
    if echo "${VISUAL:-$EDITOR}" | grep 'emacs' > /dev/null 2>&1; then
        merge_tool_candidates="$merge_tool_candidates emerge"
    fi
(snip)
Gamopetalous answered 30/6, 2009 at 15:7 Comment(2)
I need to download a new Git and see its source. My git is in a compiled format such that it is impossible to read it. @ How can you see your Git's source code without downloading a new one?Belloir
@Masi: While git itself is compiled, a lot of the commands (like mergetool) are in fact just scripts tucked away somewhere. On my system these are mostly stored in /usr/lib/git-core/Gamopetalous
K
2

As mentioned in the git mergetool man page,

--tool=<tool>

Use the merge resolution program specified by .
Valid merge tools are: kdiff3, tkdiff, meld, xxdiff, emerge, vimdiff, gvimdiff, ecmerge, diffuse, tortoisemerge, opendiff and araxis.

Now, where does that list comes from?

Actually, those tools (and their custom options) are used in the script:

<Git>/libexec/git-core/git-mergetool--lib

and used by the script git-mergetool, which does the selection based on git config merge.tool command.

But there is a bit of 'auto-selection' based on the valid_tool() function in git-mergetool--lib:

valid_tool ()

It uses get_merge_tool_cmd() which is based on mergetool.<aMergeToolName>.cmd.
If that setting remain in one of the git config files... that tool will be selected.


Right..., Jakub Narębski just pointed out the right section in the git-mergetool--lib script:

get_merge_tool () {
    # Check if a merge tool has been configured
    merge_tool=$(get_configured_merge_tool)
    # Try to guess an appropriate merge tool if no tool has been set.
    if test -z "$merge_tool"; then
        merge_tool="$(guess_merge_tool)" || exit
    fi
    echo "$merge_tool"
}

That function aptly named guess_merge_tool() (you think I should be able to find it!...) does amongst other thing, the following, which could explain it detects opendiff:

# Loop over each candidate and stop when a valid merge tool is found.
for i in $tools
do
    merge_tool_path="$(translate_merge_tool_path "$i")"
    if type "$merge_tool_path" > /dev/null 2>&1; then
        echo "$i"
        return 0
    fi
done
Kilgore answered 30/6, 2009 at 15:9 Comment(1)
Actually it is 'guess_merge_tool' function in 'git-mergetool--lib'Stretcherbearer
K
1

Compare to my 2009 answer, the scripts (like git-mergetool--lib.sh) have changed With Git 2.41 (Q2 2023):
"git mergetool"(man) and git difftool(man) learns a new configuration guiDefault to optionally favor configured guitool over non-gui-tool automatically when $DISPLAY is set.

See commit 42943b9 (18 Mar 2023) by Tao Klerks (TaoK).
(Merged by Junio C Hamano -- gitster -- in commit 9d8370d, 17 Apr 2023)

mergetool: new config guiDefault supports auto-toggling gui by DISPLAY

Signed-off-by: Tao Klerks
Acked-by: David Aguilar

When no merge.tool or diff.tool is configured or manually selected, the selection of a default tool is sensitive to the DISPLAY variable; in a GUI session a gui-specific tool will be proposed if found, and otherwise a terminal-based one.
This "GUI-optimizing" behavior is important because a GUI can make a huge difference to a user's ability to understand and correctly complete a non-trivial conflicting merge.

Some time ago the merge.guitool and diff.guitool config options were introduced to enable users to configure both a GUI tool, and a non-GUI tool (with fallback if no GUI tool configured), in the same environment.

Unfortunately, the --gui argument introduced to support the selection of the guitool is still explicit.
When using configured tools, there is no equivalent of the no-tool-configured "propose a GUI tool if we are in a GUI environment" behavior.

As proposed in [email protected], introduce new configuration options, difftool.guiDefault and mergetool.guiDefault, supporting a special value "auto" which causes the corresponding tool or guitool to be selected depending on the presence of a non-empty DISPLAY value.
Also support "true" to say "default to the guitool (unless --no-gui is passed on the commandline)", and "false" as the previous default behavior when these new configuration options are not specified.

git config now includes in its man page:

difftool.guiDefault

Set true to use the diff.guitool by default (equivalent to specifying the --gui argument), or auto to select diff.guitool or diff.tool depending on the presence of a DISPLAY environment variable value. The default is false, where the --gui argument must be provided explicitly for the diff.guitool to be used.

git config now includes in its man page:

mergetool.guiDefault

Set true to use the merge.guitool by default (equivalent to specifying the --gui argument), or auto to select merge.guitool or merge.tool depending on the presence of a DISPLAY environment variable value. The default is false, where the --gui argument must be provided explicitly for the merge.guitool to be used.

git difftool now includes in its man page:

diff.guitool variable instead of diff.tool. This may be selected automatically using the configuration variable difftool.guiDefault. The --no-gui option can be used to override these settings. If diff.guitool is not set, we will fallback in the order of merge.guitool, diff.tool, merge.tool until a tool is found.

git mergetool now includes in its man page:

This overrides a previous -g or --gui setting or mergetool.guiDefault configuration and reads the default merge tool from the configured merge.tool variable.

Example:

git config merge.guitool myguitool
git config mergetool.myguitool.cmd "(printf \"gui \" && cat \"\$REMOTE\") >\"\$MERGED\""
git config mergetool.myguitool.trustExitCode true
Kilgore answered 27/4, 2023 at 21:42 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.