Let's say I have rewritten a C function from scratch. It often happens that some lines happen to be identical before and after, in particular blank lines and closing braces. When I create a unified diff (using git diff
or plain GNU diff -u
), these identical lines splits up the hunk, making the patch harder to read for the reviewer. Diff's ambition to produce the minimal diff sometimes sacrifices readability, which is not what I want. Is there a way to make diff sacrifice minimality to keep long hunks together?
Example: Consider this, produced by diff:
--- A 2018-10-01 09:37:37.606642955 +0200
+++ B 2018-10-01 09:37:40.405675295 +0200
@@ -1,6 +1,9 @@
int fib(int n) {
- if (n <= 1) {
- return n;
+ int i, t1 = 0, t2 = 1;
+ for (i = 0; i < n; ++i) {
+ int next = t1 + t2;
+ t1 = t2;
+ t2 = next;
}
- return fib(n-1) + fib(n-2);
+ return t1;
}
In my opinion, the following equivalent patch would have been easier to read, at the cost of just one extra line:
--- A 2018-10-01 09:37:37.606642955 +0200
+++ B 2018-10-01 09:37:40.405675295 +0200
@@ -1,6 +1,9 @@
int fib(int n) {
- if (n <= 1) {
- return n;
- }
- return fib(n-1) + fib(n-2);
+ int i, t1 = 0, t2 = 1;
+ for (i = 0; i < n; ++i) {
+ int next = t1 + t2;
+ t1 = t2;
+ t2 = next;
+ }
+ return t1;
}
So, one possible heuristic would be "if there is an identical line, and lines have been added and removed both before and after the line, then consider the identical line to be changed". I can of course create my own script that applies this rule on an existing patch, but is there an existing tool to solve my problem?