1
1
"""Package containing all the input parsers specific to git
2
- Their implementation match what is described at https://git-scm.com/docs/githooks"""
2
+
3
+ Their implementation match what is described at
4
+ https://git-scm.com/docs/githooks"""
3
5
4
6
from mercurial import util
5
7
import hooklib_input
6
8
import sys
7
9
import os
8
10
11
+
9
12
class basegitinputparser (object ):
10
13
def scm (self ):
11
14
return 'git'
12
15
16
+
13
17
class gitinforesolver (object ):
14
18
def __init__ (self ):
15
19
self .reporoot = None
16
20
if 'GIT_DIR' in os .environ :
17
- self .reporoot = os .path .dirname (os .path .abspath (os .environ ["GIT_DIR" ]))
21
+ gitdir = os .environ ["GIT_DIR" ]
22
+ self .reporoot = os .path .dirname (os .path .abspath (gitdir ))
18
23
else :
19
- self .reporoot = util .popen4 ("git rev-parse --show-toplevel" )[1 ].read ().strip ()
24
+ self .reporoot = util .popen4 ("git rev-parse --show-toplevel" )[1 ]\
25
+ .read ()\
26
+ .strip ()
20
27
21
28
self ._revs = None
22
29
23
30
def commitmessagefor (self , rev ):
24
- return util .popen4 ("git cat-file commit %s | sed '1,/^$/d'" % rev )[1 ].read ().strip ()
31
+ return util .popen4 ("git cat-file commit %s | sed '1,/^$/d'" % rev )[1 ]\
32
+ .read ().strip ()
25
33
26
34
@property
27
35
def head (self ):
@@ -31,15 +39,18 @@ def head(self):
31
39
def revs (self ):
32
40
if self ._revs :
33
41
return self ._revs
34
- raw = util .popen4 ('git rev-list %s..%s' % (self .old , self .new ))[1 ].read ().strip ()
42
+ raw = util .popen4 ('git rev-list %s..%s' % (self .old , self .new ))[1 ]\
43
+ .read ()\
44
+ .strip ()
35
45
if raw != '' :
36
46
return raw .split ("\n " )
37
47
else :
38
48
return []
39
-
49
+
40
50
def setrevs (self , revs ):
41
51
self ._revs = revs
42
52
53
+
43
54
class gitpostupdateinputparser (basegitinputparser ):
44
55
"""Input parser for the 'post-update' phase
45
56
@@ -53,6 +64,7 @@ def parse(self):
53
64
resolver .setrevs (revs )
54
65
return resolver
55
66
67
+
56
68
class gitupdateinputparser (basegitinputparser ):
57
69
"""Input parser for the 'update' phase
58
70
@@ -70,6 +82,7 @@ def parse(self):
70
82
resolver .new = new
71
83
return resolver
72
84
85
+
73
86
class gitprecommitinputparser (basegitinputparser ):
74
87
"""Input parser for the 'pre-commit' phase
75
88
@@ -80,6 +93,7 @@ def parse(self):
80
93
resolver = gitinforesolver ()
81
94
return resolver
82
95
96
+
83
97
class gitpreapplypatchinputparser (basegitinputparser ):
84
98
"""Input parser for the 'pre-applypatch' phase
85
99
@@ -90,6 +104,7 @@ def parse(self):
90
104
resolver = gitinforesolver ()
91
105
return resolver
92
106
107
+
93
108
class gitpostapplypatchinputparser (basegitinputparser ):
94
109
"""Input parser for the 'post-applypatch' phase
95
110
@@ -100,6 +115,7 @@ def parse(self):
100
115
resolver = gitinforesolver ()
101
116
return resolver
102
117
118
+
103
119
class gitpostcommitinputparser (basegitinputparser ):
104
120
"""Input parser for the 'post-commit' phase
105
121
@@ -110,6 +126,7 @@ def parse(self):
110
126
resolver = gitinforesolver ()
111
127
return resolver
112
128
129
+
113
130
class gitpreautogcinputparser (basegitinputparser ):
114
131
"""input parser for the 'pre-autogc' phase
115
132
@@ -120,6 +137,7 @@ def parse(self):
120
137
resolver = gitinforesolver ()
121
138
return resolver
122
139
140
+
123
141
class gitreceiveinputparser (basegitinputparser ):
124
142
def parse (self ):
125
143
resolver = gitinforesolver ()
@@ -128,6 +146,7 @@ def parse(self):
128
146
resolver .receivedrevs = revs
129
147
return resolver
130
148
149
+
131
150
class gitpostreceiveinputparser (gitreceiveinputparser ):
132
151
"""input parser for the 'post-receive' phase
133
152
@@ -138,6 +157,7 @@ class gitpostreceiveinputparser(gitreceiveinputparser):
138
157
- head (str) => sha1 of HEAD"""
139
158
pass
140
159
160
+
141
161
class gitprereceiveinputparser (gitreceiveinputparser ):
142
162
"""input parser for the 'pre-receive' phase
143
163
@@ -148,6 +168,7 @@ class gitprereceiveinputparser(gitreceiveinputparser):
148
168
- head (str) => sha1 of HEAD"""
149
169
pass
150
170
171
+
151
172
class gitprepushinputparser (basegitinputparser ):
152
173
"""input parser for the 'pre-push' phase
153
174
@@ -163,6 +184,7 @@ def parse(self):
163
184
resolver .revstobepushed = revs
164
185
return resolver
165
186
187
+
166
188
class gitapplypatchmsginputparser (basegitinputparser ):
167
189
"""input parser for the 'applypatch-msg' phase
168
190
@@ -176,6 +198,7 @@ def parse(self):
176
198
resolver .messagefile = messagefile
177
199
return resolver
178
200
201
+
179
202
class gitprerebaseinputparser (basegitinputparser ):
180
203
"""input parser for the 'pre-rebase' phase
181
204
@@ -195,6 +218,7 @@ def parse(self):
195
218
resolver .rebased = rebased
196
219
return resolver
197
220
221
+
198
222
class gitcommitmsginputparser (basegitinputparser ):
199
223
"""input parser for the 'commit-msg' phase
200
224
@@ -208,25 +232,27 @@ def parse(self):
208
232
resolver .messagefile = messagefile
209
233
return resolver
210
234
235
+
211
236
class gitpreparecommitmsginputparser (basegitinputparser ):
212
237
"""input parser for the 'prepare-commit-msg' phase
213
238
214
239
available fields:
215
240
- reporoot (str) => root of the repo
216
241
- messagefile (str) => filename of the file containing the commit message
217
- - mode (str) => could be one of (None, 'message', 'template', 'merge', 'squash', 'commit')
218
- - sha (str) => could be a sha1 or None, relevant only when mode is 'commit'
242
+ - mode (str) => could be one of
243
+ (None, 'message', 'template', 'merge', 'squash', 'commit')
244
+ - sha (str) => a sha1 or None, not None only when mode == 'commit'
219
245
- head (str) => sha1 of HEAD"""
220
246
def parse (self ):
247
+ allowedmodes = ('message' , 'template' , 'merge' , 'squash' , 'commit' )
221
248
messagefile = sys .argv [1 ]
222
249
mode = None
223
250
sha = None
224
251
if len (sys .argv ) > 2 :
225
252
mode = sys .argv [2 ]
226
253
if len (sys .argv ) > 3 :
227
254
sha = sys .argv [3 ]
228
- if (mode is not None and
229
- mode not in ('message' , 'template' , 'merge' , 'squash' , 'commit' )):
255
+ if mode is not None and mode not in allowedmodes :
230
256
raise ValueError ('Invalid Second Argument: mode' )
231
257
if mode != 'commit' and sha is not None :
232
258
raise ValueError ('Invalid Third Argument' )
@@ -237,4 +263,3 @@ def parse(self):
237
263
resolver .mode = mode
238
264
resolver .sha = sha
239
265
return resolver
240
-
0 commit comments