2019-06-11 06:29:10 +02:00
|
|
|
#!/usr/bin/env bash
|
2018-11-20 14:40:52 +01:00
|
|
|
|
2019-02-20 18:56:37 +01:00
|
|
|
# git pre-push hook script to:
|
|
|
|
# 1) prevent "fixup!" and "squash!" commit from ending up in master, release-*
|
|
|
|
# or maint-*
|
|
|
|
# 2) Disallow pushing branches other than master, release-*
|
|
|
|
# and maint-* to origin (e.g. gitweb.torproject.org).
|
2019-02-20 08:35:27 +01:00
|
|
|
#
|
2018-11-29 10:10:30 +01:00
|
|
|
# To install this script, copy it into .git/hooks/pre-push path in your
|
|
|
|
# local copy of git repository. Make sure it has permission to execute.
|
2019-05-17 18:24:26 +02:00
|
|
|
# Furthermore, make sure that TOR_UPSTREAM_REMOTE_NAME environment
|
|
|
|
# variable is set to local name of git remote that corresponds to upstream
|
|
|
|
# repository on e.g. git.torproject.org.
|
2018-11-29 10:10:30 +01:00
|
|
|
#
|
2018-11-20 14:40:52 +01:00
|
|
|
# The following sample script was used as starting point:
|
|
|
|
# https://github.com/git/git/blob/master/templates/hooks--pre-push.sample
|
|
|
|
|
2019-02-20 18:48:52 +01:00
|
|
|
echo "Running pre-push hook"
|
|
|
|
|
2018-11-20 14:40:52 +01:00
|
|
|
z40=0000000000000000000000000000000000000000
|
|
|
|
|
2019-05-11 17:58:14 +02:00
|
|
|
upstream_name=${TOR_UPSTREAM_REMOTE_NAME:-"upstream"}
|
2018-11-20 14:40:52 +01:00
|
|
|
|
2019-04-05 17:51:24 +02:00
|
|
|
workdir=$(git rev-parse --show-toplevel)
|
|
|
|
if [ -x "$workdir/.git/hooks/pre-commit" ]; then
|
|
|
|
if ! "$workdir"/.git/hooks/pre-commit; then
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
|
2019-08-08 17:32:11 +02:00
|
|
|
PT_DIR=scripts/maint/practracker
|
|
|
|
|
|
|
|
if [ -e "${PT_DIR}/practracker.py" ]; then
|
|
|
|
if [ -e "${PT_DIR}/.enable_practracker_in_hooks" ]; then
|
|
|
|
if ! python3 "${PT_DIR}/practracker.py" "$workdir"; then
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
fi
|
2019-04-19 08:46:36 +02:00
|
|
|
fi
|
|
|
|
|
2019-05-01 11:41:49 +02:00
|
|
|
remote="$1"
|
|
|
|
|
2019-05-11 17:58:14 +02:00
|
|
|
remote_name=$(git remote --verbose | grep "$2" | awk '{print $1}' | head -n 1)
|
|
|
|
|
|
|
|
if [[ "$remote_name" != "$upstream_name" ]]; then
|
2019-05-01 11:41:49 +02:00
|
|
|
echo "Not pushing to upstream - refraining from further checks"
|
|
|
|
exit 0
|
|
|
|
fi
|
|
|
|
|
|
|
|
ref_is_upstream_branch() {
|
|
|
|
if [ "$1" == "refs/heads/master" ] ||
|
|
|
|
[[ "$1" == refs/heads/release-* ]] ||
|
|
|
|
[[ "$1" == refs/heads/maint-* ]]
|
|
|
|
then
|
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2018-11-20 14:40:52 +01:00
|
|
|
# shellcheck disable=SC2034
|
|
|
|
while read -r 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"
|
|
|
|
else
|
|
|
|
# Update to existing branch, examine new commits
|
|
|
|
range="$remote_sha..$local_sha"
|
|
|
|
fi
|
|
|
|
|
2019-02-22 16:05:07 +01:00
|
|
|
if (ref_is_upstream_branch "$local_ref" == 0 ||
|
|
|
|
ref_is_upstream_branch "$remote_ref" == 0) &&
|
|
|
|
[ "$local_ref" != "$remote_ref" ]
|
2019-02-20 18:48:52 +01:00
|
|
|
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
|
|
|
|
|
2018-11-20 14:40:52 +01:00
|
|
|
# Check for fixup! commit
|
|
|
|
commit=$(git rev-list -n 1 --grep '^fixup!' "$range")
|
|
|
|
if [ -n "$commit" ]
|
|
|
|
then
|
|
|
|
echo >&2 "Found fixup! commit in $local_ref, not pushing"
|
2018-11-29 10:10:48 +01:00
|
|
|
echo >&2 "If you really want to push this, use --no-verify."
|
2018-11-20 14:40:52 +01:00
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
# Check for squash! commit
|
|
|
|
commit=$(git rev-list -n 1 --grep '^squash!' "$range")
|
|
|
|
if [ -n "$commit" ]
|
|
|
|
then
|
|
|
|
echo >&2 "Found squash! commit in $local_ref, not pushing"
|
2018-11-29 10:10:48 +01:00
|
|
|
echo >&2 "If you really want to push this, use --no-verify."
|
2018-11-20 14:40:52 +01:00
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
|
|
|
|
exit 0
|