【问题标题】:Is Git's auto-detection scripted or is it within some Git executable?Git 的自动检测是脚本化的还是在一些 Git 可执行文件中?
【发布时间】:2010-11-07 00:55:33
【问题描述】:

此问题基于 VonC 在thread 的评论。

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

【问题讨论】:

  • Jakub Narębski 只需在 Git 脚本中找到正确的部分:查看我完成的答案。

标签: git difftool mergetool


【解决方案1】:

它是在 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)

【讨论】:

  • 我需要下载一个新的 Git 并查看它的源代码。我的 git 是一种编译格式,因此无法阅读。 @ 不下载新的怎么能看到你的Git源代码?
  • @Masi:虽然 git 本身已编译,但许多命令(如 mergetool)实际上只是隐藏在某个地方的脚本。在我的系统上,这些主要存储在 /usr/lib/git-core/
【解决方案2】:

正如git mergetool man page中提到的,

--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.&lt;aMergeToolName&gt;.cmd的get_merge_tool_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

【讨论】:

  • 其实是'git-mergetool--lib'中的'guess_merge_tool'函数
猜你喜欢
  • 2020-07-10
  • 2021-03-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-11-13
  • 1970-01-01
  • 2016-12-20
相关资源
最近更新 更多