Verify that Git commit only moves lines

Viewed 61

Does Git provide any mechanism for proving that a commit moves but does not modify lines within a file? I frequently use -w to determine whether any changes were made within a section of code whose indentation changed. If a section of code is moved vertically rather than laterally, is there anything one can do to highlight changes within the moved section?

2 Answers

No, Git doesn't provide a way of proving this. It shouldn't be too hard to parse the diff output and verify that the groups of deleted lines and added lines are the same groups. This would require some scripting, but it is doable. This project may be interesting.

If you're looking to verify this visually, you can use --color-moved, which will color moved blocks in a different color than additions or removals that aren't moved lines.

I don't think Git provides any direct way of doing this, but if sorting the old and new versions of a file yield the same result, that's equivalent to the change consisting only of moving lines.

Of course that won't distinguish between swapping two blocks of lines and completely scrambling the file.

Highlighting such changes is another matter. This question, which I asked several years ago, might be relevant:

Is there a diff-like algorithm that handles moving block of lines?

Related