建立git提交消息政策

3
我们希望确保每个提交信息的主题中都包含一个Jira工单号码。例如,它应该像这样:“MA-12:修复了关于...的问题”。
我知道可以在客户端使用commit-msg钩子轻松完成此操作。但是,这不会自动为所有开发人员设置。有没有办法在服务器端完成这项工作?
1个回答

3
你可以在服务器端设置更新钩子,类似于这个脚本(由Matthias Hryniszak padcom编写):
如果接收到的提交消息不符合正确的策略,则推送将被拒绝。
#!/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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接