当前位置:   article > 正文

git学习--commit-msg缺失导致的missing Change-Id问题分析_commit-msg hook

commit-msg hook

一.简介 
commit-msg hook 
这个hook由’git commit’触发, 且可以通过--no-verify 来略过.它接受一个参数, 包含commit msg的文件的路径.如果以非0状态退出, ‘git commit’ 将会被取消.这个hook可以用于修改message(信息)文件, 用来匹配项目的规范格式(如果有的话).也可以用于校验commit msg,并在必要时拒绝提交.缺省的’commit-msg’ hook, 当启用时,将检查重复的”Signed-off-by”行, 如果找到,则取消commit.

二.问题情景 
当执行git add “file”添加到暂存器,然后执行git commit提交到本地库的时候,git需要在commit的时候在日志中写入一个唯一标识提交的SHA-1值,即是Change-Id值. git commit时会调用commit_msg脚本检查提交信息,以便在git push的时候能正常推送到远程库。此时调用默认目录下的commit_msg钩子脚本,默认目录为“.git/hooks/commit_msg”. 如果此目录下无commit_msg脚本则commit时提交日志中无ChangeId信息,则在git push的时候出错,无法正常吧改动上传到远程服务器。 
如下为git push提交不成功出现的错误信息:

  1. [分离头指针 fbea87a] 测试
  2. 1 file changed, 0 insertions(+), 0 deletions(-)
  3. create mode 100644 ttt
  4. HostName@TianTian-Dev-81222:~/Work/test/test0$ git push origin HEAD:refs/for/master
  5. Counting objects: 3, done.
  6. Delta compression using up to 4 threads.
  7. Compressing objects: 100% (2/2), done.
  8. Writing objects: 100% (2/2), 223 bytes, done.
  9. Total 2 (delta 0), reused 0 (delta 0)
  10. remote: Processing changes: refs: 1, done
  11. remote: ERROR: missing Change-Id in commit message footer
  12. remote: Suggestion for commit message:
  13. remote: ttt
  14. remote:
  15. remote: Change-Id: Ifbea87a6358d55a189528a5f501492c37bdfec21
  16. remote:
  17. remote: Hint: To automatically insert Change-Id, install the hook:
  18. remote: gitdir=$(git rev-parse --git-dir); scp -p -P 29418 HostName@192.168.77.11:hooks/commit-msg ${gitdir}/hooks/
  19. remote:
  20. remote:
  21. To ssh://HostName@192.168.77.11:29418/test/test0
  22. ! [remote rejected] HEAD -> refs/for/master (missing Change-Id in commit message footer)
  23. error: 无法推送一些引用到 'ssh://HostName@192.168.77.11:29418/test/test0'
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23

三.解决方法 
方法一:临时解决

  1. 1> 复制Change-Id: Ifbea87a6358d55a189528a5f501492c37bdfec21
  2. 2> 执行 gitdir=$(git rev-parse --git-dir); scp -p -P 29418 HostName@192.168.77.11:hooks/commit-msg ${gitdir}/hooks/ 从服务器拷贝commit-msg脚本到.git/hooks/目录下
  3. 3> 执行git commit --amend重新修复提交,把第一步中的Change-Id: Ifbea87a6358d55a189528a5f501492c37bdfec21黏贴到提交日志的下一行。
  4. 4> 重新执行git push推送本地改动到远程服务器
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

方法二:永久解决 
存放一个全局的钩子脚本配置,commit-msg可以从服务器上拷贝,也可以用已有仓库的commit-msg脚本。本方法采用脚本一次性解决。脚本命名为commit_msg_hook.sh,只要执行一次脚本然后重启shell生效。当用户运行git commit提交代码的时候会自动调用commit-msg脚本在提交日志中自动放入Change-Id信息,不在需要手工写入。脚本内容如下:

  1. SERVER_PORT="29418"
  2. ###gerrit服务器ip地址,根据具体情况更改
  3. SERVER_IP="192.168.77.11"
  4. mkdir -p ~/.git_template/hooks
  5. scp -p -P "$SERVER_PORT" "$SERVER_IP":hooks/commit-msg ~/.git_template/hooks
  6. chmod u+x ~/.git_template/hooks/commit-msg
  7. git config --global init.templatedir ~/.git_template
  8. 这里写代码片二.
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

