代码之家  ›  专栏  ›  技术社区  ›  Andrew Keeton ShuggyCoUk

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

  •  5
  • Andrew Keeton ShuggyCoUk  · 技术社区  · 7 年前

    我正在使用Git for Windows和 xltrail extension 在使用时提供可读的输出 git diff <Excel workbook> . 我还使用了图形化的diff/merge工具 Beyond Compare 4 Pro 使用时运行的 git difftool

    git difftool <Excel workbook> ,我得到了正常的 git diff 输出时不提示打开无法比较。如何让Git、xltrail和Beyond一起工作?

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

    $ 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的屏幕截图

    Screenshot of README.md in Beyond Compare 4

    2 回复  |  直到 7 年前
        1
  •  4
  •   Andrew Keeton ShuggyCoUk    7 年前

    创建可执行脚本 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-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> 应该在无与伦比的地方开放。


    原始答案

    首先,您需要使用以下内容设置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/配置 文件,并确保有这样的内容:

    [diff]
    tool = bc4
    

    .git/配置 文件。

    我成功地复制了你的问题。 我认为可以在.gittributes文件中执行某些操作,但没有成功。

    所以。。。唯一可能的解决方案是创建自己的diff脚本,并将所有工具插在一起。

    在我这边,我尝试使用GNU/Bash脚本(在Windows上安装gitbash时可以得到支持),但是您可能需要创建一个.bat脚本。

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

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

    然后,您需要将文件扩展名与此差异链接起来,更新您的 文件,具有以下内容(对于任何想要的扩展名都适用):

    *.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的diff输出所需的选项,但这是剩下的唯一要做的事情。

        2
  •  1
  •   Felix Zumstein    7 年前

    SourceTree 它是免费的,不像超级市场。

    只需确保升级到SourceTree的最新版本 git-xltrail 已安装并激活。你甚至不需要改变任何设置,它应该只是工作。

    https://dev.to/fzumstein/how-to-diff-excel-vba-code-in-sourcetree-git-client-36k

    推荐文章