black as pre-commit hook always fails my commits

One of my developers came with a good tip, in case you have a fail by commit due to black (for example due to the single/double quotes) that's resolved with the pre-commit-hook (like with double-quote-string-fixer). You get in kind of 'nobodies land git situation'. There is a changed file in the staged files, but cannot be committed by the pre-commit-hook, git status won't see any changes, but commit fails (a really black hole in my opinion). You only get a fail on commit, but nothing can be done (excecpt a reset head on this file). Once you are in this situation and run: use commit -m 'Resolving pre-commit-hook changes' --no-verify ..... tada! It's resolved.


(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:

  • pre-commit/pre-commit#806
  • pre-commit/pre-commit#747

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 :)