【指导】git 日志长度、日志开头、tag 命名的 hook 部署(gerrit ref-update)

如题,需要对git上库的commit message的格式做要求,要求长度不小于15,开头必须是以XX开头;tag命名必须是00.00.00开头)。

方案:新增 gerrit ref-update hook

在 review_site/hooks 目录中新增 ref-update 文件即可(如下):

需要注意的时候,bash和dash的语法不一样,注意所在服务器使用的情况:

#!/bin/sh
#
# This hook script is to blocks incorrect commit messages and tag names.
# Called by "git push" with arguments: projectname refname uploader oldrev newrev
#

projectname="$2"
refname="$4"
uploader="$6"
oldrev="$8"
newrev="$10"
min_length=68

# --- Check types
# if $newrev is 0000...0000, it's a commit to delete a ref.
if [ "$newrev" = "0000000000000000000000000000000000000000" ] ;then
    newrev_type=delete
else
    newrev_type=$(git cat-file -t $newrev)
fi

case "$refname","$newrev_type" in
    refs/for/*,commit)
        # Gerrit change
        message=`git cat-file commit $newrev | sed '1,/^$/d'`
        # if comparetion: number -- [ lt--less than; gt--greater than; eq--equal ] / string -- [[ =/!= ]]
        if [ ${#message} -lt $min_length ] ;then
            echo "*" >&2
            echo "*********************************************************" >&2
            echo "*** [ERROR!] The length of your message is too short, ***" >&2
            echo "***       it should be as long as 15 characters.      ***" >&2
            echo "*********************************************************" >&2
            exit 1
        elif [ "`echo $message | cut -c 1-8`" = "[BugFix]" ] || [ "`echo $message | cut -c 1-9`" = "[Feature]" ] ;then
            exit 0
        else
            echo "*" >&2
            echo "***********************************************************" >&2
            echo "***  [ERROR!] Your message is not formatted correctly,  ***" >&2
            echo "*** it should be started with '[Feature]' or '[BugFix]' ***" >&2
            echo "***********************************************************" >&2
            exit 1
        fi
        ;;
    refs/heads/*,delete)
        # delete branch
        echo "*" >&2
        echo "***************************************************" >&2
        echo "*** [ERROR!]You cannot delete a remote branch!. ***" >&2
        echo "***      Please ask administrator for help.     ***" >&2
        echo "***************************************************" >&2
        exit 1
        ;;
    refs/tags/*,commit)
        # tag
        TAG=`echo $refname | cut -c 11-19`
        TAG_PREFIX=${TAG%%_*}
        CHECK_STRING="^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$"
        if echo "$TAG_PREFIX" | egrep -q "$CHECK_STRING"; then
        #if [[ "$TAG_PREFIX" =~ ^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$ ]] ;then
            exit 0
        else
            echo "*" >&2
            echo "********************************************************" >&2
            echo "***   [ERROR!]Your tag is not formatted correctly!   ***" >&2
            echo "***   The correct tag name should be:                ***" >&2
            echo "***           NNN.NNN.NNN_[Time]_[Description]       ***" >&2
            echo "***   Note: N is required; each N is a digit;        ***" >&2
            echo "***         time and description is optional.        ***" >&2
            echo "********************************************************" >&2
            exit 1
        fi
        ;;
    refs/tags/*,delete)
        # delete branch
        echo "*" >&2
        echo "***************************************************" >&2
        echo "***  [ERROR!]You cannot delete a remote tag!.   ***" >&2
        echo "***      Please ask administrator for help.     ***" >&2
        echo "***************************************************" >&2
        exit 1
        ;;
    *)
        # Anything else
        exit 0
        ;;
esac

# --- Finished


你可能感兴趣的:(Gerrit,Git,Shell)