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

本文介绍了如何通过Gerrit的ref-update钩子来强制规范git提交消息的长度和格式,以及tag的命名规则。规定提交消息长度至少15个字符,且需以特定前缀开始;tag必须以'00.00.00'格式开头。实现方法是在review_site/hooks目录下创建ref-update文件,并根据服务器使用的bash或dash语法进行相应配置。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

如题,需要对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


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值