git学习--commit-msg缺失导致的missing Change-Id问题分析
2016-03-23 13:45
423 查看
一.简介
commit-msg hook
这个hook由’git 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提交不成功出现的错误信息:
三.解决方法
方法一:临时解决
方法二:永久解决
存放一个全局的钩子脚本配置,commit-msg可以从服务器上拷贝,也可以用已有仓库的commit-msg脚本。本方法采用脚本一次性解决。脚本命名为commit_msg_hook.sh,只要执行一次脚本然后重启shell生效。当用户运行git commit提交代码的时候会自动调用commit-msg脚本在提交日志中自动放入Change-Id信息,不在需要手工写入。脚本内容如下:
四.commit_msg脚本解析
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提交不成功出现的错误信息:
[分离头指针 fbea87a] 测试 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 ttt HostName@TianTian-Dev-81222:~/Work/test/test0$ git push origin HEAD:refs/for/master Counting objects: 3, done. Delta compression using up to 4 threads. Compressing objects: 100% (2/2), done. Writing objects: 100% (2/2), 223 bytes, done. Total 2 (delta 0), reused 0 (delta 0) remote: Processing changes: refs: 1, done remote: ERROR: missing Change-Id in commit message footer remote: Suggestion for commit message: remote: ttt remote: remote: Change-Id: Ifbea87a6358d55a189528a5f501492c37bdfec21 remote: remote: Hint: To automatically insert Change-Id, install the hook: remote: gitdir=$(git rev-parse --git-dir); scp -p -P 29418 HostName@192.168.77.11:hooks/commit-msg ${gitdir}/hooks/ remote: remote: To ssh://HostName@192.168.77.11:29418/test/test0 ! [remote rejected] HEAD -> refs/for/master (missing Change-Id in commit message footer) error: 无法推送一些引用到 'ssh://HostName@192.168.77.11:29418/test/test0'
三.解决方法
方法一:临时解决
1> 复制Change-Id: Ifbea87a6358d55a189528a5f501492c37bdfec21 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> 执行git commit --amend重新修复提交,把第一步中的Change-Id: Ifbea87a6358d55a189528a5f501492c37bdfec21黏贴到提交日志的下一行。 4> 重新执行git push推送本地改动到远程服务器
方法二:永久解决
存放一个全局的钩子脚本配置,commit-msg可以从服务器上拷贝,也可以用已有仓库的commit-msg脚本。本方法采用脚本一次性解决。脚本命名为commit_msg_hook.sh,只要执行一次脚本然后重启shell生效。当用户运行git commit提交代码的时候会自动调用commit-msg脚本在提交日志中自动放入Change-Id信息,不在需要手工写入。脚本内容如下:
SERVER_PORT="29418" ###gerrit服务器ip地址,根据具体情况更改 SERVER_IP="192.168.77.11" mkdir -p ~/.git_template/hooks scp -p -P "$SERVER_PORT" "$SERVER_IP":hooks/commit-msg ~/.git_template/hooks chmod u+x ~/.git_template/hooks/commit-msg git config --global init.templatedir ~/.git_template 这里写代码片二.
四.commit_msg脚本解析
######################################## BEGIN: gerrit version v2.10版本的commit_msg钩子脚本如下: ######################################## #!/bin/sh # From Gerrit Code Review 2.10 # # Part of Gerrit Code Review (http://code.google.com/p/gerrit/) # # Copyright (C) 2009 The Android Open Source Project # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # unset GREP_OPTIONS CHANGE_ID_AFTER="Bug|Issue" MSG="$1" # Check for, and add if missing, a unique Change-Id # add_ChangeId() { clean_message=`sed -e ' /^diff --git .*/{ s/// q } /^Signed-off-by:/d /^#/d ' "$MSG" | git stripspace` if test -z "$clean_message" then return fi if test "false" = "`git config --bool --get gerrit.createChangeId`" then return fi # Does Change-Id: already exist? if so, exit (no change). ###grep参数-i, --ignore-case 忽略大小写方式查找以“Change-Id:”开头字符串### if grep -i '^Change-Id:' "$MSG" >/dev/null then return fi id=`_gen_ChangeId` ###$$:Shell本身的PID(ProcessID)### T="$MSG.tmp.$$" AWK=awk if [ -x /usr/xpg4/bin/awk ]; then # Solaris AWK is just too broken AWK=/usr/xpg4/bin/awk fi # How this works: # - parse the commit message as (textLine+ blankLine*)* # - assume textLine+ to be a footer until proven otherwise # - exception: the first block is not footer (as it is the title) # - read textLine+ into a variable # - then count blankLines # - once the next textLine appears, print textLine+ blankLine* as these # aren't footer # - in END, the last textLine+ block is available for footer parsing $AWK ' BEGIN { # while we start with the assumption that textLine+ # is a footer, the first block is not. isFooter = 0 footerComment = 0 blankLines = 0 } # Skip lines starting with "#" without any spaces before it. ###awk的next用法在处理逐行读取文本方面很强大,next逐行读取文本### /^#/ { next } # Skip the line starting with the diff command and everything after it, # up to the end of the file, assuming it is only patch data. # If more than one line before the diff was empty, strip all but one. /^diff --git / { blankLines = 0 while (getline) { } next } # Count blank lines outside footer comments /^$/ && (footerComment == 0) { blankLines++ next } # Catch footer comment /^\[[a-zA-Z0-9-]+:/ && (isFooter == 1) { footerComment = 1 } /]$/ && (footerComment == 1) { footerComment = 2 } # We have a non-blank line after blank lines. Handle this. (blankLines > 0) { print lines for (i = 0; i < blankLines; i++) { print "" } lines = "" blankLines = 0 isFooter = 1 footerComment = 0 } # Detect that the current block is not the footer (footerComment == 0) && (!/^\[?[a-zA-Z0-9-]+:/ || /^[a-zA-Z0-9-]+:\/\//) { isFooter = 0 } { # We need this information about the current last comment line if (footerComment == 2) { footerComment = 0 } if (lines != "") { lines = lines "\n"; } lines = lines $0 } # Footer handling: # If the last block is considered a footer, splice in the Change-Id at the # right place. # Look for the right place to inject Change-Id by considering # CHANGE_ID_AFTER. Keys listed in it (case insensitive) come first, # then Change-Id, then everything else (eg. Signed-off-by:). # # Otherwise just print the last block, a new line and the Change-Id as a # block of its own. END { unprinted = 1 if (isFooter == 0) { print lines "\n" lines = "" } changeIdAfter = "^(" tolower("'"$CHANGE_ID_AFTER"'") "):" numlines = split(lines, footer, "\n") for (line = 1; line <= numlines; line++) { if (unprinted && match(tolower(footer[line]), changeIdAfter) != 1) { unprinted = 0 print "Change-Id: I'"$id"'" } print footer[line] } if (unprinted) { print "Change-Id: I'"$id"'" } }' "$MSG" > "$T" && mv "$T" "$MSG" || rm -f "$T" } _gen_ChangeIdInput() { echo "tree `git write-tree`" if parent=`git rev-parse "HEAD^0" 2>/dev/null` then echo "parent $parent" fi echo "author `git var GIT_AUTHOR_IDENT`" echo "committer `git var GIT_COMMITTER_IDENT`" echo printf '%s' "$clean_message" } _gen_ChangeId() { _gen_ChangeIdInput | git hash-object -t commit --stdin } add_ChangeId ######################################## END ########################################
相关文章推荐
- git review自动添加change-id
- 解决git提交到gerrit ERROR: missing Change-Id in commit message footer 问题
- 部署全局git commit-msg hook
- java多线程问题
- 杭电2524矩形A + B
- 微信支付接口遇到的问题解决
- RabbitMQ监控(3)——核心监控类实现
- 使用grunt实现自动化单元测试
- 各种正则表达式
- exe4j 给jar打包exe文件
- 基本类型byte&char
- LayoutInflater作用及使用
- 第4周项目4—程序设计(3)
- 正则表达式30分钟入门教程
- 对于未来是全栈工程师的世界这一说法的认识
- Embedded binary is not signed with the same certificate as the parent app
- [nginx] 网上最全面nginx教程(近100篇文章整理)
- 关于.NET技术前途问题的讨论
- 随机抽取题库的题
- IBM Security AppScan Glass Box:一种全新的漏洞扫描思想