如何获取在预接收挂钩中提交的所有哈希? [英] How to get ALL hashes that are being committed in a pre-receive hook?

查看:80
本文介绍了如何获取在预接收挂钩中提交的所有哈希?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为Git写一个预接收钩子.如果推送了多个提交,但其中任何一个失败,则整个推送都会失败.这就是我想要的.

I am writing a pre-receive hook for Git. This is the one where if multiple commits are pushed, and any one of them fail, then the whole push fails. Which is what I want.

我的问题是,并非所有提交中的所有哈希都被传入.例如,只有最新的提交哈希.

My problem is that not all the hashes from all commits are passed in. Only the most recent commit hash is, e.g.

2个提交被推送到一个仓库中

2 commits are being pushed to a repo:

Commit 1 - 4b5w<br>
Commit 2 - 6gh7 -------------> passed in to pre-receive hook, 
                               but I want the previous hash too.

我不能使用为每个引用调用的更新钩子,因为我不希望任何提交在任何提交失败的情况下通过,例如提交1和提交2失败是不可接受的,因为当提交2失败时,我将不得不以某种方式回滚提交1.

I can't use the update hook which is called for each ref, because I don't want any commits to go through if any one of them fails, e.g. commit 1 passing and commit 2 failing is not acceptable, as I would have to somehow rollback commit 1 when commit 2 fails.

如何从所有提交到预接收钩子的提交中获取哈希?

推荐答案

您可以使用预接收钩子并仍然列出所有推送的提交.
请参阅此答案,其中包括:

You can use a pre-receive hook and still list all pushed commits.
See this answer which includes:

chomp(my @commits = `git rev-list $old..$new`);
if ($?) {
  warn "git rev-list $old..$new failed\n";
  ++$errors, next;
}

foreach my $sha1 (@commits) {
  // validate some policy
}

如torek所评论,这仅适用于master分支.

As commented by torek, this is only for the master branch.

您可以通过多个分支进行交易:

#!/bin/bash
while read oldrev newrev refname
do
    branch=$(git rev-parse --symbolic --abbrev-ref $refname)
    if [ "master" == "$branch" ]; then
        # Do something
    fi
done

这篇关于如何获取在预接收挂钩中提交的所有哈希?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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