Skip to content

Metahooks #731

Closed
Closed

Description

@hwright

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

  1. asottile commented on Mar 28, 2018

    @asottile
    Member

    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

    $ tree -I .git -a ../testrepo/
    ../testrepo/
    ├── .pre-commit-hooks.yaml
    ├── orghooks.yaml
    └── run-org-hooks
    
    0 directories, 3 files

    .pre-commit-hooks.yaml

    -   id: org-hook
        name: org-wide hooks
        language: script
        entry: ./run-org-hooks
        verbose: true

    verbose: true forces the output to always appear whether or not things pass

    ./run-org-hooks (mode: 0755)

    #!/usr/bin/env python
    import os
    import sys
    
    HERE = os.path.dirname(os.path.realpath(__file__))
    
    
    def main():
        cfg = os.path.join(HERE, 'orghooks.yaml')
        cmd = ['pre-commit', 'run', '--config', cfg, '--files'] + sys.argv[1:]
        os.execvp(cmd[0], cmd)
    
    
    if __name__ == '__main__':
        exit(main())

    Note here that we're using pre-commit's --config option as a sort of "include". --files is important here such that top-level pre-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)

    repos:
    -   repo: https://github.com/pre-commit/pre-commit-hooks
        rev: v1.2.3
        hooks:
        -   id: trailing-whitespace
        -   id: end-of-file-fixer
        -   id: autopep8-wrapper
        -   id: check-docstring-first
        -   id: check-yaml
        -   id: debug-statements
        -   id: name-tests-test
        -   id: requirements-txt-fixer
        -   id: flake8
    -   repo: https://github.com/asottile/reorder_python_imports
        rev: v1.0.1
        hooks:
        -   id: reorder-python-imports
    -   repo: https://github.com/asottile/add-trailing-comma
        rev: v0.6.4
        hooks:
        -   id: add-trailing-comma

    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)

    repos:
    -   repo: /tmp/wat/testrepo
        rev: 2d76bfbfddde6129c4ec5db31ac08abfbe362114
        hooks:
        -   id: org-hook

    running

    $ pre-commit run --all-files
    org-wide hooks...........................................................Passed
    hookid: org-hook
    
    Trim Trailing Whitespace.................................................Passed
    Fix End of Files.........................................................Passed
    autopep8 wrapper.........................................................Passed
    Check docstring is first.................................................Passed
    Check Yaml...............................................................Passed
    Debug Statements (Python)................................................Passed
    Tests should end in _test.py.............................................Passed
    Fix requirements.txt.....................................................Passed
    Flake8...................................................................Passed
    Reorder python imports...................................................Passed
    Add trailing commas......................................................Passed
    
    $ pre-commit run --files README.md
    org-wide hooks...........................................................Passed
    hookid: org-hook
    
    Trim Trailing Whitespace.................................................Passed
    Fix End of Files.........................................................Passed
    autopep8 wrapper.....................................(no files to check)Skipped
    Check docstring is first.............................(no files to check)Skipped
    Check Yaml...........................................(no files to check)Skipped
    Debug Statements (Python)............................(no files to check)Skipped
    Tests should end in _test.py.........................(no files to check)Skipped
    Fix requirements.txt.................................(no files to check)Skipped
    Flake8...............................................(no files to check)Skipped
    Reorder python imports...............................(no files to check)Skipped
    Add trailing commas..................................(no files to check)Skipped
    
    $ SKIP=trailing-whitespace pre-commit run --files setup.py
    org-wide hooks...........................................................Passed
    hookid: org-hook
    
    Trim Trailing Whitespace................................................Skipped
    Fix End of Files.........................................................Passed
    autopep8 wrapper.........................................................Passed
    Check docstring is first.................................................Passed
    Check Yaml...........................................(no files to check)Skipped
    Debug Statements (Python)................................................Passed
    Tests should end in _test.py.........................(no files to check)Skipped
    Fix requirements.txt.................................(no files to check)Skipped
    Flake8...................................................................Passed
    Reorder python imports...................................................Passed
    Add trailing commas......................................................Passed
    

    (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

  2. hwright commented on Mar 28, 2018

    @hwright
    Author

    Thanks for the response!

    Since it's been requested before, perhaps putting this trick in a more discoverable location would be useful?

  3. asottile commented on Mar 28, 2018

    @asottile
    Member

    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.

  4. asottile commented on Mar 29, 2018

    @asottile
    Member

    Thanks for the issue! Hope the example above was helpful :)

  5. RonnyPfannschmidt commented on Dec 3, 2018

    @RonnyPfannschmidt

    @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

  6. richtong commented on Jun 2, 2022

    @richtong

    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:

    repos:
      - repo: local
        hooks:
          - id: base-config
            name: base-config
            language: script
            entry: ./.pre-commit-config.base.sh
            verbose: true
    

    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)

    #!/usr/bin/env bash
    # this runs the base pre-commit file if present
    
    BASE="${BASE:-pre-commit-config.base.yaml}"
    echo "Running $BASE"
    if [[ -e $BASE ]]; then
    	echo "Run against $* files"
    	pre-commit run --config "./$BASE" --files "$@"
    fi
    

    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?

    Running .pre-commit-config.base.yaml
    Run against .pre-commit-config.base.yaml pre-commit-config.base.yaml pre-commit-config.template.yaml pre-commit-config.base.sh files
    [INFO] Initializing environment for https://github.com/adrienverge/yamllint.git.
    An unexpected error has occurred: CalledProcessError: command: ('/opt/homebrew/bin/git', 'checkout', '1.17.9')
    return code: 1
    expected return code: 0
    stdout: (none)
    stderr:
        error: pathspec '1.17.9' did not match any file(s) known to git
    
    Check the log at /Users/rich/.cache/pre-commit/pre-commit.log
    Running .pre-commit-config.base.yaml
    Run against workflow.base.yaml pre-commit-config.covidwa.yaml pre-commit-config.python.yaml files
    [INFO] Locking pre-commit directory
    [INFO] Initializing environment for https://github.com/adrienverge/yamllint.git.
    An unexpected error has occurred: CalledProcessError: command: ('/opt/homebrew/bin/git', 'checkout', '1.17.9')
    return code: 1
    expected return code: 0
    stdout: (none)
    stderr:
        error: pathspec '1.17.9' did not match any file(s) known to git
    
    Check the log at /Users/rich/.cache/pre-commit/pre-commit.log
    
  7. asottile commented on Jun 2, 2022

    @asottile
    Member

    the error indicates you're asking for 1.17.9 of yamllint and it doesn't exist -- check your config file

  8. asottile commented on Jun 2, 2022

    @asottile
    Member

    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

  9. locked as resolved and limited conversation to collaborators on Jun 2, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions