目录
Git Hook是什么
https://zhuanlan.zhihu.com/p/149294652
git commit msg格式规范校验
https://blog.csdn.net/junmoxi/article/details/89244160
https://www.jianshu.com/p/f3d17c11bb8a(定义gradle脚本将commit msg规范文件拷贝到指定目录)
commit-msg shell脚本示例:
#!/bin/bash
#############################################
# Description: check commit message
# Author: XinYi
# Version: 1.0
# CreateTime: 2021/10/22
# Administe User: jenkins
#############################################
echo "begin commit msg check..."
# CMessage=$GERRIT_CHANGE_SUBJECT
# 此种方式不兼容git commit --amend,会报错。
# CMessage=`awk '{printf("%s",$0)}' $1`
CMessage=`awk '{printf("%s",$1)}' $1`
if [ "$CMessage" = "" ]; then
echo -e "\033[31m GERRIT_CHANGE_SUBJECT 传入参数为null,请检查 \e[0m "
exit 1
fi
for i in $CMessage
do
bg=$(echo $i | awk -F"|" {'print $1'})
gn=$(echo $i | awk -F"|" {'print $2'})
xq=$(echo $i | awk -F"|" {'print $3'})
case $bg in
"新增"|"修改"|"删除")
if [ -z "$bg" ] || [ -z "$gn" ] || [ -z "$xq" ]; then
echo -e "\033[31m error: git commit 提交信息格式错误! 请修改commit信息重新提交 \e[0m
格式:新增/修改/删除|修改的功能名称|详情描述
注意:字符串之间不要有空格,有多条功能可换行按格式输入
方法如下:
第一步: git reset --soft HEAD^ # 回退到上一次commit信息
第二步: git commit -m “按照标准格式填写后从新提交”
第三步: git push origin HEAD:refs/for/master
"
exit 1
else
echo -e "\033[32m commit message 信息格式正确 \e[0m "
fi
;;
*)
echo -e "\033[31m error: git commit 提交信息格式错误! 请修改commit信息重新提交 \e[0m
格式:新增/修改/删除|修改的功能名称|详情描述
注意:字符串之间不要有空格,有多条功能可换行按格式输入
方法如下:
第一步: git reset --soft HEAD^ # 回退到上一次commit信息
第二步: git commit -m “按照标准格式填写后从新提交”
第三步: git push origin HEAD:refs/for/master
"
exit 1
;;
esac
done
gerrit commit-msg id
commit-msg id生成脚本示例:
# 判断是否没有参数,一般情况下都会有参数。参数就是.git/COMMIT_EDITMSG文件
if test "$#" != 1 ; then
echo "$0 requires an argument."
exit 1
fi
# 判断.git/COMMIT_EDITMSG文件是否存在
if test ! -f "$1" ; then
echo "file does not exist: $1"
exit 1
fi
# Do not create a change id if requested
if test "false" = "`git config --bool --get gerrit.createChangeId`" ; then
exit 0
fi
# $RANDOM will be undefined if not using bash, so don't use set -u
random=$( (whoami ; hostname ; date; cat $1 ; echo $RANDOM) | git hash-object --stdin)
dest="$1.tmp.${random}"
trap 'rm -f "${dest}"' EXIT
if ! git stripspace --strip-comments < "$1" > "${dest}" ; then
echo "cannot strip comments from $1"
exit 1
fi
if test ! -s "${dest}" ; then
echo "file is empty: $1"
exit 1
fi
# Avoid the --in-place option which only appeared in Git 2.8
# Avoid the --if-exists option which only appeared in Git 2.15
if ! git -c trailer.ifexists=doNothing interpret-trailers \
--trailer "Change-Id: I${random}" < "$1" > "${dest}" ; then
echo "cannot insert change-id line in $1"
exit 1
fi
if ! mv "${dest}" "$1" ; then
echo "cannot mv ${dest} to $1"
exit 1
fi
将commit msg格式规范校验与gerrit commit-msg id合并到一起
#!/bin/bash
# 获取当前路径,因为Git Hook里好像不支持相对路径,所以要获取绝对路径进行拼接.
curDir=$(pwd)
# echo "当前路径 $curDir"
# 提交信息格式检查
source $curDir/.git/hooks/gerrit_commit_msg_format_check
# commit_id生成
source $curDir/.git/hooks/gerrit_commit_id_check
0 条评论