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?
- 22,552
- 15
- 74
- 98
-
3No, but it might be possible to write a custom diff tool to do that kind of analysis – Lasse V. Karlsen Jul 18 '20 at 18:19
-
So far I haven't seen any diff tool (I have seen git difftool, differential in phabricator, pull requests in bitbucket, github and gitlab) which does that. It would be interesting to know if any do from pov of code review. – skumar Jul 18 '20 at 19:59
-
Let me see if I can whip something up real fast. – Lasse V. Karlsen Jul 18 '20 at 20:06
-
3Here's a basic prototype written in C# - https://github.com/lvk-stackoverflow/SO62972103 - it uses my library for working out the diff between two files, and then analyzes the result to write some basic output. Read the README shown on the front page for examples on how to use and what it does. – Lasse V. Karlsen Jul 18 '20 at 20:36
-
Unfortunately, when using a git difftool, temporary files are created that has filenames that does not in any way resemble the original filenames, so I don't know how to report which file you're looking at the result from. Some spelunking in the git options might give you some options in this regard. – Lasse V. Karlsen Jul 18 '20 at 20:37
-
I posted a question of my own regarding the name of the file being diffed - https://stackoverflow.com/questions/62973585/when-writing-a-git-difftool-is-there-a-way-for-the-tool-to-know-which-file-is-b – Lasse V. Karlsen Jul 18 '20 at 20:42
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.
- 48,483
- 5
- 42
- 63
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?
- 242,098
- 41
- 402
- 602