Created
May 5, 2018 21:16
-
-
Save stepharr/e7cb34b2b7846c8f571740d78e2bde0e to your computer and use it in GitHub Desktop.
A git hook to prevent push's if not based on latest in master
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 | |
red='\033[0;31m' | |
green='\033[0;32m' | |
yellow='\033[0;33m' | |
no_color='\033[0m' | |
BRANCH="$(git symbolic-ref HEAD 2>/dev/null)" | |
if [ $? != 0 ] | |
then | |
DETATCHED="detached HEAD at $(git rev-parse --short HEAD)" | |
echo -e "\n${red} You are currently on ${DETATCHED}. Please make sure you're on a valid branch before pushing. ${no_color}\n" | |
exit 1 | |
else | |
BRANCH=${BRANCH##refs/heads/} | |
if git merge-base --is-ancestor refs/remotes/origin/master ${BRANCH}; | |
then | |
echo -e "\n${green}Pushing \`${BRANCH}\` to remote.${no_color}\n" | |
else | |
# Write to standard error | |
echo -e "\n${red}Error branch \`${BRANCH}\` is not based off the latest commits in master. Please merge in changes from master or rebase branch \`${BRANCH}\` off the changes in master, and then try pushing again.${no_color}\n" | |
exit 1 | |
fi | |
fi | |
exit 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment