git gitolite(v3)预先接收所有提交消息的钩子 [英] git gitolite (v3) pre-receive hook for all commit messages
问题描述
我试图执行一个策略,即使其中一个提交消息不满足规则,每个推送都会被拒绝。我已经为开发人员分发了一个钩子,以便他们在本地回购站中使用它,但我也希望在推送到源时强制执行此操作。
I有两个问题:
-
我应该使用更新挂钩还是预接收挂钩? (我试图设置一个update.secondary钩子,但在我看来,它不会被解雇,而预先接受)。
如何获取推送中包含的每个提交的消息?更具体地说,我希望每个提交消息都有一个特定的有效(用于我的需要)前缀。所以我想在接受推送之前扫描提交消息中的每个提交并验证它。
我使用简单的bash来编写钩子。
谢谢!
与其使用链式更新挂钩,我会建议使用 VREFS , Gitolite V3。
您可以在这里看到所有的其论点。
由于VREF基本上就像 git update
钩子 ,您可以像此脚本中那样获取每次提交 git log --format =%s -1 $ commit
:
强制执行git commit消息策略的脚本示例: p>
#!/ 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
cwhsu 在评论中提及:
- a>。
如果您'试图用shell脚本编写服务器端钩子,你可能想看看可以git
预先接收
钩子评估传入的提交?。
我终于得到oldrev
,newrev
和refname
通过使用读入shell脚本。 全零字符SHA1
这一行
不包括=(^ $ oldrev)
,'^
'只是表示排除!
I am trying to enforce a policy where each push gets rejected when even one of the commit messages does not satisfy a rule. I've distributed a hook to the devs in order for them to use it in their local repos but I also want to enforce this when they push to the origin.
I have two questions:
Should I use the update hook or the pre-receive hook? (I've tried to setup an update.secondary hook but it seems to me it doesn't get fired, while a pre-receive does).
How can I get the message for each commit contained in the push? More specifically, I want each commit message to have a specific "valid" (for my needs) prefix. So I would like to scan for every commit in this push the commit message and validate it before I accept the push.
I am using simple bash to code the hooks.
Thanks!
Instead of using chained update hook, I would recommend using VREFS, available with Gitolite V3. You can see all its arguments here.
Since a VREF is basically like a git update
hook, you can, like in this script, get the log message for each commits with git log --format=%s -1 $commit
:
Example of a script enforcing a policy on git commit messages:
#!/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
cwhsu mentions in the comments:
if you're trying to write a server side hook with shell script, You probably want to take a look at "Can git
pre-receive
hooks evaluate the incoming commit?".
I finally get theoldrev
,newrev
andrefname
by using read in shell script.And here is also what all-zeros SHA1 means.
this line "
excludes=( ^$oldrev )
", the '^
' simply means exclude!
这篇关于git gitolite(v3)预先接收所有提交消息的钩子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!