1
安装commit-msg钩子来自动生成
admin edited this page 2025-12-19 15:44:53 +08:00
This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

这是Gerrit的另一个常见错误缺少Change-Id。Gerrit要求每个提交都有一个唯一的Change-Id。需要安装commit-msg钩子来自动生成。

解决方案:

1. 安装Gerrit的commit-msg钩子推荐方法

# 进入你的Git仓库目录
cd /e/rjb/zhini_im_android

# 下载commit-msg钩子使用SSH方式
gitdir=$(git rev-parse --git-dir)
scp -p -P 29418 rw0067680@101.43.95.130:hooks/commit-msg "${gitdir}/hooks/"

# 如果上面的命令失败OpenSSH >= 9.0),尝试:
scp -p -O -P 29418 rw0067680@101.43.95.130:hooks/commit-msg "${gitdir}/hooks/"

# 或者使用HTTP方式如果SSH不行
curl -o ".git/hooks/commit-msg" http://101.43.95.130:8080/tools/hooks/commit-msg
chmod +x ".git/hooks/commit-msg"

2. 为当前提交添加Change-Id

# 使用commit-msg钩子重新生成提交信息
git commit --amend --no-edit

# 查看提交信息确认Change-Id已添加
git log --oneline -1

3. 重新推送

git push origin HEAD:refs/for/hotfix-crash

完整的解决流程:

步骤1安装钩子

# 方法1使用scp如果OpenSSH版本<9.0
scp -p -P 29418 rw0067680@101.43.95.130:hooks/commit-msg .git/hooks/

# 方法2使用scpOpenSSH>=9.0
scp -p -O -P 29418 rw0067680@101.43.95.130:hooks/commit-msg .git/hooks/

# 方法3使用curl最简单
curl -o ".git/hooks/commit-msg" http://101.43.95.130:8080/tools/hooks/commit-msg
chmod +x ".git/hooks/commit-msg"

步骤2验证钩子安装成功

# 检查钩子文件是否存在
ls -la .git/hooks/commit-msg

# 检查文件权限
file .git/hooks/commit-msg

步骤3修正提交

# 重新提交会自动添加Change-Id
git commit --amend --no-edit

# 查看提交信息应该能看到Change-Id
git log -1 --pretty=full

步骤4重新推送

git push origin HEAD:refs/for/hotfix-crash

如果遇到权限问题:

# 确保钩子有执行权限
chmod +x .git/hooks/commit-msg

# 如果scp需要密码确保你的SSH密钥已配置

一次性安装脚本:

#!/bin/bash
echo "安装Gerrit commit-msg钩子..."

# 进入Git仓库
cd /e/rjb/zhini_im_android

# 尝试HTTP方式
echo "正在下载commit-msg钩子..."
curl -o ".git/hooks/commit-msg" http://101.43.95.130:8080/tools/hooks/commit-msg 2>/dev/null

if [ $? -eq 0 ]; then
    echo "✓ 下载成功"
else
    echo "尝试SSH方式..."
    scp -p -O -P 29418 rw0067680@101.43.95.130:hooks/commit-msg ".git/hooks/commit-msg"
fi

# 设置权限
chmod +x ".git/hooks/commit-msg"
echo "✓ 权限设置完成"

# 修正提交
echo "修正提交..."
git commit --amend --no-edit

# 查看结果
echo "提交信息:"
git log -1 --pretty=format:"%H%n%an <%ae>%n%ad%n%s%n%b" | head -10

echo "现在可以重新推送了:"
echo "git push origin HEAD:refs/for/hotfix-crash"

验证Change-Id

# 查看最近的提交应该能看到Change-Id
git log -1

# 或者查看完整的提交信息
git show --stat

# 应该能看到类似这样的内容:
# Change-Id: I1234567890abcdef1234567890abcdef12345678

重要提示:

  1. 每个提交都需要Change-IdGerrit用它来跟踪代码审查
  2. commit-msg钩子只需要安装一次之后的所有提交都会自动生成Change-Id
  3. 如果修改历史提交需要为每个修改的提交重新生成Change-Id

现在按照这些步骤操作应该就能成功推送到Gerrit了