gitgithookspre-commitpre-commit.compython-black

black as pre-commit hook always fails my commits


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?


Solution

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