Skip to content

Instantly share code, notes, and snippets.

@capnslipp
Created October 3, 2013 23:55
Show Gist options
  • Save capnslipp/6818953 to your computer and use it in GitHub Desktop.
Save capnslipp/6818953 to your computer and use it in GitHub Desktop.
Standard project .git/hooks/pre-push This pre-push hook (which I use for many of my projects) skips pushing branches and tags that start with “wip/”, “tmp/”, or “local/”. This makes it easy to keep commits marked as work-in-progress, temporary, or local-only from leaking upstream. Requires git 1.8.2, in which pre-push hooks were introduced.
#!/bin/sh
#
# Hook script that blocks pushing of branches and tags with the form 'wip/*', and commits commits the log message starts
# with "WIP" (work in progress)..
#
# This hook is called with the following parameters:
#
# $1 -- Name of the remote to which the push is being done
# $2 -- URL to which the push is being done
#
# If pushing without using a named remote those arguments will be equal.
#
# Information about the commits which are being pushed is supplied as lines to
# the standard input in the form:
#
# <local ref> <local sha1> <remote ref> <remote sha1>
#
# This sample shows how to prevent push of commits where the log message starts
# with "WIP" (work in progress).
remote="$1"
url="$2"
z40=0000000000000000000000000000000000000000
IFS=' '
while read local_ref local_sha remote_ref remote_sha
do
if [ "$local_sha" = $z40 ]
then
# Handle delete
:
else
if [ "$remote_sha" = $z40 ]
then
# New branch, examine all commits
range="$local_sha"
local_ref_type=$(git cat-file -t $local_ref)
case "$local_ref","$local_ref_type" in
refs/heads/wip/*,commit)
echo "*** Creation of branch '$local_ref' denied;\n Branches starting with 'wip/' are not allowed to be pushed." >&2
exit 1
;;
refs/tags/wip/*,commit)
echo "*** Creation of tag '$local_ref' denied;\n Tags starting with 'wip/' are not allowed to be pushed." >&2
exit 1
;;
refs/heads/tmp/*,commit)
echo "*** Creation of branch '$local_ref' denied;\n Branches starting with 'tmp/' are not allowed to be pushed." >&2
exit 1
;;
refs/tags/tmp/*,commit)
echo "*** Creation of tag '$local_ref' denied;\n Tags starting with 'tmp/' are not allowed to be pushed." >&2
exit 1
;;
refs/heads/local/*,commit)
echo "*** Creation of branch '$local_ref' denied;\n Branches starting with 'local/' are not allowed to be pushed." >&2
exit 1
;;
refs/tags/local/*,commit)
echo "*** Creation of tag '$local_ref' denied;\n Tags starting with 'local/' are not allowed to be pushed." >&2
exit 1
;;
esac
else
# Update to existing branch, examine new commits
range="$remote_sha..$local_sha"
fi
# @fixme: This needs to search for commit messages starting with WIP ("\AWIP"), not commit messages with lines that start with WIP ("^WIP").
# It appears, however, that git-rev-list's --grep option only supports basic regexp (ignoring the grep.patternType option), so this will need to be done a differnt way (perhaps with format and a grep statement to check it as a whole).
## Check for WIP commit
#commit=`git rev-list -n 1 --grep '^WIP' "$range"`
#if [ -n "$commit" ]
#then
# echo "Found WIP commit in $local_ref, not pushing"
# 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