Add a git pre-commit hook for yapf
Closed this issue · 4 comments
Since this project uses yapf to automatically reformat the python files, it might be nice to add a git pre-commit hook to automatically run this before each commit. I've had success using this approach with the black
autoformatter.
I wouldn't want to do a pre-commit hook that changes the code, but I'd support one that blocks the commit if formatting is correct and tells you the command to run to fix it.
I've also done auto-formatting on-commit with Black. Just curious, @mtlynch, why you don't like it?
Not sure if you're aware but the way pre-commit works with auto-formatting is it typically tries to change the code, and if there were changes, the commit is basically aborted, and you can then look at the changes by doing git status
, git diff
, etc... The original changes that were added to the index are still in the index.
I just thought maybe you didn't like it because you don't like the idea of it automatically modifying files in-flight, so I thought I'd explain that pre-commit + black/yapf don't do that, at least not by default. I'm not too fond of that either.
one that blocks the commit if formatting is correct and tells you the command to run to fix it.
What pre-commit + black/yapf does is automate exactly that... blocks the commit, but it runs the command to fix it for you.
I just don't like the idea of automated code changes that the developer only chooses passively. Sometimes linters and formatters make bizarre choices, so I want the developer to review them and make an explicit choice to apply them.
In general, it just makes me nervous to have any changes occur after the developer commits. Making code changes is hard, so I want there to be as little abstraction in that process so that the developer understands exactly what they're committing.