Skip to content

Exclude multiple files in pre-commit-hooks

How do you list all the files to avoid during pre-commit-hooks?

The issue

I have 2 configuration files in 2 different dockerized app.

I am using pre-commit-hooks to check, among other things, the Yaml files in my project.

The 2 files I try to exclude are: notes-app/mkdocs.yml and blog-app/mkdocs.yml.

So I tried to list them in the config file, .pre-commit-config.yaml:

repos:
  - repo: https://github.com/pre-commit/pre-commit-hooks
    rev: v5.0.0
    hooks:
    - id: check-yaml
      exclude:
        - notes-app/mkdocs.yml
        - blog-app/mkdocs.yml

which of course did not work:

 git commit -m "blog-post: about pre-commit-hooks multiple exclusions"
An error has occurred: InvalidConfigError:
==> File .pre-commit-config.yaml
==> At Config()
==> At key: repos
==> At Repository(repo='https://github.com/pre-commit/pre-commit-hooks')
==> At key: hooks
==> At Hook(id='check-yaml')
==> At key: exclude
=====> Expected string got list
Check the log at /Users/cruder/.cache/pre-commit/pre-commit.log

Solution

repos:
  - repo: https://github.com/pre-commit/pre-commit-hooks
    rev: v5.0.0
    hooks:
      - id: check-yaml
        exclude: ^(notes-app/mkdocs\.yml|blog-app/mkdocs\.yml)$

The exclude key should be a single string, not a list.

In order to resolve the issue and still keep it on one line I had to create a conditional in...REGEX.