如何在 Beyond Compare 中查看 xltrail 生成的 Excel 工作簿差异?

How can I see the xltrail-generated diff of an Excel workbook in Beyond Compare?

我将 Git 用于 Windows 和 xltrail extension to provide a readable output when using git diff <Excel workbook>. I'm also using the graphical diff/merge tool Beyond Compare 4 Pro,当使用 git difftool 时 运行 并提供一个很好的并排diff 和三向合并接口。

但是,当我 运行 git difftool <Excel workbook> 时,我得到了正常的 git diff 输出,没有提示打开 Beyond Compare。如何让 Git、xltrail 和 Beyond Compare 协同工作?

编辑 #1

下面是一个不工作的完整示例:

$ git config diff.tool; git config difftool.bc.path; git config difftool.prompt
bc
c:/Program Files/Beyond Compare 4/bcomp.exe
false


$ cat .git/config
[core]
        repositoryformatversion = 0
        filemode = false
        bare = false
        logallrefupdates = true
        symlinks = false
        ignorecase = true
[diff "xltrail"]
        command = git-xltrail-diff.exe
[merge "xltrail"]
        name = xltrail merge driver for Excel workbooks
        driver = git-xltrail-merge.exe %P %O %A %B


$ cat ~/.gitconfig
# This is Git's per-user configuration file.

[user]
        name = Andrew Keeton
        email = AKeeton@example.com

[push]
        default = simple

[alias]
        co  = checkout
        ci  = commit
        cam = commit -am
        st  = status
        br  = branch

        # short log
        shlog = log --pretty=format:\"%C(yellow bold)%h%Creset %ad %C(cyan)|%Creset %s%C(magenta)%d%Creset %C(bold)[%an]%Creset\" --date=short --name-only
        hist  = log --pretty=format:\"%C(yellow bold)%h%Creset %ad %C(cyan)|%Creset %s%C(magenta)%d%Creset %C(bold)[%an]%Creset\" --date=short --graph

[diff]
        tool = bc

[difftool "bc"]
        path = c:/Program Files/Beyond Compare 4/bcomp.exe

[difftool]
        prompt = false


$ git st
On branch master
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

        modified:   workbook.xlsm
        modified:   README.md

no changes added to commit (use "git add" and/or "git commit -a")


$ git diff README.md
diff --git a/README.md b/README.md
index a052e20..3fd700d 100644
--- a/README.md
+++ b/README.md
@@ -40,6 +40,11 @@

 ## Tips

+* Lorem ipsum dolor sit amet, consectetur adipiscing elit.
+* Curabitur a dui eu massa feugiat varius tempor eget tellus.
+* Nullam et fringilla eros.
+* Etiam euismod ipsum enim, non molestie nunc varius eu.
+
 * In Excel, press `CTRL+G` to open the *Go To* window.  This makes it easy to find the various tables and "anchors."

 ## xltrail


$ git difftool README.md
<See screenshot of README.md in Beyond Compare 4>


$ git diff workbook.xlsm
diff --xltrail a/workbook.xlsm b/workbook.xlsm
--- a/workbook.xlsm/VBA/Module/modWorksheet
+++ b/workbook.xlsm/VBA/Module/modWorksheet
@@ -14,6 +14,7 @@
     Dim dDouble1 As Double
     Dim dDouble2 As Double
     Dim dDouble3 As Double
+    Dim sString1 As String
     Dim dDouble4 As Double

     Dim iInteger1 As Integer



$ git difftool workbook.xlsm
diff --xltrail a/workbook.xlsm b/workbook.xlsm
--- a/workbook.xlsm/VBA/Module/modWorksheet
+++ b/workbook.xlsm/VBA/Module/modWorksheet
@@ -14,6 +14,7 @@
     Dim dDouble1 As Double
     Dim dDouble2 As Double
     Dim dDouble3 As Double
+    Dim sString1 As String
     Dim dDouble4 As Double

     Dim iInteger1 As Integer

Beyond Compare 4README.md的截图

完整的解决方案(由提问者添加)

