git服务器钩子设置之限制成员提交代码注释长度

2023-06-01 00:00:00 注释 钩子 长度

git代码库服务端做控制限制:限制成员提交代码注释长度

需要用到了GIT的pre-receive钩子,这个钩子会在客户端发起push请求后调用执行,

只有执行通过才能合并进代码库,话不多说直接上代码:

#!/bin/bash
#pre-receive script
#set -x #for debugging
validate_ref()
{
    # --- Arguments
    oldrev=$(git rev-parse $1)
    newrev=$(git rev-parse $2)
    refname="$3"
    commitList=`git rev-list $oldrev..$newrev`
        #echo $commitList
    split=($commitList)
    for s in ${split[@]}
    do
        echo "@@@@@@@"
        echo "$s"
        msg=`git cat-file commit $s | sed '1,/^$/d'`
        echo $msg
        if [ ${#msg} -lt 15 ];then
            echo "!!! Commit message length less than 15"
            exit 1
        else
            echo "bigger than 5"
        fi
    done
}
fail=""
# Allow dual mode: run from the command line just like the update hook, or
# if no arguments are given then run as a hook script
if [ -n "$1" -a -n "$2" -a -n "$3" ]; then
    # Output to the terminal in command line mode - if someone wanted to
    # resend an email; they could redirect the output to sendmail
    # themselves
    PAGER= validate_ref $2 $3 $1
else
    while read oldrev newrev refname
    do
        validate_ref $oldrev $newrev $refname
    done
fi
if [ -n "$fail" ]; then
    exit $fail
fi

将代码文件命名为pre-receive,并且可执行权限设置为777(注意跨平台文件格式问题)

在git服务器源代码存储的目录(如abc.git),在abc.git目录创建一个新目录custom_hooks

将pre-receive放在custom_hooks目录下,就ok了,gitlab服务器会立刻生效。

相关文章