1

这个问题是基于 VonC 在线程上的评论。

Git 对 difftool 或 mergetool 的自动检测是脚本化的还是在某些 Git 可执行文件中?

4

2 回答 2

4

它是用 git-mergetool 编写的。我在副本的第 344 行找到了这个。

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)
于 2009-06-30T15:07:27.530 回答
2

git mergetool 手册页所述,

--tool=<tool>

使用由 指定的合并解析程序。
有效的合并工具有:kdiff3、tkdiff、meld、xxdiff、emerge、vimdiff、gvimdiff、ecmerge、diffuse、tortoisemerge、opendiff 和 araxis。

现在,该列表来自哪里?

实际上,脚本中使用了这些工具(及其自定义选项):

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

并由脚本 git-mergetool 使用,该脚本根据git config merge.tool命令进行选择。

但是基于 git-mergetool--lib 中的 valid_tool() 函数有一些“自动选择”:

valid_tool ()

它使用基于mergetool.<aMergeToolName>.cmd.
如果该设置保留在其中一个 git 配置文件中......将选择该工具。


对......,Jakub Narębski刚刚指出了git-mergetool--lib剧本中的正确部分:

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"
}

该函数恰如其分地命名guess_merge_tool()(您认为我应该能够找到它!...)除其他外还有以下功能,这可以解释它检测到 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
于 2009-06-30T15:09:13.440 回答