Ginger Jesus
Ginger Jesus

Reputation: 444

Can I exclude files from every pre-commit hook except for one without repeating myself?

I would like to make a Git repo that’s REUSE compliant. I want to use the reuse pre-commit hook to help ensure that I’m compliant. The REUSE Specification requires that I make a folder called “LICENSES” that contains each of the licenses that my project uses. I’m using several other hooks, and I don’t want those hooks to check or modify anything in the LICENSES/ folder. I want every hook other than reuse to exclude LICENSES/. (Side note: at the moment, it looks like the reuse hook isn’t affected by excluding LICENSES/, but that should probably be fixed). Here’s what I’ve tried so far:

exclude: '^LICENSES/'
repos:
    -
        repo: https://github.com/pre-commit/pre-commit-hooks
        rev: v4.0.1
        hooks:
            - id: check-case-conflict
            - id: end-of-file-fixer
            - id: mixed-line-ending
            - id: trailing-whitespace
    # I’m using identity instead of reuse here for testing and to better
    # illustrate the problem.
    -
        repo: meta
        hooks:
            -
                id: identity
                exclude: "^$"

That didn’t work. It looks like the per-hook exclude pattern applies in addition to the global one. I wanted the per-hook exclude pattern to apply instead of the global one.

I could use a per-hook exclude pattern on every hook except one:

repos:
    -
        repo: https://github.com/pre-commit/pre-commit-hooks
        rev: v4.0.1
        hooks:
            -
                id: check-case-conflict
                exclude: &exclude_pattern '^LICENSES/'
            -
                id: end-of-file-fixer
                exclude: *exclude_pattern
            -
                id: mixed-line-ending
                exclude: *exclude_pattern
            -
                id: trailing-whitespace
                exclude: *exclude_pattern
    # I’m using identity instead of reuse here for testing and to better
    # illustrate the problem.
    -
        repo: meta
        hooks:
            - id: identity

But, then I would be repeating myself. I think that I might be able to repeat myself less using merge keys, but I don’t think that those are supported. Is there anyway to avoid repeating myself or to repeat myself less?

Upvotes: 16

Views: 23045

Answers (1)

anthony sottile
anthony sottile

Reputation: 69914

there is not without specifying an exclude on each hook as you've found

the top level exclude is a shortcut which is applied to all hooks

I would recommend just adding exclude to the hooks which would modify the LICENSES


disclaimer: I created pre-commit

Upvotes: 19

Related Questions