126 lines
2.8 KiB
Markdown
126 lines
2.8 KiB
Markdown
# 安装 Gerrit Commit Hook 指南
|
||
|
||
## 问题
|
||
|
||
错误信息:
|
||
```
|
||
ERROR: commit b4917f2: missing Change-Id in message footer
|
||
```
|
||
|
||
Gerrit 要求每个提交都必须包含 Change-Id。
|
||
|
||
## 解决方案:安装 commit-msg hook
|
||
|
||
### 步骤 1:安装 commit-msg hook
|
||
|
||
在您的本地电脑上执行:
|
||
|
||
```bash
|
||
cd /d/ttt/test-project
|
||
|
||
# 安装 commit-msg hook
|
||
gitdir=$(git rev-parse --git-dir); scp -p -P 29418 renjianbo@101.43.95.130:hooks/commit-msg ${gitdir}/hooks/
|
||
```
|
||
|
||
**注意**:如果 scp 命令失败,可能需要使用完整路径或配置 SSH。
|
||
|
||
### 步骤 2:修改最后一次提交(添加 Change-Id)
|
||
|
||
```bash
|
||
# 修改最后一次提交,自动添加 Change-Id
|
||
git commit --amend --no-edit
|
||
```
|
||
|
||
### 步骤 3:重新推送
|
||
|
||
```bash
|
||
# 如果创建了 SSH 配置文件
|
||
git push origin HEAD:refs/for/master
|
||
|
||
# 如果还没创建配置文件
|
||
GIT_SSH_COMMAND="ssh -o PubkeyAcceptedKeyTypes=+ssh-rsa -o PubkeyAcceptedAlgorithms=+ssh-rsa" git push origin HEAD:refs/for/master
|
||
```
|
||
|
||
## 如果 scp 命令失败
|
||
|
||
### 方法一:使用 curl 下载(推荐)
|
||
|
||
```bash
|
||
cd /d/ttt/test-project
|
||
|
||
# 创建 hooks 目录(如果不存在)
|
||
mkdir -p .git/hooks
|
||
|
||
# 下载 commit-msg hook
|
||
curl -o .git/hooks/commit-msg http://101.43.95.130:8080/tools/hooks/commit-msg
|
||
|
||
# 设置执行权限
|
||
chmod +x .git/hooks/commit-msg
|
||
```
|
||
|
||
### 方法二:手动创建 hook 文件
|
||
|
||
如果以上方法都不行,可以手动创建:
|
||
|
||
```bash
|
||
cd /d/ttt/test-project
|
||
|
||
# 创建 hooks 目录
|
||
mkdir -p .git/hooks
|
||
|
||
# 创建 commit-msg hook(简化版)
|
||
cat > .git/hooks/commit-msg << 'EOF'
|
||
#!/bin/sh
|
||
# Gerrit commit-msg hook
|
||
CHANGE_ID=$(git log -1 --format='%H' | cut -c1-8)
|
||
echo "" >> "$1"
|
||
echo "Change-Id: I${CHANGE_ID}0000000000000000000000000000000000000000" >> "$1"
|
||
EOF
|
||
|
||
# 设置执行权限
|
||
chmod +x .git/hooks/commit-msg
|
||
```
|
||
|
||
## 完整操作流程
|
||
|
||
```bash
|
||
cd /d/ttt/test-project
|
||
|
||
# 方法 1:使用 scp(如果 SSH 配置正确)
|
||
gitdir=$(git rev-parse --git-dir); scp -p -P 29418 renjianbo@101.43.95.130:hooks/commit-msg ${gitdir}/hooks/
|
||
|
||
# 方法 2:使用 curl(推荐,更简单)
|
||
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
|
||
|
||
# 修改最后一次提交
|
||
git commit --amend --no-edit
|
||
|
||
# 推送代码
|
||
git push origin HEAD:refs/for/master
|
||
```
|
||
|
||
## 验证 hook 是否安装成功
|
||
|
||
```bash
|
||
# 查看 hook 文件是否存在
|
||
ls -la .git/hooks/commit-msg
|
||
|
||
# 应该显示文件存在且有执行权限
|
||
```
|
||
|
||
## 以后的使用
|
||
|
||
安装 hook 后,以后的所有提交都会自动包含 Change-Id,不需要手动添加。
|
||
|
||
## 推送成功后的效果
|
||
|
||
推送成功后,Gerrit 会返回变更 URL,例如:
|
||
```
|
||
remote: http://101.43.95.130:8080/c/test-project/+/1 [NEW]
|
||
```
|
||
|
||
在浏览器中打开即可查看和评审代码变更。
|
||
|