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

发布于 2024-07-26 00:06:08 字数 239 浏览 7 评论 0原文

这个问题基于 VonC 在 线程中的评论

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

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

忘年祭陌 2024-08-02 00:06:08

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

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)
寄意 2024-08-02 00:06:08

正如 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..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

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
薄凉少年不暖心 2024-08-02 00:06:08

我 2009 年的答案相比,脚本(例如 git-mergetool-- lib.sh)已随 Git 2.41(2023 年第 2 季度)发生更改:
git mergetool(man)git difftool(man) 学习新的配置 guiDefault 以选择性地支持当设置 $DISPLAY 时,会自动将 guitool 配置为非 gui-tool。

请参阅 提交 42943b9(2023 年 3 月 18 日),作者:Tao Klerks (TaoK)
(由 Junio C Hamano -- gitster -- 合并于 提交 9d8370d,2023 年 4 月 17 日)

mergetool:新配置guiDefault支持自动通过 DISPLAY 切换 gui

签署人:Tao Klerks
确认人:David Aguilar

当没有配置或手动选择merge.tooldiff.tool时,默认工具的选择对DISPLAY敏感多变的; 在 GUI 会话中,如果找到,将建议使用特定于 GUI 的工具,否则将建议使用基于终端的工具。
这种“GUI 优化”行为很重要,因为 GUI 可以对用户理解和正确完成重要冲突合并的能力产生巨大影响。

不久前,引入了 merge.guitooldiff.guitool 配置选项,使用户能够配置 GUI 工具和非 GUI 工具(使用在相同环境中,如果没有配置 GUI 工具,则进行回退。

不幸的是,为支持选择 guitool 而引入的 --gui 参数仍然是明确的。
使用配置的工具时,不存在与无工具配置的“如果我们处于 GUI 环境中则建议使用 GUI 工具”行为等效的行为。

根据[email protected]中的建议,引入新的配置选项, difftool.guiDefaultmergetool.guiDefault,支持特殊值“auto”,这会导致根据非空 显示值。
还支持“true”表示“默认为 guitool(除非在命令行上传递 --no-gui)”和“false”作为未指定这些新配置选项时的先前默认行为。


git config 现在包含在其 手册页

difftool.guiDefault

设置true以默认使用diff.guitool(相当于指定
--gui 参数),或 auto 选择 diff.guitooldiff.tool
取决于 DISPLAY 环境变量值的存在。 这
默认值为 false,其中必须提供 --gui 参数
明确使用 diff.guitool

git config 现在包含在其 手册页

mergetool.guiDefault

设置true以默认使用merge.guitool(相当于
指定 --gui 参数),或 auto 选择 merge.guitool
merge.tool 取决于是否存在 DISPLAY 环境
变量值。 默认值为 false,其中 --gui 参数
必须显式提供要使用的 merge.guitool

git difftool 现在包含在其 手册页

diff.guitool 变量而不是 diff.tool。 这可能是
使用配置变量自动选择
difftool.guiDefault--no-gui 选项可用于
覆盖这些设置。 如果 diff.guitool 没有设置,我们将
merge.guitooldiff.tool 的顺序回退,
merge.tool 直到找到工具。

git mergetool 现在包含在其 手册页

这会覆盖之前的 -g--gui 设置或
mergetool.guiDefault 配置并读取默认合并
来自配置的 merge.tool 变量的工具。

例子:

git config merge.guitool myguitool
git config mergetool.myguitool.cmd "(printf \"gui \" && cat \"\$REMOTE\") >\"\$MERGED\""
git config mergetool.myguitool.trustExitCode true

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文