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

Gerrit环境下commit-received钩子无法显示已提交文件列表求助

解决Gerrit commit-received钩子无法列出提交文件的问题

问题根源

Gerrit的commit-received钩子和Git原生pre-receive钩子的输入逻辑完全不同:

  • Git原生pre-receive会传递oldrev newrev refname三组参数
  • Gerrit专属的commit-received钩子仅传递当前提交的SHA-1哈希值作为输入

你原脚本里的while read oldrev newrev refname逻辑完全不匹配Gerrit的输入格式,导致后续git diff命令因参数错误无法生成文件列表。

修复后的钩子脚本

#!/usr/bin/env bash
echo "pre receive"

# 读取Gerrit传递的单个提交ID
read commit_id

# 获取该提交的所有文件列表(仅输出文件名,剔除提交信息头)
committed_files=$(git show --name-only --pretty=format: $commit_id)

# 打印文件列表
echo "Committed files:"
echo "$committed_files"

额外注意事项

  • 确保钩子文件权限正确:执行chmod +x .git/hooks/commit-received,Gerrit要求钩子具备可执行权限才能正常触发
  • 如果需要对比当前提交与父提交的差异文件,可替换文件列表获取命令为:git diff-tree --name-only $commit_id^ $commit_id(该命令不适用于仓库初始提交)

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.23 14:39:51