Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Writing a git post-receive hook to deal with a specific branch

Here's my current hook in a bare repo that lives in the company's server: git push origin master This hooks pushes to Assembla. What i need is to push only one branch (master, ideally) when someone pushes changes to that branch on our server, and ignore pushes to other branches. Is it possible to select the branch from a bare repo and push only that branch to Assembla?

like image 645
Jorge Guberte Avatar asked Sep 08 '11 16:09

Jorge Guberte


2 Answers

A post-receive hook gets its arguments from stdin, in the form:

<oldrev> <newrev> <refname> 

Since these arguments are coming from stdin, not from a command line argument, you need to use read instead of $1 $2 $3.

The post-receive hook can receive multiple branches at once (for example if someone does a git push --all), so we also need to wrap the read in a while loop.

A working snippet looks something like this:

#!/bin/bash while read oldrev newrev refname do     branch=$(git rev-parse --symbolic --abbrev-ref $refname)     if [ "master" = "$branch" ]; then         # Do something     fi done 
like image 64
pauljz Avatar answered Sep 21 '22 15:09

pauljz


The last parameter that a post-receive hook gets on stdin is what ref was changed, so we can use that to check if that value was "refs/heads/master." A bit of ruby similar to what I use in a post-receive hook:

STDIN.each do |line|     (old_rev, new_rev, ref_name) = line.split     if ref_name =~ /master/          # do your push     end end 

Note that it gets a line for each ref that was pushed, so if you pushed more than just master, it will still work.

like image 42
ebneter Avatar answered Sep 23 '22 15:09

ebneter