Skip to content

Instantly share code, notes, and snippets.

@trotha01
Created September 23, 2014 02:37
Show Gist options
  • Save trotha01/c2d94377ef6bc9967382 to your computer and use it in GitHub Desktop.
Save trotha01/c2d94377ef6bc9967382 to your computer and use it in GitHub Desktop.
Use to automatically pull and test when doing a git checkout.
Add the shell script to .git/hooks/post-checkout
chmod +x .git/hooks/post-checkout
Try it out!
Give feedback on improvements :)
#!/bin/bash
echo "Post checkout hook running..."
# Exit early if this was only a file checkout, not a branch change ($3 == 1)
[[ $3 == 0 ]] && exit 0
# Check if the local branch is behind the remote
LOCAL=$(git rev-parse @)
REMOTE=$(git rev-parse @{u} 2> /dev/null)
BASE=$(git merge-base @ @{u} 2> /dev/null)
BRANCH=$(git rev-parse --abbrev-ref HEAD)
if [[ "$LOCAL" != "$REMOTE" && "$LOCAL" = "$BASE" ]]; then
echo "You are behind origin, pulling from origin."
echo "git pull origin $BRANCH"
git pull origin $BRANCH
fi
# Allows us to read user input below, assigns stdin to keyboard
exec < /dev/tty
# Run bin/test?
if [ -f bin/test ]; then
echo
echo "Do you wish to run bin/test?"
select yn in "Yes" "No"; do
case $yn in
Yes ) ./bin/test; break;;
No ) exit;;
esac
done
fi
# Put stdin back
exec <&-
echo "Done running post checkout hook."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment