Last active
July 6, 2016 16:49
-
-
Save egel/2058f19cf78df84ade741b7a77a38006 to your computer and use it in GitHub Desktop.
Git pre-push guardian to prevent push to protected branches.
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 | |
# General colors | |
black='\x1B[0;30m' | |
red='\x1B[0;31m' | |
green='\x1B[0;32m' # '\e[1;32m' is too bright for white bg. | |
blue='\x1B[1;34m' | |
yellow='\x1B[0;33m' | |
purple='\x1B[1;35m' | |
cyan='\x1B[0;36m' | |
endColor='\x1B[0m' | |
protected_branches=( master stage develop ) | |
current_branch=$(git symbolic-ref HEAD | sed -e 's,.*/\(.*\),\1,') | |
for i in "${protected_branches[@]}" | |
do | |
if [ ${i} == ${current_branch} ]; then | |
echo -en "${yellow}[Git pre-push]${endColor} You're about to push to ${red}${i}${endColor}, is that what you intended?" | |
read -p " [y/N] " -r < /dev/tty | |
if echo ${REPLY} | egrep -E '^[yY]$' > /dev/null | |
then | |
echo -e "Continue of pushing to ${red}${i}${endColor}" | |
exit 0 # push will execute | |
fi | |
echo -en "Abort pushing to ${red}${i}${endColor}\n" | |
exit 1 # push will not execute | |
fi | |
done | |
exit 0 # push will execute when won't match protected_branches |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Usage for git < 1.8.2: