You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

pre-commit本地钩子配置遇Missing required key: name错误,求解决

解决pre-commit local钩子配置错误

问题根源

你遇到的InvalidConfigError是因为三个核心问题:

  1. .pre-commit-config.yaml格式错误——你把name写成了独立的hook条目,它应该和id属于同一个hook的配置项
  2. local钩子必须指定entry(要执行的脚本路径)和language字段,你之前的配置缺少这些必填项
  3. 直接编辑.git/hooks/pre-commit是错误操作——pre-commit框架会自动管理这个文件,自定义逻辑应该放在独立脚本中通过配置调用

正确配置步骤

1. 创建独立的检查脚本

在monorepo根目录下新建scripts文件夹,根据你的终端环境选择对应的脚本版本:

若使用Git Bash/WSL(bash脚本)

路径:C:\Python\monorepo\scripts\check-reqs.sh
内容:

pip freeze > requirements_check.txt
cmp --silent requirements_check.txt requirements.txt

if [ $? -gt 0 ]; 
then
  echo "There are packages in the env not in requirements.txt"
  echo "Aborting commit"
  rm requirements_check.txt
  exit 1
fi

rm requirements_check.txt
exit 0

若使用Windows CMD/PowerShell(批处理脚本)

路径:C:\Python\monorepo\scripts\check-reqs.bat
内容:

pip freeze > requirements_check.txt
fc requirements_check.txt requirements.txt > nul
if errorlevel 1 (
    echo There are packages in the env not in requirements.txt
    echo Aborting commit
    del requirements_check.txt
    exit /b 1
)
del requirements_check.txt
exit /b 0

2. 修正.pre-commit-config.yaml

路径:C:\Python\monorepo\.pre-commit-config.yaml
内容:

repos:
-   repo: local
    hooks:
    -   id: check-requirements
        name: Verify environment matches requirements.txt
        entry: ./scripts/check-reqs.sh  # 用批处理则改为./scripts/check-reqs.bat
        language: system
        pass_filenames: false
  • id:自定义钩子的唯一标识
  • name:钩子的描述名称(运行时会显示)
  • entry:脚本相对于monorepo根目录的路径
  • language: system:告诉pre-commit直接使用系统环境执行脚本
  • pass_filenames: false:该钩子不需要处理提交的文件,仅执行检查逻辑

3. 重新初始化pre-commit

在PyCharm终端(monorepo根目录或子项目路径)执行:

pre-commit clean
pre-commit install

4. 测试钩子

运行以下命令验证配置是否生效:

pre-commit run --all-files

关键注意事项

  • 禁止直接修改.git/hooks/pre-commit,所有自定义逻辑都应通过.pre-commit-config.yaml配置调用独立脚本
  • Windows环境下,需确保终端能执行对应的脚本类型(bash脚本依赖Git Bash/WSL支持,批处理可直接在CMD/PowerShell运行)

内容的提问来源于stack exchange,提问作者alexei7

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.28 03:35:39