gitgit-diff

Git diff when swapping statements


I have a the following piece of code in my git repo (using git version 2.33.0.windows.2):

int foo = 0;
// foo is set in some manner
if (foo == 1) {
    DoA();
    DoB();
    DoC();
    DoD();
}
if (foo == 2) {
    DoA();
    DoC();
    DoE();
}
// etc

Now if I change this code by switching up the order of the if statements to become like this:

int foo = 0;
// foo is set in some manner
if (foo == 2) {
    DoA();
    DoC();
    DoE();
}
if (foo == 1) {
    DoA();
    DoB();
    DoC();
    DoD();
}
// etc
    

If I now do a git diff the output is this:

@@ -1,14 +1,14 @@
 int foo = 0;
 // foo is set in some manner
-if (foo == 1) {
+if (foo == 2) {
     DoA();
-    DoB();
     DoC();
-    DoD();
+    DoE();
 }
-if (foo == 2) {
+if (foo == 1) {
     DoA();
+    DoB();
     DoC();

Is there a way to make git 'recognize' that the if statements where swapped instead of some lines that were edited? Of course it is correct, but it would make it easier to review that nothing changed, except the order of the if statements.


Solution

  • Try git -c diff.algorithm=histogram diff and if you like what you see you can configure that as the default, git config diff.algorithm histogram with or without --global. I suspect the cost of the extra analysis isn't even measurable these days.