gerrit "missing Change-Id"

小鱼儿 2022-05-20 02:00 269阅读 0赞

本系列是为公司写的 git & gerrit 场景使用手册的一篇.

套路部分为解决问题的步骤,心法部分为所用到的知识点的梳理.

场景:

你用 git push 向 gerrit 提交了待审核代码,一切都很顺利,你脑袋里冒出了”代码头上加了’佛祖保佑’果然有效”的想法.

此时 git 打印出如下提示,你的内心OS同步打印 “心情 -5” :










remote: Resolving deltas: 100% (14
/14
)


remote: Processing changes: refs: 1,
done   


remote: ERROR: missing Change-Id
in
commit message footer


remote:


remote: Hint: To automatically insert Change-Id,
install
the hook:


remote:   gitdir=$(git rev-parse —git-
dir
);
scp
-p -P 29418 liux@gerrit.xxxxx.com:hooks
/commit-msg
${gitdir}
/hooks/


remote: And 
then 
amend the commit:


remote:   git commit —amend


remote:


To 
ssh
:
//liux
@121.xx.xx.xx:29418
/kaiba_admin_yunying
.git


 
! [remote rejected] HEAD -> refs
/for/master
(missing Change-Id
in
commit message footer)


error: failed to push some refs to
‘ssh://liux@121.xx.xx.xx:29418/sample_project.git’


#

套路:

#

大前提: commit-msg 文件必须已经在该项目中存在.

使用ls命令检查该文件是否存在:











cd
 project_dir



ls 
.git
/hooks/commit-msg

如果该文件不存在,则按照 git push 时产生的提示信息,获取该文件:










$ gitdir=$(git rev-parse —git-
dir
);
scp
-p -P 29418 liux@gerrit.xxxxx.com:hooks
/commit-msg
${gitdir}
/hooks/

上面的命令可以直接从 git push 产生的错误信息中复制出来.

如果要手敲该命令,别忘了把用户名换成自己的.

方法一: 使用 amend 选项生成 Change-Id:

如果缺失 Change-Id 的是最后一个 (head) commit, 使用以下命令即可解决问题:










$ git commit —amend

该命令会打开默认的 commit message 编辑器,一般是 vi.

这时什么都不用修改,直接保存退出即可 (:wq).

再次查看 git log,就会发现缺失的 Change-Id 已经被补上了. 再次 git push 即可.

方法二: 如果缺失 Change-Id 的不是最后一个 commit, 可用 reset 方法:

比如,如果缺失 Change-Id 的 commit 是 git log 中的第二个 commit, 则可以用 git reset 命令将本地分支回退到该 commit.

(但其实用 git reset 找回 Change-Id 是普通青年才干的事情,文艺青年有更优雅的办法.见方法三)

首先执行 git log, 找出缺失了 Change-Id 的 commit,并复制其 commit-id:










$ git log


commit 8e1cad33bcd98e175cba710b1eacfd631a5dda41


Author: liux <liux@xxxx.cn>


Date:   Mon Dec 19 17:43:00 2016 +0800


 


    
test
commit
“with amended commit message”


     


    
Change-Id: I9d2af0cc31423cf808cd235de0ad02abf451937d


 


commit 1a9096a34322885ac101175ddcac7dab4c52665d


Author: liux <liux@xxxx.cn>


Date:   Mon Dec 19 15:23:36 2016 +0800


 


    
test
commit-msg hook


 


……

发现是第二个 commit 缺失 Change-Id. 将代码 reset 到这个 commit, 并执行 amend:










$ git reset 1a9096a34322885ac101175ddcac7dab4c52665d


$ git commit —amend

注: 上面的 git reset 用法不会毁灭你写的代码,放心执行即可.

这时 git log 可以发现该 commit 已经补全了 change-Id.

下一步是把 git reset 撤消掉的代码重新 commit, 然后 push 即可:










$ git add ……


$ git commit -m 
“你的提交日志”


$ git push review HEAD:refs
/for/master

#

方法三: 使用交互式 rebase 找回任意提交位置的 Change-Id:

前面方法二中给出的例子是第二个提交缺失 Change-Id,这时用 git reset 还可以解决问题.

但如果你在一个方案上已经工作了一个月,生成了100个本地 commit,提交时才发现 git log 中第99个 commit 缺失 Change-Id. 如果这时还用 git reset 来找回 Change-Id ……

不要香菇,不要蓝瘦.文艺青年表示有办法优雅的解决问题: 交互式 rebase.

第一步,找到缺失 Change-Id 的那个 commit:










$ git log