在现有 C:\Users\<User>\AppData\Local\Programs\Git xltrail\ 目录中创建可执行脚本 xltrail-bc-diff.sh

#!/bin/bash
#
# xltrail-bc-diff.sh massages the output of git-xltrail-diff.exe and passes it
# into Beyond Compare in "Text Patch" mode.
#
# Input arguments from `git diff` are:
#                                        
#    path old-file old-hex old-mode new-file new-hex new-mode

path=""
tempDiffPath=`mktemp --suffix=".diff"`

# xltrail's diff output is color-coded with no option to remove it
# (see https://github.com/ZoomerAnalytics/git-xltrail/issues/30) so we use
# sed to strip the color codes.
git-xltrail-diff.exe $@ | sed 's/\x1b\[[0-9;]*m//g' > $tempDiffPath

BCompare.exe -fv="Text Patch" $tempDiffPath

将以下内容添加到您的 git 配置文件中:

[diff "xltrail-bc"]
    command = xltrail-bc-diff.sh

在存储库中应该有一个由 xltrail 创建的 .gitattributes 文件,用于将 Excel 文件与 xltrail 差异和合并工具相关联。更改 diff 条目以使用 xltrail-bc:

*.xla diff=xltrail-bc
*.xlam diff=xltrail-bc
*.xls diff=xltrail-bc
*.xlsb diff=xltrail-bc
*.xlsm diff=xltrail-bc
*.xlsx diff=xltrail-bc
*.xlt diff=xltrail-bc
*.xltm diff=xltrail-bc
*.xltx diff=xltrail-bc

现在 git diff <Excel workbook> 应该可以在 Beyond Compare 中打开。


原答案

首先,您需要用类似的东西设置您的 git:

git config --global diff.tool bc4
git config --global difftool.bc4.cmd "\"C:/Program Files (x86)/Beyond Compare 4/BCompare.exe\" \"$LOCAL\" \"$REMOTE\""
git config --global difftool.prompt false

然后,您可以 check/edit 您的 .git/config 文件,并确保有类似的东西:

[diff]
tool = bc4

如果还不够,请post你的.git/config文件。

编辑,以备进一步需要。 看起来基本上不可能像那样插入 xtrail 和 Beyond Compare。 我成功地重现了你的问题。 我虽然可以在 .gitattributes 文件中做一些事情,但没有成功。

所以...唯一可能的解决方案(复杂的解决方案)是创建您自己的差异脚本,并将所有工具连接在一起。

在我这边,我尝试使用 GNU/Bash 脚本(你可以在你的 Windows 上安装 Git Bash 来支持它),但你可能想要创建一个 .bat 文件。

首先,您需要更新 .git/config 配置文件,指定新脚本文件的路径,例如:

[diff "myComplexDiff"]
    command = C:/Users/XXX/Documents/complexGitDiff.sh

然后,您需要 link 带有此差异的文件扩展名,更新您的 .gitattributes 文件,例如(为 ba adpated任何想要的扩展名):

*.xls diff=myComplexDiff

然后,脚本可能如下所示:

#!/bin/bash -x
# Sample of information given by git
# b851185776b7826a6cc82d3aa6a8d53fa47e0c26 100644 simpleTest.xls 0000000000000000000000000000000000000000 100644
fileName=""
tmpFile="/tmp/"$( date +'%s')"$fileName.diff"
git-xltrail-diff.exe $@ >"$tmpFile"
c:\Program\ Files\Beyond\ Compare\ 4\bcomp.exe "$fileName" "$tmpFile"

实际上我没有找到 Beyond Compare 需要的选项来接受 git-xltrail-diff.exe 的差异输出,但这是唯一剩下的事情了。

同时,这也适用于 SourceTree,与 BeyondCompare 不同,它是免费的。

只需确保在 git-xltrail 安装并激活时升级到最新版本的 SourceTree。您甚至不必更改任何设置,它应该就可以正常工作。

有关详细信息,请参阅我的博客 post:https://dev.to/fzumstein/how-to-diff-excel-vba-code-in-sourcetree-git-client-36k