2012-10-26 80 views
4

我试图执行一个策略,即使其中一个提交消息不满足规则,每个推送也会被拒绝。我已经为开发者分发了一个钩子,以便他们在本地回购站中使用它,但我也希望在推到原点时执行此操作。git gitolite(v3)为所有提交消息预接收钩子

我有两个问题:

  1. 我应该使用更新钩或预receive挂钩? (我试图设置update.secondary钩子,但在我看来,它不会被解雇,而预先接收)。

  2. 如何获取包含在推送中的每个提交的消息?更具体地说,我希望每个提交消息都有一个特定的“有效”(用于我的需要)前缀。所以我想在接受推送之前扫描提交消息中的每个提交并验证它。

我使用简单的bash编码钩子。

谢谢!

+0

可否请您详细说明您的第二个问题? – ziu

回答

6

而不是使用链式更新挂钩,我会推荐使用VREFS,与Gitolite V3一起提供。 你可以看到所有的its arguments here

由于VREF基本上是像​​,你可以像在this script,获取该日志的消息每个git log --format=%s -1 $commit承诺:脚本执行上git的承诺消息的政策

例:

#!/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提到的评论:

+0

谢谢,我遵循你的方法,现在一切都好了。 – gpol

+0

我尝试使用脚本,但我得到了“remote:hooks/pre-receive:10:hooks/pre-receive:Syntax error:”(“unexpected(expect”fi“)”。我来自这一行“excludes = (^ $ oldrev)“ – cwhsu

+0

@cwhsu奇怪,我没有得到我的环境中的错误 – VonC