2012-10-26 222 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