This repository has been archived by the owner on Aug 15, 2020. It is now read-only.
forked from charignon/hooklib
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhooklib_git.py
265 lines (210 loc) · 7.71 KB
/
hooklib_git.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
"""Package containing all the input parsers specific to git
Their implementation match what is described at
https://git-scm.com/docs/githooks"""
from mercurial import util
import hooklib_input
import sys
import os
class basegitinputparser(object):
def scm(self):
return 'git'
class gitinforesolver(object):
def __init__(self):
self.reporoot = None
if 'GIT_DIR' in os.environ:
gitdir = os.environ["GIT_DIR"]
self.reporoot = os.path.dirname(os.path.abspath(gitdir))
else:
self.reporoot = util.popen4("git rev-parse --show-toplevel")[1]\
.read()\
.strip()
self._revs = None
def commitmessagefor(self, rev):
return util.popen4("git cat-file commit %s | sed '1,/^$/d'" % rev)[1]\
.read().strip()
@property
def head(self):
return util.popen4("git rev-parse HEAD")[1].read().strip()
@property
def revs(self):
if self._revs:
return self._revs
raw = util.popen4('git rev-list %s..%s' % (self.old, self.new))[1]\
.read()\
.strip()
if raw != '':
return raw.split("\n")
else:
return []
def setrevs(self, revs):
self._revs = revs
class gitpostupdateinputparser(basegitinputparser):
"""Input parser for the 'post-update' phase
Available fields:
- reporoot (str) => root of the repo
- head (str) => sha1 of HEAD
- revs (list of sha1 (str))"""
def parse(self):
revs = sys.argv[1:]
resolver = gitinforesolver()
resolver.setrevs(revs)
return resolver
class gitupdateinputparser(basegitinputparser):
"""Input parser for the 'update' phase
Available fields:
- reporoot (str) => root of the repo
- head (str) => sha1 of HEAD
- refname (str) => refname that is updated, like 'refs/heads/master'
- old (str) => old sha of the ref
- new (str) => new sha of the ref"""
def parse(self):
refname, old, new = sys.argv[1:]
resolver = gitinforesolver()
resolver.refname = refname
resolver.old = old
resolver.new = new
return resolver
class gitprecommitinputparser(basegitinputparser):
"""Input parser for the 'pre-commit' phase
Available fields:
- reporoot (str) => root of the repo
- head (str) => sha1 of HEAD"""
def parse(self):
resolver = gitinforesolver()
return resolver
class gitpreapplypatchinputparser(basegitinputparser):
"""Input parser for the 'pre-applypatch' phase
Available fields:
- reporoot (str) => root of the repo
- head (str) => sha1 of HEAD"""
def parse(self):
resolver = gitinforesolver()
return resolver
class gitpostapplypatchinputparser(basegitinputparser):
"""Input parser for the 'post-applypatch' phase
Available fields:
- reporoot (str) => root of the repo
- head (str) => sha1 of HEAD"""
def parse(self):
resolver = gitinforesolver()
return resolver
class gitpostcommitinputparser(basegitinputparser):
"""Input parser for the 'post-commit' phase
Available fields:
- reporoot (str) => root of the repo
- head (str) => sha1 of HEAD"""
def parse(self):
resolver = gitinforesolver()
return resolver
class gitpreautogcinputparser(basegitinputparser):
"""input parser for the 'pre-autogc' phase
available fields:
- reporoot (str) => root of the repo
- head (str) => sha1 of HEAD"""
def parse(self):
resolver = gitinforesolver()
return resolver
class gitreceiveinputparser(basegitinputparser):
def parse(self):
resolver = gitinforesolver()
rawrevs = hooklib_input.readlines()
revs = tuple([tuple(line.strip().split(' ')) for line in rawrevs])
resolver.receivedrevs = revs
return resolver
class gitpostreceiveinputparser(gitreceiveinputparser):
"""input parser for the 'post-receive' phase
available fields:
- reporoot (str) => root of the repo
- receivedrevs =>
(list of tuples: (<old-value> <new-value> <ref-name>))
- head (str) => sha1 of HEAD"""
pass
class gitprereceiveinputparser(gitreceiveinputparser):
"""input parser for the 'pre-receive' phase
available fields:
- reporoot (str) => root of the repo
- receivedrevs =>
(list of tuples: (<old-value> <new-value> <ref-name>))
- head (str) => sha1 of HEAD"""
pass
class gitprepushinputparser(basegitinputparser):
"""input parser for the 'pre-push' phase
available fields:
- reporoot (str) => root of the repo
- revstobepushed =>
(list of tuples: <local ref> <local sha1> <remote ref> <remote sha1>))
- head (str) => sha1 of HEAD"""
def parse(self):
resolver = gitinforesolver()
rawrevs = hooklib_input.readlines()
revs = tuple([tuple(line.strip().split(' ')) for line in rawrevs])
resolver.revstobepushed = revs
return resolver
class gitapplypatchmsginputparser(basegitinputparser):
"""input parser for the 'applypatch-msg' phase
available fields:
- reporoot (str) => root of the repo
- messagefile (str) => filename of the file containing the commit message
- head (str) => sha1 of HEAD"""
def parse(self):
messagefile = sys.argv[1]
resolver = gitinforesolver()
resolver.messagefile = messagefile
return resolver
class gitprerebaseinputparser(basegitinputparser):
"""input parser for the 'pre-rebase' phase
available fields:
- reporoot (str) => root of the repo
- upstream (str) => upstream from which the serie was forked
- rebased (str) => branch being rebased, None if current branch
- head (str) => sha1 of HEAD"""
def parse(self):
upstream = sys.argv[1]
if len(sys.argv) > 2:
rebased = sys.argv[2]
else:
rebased = None
resolver = gitinforesolver()
resolver.upstream = upstream
resolver.rebased = rebased
return resolver
class gitcommitmsginputparser(basegitinputparser):
"""input parser for the 'commit-msg' phase
available fields:
- reporoot (str) => root of the repo
- messagefile (str) => filename of the file containing the commit message
- head (str) => sha1 of HEAD"""
def parse(self):
messagefile = sys.argv[1]
resolver = gitinforesolver()
resolver.messagefile = messagefile
return resolver
class gitpreparecommitmsginputparser(basegitinputparser):
"""input parser for the 'prepare-commit-msg' phase
available fields:
- reporoot (str) => root of the repo
- messagefile (str) => filename of the file containing the commit message
- mode (str) => could be one of
(None, 'message', 'template', 'merge', 'squash', 'commit')
- sha (str) => a sha1 or None, not None only when mode == 'commit'
- head (str) => sha1 of HEAD"""
def parse(self):
allowedmodes = ('message', 'template', 'merge', 'squash', 'commit')
messagefile = sys.argv[1]
mode = None
sha = None
if len(sys.argv) > 2:
mode = sys.argv[2]
if len(sys.argv) > 3:
sha = sys.argv[3]
if mode is not None and mode not in allowedmodes:
raise ValueError('Invalid Second Argument: mode')
if mode != 'commit' and sha is not None:
raise ValueError('Invalid Third Argument')
if mode == 'commit' and sha is None:
raise ValueError('Missing Third Argument')
resolver = gitinforesolver()
resolver.messagefile = messagefile
resolver.mode = mode
resolver.sha = sha
return resolver