Files
gerrit/添加Change-Id指南.md
2025-12-22 17:12:39 +08:00

137 lines
3.3 KiB
Markdown
Raw Permalink Blame History

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.
# 为所有提交添加 Change-Id
## 问题
推送时出现错误:
```
ERROR: commit 16b3dc9: missing Change-Id in message footer
```
这是因为 Gerrit 要求每个提交都必须包含 Change-Id。
## 解决方案
### 步骤 1安装 commit-msg hook
在您的代码目录中执行:
```bash
cd /d/zhini/zhini_im
# 下载并安装 commit-msg hook
gitdir=$(git rev-parse --git-dir)
scp -p -P 29418 renjianbo@101.43.95.130:hooks/commit-msg ${gitdir}/hooks/
# 或者使用 curl如果 scp 不可用)
mkdir -p .git/hooks
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
由于您有很多提交,需要批量处理:
#### 方法一:使用 git filter-branch推荐
```bash
cd /d/zhini/zhini_im
# 为所有提交添加 Change-Id
git filter-branch -f --msg-filter '
# 如果提交信息中没有 Change-Id添加一个
if ! grep -q "^Change-Id:" "$1"; then
# 生成 Change-Id基于提交的父提交和树对象
CHANGE_ID=$(git hash-object -t commit "$1" | cut -c1-40)
echo ""
echo "Change-Id: I$CHANGE_ID" >> "$1"
fi
cat "$1"
' --tag-name-filter cat -- --branches --tags
```
#### 方法二:使用 git rebase如果提交较少
```bash
cd /d/zhini/zhini_im
# 交互式 rebase为每个提交添加 Change-Id
git rebase -i --root
# 在编辑器中,将所有 'pick' 改为 'reword'
# 保存后,对每个提交:
# 1. 编辑提交信息
# 2. 在提交信息末尾添加空行和 Change-Id
# 3. 保存并继续下一个提交
```
#### 方法三:使用 git filter-repo更现代的方法需要先安装
如果安装了 git-filter-repo
```bash
# 需要先安装 git-filter-repo
# pip install git-filter-repo
git filter-repo --message-callback '
if b"Change-Id:" not in message:
# 生成 Change-Id
import hashlib
change_id = hashlib.sha1(message + b"change-id").hexdigest()[:40]
message = message.rstrip() + b"\n\nChange-Id: I" + change_id.encode() + b"\n"
return message
'
```
### 步骤 3验证 Change-Id 已添加
```bash
# 检查最近的几个提交是否都有 Change-Id
git log --format='%H%n%B' -5 | grep -A 5 "Change-Id:"
# 或者检查所有提交
git log --format='%H' | while read commit; do
if ! git log -1 --format='%B' $commit | grep -q "^Change-Id:"; then
echo "Missing Change-Id: $commit"
fi
done
```
### 步骤 4重新推送
```bash
git push gerrit HEAD:refs/for/master
```
## 快速修复脚本
创建一个简单的脚本来批量添加 Change-Id
```bash
cd /d/zhini/zhini_im
# 安装 hook
mkdir -p .git/hooks
curl -o .git/hooks/commit-msg http://101.43.95.130:8080/tools/hooks/commit-msg
chmod +x .git/hooks/commit-msg
# 为所有提交添加 Change-Id
git filter-branch -f --msg-filter '
if ! grep -q "^Change-Id:" "$1"; then
# 使用提交的 SHA1 前40位作为 Change-Id 的基础
COMMIT_SHA=$(git hash-object -t commit "$1")
CHANGE_ID="I${COMMIT_SHA:0:40}"
echo ""
echo "Change-Id: $CHANGE_ID" >> "$1"
fi
cat "$1"
' --tag-name-filter cat -- --branches --tags
```
## 注意事项
1. **备份**:在执行 filter-branch 前,建议创建备份分支
2. **时间**:如果有大量提交,这个过程可能需要一些时间
3. **Change-Id 格式**Change-Id 应该以 `I` 开头后跟40位十六进制字符