So, I've run into this problem a few times. During a round of changes, I remove functionA()
and add functionB()
in the same place. When I run a diff
, I end up with a hideous muddled set of changes where it tries matching the two functions up on their common braces rather than showing all of functionA
as a removal and all of functionB
as an addition. For a simplified example:
int functionA(int a, bool b)
{
int c;
bool d;
if (a == b)
{
//do stuff
}
//do more stuff
}
replaced with
void functionB()
{
// do different stuff
for (int x=0; x<10; x++)
{
//do more different stuff
}
//do even more different stuff
}
the diff might yield
-int functionA(int a, bool b)
+void functionB()
{
- int c;
- bool d;
- if (a == b)
+ // do different stuff
+ for (int x=0; x<10; x++)
{
- //do stuff
+ //do more different stuff
}
- //do more stuff
+ //do even more different stuff
}
Which is useless and difficult to read, since there's really no commonality besides the braces. It will even skip braces that don't align until it finds ones that do, which is common enough. And in one case I'm looking at now, I removed 7 consecutive functions and added 3, which do roughly the same thing a much more easily readable/maintainable manner. The diff algorithm disburses the additions in a scattered way through all 7 removed functions, making an unreadable and confusing mess.
Is there any way to tweak git diff to ignore cases where the only commonality is a brace?
If it matters, my system currently has git 1.9.0 and diff (GNU difftools) 2.8.1 I tagged C++ because the system I work on is (mostly C-style) C++, but this should apply to a number of other languages.
While looking for existing questions, the nearest I could find was What is git diff --patience
for?
However, this had very little impact on my diff. (It is different, but not visibly clearer)
I also see in the git diff man page the --break-rewrites option, which does what I want, but only at the file level, not the function level.
If nothing else, I guess I can try making a habit in the future of putting new logic in a completely different part of the file from any removals in cases where I want the diff to show the addition in one block, but I'd prefer to keep related logic together.