Run pre-commit only on certain branches
Asked Answered
S

2

16

I am looking for a configuration in the .pre-commit-config.yaml to exclude pre-commit from being run on certain branches, or to run it only on some branches. I don't know if this feature not implemented, or if I am missing it in the docs.

Thanks!

Sulfamerazine answered 27/7, 2020 at 9:50 Comment(8)
.pre-commit-config.yaml is not part of git alone, it must be part of an extra tool : gitlab or jenkins or redmine or ... Can you specify which one ?Outride
How about making it run on all branches (which is probably default) and have it just ignore all branches it is irrelevant for? ie. let the script ignore the branches in code?Chorion
Later pushes go to gitlab, but AFAIK .pre-commit-config.yaml works in local (and it does indeed after I add or remove hooks). @LasseV.Karlsen this is what my question refers to. I would like pre-commit not to be run on certain branches. This configuration is available in overcommit, but I don't find it for pre-commit.Flew
why do you want / need to disable it on particular branches? seems like an odd feature (I'm the creator/maintainer)Blear
If I'm on dev or in other side branches, there are linting and other things I don't care to correct at that moment, since I just want to do quick commits. However, on release, hotfix or master branches I want those corrections to be made.Flew
Yes, exactly. Sometimes I just want to save WIP code, and it's okay when it's a WIP branch. But this code shouldn't be pushed to mainBrushwood
@anthonysottile: I would also say this is a pretty common use case. Quite often a private dev branch is used for quick'n'dirty iterative development, with rebases and force-pushes. On such a branch I find myself circumvent pre-commit quite often. Would be nice to be able to run certain steps on given branches only. This way you could make sure the easy stuff like formatting and doctests is still active, while type-hinting can be refactored later. Even better would be a way to deactivate certain steps in a commit message, which are then forbidden on official branches like main.Garling
you're looking for SKIP. but also just kinda sounds lazy tbh. I've never understood the "I want to write bad code until PR time" peopleBlear
O
1

Git hooks are just shell scripts that are executed.

So in the script you can do something similar to this:

if [ $(git branch --no-color | sed -e '/^[^*]/d' -e 's/* \(.*\)/\1/') = "main" ]; then
  # Do checks for main here
fi
Olly answered 1/11, 2022 at 23:1 Comment(1)
You can avoid using sed by passing --show-current to git branch.Eutectoid
E
1

There’s a few different ways to accomplish this:

Option 1: Removing the config from certain branches

$ git switch -c no-pre-commit
Switched to a new branch 'no-pre-commit'
$ git rm .pre-commit-config.yaml
rm '.pre-commit-config.yaml'
$ git commit -m 'Remove pre-commit config'
No .pre-commit-config.yaml file was found
- To temporarily silence this, run `PRE_COMMIT_ALLOW_NO_CONFIG=1 git ...`
- To permanently silence this, install pre-commit with the --allow-missing-config option
$ pre-commit install --allow-missing-config
pre-commit installed at .git/hooks/pre-commit
$ git commit -m 'Remove pre-commit config'
`.pre-commit-config.yaml` config file not found. Skipping `pre-commit`.
[no-pre-commit 948c59a] Remove pre-commit config
 1 file changed, 159 deletions(-)
 delete mode 100644 .pre-commit-config.yaml

If you’ve installed pre-commit for different stages, then you also have to run

pre-commit install -t <stage> --allow-missing-config

Option 2: Giving certain branches a config that does nothing

Technically, the only thing you need for a pre-commit config to be valid is a repos list, and that list can be empty. If you add a commit to certain branches that changes their .pre-commit-config.yaml to this,

repos: []

then pre-commit will do nothing when you commit to those branches.

Eutectoid answered 25/5, 2023 at 21:9 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.