Skip to content

Instantly share code, notes, and snippets.

@jkarni
Last active December 20, 2015 16:09
Show Gist options
  • Save jkarni/6159273 to your computer and use it in GitHub Desktop.
Save jkarni/6159273 to your computer and use it in GitHub Desktop.
Keep master safe.
#!/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