{"id":5851,"date":"2022-01-13T10:06:54","date_gmt":"2022-01-13T02:06:54","guid":{"rendered":"http:\/\/xinyiworld.top\/wordpress\/?p=5851"},"modified":"2022-01-26T15:06:28","modified_gmt":"2022-01-26T07:06:28","slug":"git-hook","status":"publish","type":"post","link":"http:\/\/xinyiworld.top\/wordpress_it\/?p=5851","title":{"rendered":"Git Hook"},"content":{"rendered":"<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_16 counter-hierarchy counter-decimal ez-toc-grey\">\n<div class=\"ez-toc-title-container\">\n<p class=\"ez-toc-title\">\u76ee\u5f55<\/p>\n<span class=\"ez-toc-title-toggle\"><a class=\"ez-toc-pull-right ez-toc-btn ez-toc-btn-xs ez-toc-btn-default ez-toc-toggle\" style=\"display: none;\"><i class=\"ez-toc-glyphicon ez-toc-icon-toggle\"><\/i><\/a><\/span><\/div>\n<nav><ul class=\"ez-toc-list ez-toc-list-level-1\"><li class=\"ez-toc-page-1 ez-toc-heading-level-4\"><a class=\"ez-toc-link ez-toc-heading-1\" href=\"http:\/\/xinyiworld.top\/wordpress_it\/?p=5851\/#Git_Hook%E6%98%AF%E4%BB%80%E4%B9%88\" title=\"Git Hook\u662f\u4ec0\u4e48\">Git Hook\u662f\u4ec0\u4e48<\/a><\/li><li class=\"ez-toc-page-1 ez-toc-heading-level-4\"><a class=\"ez-toc-link ez-toc-heading-2\" href=\"http:\/\/xinyiworld.top\/wordpress_it\/?p=5851\/#git_commit_msg%E6%A0%BC%E5%BC%8F%E8%A7%84%E8%8C%83%E6%A0%A1%E9%AA%8C\" title=\"git commit msg\u683c\u5f0f\u89c4\u8303\u6821\u9a8c\">git commit msg\u683c\u5f0f\u89c4\u8303\u6821\u9a8c<\/a><\/li><li class=\"ez-toc-page-1 ez-toc-heading-level-4\"><a class=\"ez-toc-link ez-toc-heading-3\" href=\"http:\/\/xinyiworld.top\/wordpress_it\/?p=5851\/#gerrit_commit-msg_id\" title=\"gerrit commit-msg id\">gerrit commit-msg id<\/a><\/li><li class=\"ez-toc-page-1 ez-toc-heading-level-4\"><a class=\"ez-toc-link ez-toc-heading-4\" href=\"http:\/\/xinyiworld.top\/wordpress_it\/?p=5851\/#%E5%B0%86commit_msg%E6%A0%BC%E5%BC%8F%E8%A7%84%E8%8C%83%E6%A0%A1%E9%AA%8C%E4%B8%8Egerrit_commit-msg_id%E5%90%88%E5%B9%B6%E5%88%B0%E4%B8%80%E8%B5%B7\" title=\"\u5c06commit msg\u683c\u5f0f\u89c4\u8303\u6821\u9a8c\u4e0egerrit commit-msg id\u5408\u5e76\u5230\u4e00\u8d77\">\u5c06commit msg\u683c\u5f0f\u89c4\u8303\u6821\u9a8c\u4e0egerrit commit-msg id\u5408\u5e76\u5230\u4e00\u8d77<\/a><\/li><\/ul><\/nav><\/div>\n<h4><span class=\"ez-toc-section\" id=\"Git_Hook%E6%98%AF%E4%BB%80%E4%B9%88\"><\/span>Git Hook\u662f\u4ec0\u4e48<span class=\"ez-toc-section-end\"><\/span><\/h4>\n<p><a class=\"wp-editor-md-post-content-link\" href=\"https:\/\/zhuanlan.zhihu.com\/p\/149294652\">https:\/\/zhuanlan.zhihu.com\/p\/149294652<\/a><\/p>\n<h4><span class=\"ez-toc-section\" id=\"git_commit_msg%E6%A0%BC%E5%BC%8F%E8%A7%84%E8%8C%83%E6%A0%A1%E9%AA%8C\"><\/span>git commit msg\u683c\u5f0f\u89c4\u8303\u6821\u9a8c<span class=\"ez-toc-section-end\"><\/span><\/h4>\n<p><a class=\"wp-editor-md-post-content-link\" href=\"https:\/\/blog.csdn.net\/junmoxi\/article\/details\/89244160\">https:\/\/blog.csdn.net\/junmoxi\/article\/details\/89244160<\/a><\/p>\n<p><a class=\"wp-editor-md-post-content-link\" href=\"https:\/\/www.jianshu.com\/p\/f3d17c11bb8a\">https:\/\/www.jianshu.com\/p\/f3d17c11bb8a<\/a>(\u5b9a\u4e49gradle\u811a\u672c\u5c06commit msg\u89c4\u8303\u6587\u4ef6\u62f7\u8d1d\u5230\u6307\u5b9a\u76ee\u5f55)<\/p>\n<p>commit-msg shell\u811a\u672c\u793a\u4f8b\uff1a<\/p>\n<pre><code class=\"language-shell line-numbers\">#!\/bin\/bash\n\n#############################################\n# Description\uff1a check commit message\n# Author:       XinYi\n# Version:      1.0\n# CreateTime:   2021\/10\/22\n# Administe User: jenkins\n#############################################\necho \"begin commit msg check...\"\n\n# CMessage=$GERRIT_CHANGE_SUBJECT\n\n# \u6b64\u79cd\u65b9\u5f0f\u4e0d\u517c\u5bb9git commit --amend\uff0c\u4f1a\u62a5\u9519\u3002\n# CMessage=`awk '{printf(\"%s\",$0)}' $1`\nCMessage=`awk '{printf(\"%s\",$1)}' $1`\n\nif [ \"$CMessage\" = \"\" ]; then\n    echo -e \"\\033[31m GERRIT_CHANGE_SUBJECT \u4f20\u5165\u53c2\u6570\u4e3anull\uff0c\u8bf7\u68c0\u67e5 \\e[0m \"\n    exit 1\nfi\n\nfor i in $CMessage\ndo\n    bg=$(echo $i | awk -F\"|\" {'print $1'})\n    gn=$(echo $i | awk -F\"|\" {'print $2'})\n    xq=$(echo $i | awk -F\"|\" {'print $3'})\n    case $bg in\n    \"\u65b0\u589e\"|\"\u4fee\u6539\"|\"\u5220\u9664\")\n        if [ -z \"$bg\" ] || [ -z \"$gn\" ] || [ -z \"$xq\" ]; then\n            echo -e \"\\033[31m error:  git commit \u63d0\u4ea4\u4fe1\u606f\u683c\u5f0f\u9519\u8bef\uff01 \u8bf7\u4fee\u6539commit\u4fe1\u606f\u91cd\u65b0\u63d0\u4ea4 \\e[0m\n            \u683c\u5f0f\uff1a\u65b0\u589e\/\u4fee\u6539\/\u5220\u9664|\u4fee\u6539\u7684\u529f\u80fd\u540d\u79f0|\u8be6\u60c5\u63cf\u8ff0  \n            \u6ce8\u610f\uff1a\u5b57\u7b26\u4e32\u4e4b\u95f4\u4e0d\u8981\u6709\u7a7a\u683c\uff0c\u6709\u591a\u6761\u529f\u80fd\u53ef\u6362\u884c\u6309\u683c\u5f0f\u8f93\u5165\n                        \u65b9\u6cd5\u5982\u4e0b\uff1a\n                        \u7b2c\u4e00\u6b65\uff1a git reset --soft HEAD^ # \u56de\u9000\u5230\u4e0a\u4e00\u6b21commit\u4fe1\u606f\n                        \u7b2c\u4e8c\u6b65\uff1a git commit -m \u201c\u6309\u7167\u6807\u51c6\u683c\u5f0f\u586b\u5199\u540e\u4ece\u65b0\u63d0\u4ea4\u201d\n                        \u7b2c\u4e09\u6b65\uff1a git push origin HEAD:refs\/for\/master\n                        \"\n                    exit 1\n\n        else\n            echo -e \"\\033[32m commit message \u4fe1\u606f\u683c\u5f0f\u6b63\u786e \\e[0m \"\n        fi\n\n    ;;\n    *)\n        echo -e \"\\033[31m error:  git commit \u63d0\u4ea4\u4fe1\u606f\u683c\u5f0f\u9519\u8bef\uff01 \u8bf7\u4fee\u6539commit\u4fe1\u606f\u91cd\u65b0\u63d0\u4ea4 \\e[0m\n            \u683c\u5f0f\uff1a\u65b0\u589e\/\u4fee\u6539\/\u5220\u9664|\u4fee\u6539\u7684\u529f\u80fd\u540d\u79f0|\u8be6\u60c5\u63cf\u8ff0\n            \u6ce8\u610f\uff1a\u5b57\u7b26\u4e32\u4e4b\u95f4\u4e0d\u8981\u6709\u7a7a\u683c\uff0c\u6709\u591a\u6761\u529f\u80fd\u53ef\u6362\u884c\u6309\u683c\u5f0f\u8f93\u5165\n            \u65b9\u6cd5\u5982\u4e0b\uff1a\n            \u7b2c\u4e00\u6b65\uff1a git reset --soft HEAD^ # \u56de\u9000\u5230\u4e0a\u4e00\u6b21commit\u4fe1\u606f\n            \u7b2c\u4e8c\u6b65\uff1a git commit -m \u201c\u6309\u7167\u6807\u51c6\u683c\u5f0f\u586b\u5199\u540e\u4ece\u65b0\u63d0\u4ea4\u201d\n            \u7b2c\u4e09\u6b65\uff1a git push origin HEAD:refs\/for\/master\n            \"\n        exit 1\n    ;;\n    esac\ndone\n<\/code><\/pre>\n<h4><span class=\"ez-toc-section\" id=\"gerrit_commit-msg_id\"><\/span>gerrit commit-msg id<span class=\"ez-toc-section-end\"><\/span><\/h4>\n<p>commit-msg id\u751f\u6210\u811a\u672c\u793a\u4f8b\uff1a<\/p>\n<pre><code class=\"language-shell line-numbers\"># \u5224\u65ad\u662f\u5426\u6ca1\u6709\u53c2\u6570\uff0c\u4e00\u822c\u60c5\u51b5\u4e0b\u90fd\u4f1a\u6709\u53c2\u6570\u3002\u53c2\u6570\u5c31\u662f.git\/COMMIT_EDITMSG\u6587\u4ef6\nif test \"$#\" != 1 ; then\n  echo \"$0 requires an argument.\"\n  exit 1\nfi\n\n# \u5224\u65ad.git\/COMMIT_EDITMSG\u6587\u4ef6\u662f\u5426\u5b58\u5728\nif test ! -f \"$1\" ; then\n  echo \"file does not exist: $1\"\n  exit 1\nfi\n\n# Do not create a change id if requested\nif test \"false\" = \"`git config --bool --get gerrit.createChangeId`\" ; then\n  exit 0\nfi\n\n# $RANDOM will be undefined if not using bash, so don't use set -u\nrandom=$( (whoami ; hostname ; date; cat $1 ; echo $RANDOM) | git hash-object --stdin)\ndest=\"$1.tmp.${random}\"\n\ntrap 'rm -f \"${dest}\"' EXIT\n\nif ! git stripspace --strip-comments &lt; \"$1\" &gt; \"${dest}\" ; then\n   echo \"cannot strip comments from $1\"\n   exit 1\nfi\n\nif test ! -s \"${dest}\" ; then\n  echo \"file is empty: $1\"\n  exit 1\nfi\n\n# Avoid the --in-place option which only appeared in Git 2.8\n# Avoid the --if-exists option which only appeared in Git 2.15\nif ! git -c trailer.ifexists=doNothing interpret-trailers \\\n      --trailer \"Change-Id: I${random}\" &lt; \"$1\" &gt; \"${dest}\" ; then\n  echo \"cannot insert change-id line in $1\"\n  exit 1\nfi\n\nif ! mv \"${dest}\" \"$1\" ; then\n  echo \"cannot mv ${dest} to $1\"\n  exit 1\nfi\n<\/code><\/pre>\n<h4><span class=\"ez-toc-section\" id=\"%E5%B0%86commit_msg%E6%A0%BC%E5%BC%8F%E8%A7%84%E8%8C%83%E6%A0%A1%E9%AA%8C%E4%B8%8Egerrit_commit-msg_id%E5%90%88%E5%B9%B6%E5%88%B0%E4%B8%80%E8%B5%B7\"><\/span>\u5c06commit msg\u683c\u5f0f\u89c4\u8303\u6821\u9a8c\u4e0egerrit commit-msg id\u5408\u5e76\u5230\u4e00\u8d77<span class=\"ez-toc-section-end\"><\/span><\/h4>\n<pre><code class=\"language-shell line-numbers\">#!\/bin\/bash\n# \u83b7\u53d6\u5f53\u524d\u8def\u5f84\uff0c\u56e0\u4e3aGit Hook\u91cc\u597d\u50cf\u4e0d\u652f\u6301\u76f8\u5bf9\u8def\u5f84\uff0c\u6240\u4ee5\u8981\u83b7\u53d6\u7edd\u5bf9\u8def\u5f84\u8fdb\u884c\u62fc\u63a5.\ncurDir=$(pwd)\n# echo \"\u5f53\u524d\u8def\u5f84 $curDir\"\n# \u63d0\u4ea4\u4fe1\u606f\u683c\u5f0f\u68c0\u67e5\nsource $curDir\/.git\/hooks\/gerrit_commit_msg_format_check\n# commit_id\u751f\u6210\nsource $curDir\/.git\/hooks\/gerrit_commit_id_check\n<\/code><\/pre>\n<button class=\"simplefavorite-button\" data-postid=\"5851\" data-siteid=\"1\" data-groupid=\"1\" data-favoritecount=\"0\" style=\"\">\u6536\u85cf <i class=\"sf-icon-star-empty\"><\/i><\/button>","protected":false},"excerpt":{"rendered":"<p>\u76ee\u5f55 Git Hook\u662f\u4ec0\u4e48git commit msg\u683c\u5f0f\u89c4\u8303\u6821\u9a8cgerrit commit-ms [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[358],"tags":[],"_links":{"self":[{"href":"http:\/\/xinyiworld.top\/wordpress_it\/index.php?rest_route=\/wp\/v2\/posts\/5851"}],"collection":[{"href":"http:\/\/xinyiworld.top\/wordpress_it\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/xinyiworld.top\/wordpress_it\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/xinyiworld.top\/wordpress_it\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/xinyiworld.top\/wordpress_it\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=5851"}],"version-history":[{"count":8,"href":"http:\/\/xinyiworld.top\/wordpress_it\/index.php?rest_route=\/wp\/v2\/posts\/5851\/revisions"}],"predecessor-version":[{"id":6026,"href":"http:\/\/xinyiworld.top\/wordpress_it\/index.php?rest_route=\/wp\/v2\/posts\/5851\/revisions\/6026"}],"wp:attachment":[{"href":"http:\/\/xinyiworld.top\/wordpress_it\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=5851"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/xinyiworld.top\/wordpress_it\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=5851"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/xinyiworld.top\/wordpress_it\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=5851"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}