在Windows中对两个文件执行差异时,获取两个文件中的差异行数

时间:2010-11-09 21:56:41

标签: windows diff winmerge

出于代码审查的目的,在修改现有源文件后,我们只审查那些源文件中已更改的代码行。
在对存储库中的源文件进行更改之后,我必须在进行同行代码审查之前传达源文件中已更改的代码行。

我能够看到使用WinMerge的视觉差异,我很高兴它为我做了什么,但我的同事的唯一要求就是知道需要审查的文件名和行号。

有没有办法只获取更改文件的行号?文件类型是文本源文件,例如Javascript,Java,XML,XSL等。

示例文件 -

foo(新版本):

a
b
c
d
e
f
g
h
i
j
k

foo(旧版):

a
b
C
d
h
i
k

我正在寻找的输出:

foo: 3, 5-7, 10-11

OR

foo:
3
5-7
10-11

我确信某些程序允许这样做。我找不到一个。有什么建议?我目前正在使用WinMerge,但在Windows上运行的任何程序都可以。如果软件是开源软件或免费软件,那将是更好的选择。

编辑: GNU DiffUtils接近我想要的,但不完全符合我的需求。查看命令行选项,我将“-q”(或“--brief”)视为简化差异,但它太简单了。 输出:

C:\Program Files\GnuWin32\bin>diff.exe -q foo1.txt foo2.txt
Files foo1.txt and foo2.txt differ

正常差异输出:

C:\Program Files\GnuWin32\bin>diff.exe foo1.txt foo2.txt
3c3
<     c
---
>     C
5,7d4
<     e
<     f
<     g
10,11c7
<     j
<     k
---
>     k
\ No newline at end of file

diff -u输出:

C:\Program Files\GnuWin32\bin>diff.exe -u foo1.txt foo2.txt
--- foo1.txt    2010-11-09 15:47:12.447916000 -0600
+++ foo2.txt    2010-11-09 15:47:36.129954700 -0600
@@ -1,11 +1,7 @@
     a
     b
-    c
+    C
     d
-    e
-    f
-    g
     h
     i
-    j
-    k
+    k
\ No newline at end of file

我正在寻找比diff -q更具信息量的东西,但信息量比diff -u和diff少。那些知道不同选项允许这种情况的差异吗?

我将始终将新近修改的文件与较旧的文件进行比较。我只需要在新文件上查看的内容。

4 个答案:

答案 0 :(得分:3)

GNU DiffUtils是开源的,免费的,并且有a Windows port

答案 1 :(得分:2)

将第一个文件保存为x.txt,将第二个文件保存为y.txt

然后运行:

diff -i --unchanged-line-format =“¥”--new-line-format =“:%dn:%L”y.txt x.txt | perl -pe's /¥/ \ n / g'| perl -pe'$ count ++; if($ _!〜/ ^ \ n $ /){print“$ count \ t”;}'

答案 2 :(得分:1)

不知道Windows原生方法,但我使用Textpad的“比较文件”功能,这非常好。

答案 3 :(得分:1)

Windows原生。我们的SD Smart Differencer生成的增量不在行上,而是在程序结构上,并且精确地指定行/列行/列以及已编辑的程序实体的类型。它通过解析源文本和使用代码结构来进行比较来进行比较。 Deltas是根据程序员感兴趣的行为来描述的:插入,替换,删除,替换。

以下是一个例子:

Java~Java1_5 SmartDifferencer Version 1.1.1
Copyright (C) 2009 Semantic Designs, Inc; All Rights Reserved; SD Confidential
Powered by DMS (R) Software Reengineering Toolkit
*** Parsing file C:/DMS/Domains/Java/Java1_5/Analyzers/SmartDifferencer/Tests/diff1_before.java ...
*** Parsing file C:/DMS/Domains/Java/Java1_5/Analyzers/SmartDifferencer/Tests/diff1_after.java ...
*** Creating suffix tree ...
*** Determining maximal pairs ...
*** Sorting maximal pairs ...
*** Determining differences ...
*** Printing edits ...
a12.5 I s12.5-12.57:field_declaration
a179.9 I s185.9-185.71:executable_statement
a193.5 I s201.9-203.9:executable_statement
a272.13 I t279.13-280.33f267.13-268.30[267.17'lineNum'~>279.17'endLineNum',268.17'columnNum'~>280.17'endColumnNum']:statement_sequence_member,statement_sequence_member
a340.68 I s352.68-352.68:',' s352.70-352.70:INTEGER s352.71-352.71:',' s352.73-352.73:INTEGER
a423.13 I t436.13-509.38f450.17-518.35[482.17'hlevel'~>468.13'endLineNum',518.17'hname'~>509.13'endColumnNum']:executable_statement,executable_statement,executable_statement,executable_statement,executable_statement,executable_statement,executable_statement,executable_statement,executable_statement,executable_statement,executable_statement,executable_statement
a423.84 I s513.84-513.84:',' s513.86-513.95:IDENTIFIER s513.96-513.96:',' s513.98-513.109:IDENTIFIER
s424.13-424.59:expression_statement S s514.25-514.47:expression_statement
s429.13-429.64:local_variable_declaration S s515.25-515.87:local_variable_declaration
s430.13-430.71:executable_statement f431.13-431.69[431.13'lineString'~>79.9'config',431.26'lineString'~>79.18'config',431.47'indexOfSpace'~>79.35'end_index']t79.9-79.56:executable_statement s432.13-432.51:executable_statement D a516.25
a433.13 I s516.25-519.25:executable_statement n433.13-439.13:executable_statement s433.13-433.14:'if' s433.16-433.16:'(' s433.17-433.30:relational_expression s433.31-433.31:')' s433.33-433.45:executable_statement s434.13-434.16:'else' n434.18-439.13:block s434.18-434.18:'{' n435.17-438.49:statement_sequence f435.17-435.71[435.17'lineString'~>79.9'config',435.28'lineString'~>79.18'config',435.49'indexOfSpace'~>79.35'end_index']t79.9-79.56:executable_statement s436.17-436.28:executable_statement s437.17-437.51:executable_statement s438.17-438.49:executable_statement s439.13-439.13:'}' D a516.25
a442.13 I s520.25-520.54:executable_statement

高级编辑操作为“I”,“S”,“R”和“D”,详细说明了在I / S / R / D的左侧或右侧添加,插入或删除的内容具有line.column精度的字符。例如,在第12行第5列,字段声明被“I”插入,仅在第12行从第5列到第57行。在第272行,代码块是“I”从第279-280行插入的标识符lineNum由endLineNum替换。在第二个文件的第424行,表达式声明已经被原来来自第514行的一个“S”取代。更多细节来自网站。

如果你想要像Perl这样的东西,你可以轻松地删除额外的细节(“:”字符和[]里面的内容。