预提交本地挂钩会产生错误:“无法识别的参数:.pre-commit-config.yaml”

问题描述

我在pre-commit文件.pre-commit-config.yaml中有以下存储库

-   repo: local
    hooks:
    -   id: check_pip
        name: Check pip file
        description: This hook checks if requirements-dev.txt is up to date.
        language: system
        entry: python -m scripts.check_pip_requirements
        args: ["--compare"]

但是它总是给我错误

错误:无法识别的参数:.pre-commit-config.yaml

当它将文件名作为参数传递给我的python脚本时。我该如何预防?

解决方法

稍微整理一下示例-并使用files仅在必要文件更改时运行:

-   repo: local
    hooks:
    -   id: check_pip
        name: Check pip file
        description: This hook checks if requirements-dev.txt is up to date.
        language: system
        entry: python -m scripts.check_pip_requirements --compare
        files: ^requirements-dev.txt$
        pass_filenames: false

请注意,我做了几件事:

  • args对于local钩子实际上没有意义,您可以将其放入entry
  • pass_filenames(与您一样)-pre-commit是一个基于将文件名传递给可执行文件的框架,但是您可以将其关闭
  • files:这样做可以使挂钩仅在requirements-dev.txt发生变化时被触发

或者(如果您希望requirements-dev.txt之外的更改需要运行此挂钩),则可以放下files并使用always_run: true


免责声明:我是预先提交的作者

,

我花了很多时间弄清楚是什么原因造成的,以及如何解决。没有很好的记录,最终我通过反复试验修复了它。我们必须在钩子中使用pass_filenames: false

-   repo: local
    hooks:
    -   id: check_pip
        name: Check pip file
        description: This hook checks if requirements-dev.txt is up to date.
        language: system
        entry: python -m scripts.check_pip_requirements
        pass_filenames: false
        args: ["--compare"]