Skip to content

Instantly share code, notes, and snippets.

@blbradley
Last active December 14, 2015 23:38
Show Gist options
  • Save blbradley/5166971 to your computer and use it in GitHub Desktop.
Save blbradley/5166971 to your computer and use it in GitHub Desktop.
git post-receive hook for tracking puppet environments on the same machine
#!/bin/sh
read oldrev newrev refname
REPO="/my/repo.git"
BRANCH=`echo $refname | sed -n 's/^refs\/heads\///p'`
BRANCH_DIR="/etc/puppet/environments"
#Unset GIT_DIR and GIT_WORK_DIR so git chooses them by current directory
#Based on behaviour of git post-receive hook, undocumented
unset GIT_DIR
unset GIT_WORK_DIR
if [ "$newrev" -eq 0 ] 2> /dev/null ; then
# branch is being deleted
echo "Deleting remote branch $BRANCH_DIR/$BRANCH"
cd $BRANCH_DIR && rm -rf $BRANCH
else
# branch is being updated
echo "Updating remote branch $BRANCH_DIR/$BRANCH"
{ cd $BRANCH_DIR/$BRANCH && git fetch \
&& git reset --hard origin/$BRANCH ; } \
|| { mkdir -p $BRANCH_DIR && cd $BRANCH_DIR \
&& git clone $REPO $BRANCH && cd $BRANCH \
&& git checkout -b $BRANCH origin/$BRANCH ; }
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment