Skip to content

Instantly share code, notes, and snippets.

@bhavanki
Created March 10, 2015 21:09
Show Gist options
  • Save bhavanki/bb3f9346169346c4b5c1 to your computer and use it in GitHub Desktop.
Save bhavanki/bb3f9346169346c4b5c1 to your computer and use it in GitHub Desktop.
Protects origin/master from your screwed-up pushes
#!/bin/bash
protected_branch='master'
current_branch=$(git symbolic-ref HEAD | sed -e 's,.*/\(.*\),\1,')
remote_name=$1
if [[ $remote_name != "origin" ]]; then
exit 0
fi
if [[ $protected_branch == $current_branch ]]; then
# This won't work if it's the first push to the remote.
num_commits=$(git log --oneline ${remote_name}/${protected_branch}..HEAD | wc -l)
if (( num_commits > 1 )); then
echo "NO. You have ${num_commits} commits, that is too many. One at a time."
exit 1
fi
if [[ ! -e ok-to-push ]]; then
echo "NO. Check your work and create ok-to-push, then try again."
exit 1
fi
read -p "You're about to push to ${protected_branch}, are you sure? [y|n] " -n 1 -r < /dev/tty
echo
if echo $REPLY | grep -E '^[Yy]$' > /dev/null
then
rm -f ok-to-push
exit 0 # push will execute
fi
exit 1 # push will not execute
else
exit 0 # push will execute
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment