Setup a git commit message policy

前端 未结 1 1921
耶瑟儿~
耶瑟儿~ 2021-01-03 12:02

We would like to ensure that every commit message have a Jira ticket number in the subject. For instance, it should be something like \"MA-12: Fixed issue about ...\".

相关标签:
1条回答
  • 2021-01-03 12:59

    You could setup an update hook on the server side, similar to this script (by Matthias Hryniszak padcom):

    If the commit message received doesn't respect the right policy, the push will be rejected.

    #!/bin/bash
    
    refname="$1"
    oldrev="$2"
    newrev="$3"
    result=0
    
    # Make sure we handle the situation when the branch does not exist yet
    if ! [ $oldrev = 0000000000000000000000000000000000000000 ] ; then
        excludes=( ^$oldrev )
    else
        excludes=( $(git for-each-ref --format '^%(refname:short)' refs/heads/) )
    fi
    
    # Get the list of incomming commits
    commits=`git rev-list $newrev "${excludes[@]}"`
    
    # For every commit in the list
    for commit in $commits
    do
      # check the log message for ticket number
      message=`git log --format=%s -1 $commit`
      ticket=`echo "$message" | grep -o "^[A-Z]\{2,3\}-[0-9]\+"`
      if [ "$ticket" = "" ] ; then
        echo "Commit $commit does not start with a ticket number"
        result=1
      fi
    done
    
    exit $result
    
    0 讨论(0)
提交回复
热议问题