Last active
November 30, 2015 01:14
-
-
Save 5t111111/7779f7ac2c844c1b5479 to your computer and use it in GitHub Desktop.
An example pre-push hook script to prevent direct pushing to master excluding the following cases -> All commit logs for the commits to be pushed start with "[ALLOW_MASTER]"
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 | |
# An example hook script to prevent direct pushing to master excluding the following cases | |
# - All commit logs for the commits to be pushed start with "[ALLOW_MASTER]" | |
# e.g [ALLOW_MASTER] Fix something | |
z40=0000000000000000000000000000000000000000 | |
while read local_ref local_sha1 remote_ref remote_sha1 | |
do | |
if [ "${remote_ref##refs/heads/}" = "master" ]; then | |
if [ "$remote_sha1" = $z40 ]; then | |
# New branch, examine all commits | |
range="$local_sha1" | |
else | |
# Update to existing branch, examine new commits | |
range="$remote_sha1..$local_sha1" | |
fi | |
# Check for non-allow-master commits | |
commit=`git rev-list --invert-grep --grep '^\[ALLOW_MASTER\]' "$range"` | |
if [ -n "$commit" ]; then | |
cat << EOT | |
============================================================================= | |
Pushing to the commits the master branch is not allowed | |
if there exists at least 1 commit log that doesn't start with [ALLOW_MASTER] | |
============================================================================= | |
EOT | |
exit 1 | |
fi | |
fi | |
done | |
exit 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
$ curl https://gist.githubusercontent.com/5t111111/7779f7ac2c844c1b5479/raw/7a298ad9c705c146e4586306a126d3f83e480236/pre-push > .git/hooks/pre-push
$ chmod 755 .git/hooks/pre-push