如何在预提交中排除多个目录

nho*_*day 12 pre-commit pre-commit.com

我想通过预提交忽略多个文件模式。例如“迁移/”。'和'测试/。' 但预提交配置文件中可用的排除参数仅接受字符串而不接受列表。我当前的配置文件:

.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/.*'
Run Code Online (Sandbox Code Playgroud)

尝试将排除更改为列表,并放入 2 个排除类别。两者都是无效配置

exclude: 
- 'migrations/.*'
- 'tests/.*'
Run Code Online (Sandbox Code Playgroud)
exclude: 'migrations/.*'
exclude: 'tests/.*'
Run Code Online (Sandbox Code Playgroud)

nho*_*day 21

我找到了我的问题的答案:

要匹配多个模式,请使用|正则表达式运算符:

exclude: 'migrations/.*|tests/.*'
Run Code Online (Sandbox Code Playgroud)

也可以工作:

exclude: '(migrations|tests)/.*'
Run Code Online (Sandbox Code Playgroud)

如果有很多模式需要匹配,则可以使用多行正则表达式格式:

exclude: |
    (?x)^(
        migrations/.*|
        tests/.*
    )$
Run Code Online (Sandbox Code Playgroud)