-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpre-receive
53 lines (40 loc) · 1.19 KB
/
pre-receive
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
#!/usr/bin/env python
import os
import sys
import re
'''
the acl should looks like:
avail|userA|branchA
avail|userB|branchB,branchA,master
unavail|userC|branchC
and put the acl file under .git/ with name acl
'''
line = sys.stdin.readline()
result = re.compile(r'^(.+?)\s(.+?)\s(.+)\n$').search(line)
if result is None : #unable to resolve the parameters
print 'Critical error'
sys.exit(1)
else :
ref = result.group(3).strip()
user = os.getenv("GIT_USER")
gitDir = os.getcwd()
aclPath = gitDir[:-5] + '/.git/acl' #get the path of current directory
if user is None :
print 'You need to push with ssh key'
exit(1)
print user , "is pushing to" , ref
access_ctrl = {}
for line in open(aclPath):
state, g_user, branches = line.strip().split('|')
access_ctrl.setdefault(g_user, {'branches': branches.split(','), 'state': state})
if user not in access_ctrl or access_ctrl[user]['state'] == 'unavail':
print "Your account is locked"
sys.exit(1)
ref_name = re.compile(r'refs/heads/(.+)$').search(ref)
if not ref_name:
print "Critical Error"
sys.exit(1)
if ref_name.group(1) not in access_ctrl[user]['branches']:
print "You don't have permission to push to ", ref
sys.exit(1)
sys.exit(0)