Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I am currently working on a source code generation tool. To make sure that my changes do no introduce any new bugs, a diff between the output of the program before and after my changes would theoretically be a valuable tool.

However, this turns out to be harder than one might think, because the tool outputs lines where the order does not matter (like import statements, function declarations, …) in a semi-randomly ordered way. Because of this, the output of diff is cluttered with many changes that are in fact only lines moved to another position in the same file.

Is there a way to make diff ignore these moves and only output the lines that have really been added or removed?

share|improve this question
    
Maybe it's easier to change your tool to generate functions and import declarations in a specific (e.g. lexicographical, if possible in your language) order? –  Daniel Beck Dec 5 '10 at 17:10
    
@Daniel Beck: See my comment to Gilles' answer below. –  klickverbot Dec 5 '10 at 21:11

3 Answers 3

up vote 1 down vote accepted

You may try to sort'em first. Something like:

sort file-a > s-file-a
sort file-b > s-file-b
diff s-file-a s-file-b

Bash (and zsh) can do this in one line with process substitution

diff <(sort file-a) <(sort file-b)
share|improve this answer
    
This might be an option, but the generated diffs would not be very useful then, because I would lose all the line number and context information… –  klickverbot Sep 5 '10 at 15:16
    
Even if I am still hoping for a better solution, I went with this approach for verifying the batch of changes I had been working on. –  klickverbot Dec 5 '10 at 21:16
    
I can foresee where this would miss some changes. Sometimes order matters, sometimes it doesn't. You discard all context. –  Rich Homolka Jun 26 '12 at 21:30

You could do a simple diff, store the result somewhere (to avoid another diff), loop through the lines in either version, then remove those from the other side.

This spawned a separate project for the working code. The code.

share|improve this answer
    
I'm not sure what that's supposed to do exactly, but it doesn't seem to produce the wanted results. As I understand the question, from the two examples in the code /tmp/old and /tmp/new no diff results would be wanted since there's just lines that have been moved around. This code however does produce results. –  Ilari Kajaste Jan 7 '11 at 14:54
    
Fixed the code. –  l0b0 Aug 24 '11 at 13:37

It sounds like you have control over the tool. Then make its output predictable: instead of emitting declarations in a semi-random order, use (say) alphabetical order as a last resort. This will not only have the benefit of removing useless cruft from diffs, but also of making the tool's output easier to read and verify for a human being.

share|improve this answer
    
Sorry, but this answer does not help me at all – if it was so easy, I would change it right away. Furthermore, I am currently merging changes from a project the generator was originally forked from, so adding such a rather far-reaching change would complicate that process even more… –  klickverbot Sep 5 '10 at 17:38

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.