In pre-push hook script, actually check local and remote refs

This commit is contained in:
rl1987 2019-02-20 19:48:52 +02:00 committed by David Goulet
parent ae5a0f39cd
commit f3eac74ed9

View File

@ -11,29 +11,20 @@
# The following sample script was used as starting point: # The following sample script was used as starting point:
# https://github.com/git/git/blob/master/templates/hooks--pre-push.sample # https://github.com/git/git/blob/master/templates/hooks--pre-push.sample
echo "Running pre-push hook"
z40=0000000000000000000000000000000000000000 z40=0000000000000000000000000000000000000000
remote="$1" remote="$1"
CUR_BRANCH=$(git rev-parse --abbrev-ref HEAD)
# Only allow pushing master, release-* and maint-* branches to origin. ref_is_upstream_branch() {
if [ "$remote" == "origin" ] if [ "$1" == "refs/heads/master" ] ||
then [[ "$1" == refs/heads/release-* ]] ||
if [ "$CUR_BRANCH" != "master" ] && [[ $CUR_BRANCH != release-* ]] && [[ "$1" == refs/heads/maint-* ]]
[[ $CUR_BRANCH != maint-* ]]
then then
echo >&2 "Not pushing $CUR_BRANCH to origin" return 1
exit 1
fi fi
fi }
if [ "$CUR_BRANCH" != "master" ] && [[ $CUR_BRANCH != release-* ]] &&
[[ $CUR_BRANCH != maint-* ]]
then
exit 0
fi
echo "Running pre-push hook"
# shellcheck disable=SC2034 # shellcheck disable=SC2034
while read -r local_ref local_sha remote_ref remote_sha while read -r local_ref local_sha remote_ref remote_sha
@ -52,6 +43,19 @@ do
range="$remote_sha..$local_sha" range="$remote_sha..$local_sha"
fi fi
if ref_is_upstream_branch "$local_ref" == 0 ||
ref_is_upstream_branch "$remote_ref" == 0
then
if [ "$remote" == "origin" ]
then
echo >&2 "Not pushing: $local_ref to $remote_ref"
echo >&2 "If you really want to push this, use --no-verify."
exit 1
else
continue
fi
fi
# Check for fixup! commit # Check for fixup! commit
commit=$(git rev-list -n 1 --grep '^fixup!' "$range") commit=$(git rev-list -n 1 --grep '^fixup!' "$range")
if [ -n "$commit" ] if [ -n "$commit" ]