commit 8aaaa749db4a5b105aa746659c5cd266ac82fffe


Author: liux <liux@xxxx.cn>


Date:   Mon Dec 19 17:43:24 2016 +0800


 


    
I am commit message 3


     


    
Change-Id: Ic89d5ce6ce4de70d1dcb315ce543c86a2b3ac003


 


commit 8e1cad33bcd98e175cba710b1eacfd631a5dda41


Author: liux <liux@xxxx.cn>


Date:   Mon Dec 19 17:43:00 2016 +0800


 


    
I am commit message 2


     


    
Change-Id: I9d2af0cc31423cf808cd235de0ad02abf451937d


 


commit 1a9096a34322885ac101175ddcac7dab4c52665d


Author: liux <liux@xxxx.cn>


Date:   Mon Dec 19 15:23:36 2016 +0800


 


    
I am commit message 1


 


commit d714bcde0c14ba4622d28952c4b2a80882b19927


Author: shangsb <shangsb@czfw.cn>


Date:   Wed Dec 14 09:20:52 2016 +0800


 


    
这是一个提交



     


    
Change-Id: I629b2bedff95491875f63634ad3da199612735b6


……

发现是 “I am commit message 1” 这个提交没有 Change-Id.

第二步,编辑交互式 rebase 的命令文件:

执行 git rebase -i, 参数为 该提交的上一个提交的 commit-id (本例中为 “表单” 那个提交):










$ git rebase -i d714bcde0c14ba4622d28952c4b2a80882b19927

这个命令会打开默认的编辑器,一般为 vi. 内容如下:










pick 1a9096a I am commit message 1


pick 8e1cad3 I am commit message 2


pick 8aaaa74 I am commit message 3


# Rebase d714bcd..8aaaa74 onto d714bcd


#


# Commands:


#  p, pick = use commit


#  r, reword = use commit, but edit the commit message


#  e, edit = use commit, but stop for amending


#  s, squash = use commit, but meld into previous commit


#  f, fixup = like “squash”, but discard this commit’s log message


#  x, exec = run command (the rest of the line) using shell


#


# These lines can be re-ordered; they are executed from top to bottom.


#


# If you remove a line here THAT COMMIT WILL BE LOST.


#


# However, if you remove everything, the rebase will be aborted.


#


# Note that empty commits are commented out

可以将这个文件理解为 git rebase 的内嵌脚本.其命令写法已经在下面的注释里给出了.

这里不赘述,仅给出最终要将该文件编辑成什么样子:










reword 1a9096a I am commit message 1


pick 8e1cad3 I am commit message 2


pick 8aaaa74 I am commit message 3


# Rebase d714bcd..8aaaa74 onto d714bcd


#


# Commands:


#  p, pick = use commit


#  r, reword = use commit, but edit the commit message


#  e, edit = use commit, but stop for amending


#  s, squash = use commit, but meld into previous commit


#  f, fixup = like “squash”, but discard this commit’s log message


#  x, exec = run command (the rest of the line) using shell


#


# These lines can be re-ordered; they are executed from top to bottom.


#


# If you remove a line here THAT COMMIT WILL BE LOST.


#


# However, if you remove everything, the rebase will be aborted.


#


# Note that empty commits are commented out

即: 将缺失了 Change-Id 的 commit 前面的 “pick” 改为 “reword” 即可. 保存退出 (:wq)

注1: 上述文件中 commit 的顺序是和 git log 显示的顺序相反的: git log 为最新的在最前; 上述文件为 最新的在最后.

注2: 如果进入该模式后,却不确定该怎么改,这时不要担心,直接退出编辑则什么都不会发生 (:q!)

注3: 如果没有搞清楚运作机制,就要注意,除了按需把 pick 改为 reword 外,不要做其他改动.尤其注意不要删除任何行 (被删除的那行对应的提交将丢失).

注4: 你应该已经发现,有多个 commit 缺失 Change-Id 的情况也可以用该方法一次性处理.

第三步,逐个编辑 commit-msg:

上一步打开的文件保存退出后,git会逐个打开被你标注了 reword 的提交日志页面.

不需要修改任何东西,逐个保存退出即可 (一路 :wq).

第四步,再次提交:

用 git log 查看提交日志,会发现缺失的 Change-Id 都生成了. 愉快的提交代码吧!










$ git push review HEAD:refs
/for/master

from: https://blog.csdn.net/liuxu0703/article/details/54343096

发表评论

表情:
评论列表 (有 0 条评论,269人围观)

还没有评论,来说两句吧...

相关阅读