views:

163

answers:

2

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?

+3  A: 

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)
kwatford
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?
Masi
@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/
kwatford
+2  A: 

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
VonC
Actually it is 'guess_merge_tool' function in 'git-mergetool--lib'
Jakub Narębski