美文网首页
git hook 提交记录 格式控制

git hook 提交记录 格式控制

作者: Abbott思宇 | 来源:发表于2018-06-06 13:17 被阅读0次

    客户端钩子 commit-msg

    用于在本地合入的时候,检查提交记录是否满足规范

    #!/bin/sh
    
    MSG=`awk '{printf("%s",$0)}' $1`
    
    if [[ ! ${MSG} =~ ^\[story/bug\ name\]:\ {0,1}[[:digit:]]{1,5}.+\[story/bug\ url\]:\ {0,1}https{0,1}://.+\[memo\]:\ {0,1}.+ ]]; then
      echo >&2
      echo "  $0 &2
      exit 1
    fi
    
    if [ -z "$refname" -o -z "$oldrev" -o -z "$newrev" ]; then
      echo "usage: $0 &2
        exit 1
        ;;
    esac
    
    # --- Finished
    exit 0
    

    服务端钩子 update

    用于检查push是,所有的合入记录是否满足规范

    #!/bin/sh
    
    # --- Command line
    refname="$1"
    oldrev="$2"
    newrev="$3"
    
    # --- Safety check
    if [ -z "$GIT_DIR" ]; then
      echo "Don't run this script from the command line." >&2
      echo " (if you want, you could supply GIT_DIR then run" >&2
      echo "  $0 <ref> <oldrev> <newrev>)" >&2
      exit 1
    fi
    
    if [ -z "$refname" -o -z "$oldrev" -o -z "$newrev" ]; then
      echo "usage: $0 <ref> <oldrev> <newrev>" >&2
      exit 1
    fi
    
    # --- Check types
    # if $newrev is 0000...0000, it's a commit to delete a ref.
    zero="0000000000000000000000000000000000000000"
    if [ "$newrev" = "$zero" ]; then
      newrev_type=delete
    else
      newrev_type=$(git cat-file -t $newrev)
    fi
    
    # echo refname: ${refname}
    # echo oldrev: ${oldrev}
    # echo newrev: ${newrev}
    
    case "$refname","$newrev_type" in
      refs/heads/*,commit)
        # commit message format check
        LIST=`git rev-list $oldrev..$newrev`
        for HASH in ${LIST};
            do
              MSG=`git cat-file commit $HASH | sed '1,/^$/d'`
              echo ==========
              echo ${MSG}
              if [[ ${MSG} =~ ^\[story/bug\ name\]:{0,1}\.{0,3}\ {0,1}[[:digit:]]{1,5}.+\[story/bug\ (url|URL)\]:{0,1}\.{0,3}\ {0,1}https{0,1}://.+\[memo\]:{0,1}\.{0,3}\ {0,1}.* ]]; then
                echo valid commit message!
              elif [[ ${MSG} =~ ^Merge\ commit\ \'[[:alnum:]]{40}\'\ into\ .+ ]]; then
                echo valid commit message!
              elif [[ ${MSG} =~ ^Merge\ remote-tracking\ branch\ \'[[:alnum:]]+/[[:alnum:]]+/[[:alnum:]]+\'\ into\ .+ ]]; then
                echo valid commit message!
              elif [[ ${MSG} =~ ^Merge\ branch\ \'.+\'\ into\ .+ ]]; then
                echo valid commit message!
              elif [[ ${MSG} =~ ^Merge\ branch\ \'.+\'\ of\ .+\ into\ .+ ]]; then
                echo valid commit message!
              else
                echo >&2 "$(printf "Invalid commit message!\n\nregex pattern:\n-----\n^\[story/bug\ name\]:\ {0,1}[[:digit:]]{1,5}.+\[story/bug\ url\]:\ {0,1}https{0,1}://.+\[memo\]:\ {0,1}.*\n\nplease use commit message shown below.\n-----\n[story/bug name]:\n[story/bug url]:\n[memo]:")"
                exit 1
              fi
            done
        ;;
      *)
        # Anything else (is there anything else?)
        echo "*** Update hook: unknown type of update to ref $refname of type $newrev_type" >&2
        exit 1
        ;;
    esac
    
    # --- Finished
    exit 0
    

    相关文章

      网友评论

          本文标题:git hook 提交记录 格式控制

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