Makefile/git scripts: Add comments to keep owned file lists in sync
[tor.git] / scripts / git / pre-push.git-hook
blob8b5efb0a9d2e4a49d125a98205cabdf14fd4581e
1 #!/usr/bin/env bash
3 # git pre-push hook script to:
4 # 0) Call the pre-commit hook, if it is available
5 # 1) prevent "fixup!" and "squash!" commit from ending up in master, release-*
6 # or maint-*
7 # 2) Disallow pushing branches other than master, release-*
8 # and maint-* to origin (e.g. gitweb.torproject.org)
10 # To install this script, copy it into .git/hooks/pre-push path in your
11 # local copy of git repository. Make sure it has permission to execute.
12 # Furthermore, make sure that TOR_UPSTREAM_REMOTE_NAME environment
13 # variable is set to local name of git remote that corresponds to upstream
14 # repository on e.g. git.torproject.org.
16 # The following sample script was used as starting point:
17 # https://github.com/git/git/blob/master/templates/hooks--pre-push.sample
19 # Are you adding a new check to the git hooks?
20 # - Common checks belong in the pre-commit hook
21 # - Push-only checks belong in the pre-push hook
23 echo "Running pre-push hook"
25 z40=0000000000000000000000000000000000000000
27 upstream_name=${TOR_UPSTREAM_REMOTE_NAME:-"upstream"}
29 workdir=$(git rev-parse --show-toplevel)
31 cd "$workdir" || exit 1
33 remote="$1"
34 remote_name=$(git remote --verbose | grep "$2" | awk '{print $1}' | head -n 1)
37 ref_is_upstream_branch() {
38 if [ "$1" == "refs/heads/master" ] ||
39 [[ "$1" == refs/heads/release-* ]] ||
40 [[ "$1" == refs/heads/maint-* ]]; then
41 return 1
45 # shellcheck disable=SC2034
46 while read -r local_ref local_sha remote_ref remote_sha
48 if [ "$local_sha" = $z40 ]; then
49 # Handle delete
51 else
52 if [ "$remote_sha" = $z40 ]; then
53 # New branch, examine commits not in master
54 range="master...$local_sha"
55 else
56 # Update to existing branch, examine new commits
57 range="$remote_sha..$local_sha"
60 # Call the pre-commit hook for the common checks, if it is executable
61 if [ -x scripts/git/pre-commit.git-hook ]; then
62 # Only check the files newly modified in this branch
63 CHECK_FILTER="git diff --name-only --diff-filter=ACMR $range"
64 # Use the appropriate owned tor source list to filter the changed
65 # files
66 if [ -d src/lib ]; then
67 # This is the layout in 0.3.5
68 # Keep these lists consistent:
69 # - OWNED_TOR_C_FILES in Makefile.am
70 # - CHECK_FILES in pre-commit.git-hook and pre-push.git-hook
71 # - try_parse in check_cocci_parse.sh
72 CHECK_FILES="$($CHECK_FILTER \
73 src/lib/*/*.[ch] \
74 src/core/*/*.[ch] \
75 src/feature/*/*.[ch] \
76 src/app/*/*.[ch] \
77 src/test/*.[ch] \
78 src/test/*/*.[ch] \
79 src/tools/*.[ch] \
81 elif [ -d src/common ]; then
82 # This was the layout before 0.3.5
83 CHECK_FILES="$($CHECK_FILTER \
84 src/common/*/*.[ch] \
85 src/or/*/*.[ch] \
86 src/test/*.[ch] \
87 src/test/*/*.[ch] \
88 src/tools/*.[ch] \
92 # We want word splitting here, because file names are space
93 # separated
94 # shellcheck disable=SC2086
95 if ! scripts/git/pre-commit.git-hook $CHECK_FILES ; then
96 exit 1
100 if [[ "$remote_name" != "$upstream_name" ]]; then
101 echo "Not pushing to upstream - refraining from further checks"
102 continue
105 if (ref_is_upstream_branch "$local_ref" == 0 ||
106 ref_is_upstream_branch "$remote_ref" == 0) &&
107 [ "$local_ref" != "$remote_ref" ]; then
108 if [ "$remote" == "origin" ]; then
109 echo >&2 "Not pushing: $local_ref to $remote_ref"
110 echo >&2 "If you really want to push this, use --no-verify."
111 exit 1
112 else
113 continue
117 # Check for fixup! commit
118 commit=$(git rev-list -n 1 --grep '^fixup!' "$range")
119 if [ -n "$commit" ]; then
120 echo >&2 "Found fixup! commit in $local_ref, not pushing"
121 echo >&2 "If you really want to push this, use --no-verify."
122 exit 1
125 # Check for squash! commit
126 commit=$(git rev-list -n 1 --grep '^squash!' "$range")
127 if [ -n "$commit" ]; then
128 echo >&2 "Found squash! commit in $local_ref, not pushing"
129 echo >&2 "If you really want to push this, use --no-verify."
130 exit 1
133 done
135 exit 0