git gitolite(v3)预接收所有提交消息的挂钩

gpo*_*pol 4 git hook gitolite githooks

我试图强制执行一个策略,即使其中一个提交消息不满足规则,每个推送都会被拒绝.我已经向开发人员分发了一个钩子,以便他们在他们的本地存储库中使用它,但我也希望在他们推送到原点时强制执行此操作.

我有两个问题:

  1. 我应该使用更新挂钩还是预接收挂钩?(我试图设置一个update.secondary钩子,但在我看来它不会被解雇,而预先接收会这样做).

  2. 如何获取推送中包含的每个提交的消息?更具体地说,我希望每个提交消息都具有特定的"有效"(对于我的需要)前缀.因此,我想在此推送提交消息中扫描每个提交,并在接受推送之前对其进行验证.

我使用简单的bash来编写钩子.

谢谢!

Von*_*onC 7

我建议使用Gitolite V3提供的VREFS,而不是使用链式更新挂钩.你可以在这里看到它的所有参数.

由于VREF基本上就像一个钩子,因此您可以像在此脚本中一样获取每个提交的日志消息:git update git log --format=%s -1 $commit

在git commit消息上实施策略的脚本示例:

#!/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
Run Code Online (Sandbox Code Playgroud)

cwhsu在评论中提到: