pre-commit配置https仓库却使用git://协议访问GitHub超时报错
问题说明
- 目标:通过pre-commit在代码提交前自动执行
terraform validate完成Terraform语法校验 - 环境信息:pre-commit版本为2.19.0,全局Git配置未设置任何协议重写规则
- 异常现象:
.pre-commit-config.yaml中明确填写HTTPS协议的pre-commit-terraform仓库地址,但执行git commit、pre-commit autoupdate时,pre-commit会尝试访问git://开头的同路径仓库地址,触发连接超时报错,返回码128。
使用的.pre-commit-config.yaml配置内容如下(第一行存在冗余内容):
https://github.com/antonbabenko/pre-commit-terraform# See https://pre-commit.com for more information # See https://pre-commit.com/hooks.html for more hooks repos: - repo: https://github.com/pre-commit/pre-commit-hooks rev: v3.2.0 hooks: - id: trailing-whitespace - id: end-of-file-fixer - id: check-yaml - id: check-added-large-files - repo: https://github.com/antonbabenko/pre-commit-terraform rev: 1.74.1 hooks: - id: terraform_validate
执行提交时的报错信息:
[INFO] Initializing environment for git://github.com/antonbabenko/pre-commit-terraform. An unexpected error has occurred: CalledProcessError: command: ('/usr/lib/git-core/git', 'fetch', 'origin', '--tags') return code: 128 expected return code: 0 stdout: (none) stderr: fatal: unable to connect to github.com: github.com[0: 140.82.113.3]: errno=Connection timed out Check the log at /home/me/.cache/pre-commit/pre-commit.log
全局~/.gitconfig配置:
[user] email = <my_email_address> name = Matthew Thompson
执行git config --show-origin --list的输出:
file:/home/matthew/.gitconfig user.email=<my_email_address> file:/home/matthew/.gitconfig user.name=Matthew Thompson
原因分析
- 配置文件存在语法问题:配置第一行误将pre-commit-terraform仓库地址写在注释标记
#之前,属于无效YAML内容,后续修复网络问题后必须先删除这段冗余地址,否则会触发配置解析错误。 - 核心异常原因:pre-commit 2.19.0及更早版本存在默认逻辑,针对GitHub托管的仓库,即使配置中填写的是HTTPS地址,拉取标签时也会自动尝试使用
git://协议(默认走9418端口)通信。该协议无加密,大部分网络环境会封禁此端口,直接导致连接超时。由于本地未配置Git协议重写规则,请求不会自动转换为HTTPS协议,最终拉取失败。
修复方案
二选一即可:
- 方案一(推荐):将pre-commit升级到2.20.0及以上版本,该版本起移除了自动切换Git协议的逻辑,会严格使用配置文件中填写的HTTPS地址拉取仓库,不会主动发起
git://协议请求。 - 方案二(暂时无法升级pre-commit时使用):执行以下命令配置Git全局规则,强制将所有指向GitHub的
git://协议请求重写为HTTPS协议(走常规443端口,普通网络环境均可正常访问):
git config --global url."https://github.com/".insteadOf git://github.com/
配置完成后先执行pre-commit clean清空本地异常缓存,再重新执行提交操作即可。
内容的提问来源于stack exchange,提问作者Matt
相关产品推荐
相关产品推荐

