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?
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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With