git gitolite(v3)预先接收所有提交消息的钩子 [英] git gitolite (v3) pre-receive hook for all commit messages

查看:133
本文介绍了git gitolite(v3)预先接收所有提交消息的钩子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



I有两个问题:


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

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


我使用简单的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 在评论中提及:




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:

  1. 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).

  2. 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:

这篇关于git gitolite(v3)预先接收所有提交消息的钩子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