aboutsummaryrefslogtreecommitdiffstats
path: root/scripts/pre-push
blob: 670f1c9796eb87363d33620ae6f602da83dc5616 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
#!/bin/sh

# This is a git pre-push hook script.
# It limits what you can push toward https://github.com/erlang/otp.git
#
# To activate, make a copy as .git/hooks/pre-push in your repo.

# Called by "git push"
# after it has checked the remote status, but before anything has been
# pushed.  If this script exits with a non-zero status nothing will be pushed.
#
# This hook is called with the following parameters:
#
# $1 -- Name of the remote to which the push is being done
# $2 -- URL to which the push is being done
#
# If pushing without using a named remote those arguments will be equal.
#
# Information about the commits which are being pushed is supplied as lines to
# the standard input in the form:
#
#   <local ref> <local sha1> <remote ref> <remote sha1>
#

NEW_RELEASES="22 21 20 19 18 17"
OLD_RELEASES="r16 r15 r14 r13"
RELEASES="$NEW_RELEASES $OLD_RELEASES"

# First commit on master, not allowed in other branches
MASTER_ONLY=f633fe962ea7078c32f8c81d34950c0ebce0f472

# Number of commits and files allowed in one push by this script
NCOMMITS_MAX=100
NFILES_MAX=100


# Example testing this script for "git push upstream OTP-20.3.8.2":
#
#> null=0000000000000000000000000000000000000000
#> echo "refs/tags/OTP-20.3.8.2 dummysha refs/tags/OTP-20.3.8.2 $null" | scripts/pre-push upstream https://github.com/erlang/otp.git

# Example to test "git push upstream master"
#
#> local_sha=`git rev-parse master`
#> remote_sha=`git rev-parse upstream/master`
#> echo "refs/heads/master $local_sha refs/heads/master $remote_sha" | scripts/pre-push upstream https://github.com/erlang/otp.git


remote="$1"
url="$2"

null=0000000000000000000000000000000000000000

#echo "pre-push hook: remote=$remote"
#echo "pre-push hook: url=$url"

