问题描述
我们希望确保每个提交消息都有主题中的Jira门票号码。例如,它应该是类似于MA-12:修正了......的问题。
我知道这很容易在客户端使用commit-味精钩。但是这不会为所有开发人员自动设置。有什么办法可以在服务器端做到这一点?
您可以设置在服务器端,类似(通过):
如果收到的提交消息不符合正确的策略,推送将被拒绝。
$ b
#!/ bin / bash
refname =$ 1
oldrev =$ 2
newrev =$ 3
结果= 0
#确保我们处理分支尚不存在时的情况
if! [$ oldrev = 0000000000000000000000000000000000000000];那么
不包括=(^ $ oldrev)
else
excludes =($(git for-each-ref --format'^%(refname:short)'refs / heads /))
fi
#获取未完成的提交列表
提交=`git rev-list $ newrev$ {excludes [@]}`
#对于列表中的每个提交
提交$ commit
do
#检查日志消息中的票号
message =`git log --format =%s - 1 $ commit`
ticket =`echo$ message| grep -o^ [A-Z] \ {2,3 \} - [0-9] \ +`
if [$ ticket=];那么
echoCommit $ commit不以一个票号开始
result = 1
fi
完成
出口$ result
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 ...".
I know that this can easily be done on client side using commit-msg hook. But this will not be automatically setup for all developers. Is there any way we could do this on server side ?
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
这篇关于设置一个git commit消息策略的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!