解决提交 gerrit missing Change-Id

保留现场

1
2
3
4
5
6
7
8
remote: Resolving deltas: 100% (114/114)
remote: Processing changes: refs: 1,done
remote: ERROR: missing Change-Idincommit message footer
remote:
remote: Hint: To automatically insert Change-Id,installthe hook:
remote: gitdir=$(git rev-parse --git-dir);scp-p -P XX XX@gerrit.xxxxx.com:hooks/commit-msg${gitdir}/hooks/
remote: And then amend the commit:
remote: git commit --amend

探究原因

理解 change-id

代码审核是要对一个完整的变更进行审核,比如一次 Bug 修复,有多次提交 Commit,每次的 Commit Id 都不同,那么如何将多个不同的 Commit ID 关联到同一个 Chanege-Id 呢?我们需要将 Change-Id 添加到 Commit 的 footer(最后一行)中,这样就可以将多个 Commit 关联到同一个 Change-Id 了。


Change-Id 为避免与提交 Id 冲突,通常以大写字母 I 为前缀。此外,我们需要明确,Change-Id 是 Gerrit 的概念,不是 Git 的概念。你只有用 Gerrit 才会有 Change-Id,而 Git 只有提交 Id。

那么这个 Change-Id 是怎么生成的呢?

理解 git hooks

我在Git hooks 钩子的使用中有详细解释。在这里简单的介绍一下,钩子 (hooks) 是一些在.git/hooks目录的脚本,在被特定的事件触发后被调用。比如执行git commitgit pushgit pull等命令时,脚本会被调用。

Gerrit 也提供了一个标准的commit-msg钩子,当我们在执行git commit时,会被调用。会自动生成Change-Id,并将其添加到commit的 footer 中。

通常我们从远程下载代码后,会自动下载commit-msg钩子,并将其添加到.git/hooks目录中。正常来说hooks是不会加入代码仓库的,这应该取决于 Gerrit 的配置。

这次错误应该是我在测试钩子的时候,将 Gerrit 标准钩子删除了,导致无法正确生成 Change-Id。

解决方法

报错时其实已经提供了解决方式:

1
2
3
4
5
6
7
8
# 提示让我们安装远程的钩子
remote: Hint: To automatically insert Change-Id,installthe hook:

# 在命令行输入以下两条命令:
# 这条命令将找到该项目的 git 目录,并将其赋值给 gitdir 这个变量
gitdir=$(git rev-parse --git-dir)
# 执行 scp 命令,从 gerrit 代码服务器将钩子脚本文件 commit-msg 下载到项目的钩子目录下 (一般是 .git/hooks/)
scp-p -P XX XX@gerrit.xxxxx.com:hooks/commit-msg${gitdir}/hooks/

安装完之后重新git commit --amend,就可以正常生成 Change-Id 了。

使用 Syncthing 多端丝滑同步与备份 Gerrit 批量添加抄送提醒
You need to set install_url to use ShareThis. Please set it in _config.yml.

评论

Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×