Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Run a pre-commit hook only if all other hooks are successful

I have a pre-commit setup with several pretty standard repos (for a Python project anyways), and one heavily magical project-specific action.

Something like this:

repos:
  - repo: https://github.com/timothycrosley/isort
    ...
  - repo: https://github.com/psf/black
    ...
  - repo: https://gitlab.com/pycqa/flake8
    ...

  - repo: local
    hooks:
      - id: local_project_specific_magic
        name: local-magic-script
        entry: magic_script.sh
        language: script

This all runs fine when all of the checks are successful.

What I need to achieve is to have the final local_project_specific_magic hook not execute if any of the previous hooks fail. Is this doable?


I have tried to add fail_fast: true and that seems to work, but it also prevents other hooks from running if any of them fail. For example, even if isort fixes some imports, I still want black to do its thing.

like image 565
frnhr Avatar asked Nov 16 '25 19:11

frnhr


1 Answers

fail_fast: true is as close as you're going to get without significant surgery


you could imagine though that each other hook does something like:

entry: bash -c 'black "$@" || touch .fail' --

and then your script does something like if [ -f .fail ]; then echo 'some other hook failed' && exit 1; fi

you would also need an always_run: true hook at the beginning to make sure .fail doesn't exist as well (rm -f .fail)

but this all sounds like a big, unmaintainable hack. I suspect you have an XY problem as your requirement seems extremely strange -- perhaps elaborate on why you would want this setup?


disclaimer: I created pre-commit

like image 149
Anthony Sottile Avatar answered Nov 18 '25 19:11

Anthony Sottile