Created
January 3, 2019 18:22
-
-
Save zilahir/5f5c2d6eb6d000d75b031d184f31896f to your computer and use it in GitHub Desktop.
protect master branch on git
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 | |
protected_branch='master' | |
policy="\n\n[Policy] Never push code directly to the "$protected_branch" branch! (Prevented with pre-push hook.)\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