Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a diff tool (patch) that is aware of indentation?

Tags:

I'm regularly using the gnu-utils patch and diff. Using git, I often do:

git diff 

Often simple changes create a large patch because the only that changed was, for example, adding a if/else loop and everything inside is indented to the right.

Reviewing such a patch can be cumbersome because only line by line manual comparison can indicate if anything has essentially changed within the indented code. We may be speaking about a few lines of code only, or about dozens (or much more) of nested code. (I know: such an hypothetically large function would better be split into smaller functions, but that's beside the point).

Can't GNU diff/patch be aware when the only change within a code block is the indentation and let the developer know as much?

Are there any other diff tools that operate this way?

Edit: Ok, there is --ignore-space-change but then we are in a either/or situation: either we have a human-more-readable patch or we have a complete patch that the machine would know how to read. Can't we have the best of both world with a more elaborate diff tool that would show to the human space changes for what they are while allowing the machine to apply the patch fully?

like image 606
augustin Avatar asked Nov 10 '10 11:11

augustin


People also ask

What algorithm does diff use?

This research was published in a 1976 paper co-written with James W. Hunt, who developed an initial prototype of diff. The algorithm this paper described became known as the Hunt–Szymanski algorithm.

What is indentation where it is applied?

When referring to text, indent or indentation is the increase or decrease of space between the left and right margin of a paragraph. To indent text, move the cursor to the front of the line and press the Tab on the keyboard. Example of indented text.


1 Answers

With GNU diff you can pass -b or --ignore-space-change to ignore changes in the amount of white space in a patch.

If you use emacs and have been sent a patch, you can also use M-x diff-ignore-whitespace-hunk to reformat the patch to ignore white space in a particular hunk. Or diff-refine-hunk to highlight changes at a character by character level, which tends to point out the "meat" of a change.

As for applying patches, you can use the -l or --ignore-whitespace with GNU patch to ignore tabs and spaces changes. Just be careful with Python code :-)

like image 93
richq Avatar answered Oct 05 '22 03:10

richq