为存储库设置 gitlint 预提交本地

问题描述

我正在尝试在存储库中添加 gitlint 预提交挂钩。 .pre-commit-config.yaml 文件如下所示:

# See https://pre-commit.com for more information
# See https://pre-commit.com/hooks.html for more hooks
repos:
  - repo: local
    hooks:
      - id: gitlint
        name: gitlint
        entry: gitlint
        language: system

但是,我不断收到:

- hook id: gitlint
- exit code: 253

Usage: gitlint [OPTIONS] COMMAND [ARGS]...
Try 'gitlint --help' for help.

Error: No such command '.pre-commit-config.yaml'.

这是我跑后:

pip install gitlint
pre-commit install --hook-type commit-msg

我做错了什么?

解决方法

由于以下几个原因,您的配置已损坏:

  • 您已经为所有 gitlint 配置了 stages,这意味着它将在您不想要的其他 git 钩子上运行(例如 pre-commitpre-push 等)。 )。要解决此问题,您需要设置 stages: [commit-msg]
  • 您还缺少一些其他设置,例如 gitlint 的正确参数等。
  • 此外,您正在使用 language: system,这意味着您依赖于您的贡献者来设置工具——这忽略了预提交的要点,并且是不受支持的逃生舱口。拥有托管工具的常用方法是重用现有存储库(见下文)或使用 additional_dependencies 以托管方式安装工具

支持使用 gitlint 的方式来自存储库本身

repos:
-   repo: https://github.com/jorisroovers/gitlint
    rev: ''  # pick a tag / sha to use
    hooks:
    -   id: gitlint

免责声明:我是 pre-commit 的创建者,并为 gitlint 贡献了 pre-commit 支持

,

错误消息不是很有帮助。我只是缺少 args 和 stage 字段:

# See https://pre-commit.com for more information
# See https://pre-commit.com/hooks.html for more hooks
---
repos:
   - repo: local
     hooks:
       - id: gitlint
         name: gitlint
         language: python
         entry: gitlint
         args: [--staged,--msg-filename]
         stages: [commit-msg]