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:
|
2019-08-20 04:21:02 +02:00
|
|
|
# 0) Call the pre-commit hook, if it is available
|
2021-05-25 13:46:34 +02:00
|
|
|
# 1) prevent "fixup!" and "squash!" commit from ending up in main, release-*
|
2019-02-20 18:56:37 +01:00
|
|
|
# or maint-*
|
2021-05-25 13:46:34 +02:00
|
|
|
# 2) Disallow pushing branches other than main, release-*
|
2019-08-20 04:21:02 +02:00
|
|
|
# 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-10-23 07:43:27 +02:00
|
|
|
# Are you adding a new check to the git hooks?
|
|
|
|
# - Common checks belong in the pre-commit hook
|
|
|
|
# - Push-only checks belong in the pre-push hook
|
|
|
|
|
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
|
|
|
|
2020-02-12 11:32:58 +01:00
|
|
|
# The working directory
|
2019-04-05 17:51:24 +02:00
|
|
|
workdir=$(git rev-parse --show-toplevel)
|
2020-02-12 11:32:58 +01:00
|
|
|
# The .git directory
|
|
|
|
# If $workdir is a worktree, then $gitdir is not $workdir/.git
|
|
|
|
gitdir=$(git rev-parse --git-dir)
|
2019-04-05 17:51:24 +02:00
|
|
|
|
2019-10-28 04:28:50 +01:00
|
|
|
cd "$workdir" || exit 1
|
|
|
|
|
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)
|
|
|
|
|
2019-05-01 11:41:49 +02:00
|
|
|
|
|
|
|
ref_is_upstream_branch() {
|
2021-05-25 13:46:34 +02:00
|
|
|
if [ "$1" == "refs/heads/main" ] ||
|
2019-10-24 06:03:48 +02:00
|
|
|
[[ "$1" == refs/heads/release-* ]] ||
|
|
|
|
[[ "$1" == refs/heads/maint-* ]]; then
|
|
|
|
return 1
|
|
|
|
fi
|
2019-05-01 11:41:49 +02:00
|
|
|
}
|
|
|
|
|
2018-11-20 14:40:52 +01:00
|
|
|
# shellcheck disable=SC2034
|
|
|
|
while read -r local_ref local_sha remote_ref remote_sha
|
|
|
|
do
|
2019-10-24 06:03:48 +02:00
|
|
|
if [ "$local_sha" = $z40 ]; then
|
|
|
|
# Handle delete
|
|
|
|
:
|
|
|
|
else
|
|
|
|
if [ "$remote_sha" = $z40 ]; then
|
2021-05-25 13:46:34 +02:00
|
|
|
# New branch, examine commits not in main
|
|
|
|
range="main...$local_sha"
|
2019-10-24 06:03:48 +02:00
|
|
|
else
|
|
|
|
# Update to existing branch, examine new commits
|
|
|
|
range="$remote_sha..$local_sha"
|
|
|
|
fi
|
2018-11-20 14:40:52 +01:00
|
|
|
|
2019-10-24 05:59:02 +02:00
|
|
|
# Call the pre-commit hook for the common checks, if it is executable
|
2020-02-12 11:32:58 +01:00
|
|
|
pre_commit=${gitdir}/hooks/pre-commit
|
|
|
|
if [ -x "$pre_commit" ]; then
|
2019-10-31 05:51:31 +01:00
|
|
|
# Only check the files newly modified in this branch
|
|
|
|
CHECK_FILTER="git diff --name-only --diff-filter=ACMR $range"
|
|
|
|
# Use the appropriate owned tor source list to filter the changed
|
|
|
|
# files
|
2020-01-23 16:26:09 +01:00
|
|
|
# This is the layout in 0.3.5
|
|
|
|
# Keep these lists consistent:
|
|
|
|
# - OWNED_TOR_C_FILES in Makefile.am
|
|
|
|
# - CHECK_FILES in pre-commit.git-hook and pre-push.git-hook
|
|
|
|
# - try_parse in check_cocci_parse.sh
|
|
|
|
CHECK_FILES="$($CHECK_FILTER \
|
2019-10-31 05:51:31 +01:00
|
|
|
src/lib/*/*.[ch] \
|
|
|
|
src/core/*/*.[ch] \
|
|
|
|
src/feature/*/*.[ch] \
|
|
|
|
src/app/*/*.[ch] \
|
|
|
|
src/test/*.[ch] \
|
|
|
|
src/test/*/*.[ch] \
|
|
|
|
src/tools/*.[ch] \
|
2020-01-23 16:26:09 +01:00
|
|
|
)"
|
2019-10-23 07:43:27 +02:00
|
|
|
|
2020-07-02 19:47:12 +02:00
|
|
|
export TOR_EXTRA_PRE_COMMIT_CHECKS=1
|
2019-10-31 05:51:31 +01:00
|
|
|
# We want word splitting here, because file names are space
|
|
|
|
# separated
|
|
|
|
# shellcheck disable=SC2086
|
2020-02-12 11:32:58 +01:00
|
|
|
if ! "$pre_commit" $CHECK_FILES ; then
|
2019-10-31 05:51:31 +01:00
|
|
|
exit 1
|
|
|
|
fi
|
2019-10-23 07:43:27 +02:00
|
|
|
fi
|
|
|
|
|
|
|
|
if [[ "$remote_name" != "$upstream_name" ]]; then
|
|
|
|
echo "Not pushing to upstream - refraining from further checks"
|
|
|
|
continue
|
|
|
|
fi
|
|
|
|
|
2019-10-24 06:03:48 +02:00
|
|
|
if (ref_is_upstream_branch "$local_ref" == 0 ||
|
|
|
|
ref_is_upstream_branch "$remote_ref" == 0) &&
|
|
|
|
[ "$local_ref" != "$remote_ref" ]; 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
|
|
|
|
commit=$(git rev-list -n 1 --grep '^fixup!' "$range")
|
|
|
|
if [ -n "$commit" ]; then
|
|
|
|
echo >&2 "Found fixup! commit in $local_ref, not pushing"
|
|
|
|
echo >&2 "If you really want to push this, use --no-verify."
|
|
|
|
exit 1
|
|
|
|
fi
|
2019-10-23 07:43:27 +02:00
|
|
|
|
2019-10-24 06:03:48 +02:00
|
|
|
# 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"
|
|
|
|
echo >&2 "If you really want to push this, use --no-verify."
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
fi
|
2018-11-20 14:40:52 +01:00
|
|
|
done
|
|
|
|
|
|
|
|
exit 0
|