Skip to content

Instantly share code, notes, and snippets.

@tilap
Last active August 29, 2015 14:22
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 tilap/1f205458786801d2063b to your computer and use it in GitHub Desktop.
Save tilap/1f205458786801d2063b to your computer and use it in GitHub Desktop.
Git Pre-push hook to avoid pushing on some branch or ask confirmation for some other branches
#!/bin/bash
## CONFIG
# Pattern of branch name you won't be allowed to push force
NOPUSHFORCE_BRANCHES_PATTERN="^(dev|release-*|patch-*)"
# Pattern of branch name you won't be allowed to push
NOPUSH_BRANCHES_PATTERN="^(master)"
# Pattern of branch name that will ask to confirm before pushing
CONFIRM_BRANCH_PATTERN="^(master|production)"
## VARS
FORCE_PUSH_PATTERN="force|delete|-f"
CURRENT_BRANCH=$(git symbolic-ref HEAD | sed -e 's,.*/\(.*\),\1,')
PUSH_COMMAND=`ps -ocommand= -p $PPID`
## Deny push force
if [[ "$CURRENT_BRANCH" =~ $NOPUSHFORCE_BRANCHES_PATTERN && "$PUSH_COMMAND" =~ $FORCE_PUSH_PATTERN ]]; then
echo "Pre-push hook: push force on branch \"$CURRENT_BRANCH\" is not allowed"
exit 1
fi
## Deny simple push
if [[ "$CURRENT_BRANCH" =~ $NOPUSH_BRANCHES_PATTERN ]]; then
echo "Pre-push hook: push on branch \"$CURRENT_BRANCH\" is not allowed"
exit 1
fi
## Ask confirmation
if [[ "$CURRENT_BRANCH" =~ $CONFIRM_BRANCH_PATTERN ]]
then
read -p "You're about to push on \"$CURRENT_BRANCH\". Are you really sure? [Y|n] " -n 1 -r < /dev/tty
echo
if echo $REPLY | grep -E '^[YyOo]$' > /dev/null ; then
exit 0
else
exit 1
fi
fi
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment