Created
September 23, 2014 02:37
-
-
Save trotha01/c2d94377ef6bc9967382 to your computer and use it in GitHub Desktop.
Use to automatically pull and test when doing a git checkout.
This file contains hidden or 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
Add the shell script to .git/hooks/post-checkout | |
chmod +x .git/hooks/post-checkout | |
Try it out! | |
Give feedback on improvements :) |
This file contains hidden or 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/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