Last active
September 27, 2024 14:26
-
-
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
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 | |
## 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