aboutsummaryrefslogtreecommitdiff
path: root/scripts/maint/pre-push.git-hook
blob: 26296023fb3047647d6810293342ef8835d1b2f7 (plain)
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
#!/bin/bash

# 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.
#
# This is git pre-push hook script to prevent "fixup!" and "squash!" commits
# from ending up in upstream branches (master, release-* or maint-*).
#
# The following sample script was used as starting point:
# https://github.com/git/git/blob/master/templates/hooks--pre-push.sample

z40=0000000000000000000000000000000000000000

CUR_BRANCH=$(git rev-parse --abbrev-ref HEAD)
if [ "$CUR_BRANCH" != "master" ] && [[ $CUR_BRANCH != release-* ]] &&
        [[ $CUR_BRANCH != maint-* ]]
then
        exit 0
fi

echo "Running pre-push hook"

# 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

                # 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

                # 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
done

exit 0