noobcoder noobcoder - 3 months ago 8
Git Question

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

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?

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
}