Reviewing moved files with git
This might be a well-known trick already, but just in case it’s not…
Reviewing a patch can be a bit painful when a file that has been changed and moved or renamed at one go (and there can be perfectly valid reasons for doing this). A nice thing about git is that you can reference files in an arbitrary tree while using git diff, so reviewing such changes can become easier if you do something like this:
$ git am 0001-the-thing-I-need-to-review.patch
$ git diff HEAD^:old/path/to/file.c new/path/to/file.c
This just references file.c in its old path, which is available in the commit before HEAD, and compares it to the file at the new path in the patch you just merged.
Of course, you can also use this to diff a file at some arbitrary point in the past, or in some arbitrary branch, with the same file at the current HEAD or any other point.
Hopefully this is helpful to someone out there!
Update: As Alex Elsayed points out in the comments, git diff -M/-C can be used to similar effect. The above example, for example, could be written as:
$ git am 0001-the-thing-I-need-to-review.patch
$ git show -C6 Comments
Comments are closed.
You might also be interested in the -M (track moves) and -C (track copies) options – with those, so long as the move or copy is the only change,
git diff -M -C -Cwill give the same result as your command above with a lot less typing. If there are other changes, it’ll still do the right thing – it’ll mark the diff hunk as a move, and only show the changed lines.Although ‘diff’ is most relevant for ‘apply’ – for ‘am’, which commits it,
git format-patch -M -C -C -1 --stdoutis a good option, as isgit log --patch -M -C -CThanks for pointing this out! I guess
-Cimplies-M? And why the multiple-C?The duplication of -C is the same as specifying -C – so -C -C is the same as -C2. It’s mostly just habit; I first learned it as -M -C -C.
Gah, it cut out my -C<number-of-repetions>
It’s in the ‘git diff’ manpage – a higher -C number makes it ‘try harder’ to find copies.
Are you trying to review the RAOP-related patches for PulseAudio? Asking because they do contain such inconvenient-to-review code-moving.