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:
- Add some whitespace/linebreaks to some code that break the formatting
- Stage those changes
- Try to commit them -> the hook complains (very fast)
- Run
git clang-format --staged --extensions 'cpp,h,hpp,c'
to fix the formatting -> this is very fast - Try to commit them -> the hook still complains because the staged changes are still wrongly formatted (the old hook didn't complain!)
- Add some non-formatting change, e.g. a comment, to the code and stage the fixed formatting and this code
- Try to commit the changes -> now the commit goes through