1

I have a self defined pre-commit hook which I want to skip if an earlier hook fails. If an earlier hook fails, a logfile named pre-commit.log is generated.

A .pre-commit-config.yaml could look like this:

repos:
-   repo: https://gitlab.com/pycqa/flake8
    rev: 3.9.2
    hooks:
    - id: flake8
      log_file: pre-commit.log
-   repo: local
    hooks:
    - id: skript
      name: skript
      entry: bash skript.sh
      language: system
      log_file: pre-commit.log

My bash skript.sh looks like this:

if [ -f "pre-commit.log" ]; then
  echo "Error in earlier pre-commit! We skip this script."
  echo "See 'pre-commit.log' for detailed information."
  exit 1
else
  echo "All pre-commits successful."
  # some other code
fi

Right now I check if the "pre-commit.log" exists and if so I return exit 1. Because of this, the output for this hook is Failed. But I would prefer the status Skipped because there was no failure.

My goal is to see in the console or logfile the text Skipped, like in this image for jupyter-notebook-cleanup. If I write exit 0 I get Passed like for isort and black and if I return exit 1 or another number greater 1 I get Failed like for flake8.

console output

How can I get the message Skipped?

John Kugelman
  • 349,597
  • 67
  • 533
  • 578
mosc9575
  • 5,618
  • 2
  • 9
  • 32
  • If you want a hook to be skipped, it seems you simply want it to return 0 so git considers it to have passed. How do you want git to behave when your hook is "skipped"? – William Pursell Feb 24 '22 at 13:40
  • If I return `0`, the check is `Passed`. Since an earlier hook is failed, git will reject the commit anyway. – mosc9575 Feb 24 '22 at 13:51
  • What is "check is Passed"? git does not report status at all. It just rejects the commit. If you want a status line that says "skipped", then write `if test -f pre-commit.log; then echo "Skipping pre-commit hook"; exit 0; fi` – William Pursell Feb 24 '22 at 14:01
  • Ah...you have edited the question. What framework are you using that is generating that status report? – William Pursell Feb 24 '22 at 14:03
  • I am using a terminal on a Linux machine calling `git commit "message"`. I follow the process descripted [here](https://pre-commit.com/index.html#usage). – mosc9575 Feb 24 '22 at 14:04
  • Looks like those scripts do not support your feature: https://github.com/pre-commit/pre-commit/blob/master/pre_commit/commands/run.py#L199-L204. It should be an easy change to make. Submit a PR! – William Pursell Feb 24 '22 at 14:30
  • Note that all of that status reporting is coming out of your hook scripts. git knows nothing about it, and doesn't care about anything other than the status code returned by the script. – William Pursell Feb 24 '22 at 14:31
  • That sounds very plausible. Thank you for your help. – mosc9575 Feb 24 '22 at 14:34
  • 1
    The test suite built by the autotools followed the convention that 77 was the return code for a skipped test. If you do submit a PR, I would recommend following that convention. – William Pursell Feb 24 '22 at 14:35
  • Ah....but look here: https://github.com/pre-commit/pre-commit/blob/master/pre_commit/commands/run.py#L134. Perhaps the functionality you seek is already there. – William Pursell Feb 24 '22 at 14:37

1 Answers1

2

there is no way to influence that text be Skipped

Skipped only appears in the following cases:

  • the hook was not run because its hookid appeared inside the SKIP environment variable
  • there were no files which matched files / types / types_or and pass_filenames was true (the default) and always_run was false (the default)

your best bet is to exit 0 in the non-failure case, this will appear as Passed in the output


disclaimer: I wrote the pre-commit tool you're using

anthony sottile
  • 61,815
  • 15
  • 148
  • 207
  • Thanks for your response. Would you accept a Feature Request like @William Pursell mentioned with the code `77`? Or is this something you see no benefit? – mosc9575 Feb 24 '22 at 14:47
  • 1
    no, pre-commit can run any executable and it cannot guarantee the conventions of any executable follows some arbitrary "77" code – anthony sottile Feb 24 '22 at 14:48