-
Notifications
You must be signed in to change notification settings - Fork 3
/
hooklib_input.py
160 lines (124 loc) · 4.22 KB
/
hooklib_input.py
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
from hooklib_git import *
from hooklib_hg import *
import os
import sys
def readlines():
"""Extracted to make mocking it easier"""
return sys.stdin.readlines()
class prepushinputparser(object):
@staticmethod
def findscm():
return gitprepushinputparser()
class prereceiveinputparser(object):
@staticmethod
def findscm():
return gitprereceiveinputparser()
class postreceiveinputparser(object):
@staticmethod
def findscm():
return gitpostreceiveinputparser()
class preparecommitmsginputparser(object):
@staticmethod
def findscm():
return gitpreparecommitmsginputparser()
class preautogcinputparser(object):
@staticmethod
def findscm():
return gitpreautogcinputparser()
class prerebaseinputparser(object):
@staticmethod
def findscm():
return gitprerebaseinputparser()
class postcommitinputparser(object):
@staticmethod
def findscm():
return gitpostcommitinputparser()
class preapplypatchinputparser(object):
@staticmethod
def findscm():
return gitpreapplypatchinputparser()
class postapplypatchinputparser(object):
@staticmethod
def findscm():
return gitpostapplypatchinputparser()
class applypatchmsginputparser(object):
@staticmethod
def findscm():
return gitapplypatchmsginputparser()
class commitmsginputparser(object):
@staticmethod
def findscm():
return gitcommitmsginputparser()
class postupdateinputparser(object):
@staticmethod
def findscm():
"""Find the correct type of postupdateinputparser
based on the SCM used"""
if 'GIT_DIR' in os.environ:
return gitpostupdateinputparser()
else:
raise NotImplementedError("No implemented for your SCM")
class updateinputparser(object):
@staticmethod
def findscm():
"""Find the correct type of updateinputparser based on the SCM used"""
if 'GIT_DIR' in os.environ:
return gitupdateinputparser()
elif 'HG_NODE' in os.environ:
return hgupdateinputparser()
else:
raise NotImplementedError("No implemented for your SCM")
class precommitinputparser(object):
@staticmethod
def findscm():
if 'GIT_DIR' in os.environ:
return gitprecommitinputparser()
else:
raise NotImplementedError("No implemented for your SCM")
class dummyinputparser(object):
@staticmethod
def findscm():
return dummyinputparser()
def parse(self):
return None
class inputparser(object):
@staticmethod
def fromphases(p):
for scm, phasename in p:
try:
parserforphase = inputparser.fromphase(phasename)
if parserforphase.scm() == scm:
return parserforphase
else:
continue
except NotImplementedError:
pass
raise NotImplementedError("Couldn't find phase matching"
" conditions")
@staticmethod
def fromphase(phase):
"""Factory method to return an appropriate input parser
For example if the phase is 'post-update' and that the git env
variables are set, we infer that we need a git postupdate
inputparser"""
phasemapping = {
None: dummyinputparser,
'applypatch-msg': applypatchmsginputparser,
'pre-applypatch': preapplypatchinputparser,
'post-applypatch': postapplypatchinputparser,
'pre-commit': precommitinputparser,
'prepare-commit-msg': preparecommitmsginputparser,
'commit-msg': commitmsginputparser,
'post-commit': postcommitinputparser,
'pre-rebase': prerebaseinputparser,
'pre-push': prepushinputparser,
'pre-receive': prereceiveinputparser,
'update': updateinputparser,
'post-receive': postreceiveinputparser,
'post-update': postupdateinputparser,
'pre-auto-gc': preautogcinputparser,
}
try:
return phasemapping[phase].findscm()
except KeyError:
raise NotImplementedError("Unsupported hook type %s" % phase)