Repository navigation
Description
I'd like to be able to configure pre-commit to run a hook, which itself specifies one or more other hooks to run, along with their configuration.
Consider the case of an organization with multiple repositories which wants to enforce a single yapf configuration between them. Today, each repository has to include something like the following stanza in its configuration file:
- repo: git://github.com/pre-commit/mirrors-yapf
sha: v0.21.0
hooks:
- id: yapf
args:
- -i
- --style=...
Instead, it would be useful to be able to have a git repository with an entry in its .pre-commit-hooks.yaml which looks something like this:
- id: my-org-hooks
name: Hooks for my organization
metahooks:
- repo: git://github.com/pre-commit/mirrors-yapf
sha: v0.21.0
hooks:
- id: yapf
args:
- -i
- --style=...
The individual projects could then just put the following in their .pre-commit-config.yaml files:
- repo: git://myrepo.git
sha: ...
hooks:
- id: my-org-hooks
In this case, my-org-hooks would run each of the entries in hooks as a separate hook with the given arguments, reporting the results independently, perhaps with the following output:
Hook 1...............................................(no files to check)Skipped
Hook 2...................................................................Passed
Hooks for my organization:
yapf...................................................................Passed
Any organization-wide changes could then be applied to the centralized location, rather than having to be changed everywhere.
Activity
asottile commentedon Mar 28, 2018
So, while I get your usecase -- and it sounds good in theory -- it tends not to work in practice. In a lot of ways, pre-commit was designed as a reaction to centralized managed hooks. That said, there's some features that enable this to work currently.
A centrally managed approach will probably work for a small organization (5-10 individuals, all with similar mindsets, with agreement on style, etc.) and with very little variation between projects (they all run the same interpreter versions, don't have special needs for exclusion, etc.). The complication (and really where pre-commit's per-repository configuration shines) is when there's special cases, separate teams (with separate ideas of style, etc.) and require overriding.
That said, here's how you can do this with the facilities provided today:
example
layout
.pre-commit-hooks.yamlverbose: trueforces the output to always appear whether or not things pass./run-org-hooks(mode:0755)Note here that we're using pre-commit's
--configoption as a sort of "include".--filesis important here such that top-levelpre-commit run --files ...is honored.orghooks.yaml(basically any old.pre-commit-config.yaml!)(this particular one was copied from https://github.com/asottile/all-repos)
consuming repository
.pre-commit-config.yaml(note, I'm using local paths and a sha here because I wanted to test the idea, in reality you'd put this in a clonable repository and use tags)
running
(though there's no way to show it in github comments, the output of the inner hooks will be uncolored due to not going to a
tty)Note that @dan98765 actually asked for a similar thing in #548 but it wasn't really workable at the time.
Also note, pre-commit already has a thing called "meta hooks" -- I just haven't gotten around to documenting them :D
hwright commentedon Mar 28, 2018
Thanks for the response!
Since it's been requested before, perhaps putting this trick in a more discoverable location would be useful?
asottile commentedon Mar 28, 2018
To be honest, I don't really think it makes sense because I don't want to support a workflow that really can't work. This is a hack at best and won't become a first class citizen. Additionally, beyond documenting (already documented!)
--config, this isn't doing anything particularly clever.asottile commentedon Mar 29, 2018
Thanks for the issue! Hope the example above was helpful :)
RonnyPfannschmidt commentedon Dec 3, 2018
@asottile while this one as is has gotten closed something else came to my attention while working on a project like this myself
i'd like to track the configuration of python hooks all at once at a meta repository that is versioned, so i can track updates centrally while having a sense on the fixed state of each consumer, so that the users of the meta hooks just update one version number to get on track
richtong commentedon Jun 2, 2022
Hmm, this example does not seem to work, but it is exactly what I need. Fir for the base script, I needed to add enough so pre-commit understood it, that is to just call the script:
So this just calls a shell script and then shell script does the work of running the shared scripts (this is just in a local directory now)
But when I try to do a "pre-commit install" and then "pre-commit run" I get a strange error looks like pre-commit is trying to check out a version number?
asottile commentedon Jun 2, 2022
the error indicates you're asking for 1.17.9 of yamllint and it doesn't exist -- check your config file
asottile commentedon Jun 2, 2022
and in the future please open a new issue with full details rather than grave digging a years old issue with a new unrelated off topic question