Last active
December 14, 2015 23:38
-
-
Save blbradley/5166971 to your computer and use it in GitHub Desktop.
git post-receive hook for tracking puppet environments on the same machine
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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