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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4139083/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 15:35:15  来源:igfitidea点击:

Get line numbers of differences in two files when performing a diff on two files in Windows

windowsdiffwinmerge

提问by Zoot

For the purposes of code review, after an existing source file is modified, we only review lines of code that have been changed in those source files.
After I make changes to source files in a repository, I must communicate the lines of code that have changed in a source file before we do our peer code reviews.

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

I am able to see a visual diff using WinMerge, and I am happy with what it does for me, but the only requirement for my co-workers is simply to know the file name and the line numbers that need to be reviewed.

我可以使用 WinMerge 看到视觉差异,我对它为我所做的感到满意,但对我的同事的唯一要求只是知道需要查看的文件名和行号。

Is there a way to get only the line numbers of a changed file? The types of files would be text source files, such as Javascript, Java, XML, XSL, etc.

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

Sample files--

示例文件--

foo (new version):

foo(新版本):

a
b
c
d
e
f
g
h
i
j
k

foo (old version):

foo(旧版本):

a
b
C
d
h
i
k

The output I'm looking for:

我正在寻找的输出:

foo: 3, 5-7, 10-11

OR

或者

foo:
3
5-7
10-11

I'm sure that some program allows for this. I just can't find one. Any suggestions? I'm currently using WinMerge, but any program that runs on Windows will be fine with me. It would be preferable if the software is open-source or freeware.

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

EDIT: GNU DiffUtils is close to what I want, but not exactly fitting my needs. Looking at the command line options, I see "-q" (or "--brief") as a simplified diff, but it's TOO simple. Output:

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

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

Normal diff output:

正常差异输出:

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 output:

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

I'm looking for something more informative than diff -q, but less informative than diff -u and diff. Any diff ninjas out there who are aware of different options to allow this?

我正在寻找比 diff -q 提供更多信息的东西,但比 diff -u 和 diff 提供的信息少。任何不同的忍者都知道允许这样做的不同选项吗?

I will always be comparing a new, recently modified file to an older one. I only need the things that need reviewing on the new file.

我将始终将最近修改过的新文件与旧文件进行比较。我只需要对新文件进行的内容。

采纳答案by Ben Voigt

GNU DiffUtils is open source, free, and has a Windows port.

GNU DiffUtils 是开源的、免费的,并且有一个 Windows 端口

回答by Josafa Pontes

Save the first file as x.txt and the second as y.txt

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

Then run:

然后运行:

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";}'

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";}'

回答by Simon Catlin

Not aware of a Windows-native method, but I use Textpad's "compare files" feature for this, and it's pretty good.

不知道 Windows 本地方法,但我为此使用了 Textpad 的“比较文件”功能,而且非常好。

回答by Ira Baxter

Windows native. Our SD Smart Differencerproduces deltas not on lines but on program structures, and is precise as to line/column-line/column and the type of program entity that was edited. It compares by parsing the source text, and using the code structure to drive the comparison. Deltas are described in terms of actions of interest to programmers: Insert, Replace, Delete, Substitute.

Windows 原生。我们的SD Smart Differencer不是在行上而是在节目结构上产生增量,并且精确到行/列-行/列以及被编辑的节目实体类型。它通过解析源文本进行比较,并使用代码结构来驱动比较。增量是根据程序员感兴趣的操作来描述的:插入、替换、删除、替换。

Here's an example:

下面是一个例子:

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

The high-level editing actions are "I", "S", "R" and "D", with details about what was added, inserted or deleted on left or right sides of the I/S/R/D character with line.column precision. For instance, at line 12 column 5, a field-declaration was "I"nserted, just in line 12 from column 5 to column 57. At line 272, a block of code was "I"nserted from lines 279-280 with the identifier lineNum replaced by endLineNum. At line 424 in the second file, the expresssion statement has been "S"ubstituted by one originally from line 514. More details at the website.

高级编辑操作是“I”、“S”、“R”和“D”,详细说明在 I/S/R/D 字符的左侧或右侧添加、插入或删除的内容,并带有线.列精度。例如,在第 12 行第 5 列,字段声明是“I”插入的,就在第 12 行从第 5 列到第 57 列。在第 272 行,从第 279-280 行“I”插入了一个代码块标识符 lineNum 替换为 endLineNum。在第二个文件的第 424 行,表达式语句已被“S”替换为最初来自第 514 行的语句。更多详细信息,请访问网站。

You can easily strip out extra details (stuff following ":" characters and inside []) if you want with something like Perl.

如果你想使用 Perl 之类的东西,你可以很容易地去掉额外的细节(“:”字符后面和 [] 里面的东西)。