5

我正在使用带有xltrail 扩展名的 Git for Windows,以便在使用git diff <Excel workbook>. 我还在使用Beyond Compare 4 Pro图形差异/合并工具,它在使用时运行git difftool并提供了一个很好的并排差异和三向合并界面。

但是,当我运行时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 4 中 README.md 的屏幕截图

Beyond Compare 4 中 README.md 的屏幕截图

4

2 回答 2

4

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

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

#!/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:
#    $1   $2       $3      $4       $5       $6      $7
#    path old-file old-hex old-mode new-file new-hex new-mode

path="$1"
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

在存储库中应该有一个.gitattributes由 xltrail 创建的文件,用于将 Excel 文件与 xltrail 差异和合并工具相关联。更改要使用的差异条目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

然后,您可以检查/编辑您的.git/config文件,并确保具有类似的内容:

[diff]
tool = bc4

如果还不够,请发布您的.git/config文件。

编辑,以满足进一步的需求。似乎基本上不可能像这样插入 xtrail 和 Beyond Compare。我成功地复制了你的问题。我虽然可以在 .gitattributes 文件中完成某些事情,但没有成功。

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

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

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

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

然后,您需要将文件扩展名与此差异链接,更新您的.gitattributes文件,使用类似(适用于任何想要的扩展名):

*.xls diff=myComplexDiff

然后是脚本,它看起来像这样:

#!/bin/bash -x
# Sample of information given by git
# b851185776b7826a6cc82d3aa6a8d53fa47e0c26 100644 simpleTest.xls 0000000000000000000000000000000000000000 100644
fileName="$1"
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 的差异输出,但这是唯一剩下要做的事情。

于 2018-11-02T11:55:43.947 回答
1

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

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

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

于 2019-01-21T18:07:10.210 回答