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

StackOverflow https://stackoverflow.com/questions/4139083

  •  30-09-2019
  •  | 
  •  

Question

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.

Was it helpful?

Solution

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

OTHER TIPS

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

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

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:

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.

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top