【问题标题】:git gitolite (v3) pre-receive hook for all commit messagesgit gitolite (v3) 所有提交消息的预接收钩子
【发布时间】:2012-10-26 13:13:08
【问题描述】:

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

我有两个问题:

  1. 我应该使用更新挂钩还是预接收挂钩? (我尝试设置一个 update.secondary 挂钩,但在我看来它不会被触发,而预接收会触发)。

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

我正在使用简单的 bash 来编写钩子代码。

谢谢!

【问题讨论】:

  • 能否详细说明您的第二个问题?

标签: git hook gitolite githooks


【解决方案1】:

我建议不要使用链式更新挂钩,而是使用 Gitolite V3 提供的VREFS。 你可以看到所有its arguments here

由于 VREF 基本上类似于 git update hook,因此您可以像在 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 在 cmets 中提及:

【讨论】:

  • 谢谢,我遵循了你的方法,现在一切似乎都正常了。
  • 我尝试使用脚本,但我得到“远程:钩子/预接收:10:钩子/预接收:语法错误:“(”意外(期望“fi”)”。我来自这一行 "excludes=( ^$oldrev )"
  • @cwhsu 很奇怪,我的环境中没有出现错误。
  • @VonC 我会在其他机器上试试。我也在其他线程中看到了这个脚本,没有人抱怨它。这让我很困惑......无论如何,谢谢你的回复。
  • @cwhsu 是的,这似乎与您当前的回购协议git status 有关。我已经编辑了答案以添加双引号。
猜你喜欢
  • 2013-12-15
  • 1970-01-01
  • 2018-01-07
  • 2011-07-20
  • 2019-07-12
  • 2015-01-13
  • 2018-05-21
  • 1970-01-01
相关资源
最近更新 更多