• Uncategorized

About git : How-to-get-ALL-hashes-that-are-being-committed-in-a-pre-receive-hook

Question Detail

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?

Question Answer

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

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.

You may also like...

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.