-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpre-receive
65 lines (56 loc) · 1.56 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
54
55
56
57
58
59
60
61
62
63
64
65
#!/usr/bin/env python
import re
import sys
import subprocess
CREDBG = '\33[41m'
CEND = '\33[0m'
CRED = '\33[91m'
CGREEN = '\33[92m'
def isPushTag(msg):
return 'refs/tags' in msg
def git(args):
args = ['git'] + args
git = subprocess.Popen(args, stdout = subprocess.PIPE)
details = git.stdout.read()
details = details.decode('utf-8','replace').strip()
return details
def getCommitMsg(old, new):
out = git(['log', old+'..'+new, '--pretty=format:%s'])
return out
def isAutoMsg(msg):
pat = re.compile(r'^(?:Merge|Rvert)')
return pat.match(msg)
#TODO check content in []
def verifyCommitMsgs(msgs):
match = None
pat = re.compile(r'^\[.{0,64}\]\[.*\]\[.*\]')#[test][test][test]
for msg in msgs:
msg = msg.encode('utf-8')
print('start check: '+msg)
if isAutoMsg(msg):# Merge or Revert msg
continue
match = pat.match(msg)
if not match:
print(CREDBG + msg + ' [ERROR format!!]' + CEND)
return False
else:
continue
return True
def main():
print(CGREEN + 'Start Verify Commit Message' + CEND)
(old, new, branch) = sys.stdin.read().split()
print("old:"+old)
print("new:"+new)
print("branch:"+branch)
if isPushTag(branch):
print("is a tag")
sys.exit(0)#success exit
com_msg = getCommitMsg(old, new).split('\n')
result = verifyCommitMsgs(com_msg)
if result:
print(CGREEN + 'Verify Commit Message success' + CEND)
sys.exit(0)
else:
sys.exit(1)
if __name__ == '__main__':
main()