Originally posted at community/community#9632 (comment)
Omg yes, please! This is one of my biggest pet peeves/code review slowdowns when performing large refactors to cleanup a legacy codebase.
The following were some manual review notes/commands I added to a recent PR to assist others who were reviewing:
The following
git diffcommand will make it easier to see what actually changed in the code here, as opposed to what just moved around without being changed; to make review/etc easier. Dimmed code in the diff has been moved, but not changed.This will get all the changes made in this branch, using
devalias/improve-first-useas the base:git \ -c color.diff.oldMoved="white dim" \ -c color.diff.oldMovedAlternative="white dim" \ -c color.diff.newMoved="white dim" \ -c color.diff.newMovedAlternative="white dim" \ -c color.diff.newMovedDimmed="white dim" \ -c color.diff.newMovedAlternativeDimmed="white dim" \ diff --color-moved=dimmed-zebra --color-moved-ws=ignore-all-space --minimal \ devalias/improve-first-use HEADThis will get all changes made for a specific commit hash (
55a259f), as compared to the commit hash just before it (55a259f~):git \ -c color.diff.oldMoved="white dim" \ -c color.diff.oldMovedAlternative="white dim" \ -c color.diff.newMoved="white dim" \ -c color.diff.newMovedAlternative="white dim" \ -c color.diff.newMovedDimmed="white dim" \ -c color.diff.newMovedAlternativeDimmed="white dim" \ diff --color-moved=dimmed-zebra --color-moved-ws=ignore-all-space --minimal \ 55a259f~ 55a259fThis will get all changes made for a specific commit hash (
55a259f), as compared to the commit hash just before it (55a259f~), only for the specific file/path specified:git \ -c color.diff.oldMoved="white dim" \ -c color.diff.oldMovedAlternative="white dim" \ -c color.diff.newMoved="white dim" \ -c color.diff.newMovedAlternative="white dim" \ -c color.diff.newMovedDimmed="white dim" \ -c color.diff.newMovedAlternativeDimmed="white dim" \ diff --color-moved=dimmed-zebra --color-moved-ws=ignore-all-space --minimal \ 55a259f~ 55a259f -- oauth/vendor/slack/index.jsYou can simplify this for future use by adding it to your
gitaliases in your~/.gitconfig, something like the following:[alias] diff-refactor = \ -c color.diff.oldMoved='white dim' \ -c color.diff.oldMovedAlternative='white dim' \ -c color.diff.newMoved='white dim' \ -c color.diff.newMovedAlternative='white dim' \ -c color.diff.newMovedDimmed='white dim' \ -c color.diff.newMovedAlternativeDimmed='white dim' \ diff --ignore-blank-lines --color-moved=dimmed-zebra --color-moved-ws=ignore-all-space --minimalWhich would then allow you to use it as just:
⇒ git diff-refactor ⇒ git diff-refactor devalias/improve-first-use HEAD ⇒ git diff-refactor 55a259f~ 55a259f ⇒ git diff-refactor 55a259f~ 55a259f -- oauth/vendor/slack/index.jsYou could potentially add the following as well, though i'm not sure if they will do anything new/useful when combined with
--color-moved:--ignore-all-space --ignore-space-change --ignore-space-at-eol --ignore-cr-at-eol --ignore-blank-lines
I wonder how hard it would be to take the output from git diff --color-moved, parse it, and then render it in a GitHub comment/similar..?
Some potentially useful references:
- https://stackoverflow.com/questions/11509830/how-to-add-color-to-githubs-readme-md-file/73613687#73613687
-
Now since May 2022, Github can accept LATEX code on Markdown, so you can use the
\color{namecolor}inside the$$$$ - https://github.blog/2022-05-19-math-support-in-markdown/
- https://en.wikibooks.org/wiki/LaTeX/Colors
-
- Bash color codes: https://gist.github.com/vratiu/9780109
- https://stackoverflow.com/questions/1380333/highlighting-added-deleted-lines-ignoring-moves-in-a-patch-file
Super hacky, but this uses sed to try and strip out parts of the diff based on the color codes:
git \
-c color.diff.oldMoved='white dim' \
-c color.diff.oldMovedAlternative='white dim' \
-c color.diff.newMoved='white dim' \
-c color.diff.newMovedAlternative='white dim' \
-c color.diff.newMovedDimmed='white dim' \
-c color.diff.newMovedAlternativeDimmed='white dim' \
diff --ignore-blank-lines --color-moved=dimmed-zebra --color-moved-ws=ignore-all-space --minimal --color HEAD~1 HEAD | sed -r "s/\x1B\[2;37m\+(.*)\x1B\[m/\1/g" | sed -r "s/\x1B\[2m-(.*)\x1B\[m/\1/g" | sed -r "s/\x1B\[([0-9]{1,2}(;[0-9]{1,2})?)?[mGK]//g"Strip 'dim white' +:
sed -r "s/\x1B\[2;37m\+(.*)\x1B\[m/\1/g"Strip 'dim' -, keeping the content between:
sed -r "s/\x1B\[2m-(.*)\x1B\[m/\1/g"Strip all colour codes:
sed -r "s/\x1B\[([0-9]{1,2}(;[0-9]{1,2})?)?[mGK]//g"If we then put that colour stripped output into a standard GitHub code block with diff formatting, it will only use the + and - lines to determine the colouring to show, and so will highlight the 'actual' changes in red/green.
See also, my followup comments on that thread: