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

pre-commit hooks terminate if previous stage fails


I'm using the pre-commit hooks configuration https://pre-commit.com/ to enable pre-commit hooks

repos:
  - repo: local
    hooks:
      - id: pytest-check
        name: pytest-check
        entry: pytest
        language: system
        pass_filenames: false
        always_run: false
      - id: flake8
        name: flake8
        entry: flake8
        language: python
        types: [python]
        args: ['src/']

If pytest-check fails it will also execute the flake8 hook.
Is it possible to terminate the execution if a previous hook failed, in this case flake8 would not run if the pytest-check failed. I read through the docs but couldn't find any information on this...


Solution

  • you're looking for fail_fast: true

    it can be specified both at the top level and at the hook level


    an aside you have a few unrelated problems with your configuration:

    • ~generally you don't want to run tests as part of pre-commit, they'll be slow which will frustrate your users and often lead to them turning the whole thing off
    • always_run: false is the default, no need to specify it
    • flake8 as you've configured it is both double-linting and fork bombing (you're passing src/ and pre-commit is additionally passing filenames, and you've misconfigured the multiprocessing mode) -- I'd recommend using the pycqa/flake8 repository directly which configures this correctly
    • for repo: local hooks there's no reason to use args -- just specify it directly in entry

    disclaimer: I wrote pre-commit