Skip to content

Run clang-format on the stage instead of the working dir

This has two effects:
a) Only the staged files are checked instead of all files in the working directory, so that it's way faster than the current hook and it doesn't complain about formatting issue in code you don't want to commit (e.g. local debugging code).
b) More importantly, it prevents you from committing wrongly formatted code that you have clang-formatted in the working directory after the commit hook complained but that you forgot to stage after fixing the formatting.

a) makes commits much less painful in larger projects and b) regularly happens to me.

Test plan:

  1. Add some whitespace/linebreaks to some code that break the formatting
  2. Stage those changes
  3. Try to commit them -> the hook complains (very fast)
  4. Run git clang-format --staged --extensions 'cpp,h,hpp,c' to fix the formatting -> this is very fast
  5. Try to commit them -> the hook still complains because the staged changes are still wrongly formatted (the old hook didn't complain!)
  6. Add some non-formatting change, e.g. a comment, to the code and stage the fixed formatting and this code
  7. Try to commit the changes -> now the commit goes through
Edited by Ingo Klöcker

Merge request reports