I'm trying to use pre-commit to manage Black as a Git pre-commit hook, but I must be doing it wrong.
In my pre-commit config file I have:
- repo: https://github.com/psf/black rev: 19.3b0 hooks: - id: black
What I'm expecting to happen is for Black to just modify the staged file, and for the commit to succeed. Because the whole point of Black is that it auto-enforces Python code style rules, no questions asked.
What actually happens when I stage a (non-black-compliant) file and try to commit: Black goes ahead and modifies the file to make it compliant, as intended... But the problem is that it returns a "fail". So the commit fails. And then I have to unstage the file, then restage it before committing again... and only then does the commit succeed.
This is a huge annoyance and can't possibly be the intended workflow?
What am I doing wrong?
Use the --no-verify option to skip git commit hooks, e.g. git commit -m "commit message" --no-verify . When the --no-verify option is used, the pre-commit and commit-msg hooks are bypassed.
If you want enforcement, use an update hook in the central repo. If the hook is doing per-commit verification, you can still provide a pre-commit hook; developers will likely adopt it voluntarily, so that they can find out right away when they've done something wrong, rather than waiting until they try to push.
pre-commit hooks are a mechanism of the version control system git. They let you execute code right before the commit. Confusingly, there is also a Python package called pre-commit which allows you to create and use pre-commit hooks with a way simpler interface.
commit-msg This hook is invoked by git commit, and can be bypassed with the --no-verify option. It takes a single parameter, the name of the file that holds the proposed commit log message. Exiting with a non-zero status causes the git commit to abort. Under my_git_project/.
(author of pre-commit
here)
the framework intentionally does not provide a way to auto-commit modifications. Here's a few issues which have asked for such:
A comment from one of those issues:
pre-commit itself will never touch the staging area. These are good ways to silently break commits. In my mind this is one of the worst things that [other frameworks do and suggest] -- hooks are very frequently not perfect and magically changing what's being committed should not be taken lightly.
That said, if you would like to foot gun, your hook can call
git add -u
and pre-commit won't know any better :) a sketch of that (untested, discouraged)- id: yapf entry: bash -c 'yapf "$@"; git add -u' --
(note: using
bash
will potentially reduce portability)
Another comment notes
Fortunately,
git add -u && !!
is pretty easy to run if you're ok firing from the hip :)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With