Move all git maintenance scripts to separate directory
[tor.git] / scripts / git / pre-push.git-hook
blobe7a72efa08dcb9c293d2af089d2c2ca0e918bfe5
1 #!/bin/bash
3 # git pre-push hook script to:
4 # 1) prevent "fixup!" and "squash!" commit from ending up in master, release-*
5 # or maint-*
6 # 2) Disallow pushing branches other than master, release-*
7 # and maint-* to origin (e.g. gitweb.torproject.org).
9 # To install this script, copy it into .git/hooks/pre-push path in your
10 # local copy of git repository. Make sure it has permission to execute.
12 # The following sample script was used as starting point:
13 # https://github.com/git/git/blob/master/templates/hooks--pre-push.sample
15 echo "Running pre-push hook"
17 z40=0000000000000000000000000000000000000000
19 remote="$1"
21 ref_is_upstream_branch() {
22 if [ "$1" == "refs/heads/master" ] ||
23 [[ "$1" == refs/heads/release-* ]] ||
24 [[ "$1" == refs/heads/maint-* ]]
25 then
26 return 1
30 # shellcheck disable=SC2034
31 while read -r local_ref local_sha remote_ref remote_sha
33 if [ "$local_sha" = $z40 ]
34 then
35 # Handle delete
37 else
38 if [ "$remote_sha" = $z40 ]
39 then
40 # New branch, examine all commits
41 range="$local_sha"
42 else
43 # Update to existing branch, examine new commits
44 range="$remote_sha..$local_sha"
47 if (ref_is_upstream_branch "$local_ref" == 0 ||
48 ref_is_upstream_branch "$remote_ref" == 0) &&
49 [ "$local_ref" != "$remote_ref" ]
50 then
51 if [ "$remote" == "origin" ]
52 then
53 echo >&2 "Not pushing: $local_ref to $remote_ref"
54 echo >&2 "If you really want to push this, use --no-verify."
55 exit 1
56 else
57 continue
61 # Check for fixup! commit
62 commit=$(git rev-list -n 1 --grep '^fixup!' "$range")
63 if [ -n "$commit" ]
64 then
65 echo >&2 "Found fixup! commit in $local_ref, not pushing"
66 echo >&2 "If you really want to push this, use --no-verify."
67 exit 1
70 # Check for squash! commit
71 commit=$(git rev-list -n 1 --grep '^squash!' "$range")
72 if [ -n "$commit" ]
73 then
74 echo >&2 "Found squash! commit in $local_ref, not pushing"
75 echo >&2 "If you really want to push this, use --no-verify."
76 exit 1
79 done
81 exit 0