1.使用外置比较工具

Posted on Posted in 8.定制svn体验

使用外置比较工具

选项--diff-cmd--diff3-cmd的形式相似,也有类似名称的运行配置参数(见“配置”一节),这会导致一个错误的观念,也就是在Subversion中使用外置的比较(或“diff”)和合并工具会非常的容易,虽然Subversion可以使用大多数类似的工具,但是设置这些工具绝非易事。

Subversion和外置比较和合并工具的接口可以追溯到很久以前,当时Subversion的唯一文本比较能力是建立在GNU的工具链之上,特别是diffdiff3工具,为了得到Subversion需要的方式,它使用非常复杂的选项和参数调用这些工具,而这些选项和参数都是工具特定的,渐渐的,Subversion发展了自己的比较区别库作为备份机制。[47]--diff-cmd--diff3-cmd选项是添加到Subversion的命令行客户端,所以用户可以更加容易的指明他们最喜欢的使用的GNU
 
diff和diff3工具,而不是新奇的内置比较库,如果使用了这些选项,Subversion会忽略内置的比较库,转而使用外置程序,使用冗长的参数列表,目前还是这种情况。

人们很快意识到使用简单的配置机制必须使Subversion使用位于特定位置的GNU

 diff和diff3工具,毕竟,Subversion并不验证其被告之要执行的程序是否是GNU的工具链的比较工具。唯一可以配置的方面是外置工具在系统的位置—而不是选项集,参数顺序等等。Subversion一直将这些GNU工具选项发给你的外置比较工具,而不管程序是否可以理解那些选项,那不是所有用户直觉的方式。

使用外置比较和合并工具的关键是使用包裹脚本将Subversion的输出转化为你的脚本程序可以理解的形式,然后将这些比较工具的输出转化为你的Subversion期望的格式—GNU工具可能使用的格式,下面的小节覆盖了那些期望格式的细节。

注意

何时启动文本比较或合并的决定完全是Subversion的决定,而这个决定是根据文件的svn:mime-type属性作出的,这意味着,例如,即使你有一个可以识别Microsoft Word格式的比较或合并工具,当你对一个Word文件设置为非人工可读(例如application/msword)时,依然不会调用这个识别Word的工具。关于MIME type的设定,可以见“文件内容类型”一节

外置 diff

Subversion可以调用适合GNU参数的diff工具,并期望外置程序能够返回成功的错误代码。对于大多数可用的diff程序,只有第6、7参数,diff两边文件的路径。需要注意Subversion对于每个修改的文件都要以异步方式(或“后台”)运行diff程序,你会得到许多并行的实例。最后,Subversion期望你的程序在发现区别时返回错误代码1,没有区别则返回0—任何其他的返回值都被认为是严重错误。 [48]

例 7.2 “diffwrap.sh”例 7.3 “diffwrap.bat”分别是Bourne shell和Windows批处理外置diff工具的包裹器模版。

例 7.2. diffwrap.sh

#!/bin/sh # Configure your favorite diff program here. DIFF="/usr/local/bin/my-diff-tool" # Subversion provides the paths we need as the sixth and seventh  # parameters. LEFT=${6} RIGHT=${7} # Call the diff command (change the following line to make sense for # your merge program). $DIFF --left $LEFT --right $RIGHT # Return an errorcode of 0 if no differences were detected, 1 if some were. # Any other errorcode will be treated as fatal.

       
       

例 7.3. diffwrap.bat

@ECHO OFF REM Configure your favorite diff program here. SET DIFF="C:\Program Files\Funky Stuff\My Diff Tool.exe" REM Subversion provides the paths we need as the sixth and seventh  REM parameters. SET LEFT=%6 SET RIGHT=%7 REM Call the diff command (change the following line to make sense for REM your merge program). %DIFF% --left %LEFT% --right %RIGHT% REM Return an errorcode of 0 if no differences were detected, 1 if some were. REM Any other errorcode will be treated as fatal.

       
                   

外置 diff3

Subversion按照符合GNU的diff3的参数调用合并程序,期望外置程序会返回成功的错误代码,并且完整合并的文件结果打印到标准输出(这样Subversion可以重定向这些东西到适当的版本控制下的文件)。对于大多数可选的合并程序,只有第9、10和11参数,分别代表“mine”、“older”和“yours”的路径。需要注意,因为Subversion依赖于你的合并程序的输出,你的包裹脚本在输出发送到Subversion之前不要退出。当最终退出,如果合并成功返回0,如果有为解决的冲突则返回1—其它返回值都是严重错误。

例 7.4 “diff3wrap.sh”例 7.5 “diff3wrap.bat”分别是Bourne shell和Windows批处理外置diff工具的包裹器模版。

例 7.4. diff3wrap.sh

#!/bin/sh # Configure your favorite diff3/merge program here. DIFF3="/usr/local/bin/my-merge-tool" # Subversion provides the paths we need as the ninth, tenth, and eleventh  # parameters. MINE=${9} OLDER=${10} YOURS=${11} # Call the merge command (change the following line to make sense for # your merge program). $DIFF3 --older $OLDER --mine $MINE --yours $YOURS # After performing the merge, this script needs to print the contents # of the merged file to stdout.  Do that in whatever way you see fit. # Return an errorcode of 0 on successful merge, 1 if unresolved conflicts # remain in the result.  Any other errorcode will be treated as fatal.

       
       

例 7.5. diff3wrap.bat

@ECHO OFF REM Configure your favorite diff3/merge program here. SET DIFF3="C:\Program Files\Funky Stuff\My Merge Tool.exe" REM Subversion provides the paths we need as the ninth, tenth, and eleventh  REM parameters.  But we only have access to nine parameters at a time, so we REM shift our nine-parameter window twice to let us get to what we need. SHIFT SHIFT SET MINE=%7 SET OLDER=%8 SET YOURS=%9 REM Call the merge command (change the following line to make sense for REM your merge program). %DIFF3% --older %OLDER% --mine %MINE% --yours %YOURS% REM After performing the merge, this script needs to print the contents REM of the merged file to stdout.  Do that in whatever way you see fit. REM Return an errorcode of 0 on successful merge, 1 if unresolved conflicts REM remain in the result.  Any other errorcode will be treated as fatal.

       
           


[47] Subversion的开发者很好,但最好的也会发生错误。

[48] GNU的diff手册这样说的:“返回0意味着没有区别,1是有有区别,其它值意味着出现问题。”