Read more

Git: Parsing large diffs as a human

Michael Leimstädtner
January 14, 2021Software engineer at makandra GmbH

I just finished migrating a project from the Asset Pipeline to Webpacker, this is what my diff to master looks like:

5.825 files changed, 44.805 insertions(+), 529.948 deletions(-)
warning: inexact rename detection was skipped due to too many files.
warning: you may want to set your diff.renameLimit variable to at least 5134 and retry the command.

Illustration online protection

Rails Long Term Support

Rails LTS provides security patches for old versions of Ruby on Rails (2.3, 3.2, 4.2 and 5.2)

  • Prevents you from data breaches and liability risks
  • Upgrade at your own pace
  • Works with modern Rubies
Read more Show archive.org snapshot

There is no way me or my peer reviewer is able to parse 500k+ lines of code. Fortunately, git has some handy diff options ready:

option comment
--color-moved=dimmed_zebra Highlight only changes in renamed files
--find-renames=X% Default: 50%. If not all renames are matched, use a smaller amount.
--diff-algorithm Try out different algorithms if the diff doesn't look right
--no-color-moved-ws Hides white-space changes for moved files
--diff-filter=X Restrict diff to: Added (A), Copied (C), Deleted (D), Modified (M), Renamed (R), Unknown (X) files

To tackle the rename limit mentioned above, update the local or global git configuration.

git config diff.renameLimit 9999

Furthermore, I encourage you to further split the diff into smaller chunks:

  • By file extension (e.g.: first review your Ruby code, then JS, then the rest)
  • By excluding directories (e.g.: handle application and vendor code separately)

Overall I ended up splitting up my code into these (human readable) chunks:

git diff master --color-moved=dimmed_zebra --find-renames=25% '*.sass' ':!vendor'
git diff master --color-moved=dimmed_zebra --find-renames=25% '*.coffee' ':!vendor'
git diff master --color-moved=dimmed_zebra --find-renames=25% '*.js' ':!vendor'
git diff master --color-moved=dimmed_zebra --find-renames=25% '*.haml' ':!vendor'
git diff master --color-moved=dimmed_zebra --find-renames=25% '*.rb' ':!vendor'
git diff master --color-moved=dimmed_zebra --find-renames=25% ':!*.coffee' ':!*.sass' ':!*.js' ':!*.haml' ':!*.rb' ':!vendor'
git diff master --color-moved=dimmed_zebra --find-renames=25% vendor/
Michael Leimstädtner
January 14, 2021Software engineer at makandra GmbH
Posted by Michael Leimstädtner to makandra dev (2021-01-14 13:50)