Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to exclude multiple directories in pre-commit

I have multiple file patterns I want to ignore with pre-commit. For example 'migrations/.' and 'tests/.' The exclude parameter available in the pre-commit configuration file only accepts a string and not a list though. My current configuration file:

.pre-commit-config.yaml

repos:
-   repo: https://github.com/psf/black
    rev: 23.1.0
    hooks:
    - id: black
      language_version: python3.8
-   repo: https://github.com/PyCQA/flake8
    rev: 6.0.0
    hooks:
    - id: flake8
exclude: 'migrations/.*'

Tried changing exclude to a list and also putting in 2 exclude categories. Both were invalid configuations

exclude: 
- 'migrations/.*'
- 'tests/.*'
exclude: 'migrations/.*'
exclude: 'tests/.*'
like image 949
nholaday Avatar asked Sep 09 '25 18:09

nholaday


1 Answers

I found an answer to my question:

To match multiple patterns use the | regex operator:

exclude: 'migrations/.*|tests/.*'

Also would work:

exclude: '(migrations|tests)/.*'

If there are a lot of patterns to match there is a multi-line regex format:

exclude: |
    (?x)^(
        migrations/.*|
        tests/.*
    )$
like image 173
nholaday Avatar answered Sep 13 '25 19:09

nholaday