Skip to content

Instantly share code, notes, and snippets.

@zilahir
Created November 10, 2018 23:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zilahir/29e51a76f4dc1739c7635cfde837099c to your computer and use it in GitHub Desktop.
Save zilahir/29e51a76f4dc1739c7635cfde837099c to your computer and use it in GitHub Desktop.
pre-push lock master branch
#!/bin/bash
protected_branch='master'
policy="\n\n[Policy] Never push code directly to the "$protected_branch" branch! ("$protected_branch" is locked! )\n\n"
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
do_exit(){
echo -e $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
# Prevent ALL pushes to protected_branch
if [[ $push_command =~ $protected_branch ]] || [ $current_branch = $protected_branch ]; then
do_exit
fi
unset do_exit
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment