Last active
December 20, 2015 16:09
-
-
Save jkarni/6159273 to your computer and use it in GitHub Desktop.
Keep master safe.
This file contains 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 | |
# Prompt before pushing to master. If it's a force push or delete, bail | |
# Don't be overly reliant on this, though; I'm not sure what the corner cases are. | |
# As usual, put this is .git/hooks directory, and make it executable with "chmod 755 pre-push" | |
# Requires git >= 1.8.3 | |
PROT_BRANCH='master' | |
UNSAFE='force|delete|\-f' | |
CONFIRM_MSG="Pushing to master - are you absolutely sure you want to continue?" | |
COMMAND=$(ps -ocommand= -p $PPID) | |
abort() { | |
echo " Unsafe action:" | |
echo "\t" $COMMAND | |
echo "On branch master. Aborting push..." | |
exit 1 | |
} | |
confirm() { | |
echo $CONFIRM_MSG | |
read -p "Confirm: [y/n]" -n 1 | |
echo "" | |
if [[ ! $REPLY =~ ^[Yy]$ ]]; then | |
exit 1 | |
fi | |
} | |
exec < /dev/tty | |
if [[ $(git rev-parse --abbrev-ref HEAD) == $PROT_BRANCH ]]; then | |
if [[ $COMMAND =~ $UNSAFE ]]; then | |
abort | |
else | |
confirm | |
fi | |
fi | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment