Search code examples
gitgithooksgit-post-receive

git post-receive hook and multiple commits in one push


I'm trying to understand what happens in a git post-receive hook when multiple commits are pushed together. From the documentation and the examples I've seen, I would expect it to accept a list of references from STDIN, allowing me to take action on every separate commit, but it doesn't seem to work that way? Here's what I have:

My post-receive hook, obviously this is just for testing:

#!/usr/bin/perl

use Data::Dumper;
my @input = <STDIN>;    
print STDERR Dumper(\@input);

I edit two files, and commit them separately:

nelson% git log origin/master..HEAD
commit 93f96201f2cfd3e83a9a609ec644dc873aefeb17
Author: cecukemon <cecukemon@...>
Date:   Thu May 28 11:09:19 2015 +0200

    commit 2

commit 8bbcc8370101d44c8a7302fb365f257e62a61e9d
Author: cecukemon <cecukemon@...>
Date:   Thu May 28 11:09:09 2015 +0200

    commit 1

Push them as one:

nelson% git push
Counting objects: 6, done.
Delta compression using up to 12 threads.
Compressing objects: 100% (4/4), done.
Writing objects: 100% (6/6), 542 bytes | 0 bytes/s, done.
Total 6 (delta 0), reused 0 (delta 0)
remote: $VAR1 = [
remote:           'fa45b972bb59fbe92d7331cfad5d2933a53414ce 93f96201f2cfd3e83a9a609ec644dc873aefeb17 refs/heads/master
remote: '
remote:         ];
To /home/cecukemon/hooktest
   fa45b97..93f9620  master -> master

Both commits have been successfully pushed:

nelson% git log origin/master..HEAD
nelson%

and the post-receive hook was successfully executed, but I only see one of the commits in the reference list ($VAR1 in the push output).

Am I fundamentally misunderstanding something about how post-receive hooks work?


Solution

  • You get the old and new ref values, so if multiple commits were added you can usually get them with $old..$new (passed to git rev-list for example).

    while read old new ref; do
            while read commit; do
                    # check the commit here
            done <<EOD
    $(git rev-list $old..$new)
    EOD