Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Not able to detect branch from Git post-receive hook

I've got a post receive hook setup on the remote repo that tries to determine the branch name of the incoming push as follows:

$branch = `git rev-parse --abbrev-ref HEAD`

What i'm finding, though, is that no matter what branch I push from my $branch variable gets set with 'master'.

Any ideas?

like image 380
jsleeuw Avatar asked May 12 '11 05:05

jsleeuw


2 Answers

The post-receive hook gets the same data as the pre-receive and not as arguments, but from stdin. The following is sent for all refs:

oldRev (space) newRev (space) refName (Line feed)

You could parse out the ref name with this bash script:

while read oldrev newrev ref
do
    echo "$ref"
done
like image 90
ralphtheninja Avatar answered Oct 20 '22 18:10

ralphtheninja


You could also do something like this using bash variable substitution:

read oldrev newrev ref

branchname=${ref#refs/heads/}

git checkout ${branchname}
like image 10
Dave Augustus Avatar answered Oct 20 '22 18:10

Dave Augustus