#!/bin/sh
read oldrev newrev refname
REPO="git@myhost:puppet-environments.git"
BRANCH=`echo $refname | sed -n 's/^refs\/heads\///p'`
BRANCH_DIR="/etc/puppet/environments"
SSH_ARGS="-i /home/git/.ssh/id_rsa"
SSH_DEST="puppet@myhost"
if [ "$BRANCH" == "master" ]
then
BRANCHDEST="production"
else
BRANCHDEST=$BRANCH
fi
if [ "$newrev" -eq 0 ] 2> /dev/null ; then
# branch is being deleted
echo "Deleting remote branch $BRANCH_DIR/$BRANCHDEST"
ssh $SSH_ARGS $SSH_DEST /bin/sh <<-EOF
cd $BRANCH_DIR && rm -rf $BRANCHDEST
EOF
else
# branch is being updated
echo "Updating remote branch $BRANCH_DIR/$BRANCHDEST"
ssh $SSH_ARGS $SSH_DEST /bin/sh <<-EOF
{ cd $BRANCH_DIR/$BRANCHDEST && git pull origin $BRANCH && ssh-agent -k; } \
|| { mkdir -p $BRANCH_DIR && cd $BRANCH_DIR \
&& git clone $REPO $BRANCHDEST && cd $BRANCHDEST \
&& git checkout -b $BRANCH origin/$BRANCH \
&& ssh-agent -k; }
EOF
fi