Gerrit的commit-msg hook使用指南

Git hooks是Git提供的一种机制,这些hooks能够直接被git commit命令调用,无需开发人员手工设置。

Gerrit提供的commit-msg hook,被调用时,可以自动根据模板编辑commit message,开发人员不必再手工输入commit message。

1.commit-msg hook实现机制

在Gerrit中,该hook实际上就是一个简短的Shell脚本实现。
commit-msg hook可以接收一个文件名作为参数,该文件中包含写好的commit message,具体逻辑如下:

  • 如果已有Change-Id行则什么都不做,返回状态0。
  • 如果没有Change-Id行,则
    • 如果有重复的"Signed-of-by"行,如果有则异常退出,返回状态非0。
    • 否则在Signed-off-by行之前追加插入Change-Id行;
    • 或Acked-by行之前追加插入Change-Id行;
    • 或在commit message末尾插入一个空行,然后在空行之后追加插入Change-Id行。

当然,也可以设置Gerrit配置文件gerrit.createChangeId=false禁用该hook。

2.commit-msg hook的两种安装方式

1) 手工为每个Git库安装
curl -Lo ~/.git/hooks/commit-msg http://gerrit_server/tools/hooks/commit-msg
或scp -p -P 29418 myname@gerrit_server:hooks/commit-msg ~/.git/hooks/

然后,chmod u+x .git/hooks/commit-msg

2) 手工复制一次,自动为每个Git库安装

commit-msg hook统一复制到$GIT_TEMPLATE_DIR路径下。

这样,后续每次git init创建Git库时,被自动复制到新Git库的.git/hooks/目录下。

3.commit-msg hook使用方式

第一次执行git commit命令时,只要输入具体的commit message,或者根本不必输入任何commit message,而直接采用默认的模板文件内容。git会自动调用commit-msg hook,生成并插入一个Change-Id行。

后续执行git commit --amend时,无需再手工输入任何commit message,甚至可以直接执行如下命令git commit --amend --no-edit。


猜你喜欢

转载自blog.csdn.net/taiyangdao/article/details/71079021
今日推荐