四.commit_msg脚本解析

  1. ########################################
  2. BEGIN: gerrit version v2.10版本的commit_msg钩子脚本如下:
  3. ########################################
  4. #!/bin/sh
  5. # From Gerrit Code Review 2.10
  6. #
  7. # Part of Gerrit Code Review (http://code.google.com/p/gerrit/)
  8. #
  9. # Copyright (C) 2009 The Android Open Source Project
  10. #
  11. # Licensed under the Apache License, Version 2.0 (the "License");
  12. # you may not use this file except in compliance with the License.
  13. # You may obtain a copy of the License at
  14. #
  15. # http://www.apache.org/licenses/LICENSE-2.0
  16. #
  17. # Unless required by applicable law or agreed to in writing, software
  18. # distributed under the License is distributed on an "AS IS" BASIS,
  19. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  20. # See the License for the specific language governing permissions and
  21. # limitations under the License.
  22. #
  23. unset GREP_OPTIONS
  24. CHANGE_ID_AFTER="Bug|Issue"
  25. MSG="$1"
  26. # Check for, and add if missing, a unique Change-Id
  27. #
  28. add_ChangeId() {
  29. clean_message=`sed -e '
  30. /^diff --git .*/{
  31. s///
  32. q
  33. }
  34. /^Signed-off-by:/d
  35. /^#/d
  36. ' "$MSG" | git stripspace`
  37. if test -z "$clean_message"
  38. then
  39. return
  40. fi
  41. if test "false" = "`git config --bool --get gerrit.createChangeId`"
  42. then
  43. return
  44. fi
  45. # Does Change-Id: already exist? if so, exit (no change).
  46. ###grep参数-i, --ignore-case 忽略大小写方式查找以“Change-Id:”开头字符串###
  47. if grep -i '^Change-Id:' "$MSG" >/dev/null
  48. then
  49. return
  50. fi
  51. id=`_gen_ChangeId`
  52. ###$$:Shell本身的PID(ProcessID)###
  53. T="$MSG.tmp.$$"
  54. AWK=awk
  55. if [ -x /usr/xpg4/bin/awk ]; then
  56. # Solaris AWK is just too broken
  57. AWK=/usr/xpg4/bin/awk
  58. fi
  59. # How this works:
  60. # - parse the commit message as (textLine+ blankLine*)*
  61. # - assume textLine+ to be a footer until proven otherwise
  62. # - exception: the first block is not footer (as it is the title)
  63. # - read textLine+ into a variable
  64. # - then count blankLines
  65. # - once the next textLine appears, print textLine+ blankLine* as these
  66. # aren't footer
  67. # - in END, the last textLine+ block is available for footer parsing
  68. $AWK '
  69. BEGIN {
  70. # while we start with the assumption that textLine+
  71. # is a footer, the first block is not.
  72. isFooter = 0
  73. footerComment = 0
  74. blankLines = 0
  75. }
  76. # Skip lines starting with "#" without any spaces before it.
  77. ###awk的next用法在处理逐行读取文本方面很强大,next逐行读取文本###
  78. /^#/ { next }
  79. # Skip the line starting with the diff command and everything after it,
  80. # up to the end of the file, assuming it is only patch data.
  81. # If more than one line before the diff was empty, strip all but one.
  82. /^diff --git / {
  83. blankLines = 0
  84. while (getline) { }
  85. next
  86. }
  87. # Count blank lines outside footer comments
  88. /^$/ && (footerComment == 0) {
  89. blankLines++
  90. next
  91. }
  92. # Catch footer comment
  93. /^\[[a-zA-Z0-9-]+:/ && (isFooter == 1) {
  94. footerComment = 1
  95. }
  96. /]$/ && (footerComment == 1) {
  97. footerComment = 2
  98. }
  99. # We have a non-blank line after blank lines. Handle this.
  100. (blankLines > 0) {
  101. print lines
  102. for (i = 0; i < blankLines; i++) {
  103. print ""
  104. }
  105. lines = ""
  106. blankLines = 0
  107. isFooter = 1
  108. footerComment = 0
  109. }
  110. # Detect that the current block is not the footer
  111. (footerComment == 0) && (!/^\[?[a-zA-Z0-9-]+:/ || /^[a-zA-Z0-9-]+:\/\//) {
  112. isFooter = 0
  113. }
  114. {
  115. # We need this information about the current last comment line
  116. if (footerComment == 2) {
  117. footerComment = 0
  118. }
  119. if (lines != "") {
  120. lines = lines "\n";
  121. }
  122. lines = lines $0
  123. }
  124. # Footer handling:
  125. # If the last block is considered a footer, splice in the Change-Id at the
  126. # right place.
  127. # Look for the right place to inject Change-Id by considering
  128. # CHANGE_ID_AFTER. Keys listed in it (case insensitive) come first,
  129. # then Change-Id, then everything else (eg. Signed-off-by:).
  130. #
  131. # Otherwise just print the last block, a new line and the Change-Id as a
  132. # block of its own.
  133. END {
  134. unprinted = 1
  135. if (isFooter == 0) {
  136. print lines "\n"
  137. lines = ""
  138. }
  139. changeIdAfter = "^(" tolower("'"$CHANGE_ID_AFTER"'") "):"
  140. numlines = split(lines, footer, "\n")
  141. for (line = 1; line <= numlines; line++) {
  142. if (unprinted && match(tolower(footer[line]), changeIdAfter) != 1) {
  143. unprinted = 0
  144. print "Change-Id: I'"$id"'"
  145. }
  146. print footer[line]
  147. }
  148. if (unprinted) {
  149. print "Change-Id: I'"$id"'"
  150. }
  151. }' "$MSG" > "$T" && mv "$T" "$MSG" || rm -f "$T"
  152. }
  153. _gen_ChangeIdInput() {
  154. echo "tree `git write-tree`"
  155. if parent=`git rev-parse "HEAD^0" 2>/dev/null`
  156. then
  157. echo "parent $parent"
  158. fi
  159. echo "author `git var GIT_AUTHOR_IDENT`"
  160. echo "committer `git var GIT_COMMITTER_IDENT`"
  161. echo
  162. printf '%s' "$clean_message"
  163. }
  164. _gen_ChangeId() {
  165. _gen_ChangeIdInput |
  166. git hash-object -t commit --stdin
  167. }
  168. add_ChangeId
  169. ########################################
  170. END
  171. ########################################
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小舞很执着/article/detail/968116
推荐阅读
相关标签
  

闽ICP备14008679号