在 Windows 中对两个文件执行比较时获取两个文件中差异的行号
出于代码审查的目的,修改现有源文件后,我们仅审查这些源文件中已更改的代码行。
在对存储库中的源文件进行更改后,我必须在进行同行代码审查之前传达源文件中已更改的代码行。
我能够使用 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
或者
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
正常 diff 输出:
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 的信息更少。任何差异忍者都知道有不同的选择来实现这一点吗?
我总是将最近修改的新文件与旧文件进行比较。我只需要新文件中需要检查的内容。
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.
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.
Sample files--
foo (new version):
a
b
c
d
e
f
g
h
i
j
k
foo (old version):
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.
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:
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:
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?
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
GNU DiffUtils 是开源、免费的,并且有Windows 端口。
GNU DiffUtils is open source, free, and has a Windows port.
将第一个文件保存为 x.txt,第二个文件保存为 y.txt
然后运行:
diff -i --unchanged-line-format="¥" --new-line-format=":%dn: %L" y。 txt x.txt | txt perl -pe 's/¥/\n/g' | perl -pe 's/¥/\n/g' | perl -pe '$count++; if ($_ !~ /^\n$/){print "$count\t";}'
Save the first file as x.txt and the second as 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";}'
不知道 Windows 本地方法,但我使用 Textpad 的“比较文件”功能来实现此目的,而且效果非常好。
Not aware of a Windows-native method, but I use Textpad's "compare files" feature for this, and it's pretty good.
Windows 原生。我们的 SD Smart Differencer 不是在线路上而是在程序结构上产生增量,并且对于线路/列-行/列以及编辑的程序实体的类型。它通过解析源文本进行比较,并使用代码结构来驱动比较。增量是根据程序员感兴趣的操作来描述的:插入、替换、删除、替换。
举个例子:
高级编辑操作是“I”、“S”、“R”和“D”,在 I/S/R/ 的左侧或右侧详细说明添加、插入或删除的内容。具有行.列精度的 D 字符。例如,在第 12 行第 5 列,插入了一个字段声明“I”,就在第 12 行第 5 列到第 57 列中。在第 272 行,从第 279-280 行“I”插入了一个代码块,其中包含标识符 lineNum 替换为 endLineNum。在第二个文件的第 424 行,表达式语句已被最初来自第 514 行的表达式语句“S”替换。更多详细信息请访问网站。
如果您想要使用 Perl 之类的东西,您可以轻松地删除额外的细节(“:”字符后面和 [] 内部的内容)。
Windows native. Our SD Smart Differencer produces 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.
Here's an example:
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.
You can easily strip out extra details (stuff following ":" characters and inside []) if you want with something like Perl.