summaryrefslogtreecommitdiff
path: root/check-rev-policy
blob: c7f6c4e0dcae84ea95d97b895ffb9951dd69e9aa (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
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
#!/bin/sh -eu
set -eu

server=git.ws.local

check_commit()
{
	commit=$1
	
	# Check the email
	email="`git log $commit -1 --pretty=format:%ae`"
	case "$email" in 
	*localhost.localdomain|*\(none\))
		cat <<EOF >&2
---
The commits you are trying to push contain the author email
address '$email'. Please configure your 
name and email address. 
---
EOF
		exit 1
		;;
	esac

	subj="`git log $commit -1 --pretty=format:%s`"
	case "$subj" in
	*Merge\ branch*of*[gs][is][th]\:*)
		cat <<EOF >&2
---
The commit:

EOF
		git log $commit -1 >&2
		cat <<EOF >&2

Looks like it was produced by typing 'git pull' without the --rebase
option when you had local changes. Running 'git  pull --rebase' now
will fix the problem. Then please try, 'git push' again. 
---
EOF
		exit 1
		;;
	esac
}

check_ref_update() 
{
	oldrev=$1
	newrev=$2
	refname=$3
	
	change_type=update
	if expr $oldrev : "^0\+$" > /dev/null 2>&1; then
		change_type=create
	fi

	if expr $newrev : "^0\+$" > /dev/null 2>&1; then
		if [ x$change_type = xcreate ] ; then
			# Deleting an invalid ref, allow
			return 0
		fi
		change_type=delete
	fi

	case $refname in
	refs/heads/*)
		range=
		case $change_type in
		create)
			range="$newrev"
			;;
		update)
			range="$oldrev..$newrev"
			;;
		esac
		
		if [ -n "$range" ]; then
			for merged in `git rev-parse --symbolic-full-name \
				--not --branches | egrep -v "^/^$refname$" | \
				git rev-list --reverse --stdin "$range"`; do
				check_commit $merged
			done
		fi
		;;

	ref/tags/*)
		;;

	# Remote tracking branch
	ref/remotes/*)
		cat <<EOF >&2
---
You are trying to push the remote tracking branch:

   $refname

to $server
---	
EOF
		exit 1
		;;

	# Something completely different
	*)
		cat <<EOF >&2
---
You are trying to push the ref:

  $refname

to $server. This isn't a branch or tag.
---
EOF
		exit 1
		;;
	esac

	return 0
}

if [ $# = 3 ] ; then
	check_ref_update $@ 
else
	echo "usage: check-rev-policy oldrev newrev refname" >&2
	exit 2
fi

exit 0