美文网首页
通过hook 自定义 commit 提交信息规则

通过hook 自定义 commit 提交信息规则

作者: 繁华丶凋零 | 来源:发表于2022-08-11 13:54 被阅读0次

    实现机制: 新增文件

    .git/hooks/commit-msg ,
    .git/hooks/commit-template

    1.commit-msg

    cat commit-msg
    此脚本逻辑判断规则根据自己公司规则自行修改

    #!/bin/sh
    #$1实则为  .git/COMMIT_EDITMSG
    MSG=$1
    
    echo '-----------------'
    commit_info=`grep -vE '^#|^$' $MSG|head -n1`
    if [[ $commit_info =~ \[[a-z0-9]+\]\[[a-z0-9]+\].+ ]];then
    #    echo 'commit info check pass'
    
    
        if [[ $commit_info =~ '[refill]' ]] || [[ $commit_info =~ '[revert]' ]];then
            if [[ $commit_info =~ '[refill]'\[[a-z0-9]+\]\[[a-z0-9]+\].+ ]] || [[ $commit_info =~ '[revert]'\[[a-z0-9]+\]\[[a-z0-9]+\].+ ]];then
    #            echo -e "\033[33m commit info check pass \033[0m"
                echo ''
            else
                echo -e "\033[31m commit info ($commit_info) check fail,will auto exit with error \033[0m"
                exit 1
            fi
        fi
    
        echo -e "\033[33m commit info check pass \033[0m"
    else
    #    echo "commit info $commit_info check fail,will auto exit with error"
        echo -e "\033[31m commit info ($commit_info) check fail,will auto exit with error \033[0m"
        exit 1
    fi
    
    grep -vE '^#|^$' $MSG|grep '\[root cause\]:' > /dev/null
    if [[ $? -eq 1 ]];then
    #    echo 'not have root cause in commit'
        echo -e "\033[31m not have root cause in commit \033[0m"
        exit 1
    fi
    
    grep -vE '^#|^$' $MSG|grep '\[self test\]:' > /dev/null
    if [[ $? -eq 1 ]];then
    #    echo 'not have self test in commit'
        echo -e "\033[31m not have self test in commit \033[0m"
        exit 1
    fi
    
    grep -vE '^#|^$' $MSG| sed -n '2,$p'|while read line
    do
        if [[ $line =~ '[root cause]' ]];then
            if [[ $line =~ '[root cause]:'.+ ]];then
    #            echo 'root cause check pass'
                echo -e "\033[33m root cause check pass \033[0m"
            else
                echo -e "\033[31m root cause ($line) check fail,will auto exit with error \033[0m"
                exit 1
            fi
        elif [[ $line =~ '[self test]' ]];then
            if [[ $line =~ '[self test]:''yes'.* ]] || [[ $line =~ '[self test]:''no'.+ ]];then
                echo -e "\033[33m self test check pass \033[0m"
            else
                echo -e "\033[31m self test ($line) check fail,will auto exit with error \033[0m"
                exit 1
            fi
        elif [[ $line =~ '[codebeamerID]:#'.+ ]];then
            if [[ $line =~ '[codebeamerID]:#'[0-9]{5,} ]];then
                echo -e "\033[33m codebeamer ID check pass \033[0m"
            else
                echo -e "\033[31m codebeamer ID ($line) check fail,will auto exit with error \033[0m"
                exit 1
            fi
        elif [[ $line =~ '[jiraID]:#'.+ ]];then
            if [[ $line =~ '[jiraID]:#'[A-Z]{1,}-[0-9]{1,} ]];then
                echo -e "\033[33m jira ID check pass \033[0m"
            else
                echo -e "\033[31m jira ID ($line) check fail,will auto exit with error \033[0m"
                exit 1
            fi
        else
            continue
        fi
    done
    
    #这里的断言原因 是 while read line  exit 1 后 脚本不会退出 ,git commit 保存后不会抛出异常
    if [[ $? -eq 1 ]];then
        exit 1
    fi
    echo '-----------------'
    
    
    
    
    #原生生成change 步骤(未改动)
    if test "$#" != 1 ; then
      echo "$0 requires an argument."
      exit 1
    fi
    
    if test ! -f "$1" ; then
      echo "file does not exist: $1"
      exit 1
    fi
    
    # Do not create a change id if requested
    if test "false" = "`git config --bool --get gerrit.createChangeId`" ; then
      exit 0
    fi
    
    # $RANDOM will be undefined if not using bash, so don't use set -u
    random=$( (whoami ; hostname ; date; cat $1 ; echo $RANDOM) | git hash-object --stdin)
    dest="$1.tmp.${random}"
    
    trap 'rm -f "${dest}"' EXIT
    
    if ! git stripspace --strip-comments < "$1" > "${dest}" ; then
       echo "cannot strip comments from $1"
       exit 1
    fi
    
    if test ! -s "${dest}" ; then
      echo "file is empty: $1"
      exit 1
    fi
    
    # Avoid the --in-place option which only appeared in Git 2.8
    # Avoid the --if-exists option which only appeared in Git 2.15
    if ! git -c trailer.ifexists=doNothing interpret-trailers \
          --trailer "Change-Id: I${random}" < "$1" > "${dest}" ; then
      echo "cannot insert change-id line in $1"
      exit 1
    fi
    
    if ! mv "${dest}" "$1" ; then
      echo "cannot mv ${dest} to $1"
      exit 1
    fi
    
    

    2.commit-template

    cat commit-template
    此文件规则根据自己公司规则自行修改

    #example:
    #    [feature][baseband] add the secondary classification function
    #
    #    [root cause]:1、add the secondary classification function in the source number estimation2、add the parameters of the source number estimation in the configuration file
    #    [self test]:yes
    #    [is there dependence]:radio
    #    [codebeamerID]:
    #    [jiraID]:#xxx-111
    
    [type(必须)][scope(必须)][dependency id(可选)] message body(必须)
    [root cause]:(必须)
    [self test]:(必须/no需给出理由)
    [is there dependence]:(可选)
    [codebeamerID]:#(可选 #号不可删除)
    [jiraID]:#(可选 #号不可删除)
    

    3.自动化调用

    3.1工程目录下创建文件夹,放入 commit-template, commit-msg

    mkdir .githooks
    cp ~/commit-template ~/commit-msg  .githooks
    

    3.2修改编译命令

    一般工程都有初始编译脚本,脚本里添加如下命令:

    cp .githooks/commit-template .githooks/commit-msg .git/hooks
    #应用commit 模板文件
    git config --global commit.template .git/hooks/commit-template
    

    3.3本人实现机制

    修改工程下makefile文件
    添加如下字段

    # Config git hook path
    $(shell git config core.hookspath .githooks)
    

    相关文章

      网友评论

          本文标题:通过hook 自定义 commit 提交信息规则

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