美文网首页
GIT服务器钩子设置

GIT服务器钩子设置

作者: jayswu | 来源:发表于2019-11-11 15:37 被阅读0次

    项目越来越大,人员越来越多,代码变动很难通知到每一个项目组成员,尤其是一些核心配置的变动,故此需要在GIT代码库服务端做控制,客户端也可以,但是需要每一个人都在本地进行配置,麻烦亦没必要

    核心诉求:

    1、限制成员提交代码注释长度

    2、核心配置文件修改需要邮件通知到每一个开发人员

    要解决第一个诉求用到了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

    ```

    相关文章

      网友评论

          本文标题:GIT服务器钩子设置

          本文链接:https://www.haomeiwen.com/subject/foytictx.html