Search code examples
prettierpre-commit-hookpre-commitpre-commit.com

Prettier using pre-commit(.com) does not re-stage changes


I started using Prettier + eslint using pretty-quick & husky (option 2). It behaves as-expected, i.e., reformats code during a commit (or amend) and includes the changes in the commit.

My monorepo needed (several) more pre-commit hooks, so I ended up migrating to pre-commit.com (option 3). Now, when I commit or amend, Prettier modifies the files and returns Failed status. This creates for a rather annoying workflow where I am forced to add the files and attempt to commit the changes again.

Is there some way to re-stage the changes as part of the commit?


Solution

  • pre-commit intentionally never touches the staging area. silently changing what is being committed is dangerous. the tool always gives you an opportunity to validate what the hooks do before committing

    you can read more about this in the issue tracker:

    My recommendation if you want to fire from the hip is to run git add -u && !! immediately afterwards -- this will stage the changed files and rerun the command you just ran

    if you want to venture into unsupported territory, you can run this as part of the command (as demonstrated in one of the issues) -- but this subtly breaks a lot of the guarantees that the tool has

      - id: yapf
        entry: bash -c 'yapf "$@"; git add -u' --
    

    disclaimer: I am the author of pre-commit