Skip to content

Instantly share code, notes, and snippets.

@lostphilosopher
Created June 4, 2015 00:55
Show Gist options
  • Save lostphilosopher/77185d675b7d961fd61d to your computer and use it in GitHub Desktop.
Save lostphilosopher/77185d675b7d961fd61d to your computer and use it in GitHub Desktop.
pre-push that prevents a push to master
# Color Definitions
RED=$(tput setaf 1)
GREEN=$(tput setaf 2)
BOLD=$(tput bold)
NORMAL=$(tput sgr0)
printf "${BOLD}Preventing restricted pushes...${NORMAL}\n\n"
protected_branch='master'
policy='${RED}[Error] Pushing to or deleting '$protected_branch' is prevented in this repo via git hooks${NORMAL}'
current_branch=$(git symbolic-ref HEAD | sed -e 's,.*/\(.*\),\1,')
push_command=$(ps -ocommand= -p $PPID)
is_destructive='force|delete|\-f'
will_remove_protected_branch=':'$protected_branch
is_pushing_to_master=$protected_branch
do_exit(){
echo $policy
exit 1
}
if [[ $push_command =~ $is_destructive ]] && [ $current_branch = $protected_branch ]; then
do_exit
fi
if [[ $push_command =~ $is_destructive ]] && [[ $push_command =~ $protected_branch ]]; then
do_exit
fi
if [[ $push_command =~ $will_remove_protected_branch ]]; then
do_exit
fi
if [[ $push_command =~ $is_pushing_to_master ]]; then
do_exit
fi
unset do_exit
printf "${GREEN}Allowed, proceeding with push!${NORMAL}\n\n"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment