美文网首页程序员程序员的日常
使用Beyond Compare进行svn diff

使用Beyond Compare进行svn diff

作者: Fengya | 来源:发表于2016-07-14 16:51 被阅读2443次

    问题描述

    最近发现了一款很好用的diffmerge的软件Beyond Compare。于是考虑是不是能够将这个软件使用在日常的文件对比和版本管理中。

    问题解决

    首先下载Beyond Compare的最新版本。安装好了之后安装命令行工具,执行Install Commond Line Tools。这样我们就可以使用bcompbcompare两个命令来进行文件对比了。

    bcomp会打开app窗口并保持而不是立刻返回。bcompare会在打开窗口后立刻返回。所以这里我们使用bcomp命令。

    ~中新建一个脚本.BCDiff,脚本内容如下:

    #!/bin/bash
    
    DIFF="bcomp"
    
    LEFT=${6}
    RIGHT=${7}
    
    $DIFF $LEFT $RIGHT
    exit 0    # 这个返回0是为了防止上面的diff命令的返回值被svn diff接收到以为是出错
    

    之后修改~/.subversion/config文件中的diff-cmd一行为diff-cmd = ~/.BCDiff即可。

    此外注意自己写的那个脚本要添加运行权限。

    chmod +x ./.BCDiff
    

    相关原理

    svn diff机制

    svn diff为我们提供了一个使用自己的对比程序的机制。用在diff program位置的程序会接受一组形如

    -u -L tag1 (revision 171313) -L tag2 (working copy) file1 file2
    

    的参数。其中第六个第七个参数为对比的两个文件的绝对路径。因此我们可以利用一个脚本把这两个路径传递给需要的diff程序。这不仅适用与使用beyond compare,也适用于其他对比方式比如vimdiff

    Beyond Compare机制

    Beyond Compare的命令行形式bcomp使用方法如下:

    Beyond Compare
    Copyright (c) 1996-2016 Scooter Software.  All rights reserved.
    
    Syntax:
      bcomp [options] [<Left> [<Right> [<Center> [<Output>]]]]
      bcomp @<ScriptFile> [options]
    
    For more information on the options below check the help.
    
    Options:
      -nobackups               Disables backup file creation
      -ro                      Disables editing on all sides
      -ro#                     Disables editing on specified side
      -title#=<title>          Shows description instead of filename in path edit
      -vcs#=<path>             Uses version control path instead of real filename
                                 for file format matching, displayed in path
                                 edits if -title isn't defined
    
    File View Options:
      -fv[=<type>]             Opens new file view
      -qc[=<type>]             Silently compares two files
      -savetarget=<filename>   Saves to specified filename instead of original file
    
    Folder View Options:
      -sync                    Opens new Folder Sync view
    
    Merge Options:
      -automerge               Automatically merges files without interaction
      -favorleft, favorright   Suppresses output coloring for favored side
      -force                   Adds conflict markers to output if -automerge fails
      -iu                      Turns on "Ignore Unimportant Differences"
      -mergeoutput=<filename>  Specifies an output file
      -reviewconflicts         Opens interactive window if -automerge fails
    
    Script Options:
      -closescript             Closes script window when finished
      -silent                  Runs script without showing window
    
    A # character in a switch should be replaced by a side number:
      1=Left, 2=Right, 3=Center, 4=Output
    
    Comparison result return values:
        0  Success
        1  Binary same
        2  Rules-based same
       11  Binary differences
       12  Similar
       13  Rules-based differences
       14  Conflicts detected
      100  Error
      101  Conflicts detected, merge output not saved
      102+ Error
    

    我们一共可以传递四个文件路径参数进去。传递两个表示一左一右。三个表示一左一右还有一个中间的。四个表示一左一右一个中间的还有一个在下面是输出的文件路径。

    这里我们传递两个参数进行对比。

    但是要注意的是下面的返回值。默认情况使用会按照两个文件的对比情况返回对应的值。但是svn diff接受这些非零的值的时候会认为对比出现了问题从而终止整个程序。因此在上面的脚本中我们加上了exit 0用于返回一个成功给svn diff的外部程序。

    综上,我们就可以使用Beyond Compare来进行svn diff对比文件了。

    相关文章

      网友评论

        本文标题:使用Beyond Compare进行svn diff

        本文链接:https://www.haomeiwen.com/subject/wzwejttx.html