为 webstorm 配置外部 git diff 工具

configuring external git diff tool for webstorm

我正在尝试将 webstorm 配置为外部差异工具。 Webstorm 接受两个参数,它们应该是可比较文件的路径,所以如果我 运行 以下内容:

webstorm diff E/repo/file1.txt E/repo/file2.txt

一切正常。现在我尝试将配置放入 config:

[diff]
    external = webstorm diff $LOCAL $REMOTE

我尝试回显 $LOCAL$REMOTE 参数,它们似乎包含 git 创建的临时文件的完整路径,所以一切都应该正常工作,但是当我运行

git diff master feature

webstorm 正在启动,但随后我收到以下消息external diff died, stopping at file.txt.我做错了什么?

我还找到了其他几个解决方案: the first one 建议如下:

diff $(cd $(dirname $LOCAL) && pwd)/$(basename $LOCAL) $(cd $(dirname $REMOTE) && pwd)/$(basename $REMOTE)

但我得到以下输出:

dirname: too few arguments
Try `dirname --help' for more information.
basename: too few arguments
Try `basename --help' for more information.
dirname: too few arguments
Try `dirname --help' for more information.
basename: too few arguments
Try `basename --help' for more information.

而webstorm也没有对比就退出了。此配置试图做什么?

$(cd $(dirname $LOCAL) && pwd)/$(basename $LOCAL)

下一个解决方案来自 git manual: 它建议我创建包装脚本:

The diff wrapper checks to make sure seven arguments are provided and passes two of them to your merge script. By default, Git passes the following arguments to the diff program:

path old-file old-hex old-mode new-file new-hex new-mode

Because you only want the old-file and new-file arguments, you use the wrapper script to pass the ones you need.

$ cat /usr/local/bin/extDiff 
#!/bin/sh
[ $# -eq 7 ] && /usr/local/bin/extMerge "" ""

我还没有测试过这个,但是在我明白为什么前两个不起作用之后我会的。

在配置中,你写了diff这个词,似乎是多余的。改为尝试:

external = webstorm $LOCAL $GLOBAL

原题中我自己问题的答案:

I try to put the configuration into config ...and it fails:

[diff]
    external = webstorm diff $LOCAL $REMOTE

diff.external 只接受要启动的程序名称。它启动这个程序并向那里传递 7 个参数。 diff.external 配置设置的上下文中没有 $LOCAL$REMOTE 变量。这就是它失败的原因。

The next solution is from git manual: It suggests that I create wrapper script

由于diff.external除了程序名之外不接受任何东西,我们需要找到一种方法来过滤掉这7个参数,只取webstorm需要的两个。这就是为什么我们需要包装脚本来过滤掉那些参数并仅用两个参数启动 webstorm。

所以我现在使用的配置是这样的:

[diff]
    tool = webstorm
[difftool]  
    prompt = false
[difftool "webstorm"]
    cmd = webstorm diff $PWD/$REMOTE $LOCAL
    trustExitCode = false

difftool 在其 cmd 块中指定当您从命令行执行 git difftool -t "webstorm" 时向 运行 发送的命令。所以它会 运行 webstorm diff $PWD/$REMOTE $LOCAL。请注意,$REMOTE$LOCAL 变量可用于 difftool.cmd 块内。在我的例子中,$REMOTE 变量包含相对于当前目录的文件路径,即简单的 file1.txt,而 $LOCAL 具有完整路径,即 E:/temp/file1.txt(由创建的临时文件git 进行比较)。由于 webstorm 需要完整路径,我添加了 $PWD 变量,该变量具有当前目录的路径并将其与 $REMOTE 组合我得到完整路径:$PWD/$REMOTE -E:/myrepo/file.txt。最后一件事,我为 gitdiff 使用了上面的 -t 选项来明确指定要使用的 diff 工具的配置。如果你想 git 使用一些默认的 difftool 配置,请在 diff.tool 配置变量中指定它(在我的例子中是如何完成的)。