if [ "$url" = 'https://github.com/erlang/otp.git' -o "$url" = '[email protected]:erlang/otp.git' ]
then
    if [ $remote = "$url" ]; then
        echo "$0 says:"
        echo "***"
        echo "*** Push to $url without using a named remote is NOT ALLOWED!!!!"
        echo "***"
        exit 1
    fi
    IFS=' '
    while read local_ref local_sha remote_ref remote_sha
    do
	#echo "pre-push hook:  local_ref=$local_ref"
	#echo "pre-push hook: remote_ref=$remote_ref"
	#echo "pre-push hook:  local_sha=$local_sha"
	#echo "pre-push hook: remote_sha=$remote_sha"

	if [ "$local_sha" = $null ]
	then
            echo "$0 says:"
            echo "***"
            echo "*** DELETE push to '$remote' NOT ALLOWED!!!!!"
            echo "***"
            exit 1
	fi
	if [ "$local_ref" != "$remote_ref" ]
	then
	    echo "$0 says:"
	    echo "***"
	    echo "*** RENAME push: $local_ref pushed as $remote_ref to '$remote' NOT ALLOWED!!!!"
	    echo "***"
	    exit 1
	fi
	case "$remote_ref" in
            refs/heads/master | refs/heads/maint | refs/heads/maint-[0-9][0-9] | refs/heads/maint-r[0-9][0-9])
		branch=${remote_ref#refs/heads/}
                if [ "$remote_sha" = $null ]
                then
                    echo "$0 says:"
		    echo "***"
		    echo "*** UNKNOWN BRANCH: '$branch' does not exist at '$remote'!!!!"
		    echo "***"
		    exit 1
                fi
		if ! git log -1 --oneline $remote_sha > /dev/null 2>&1
		then
		    echo "$0 says:"
		    echo "***"
		    echo "*** The top of '$branch' at '$remote' ($remote_sha)"
		    echo "*** does not exist locally!!!"
		    echo "*** You probably need to refresh local '$branch' and redo merge."
		    echo "***"
		    exit 1
		fi
		if ! git merge-base --is-ancestor $remote_sha $local_sha
		then
		    echo "$0 says:"
		    echo "***"
		    echo "*** FORCE push branch to '$remote' NOT ALLOWED!!!"
		    echo "***"
		    exit 1
		fi
                if [ $remote_ref != refs/heads/master -a "$MASTER_ONLY" ] && git merge-base --is-ancestor $MASTER_ONLY $local_sha
                then
		    echo "$0 says:"
		    echo "***"
		    echo "*** INVALID MERGE: Commit $MASTER_ONLY should not be reachable from '$branch'!!!!"
                    echo "***                You have probably merged master into '$branch' by mistake"
		    echo "***"
		    exit 1
                fi
                if [ ${remote_ref#refs/heads/maint-} != $remote_ref ] && git merge-base --is-ancestor refs/remotes/$remote/maint $local_sha
                then
		    echo "$0 says:"
		    echo "***"
		    echo "*** INVALID MERGE: Branch maint should not be reachable from '$branch'!!!!"
                    echo "***                You have probably merged maint into '$branch' by mistake."
		    echo "***"
		    exit 1
                fi
                if [ $remote_ref = refs/heads/maint -o $remote_ref = refs/heads/master ]; then
                    for x in $RELEASES; do
                        if ! git merge-base --is-ancestor refs/remotes/$remote/maint-$x $local_sha; then
                            echo "$0 says:"
		            echo "***"
		            echo "*** WARNING: Branch '$remote/maint-$x' is not reachable from '$branch'!!!!"
                            echo "***          Someone needs to merge 'maint-$x' forward and push."
		            echo "***"
                        fi
                    done
                fi
                if [ $remote_ref = refs/heads/master ] && ! git merge-base --is-ancestor refs/remotes/$remote/maint $local_sha
                then
                    echo "$0 says:"
		    echo "***"
		    echo "*** INVALID PUSH: Branch '$remote/maint' is not reachable from master!!!!"
                    echo "***               Someone needs to merge maint forward to master and push."
		    echo "***"
		    exit 1
                fi
		NCOMMITS=`git rev-list --count $remote_sha..$local_sha`
		if [ $NCOMMITS -gt $NCOMMITS_MAX ]
		then
		    echo "$0 says:"
		    echo "***"
		    echo "*** HUGE push: $NCOMMITS commits (> $NCOMMITS_MAX) to '$branch' at '$remote' NOT ALLOWED!!!!"
		    echo "***"
		    exit 1
		fi
		NFILES=`git diff --name-only $remote_sha $local_sha | wc --lines`
		if [ $NFILES -gt $NFILES_MAX ]
		then
		    echo "$0 says:"
		    echo "***"
		    echo "*** HUGE push: $NFILES changed files (> $NFILES_MAX) to '$branch' at '$remote' NOT ALLOWED!!!!"
		    echo "***"
		    exit 1
		fi
		;;
	    refs/tags/OTP-*)
		tag=${remote_ref#refs/tags/}
		REL="UNKNOWN"
		for x in $NEW_RELEASES; do
		    if [ ${tag#OTP-$x.} != $tag ]
		    then
			REL=$x
			break
		    fi
		done
		if [ $REL = "UNKNOWN" ]
		then
		    echo "$0 says:"
		    echo "***"
		    echo "*** Unknown OTP release number in tag '$tag'"
		    echo "***"
		    exit 1
		fi
		if [ "$remote_sha" != $null ]
		then
		    echo "$0 says:"
		    echo "***"
		    echo "*** FORCE push tag to '$remote' NOT ALLOWED!!!"
		    echo "*** Tag '$tag' already exists at '$remote'."
		    echo "***"
		    exit 1
		fi
		;;
            refs/heads/*)
		branch=${remote_ref#refs/heads/}
		echo "$0 says:"
		echo "***"
		echo "*** UNKNOWN branch name: '$branch' pushed to '$remote' NOT ALLOWED!!!!"
		echo "***"
		exit 1
		;;
	    refs/tags/*)
		tag=${remote_ref#refs/tags/}
		echo "$0 says:"
		echo "***"
		echo "*** UNKNOWN tag name: '$tag' pushed to '$remote' NOT ALLOWED!!!!"
		echo "***"
		exit 1
		;;
	    *)
		echo "$0 says:"
		echo "***"
		echo "*** STRANGE ref: '$remote_ref' pushed to '$remote' NOT ALLOWED!!!!"
		echo "***"
		exit 1
		;;
	esac
    done
else
    echo "$0: No checks done for remote '$remote' at $url."
fi

exit 0