设置git提交消息策略

时间:2014-07-29 08:02:29

标签: git

我们希望确保每个提交邮件在主题中都有一个Jira票号。例如,它应该像" MA-12:修复了关于......"。

的问题

我知道这可以使用commit-msg hook在客户端轻松完成。但是这不会自动为所有开发人员设置。我们有什么方法可以在服务器端执行此操作吗?

1 个答案:

答案 0 :(得分:3)

您可以在服务器端设置update hook,类似to this scriptMatthias 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
相关问题