How to get ALL hashes that are being committed in a pre-receive hook?

本小妞迷上赌 提交于 2019-11-29 22:57:08

问题


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

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.

How do I get the hashes from ALL commits being passed to the pre-receive hook?


回答1:


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
}

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

You can deal with multiple branches:

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



回答2:


while read old new ref; do 
    [[ $new = *[^0]* ]] && news="$news $new"
done
git rev-list $news --not --all

This will avoid things like fastforwards over previously-pushed commits triggering wasted revalidation of unchanged content.



来源:https://stackoverflow.com/questions/39006695/how-to-get-all-hashes-that-are-being-committed-in-a-pre-receive-hook

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!