Last active
April 4, 2017 09:07
-
-
Save agateau/d17814967410d1c68ac2d59d7b5f3724 to your computer and use it in GitHub Desktop.
Git hook to prevent pushing to a list of branches
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 | |
# Copy this as .git/hooks/pre-push | |
# Customize PROTECTED_BRANCHES | |
# Make it executable | |
set -e | |
PROTECTED_BRANCHES="master release" | |
confirm() { | |
local local_branch | |
local remote_branch | |
local_branch=$1 | |
remote_branch=$2 | |
echo "Pushing to $remote_branch" | |
read -p "You're about to push $local_branch to $remote_branch, is that what you intended? [y|n] " -n 1 -r < /dev/tty | |
echo | |
if echo $REPLY | grep -E '^[Yy]$' > /dev/null ; then | |
echo "Proceeding" | |
else | |
exit 1 # push will not execute | |
fi | |
} | |
IFS=' ' | |
while read local_ref local_sha remote_ref remote_sha ; do | |
for branch in $PROTECTED_BRANCHES ; do | |
if [ "$remote_ref" = "refs/heads/$branch" ] ; then | |
confirm $local_ref $branch | |
exit 0 | |
fi | |
done | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment