Show More
@@ -1,748 +1,748 b'' | |||
|
1 | 1 | # hgweb/webutil.py - utility library for the web interface. |
|
2 | 2 | # |
|
3 | 3 | # Copyright 21 May 2005 - (c) 2005 Jake Edge <jake@edge2.net> |
|
4 | 4 | # Copyright 2005-2007 Matt Mackall <mpm@selenic.com> |
|
5 | 5 | # |
|
6 | 6 | # This software may be used and distributed according to the terms of the |
|
7 | 7 | # GNU General Public License version 2 or any later version. |
|
8 | 8 | |
|
9 | 9 | from __future__ import absolute_import |
|
10 | 10 | |
|
11 | 11 | import copy |
|
12 | 12 | import difflib |
|
13 | 13 | import os |
|
14 | 14 | import re |
|
15 | 15 | |
|
16 | 16 | from ..i18n import _ |
|
17 | 17 | from ..node import hex, nullid, short |
|
18 | 18 | |
|
19 | 19 | from .common import ( |
|
20 | 20 | ErrorResponse, |
|
21 | 21 | HTTP_BAD_REQUEST, |
|
22 | 22 | HTTP_NOT_FOUND, |
|
23 | 23 | paritygen, |
|
24 | 24 | ) |
|
25 | 25 | |
|
26 | 26 | from .. import ( |
|
27 | 27 | context, |
|
28 | 28 | error, |
|
29 | 29 | match, |
|
30 | 30 | mdiff, |
|
31 | 31 | obsutil, |
|
32 | 32 | patch, |
|
33 | 33 | pathutil, |
|
34 | 34 | pycompat, |
|
35 | 35 | scmutil, |
|
36 | 36 | templatefilters, |
|
37 | 37 | templatekw, |
|
38 | 38 | templateutil, |
|
39 | 39 | ui as uimod, |
|
40 | 40 | util, |
|
41 | 41 | ) |
|
42 | 42 | |
|
43 | 43 | from ..utils import ( |
|
44 | 44 | stringutil, |
|
45 | 45 | ) |
|
46 | 46 | |
|
47 | 47 | archivespecs = util.sortdict(( |
|
48 | 48 | ('zip', ('application/zip', 'zip', '.zip', None)), |
|
49 | 49 | ('gz', ('application/x-gzip', 'tgz', '.tar.gz', None)), |
|
50 | 50 | ('bz2', ('application/x-bzip2', 'tbz2', '.tar.bz2', None)), |
|
51 | 51 | )) |
|
52 | 52 | |
|
53 | 53 | def archivelist(ui, nodeid, url=None): |
|
54 | 54 | allowed = ui.configlist('web', 'allow_archive', untrusted=True) |
|
55 | 55 | archives = [] |
|
56 | 56 | |
|
57 | 57 | for typ, spec in archivespecs.iteritems(): |
|
58 | 58 | if typ in allowed or ui.configbool('web', 'allow' + typ, |
|
59 | 59 | untrusted=True): |
|
60 | 60 | archives.append({ |
|
61 | 61 | 'type': typ, |
|
62 | 62 | 'extension': spec[2], |
|
63 | 63 | 'node': nodeid, |
|
64 | 64 | 'url': url, |
|
65 | 65 | }) |
|
66 | 66 | |
|
67 | 67 | return templateutil.mappinglist(archives) |
|
68 | 68 | |
|
69 | 69 | def up(p): |
|
70 | 70 | if p[0:1] != "/": |
|
71 | 71 | p = "/" + p |
|
72 | 72 | if p[-1:] == "/": |
|
73 | 73 | p = p[:-1] |
|
74 | 74 | up = os.path.dirname(p) |
|
75 | 75 | if up == "/": |
|
76 | 76 | return "/" |
|
77 | 77 | return up + "/" |
|
78 | 78 | |
|
79 | 79 | def _navseq(step, firststep=None): |
|
80 | 80 | if firststep: |
|
81 | 81 | yield firststep |
|
82 | 82 | if firststep >= 20 and firststep <= 40: |
|
83 | 83 | firststep = 50 |
|
84 | 84 | yield firststep |
|
85 | 85 | assert step > 0 |
|
86 | 86 | assert firststep > 0 |
|
87 | 87 | while step <= firststep: |
|
88 | 88 | step *= 10 |
|
89 | 89 | while True: |
|
90 | 90 | yield 1 * step |
|
91 | 91 | yield 3 * step |
|
92 | 92 | step *= 10 |
|
93 | 93 | |
|
94 | 94 | class revnav(object): |
|
95 | 95 | |
|
96 | 96 | def __init__(self, repo): |
|
97 | 97 | """Navigation generation object |
|
98 | 98 | |
|
99 | 99 | :repo: repo object we generate nav for |
|
100 | 100 | """ |
|
101 | 101 | # used for hex generation |
|
102 | 102 | self._revlog = repo.changelog |
|
103 | 103 | |
|
104 | 104 | def __nonzero__(self): |
|
105 | 105 | """return True if any revision to navigate over""" |
|
106 | 106 | return self._first() is not None |
|
107 | 107 | |
|
108 | 108 | __bool__ = __nonzero__ |
|
109 | 109 | |
|
110 | 110 | def _first(self): |
|
111 | 111 | """return the minimum non-filtered changeset or None""" |
|
112 | 112 | try: |
|
113 | 113 | return next(iter(self._revlog)) |
|
114 | 114 | except StopIteration: |
|
115 | 115 | return None |
|
116 | 116 | |
|
117 | 117 | def hex(self, rev): |
|
118 | 118 | return hex(self._revlog.node(rev)) |
|
119 | 119 | |
|
120 | 120 | def gen(self, pos, pagelen, limit): |
|
121 | 121 | """computes label and revision id for navigation link |
|
122 | 122 | |
|
123 | 123 | :pos: is the revision relative to which we generate navigation. |
|
124 | 124 | :pagelen: the size of each navigation page |
|
125 | 125 | :limit: how far shall we link |
|
126 | 126 | |
|
127 | 127 | The return is: |
|
128 | 128 | - a single element mappinglist |
|
129 | 129 | - containing a dictionary with a `before` and `after` key |
|
130 | 130 | - values are dictionaries with `label` and `node` keys |
|
131 | 131 | """ |
|
132 | 132 | if not self: |
|
133 | 133 | # empty repo |
|
134 | 134 | return templateutil.mappinglist([ |
|
135 | 135 | {'before': templateutil.mappinglist([]), |
|
136 | 136 | 'after': templateutil.mappinglist([])}, |
|
137 | 137 | ]) |
|
138 | 138 | |
|
139 | 139 | targets = [] |
|
140 | 140 | for f in _navseq(1, pagelen): |
|
141 | 141 | if f > limit: |
|
142 | 142 | break |
|
143 | 143 | targets.append(pos + f) |
|
144 | 144 | targets.append(pos - f) |
|
145 | 145 | targets.sort() |
|
146 | 146 | |
|
147 | 147 | first = self._first() |
|
148 | 148 | navbefore = [{'label': '(%i)' % first, 'node': self.hex(first)}] |
|
149 | 149 | navafter = [] |
|
150 | 150 | for rev in targets: |
|
151 | 151 | if rev not in self._revlog: |
|
152 | 152 | continue |
|
153 | 153 | if pos < rev < limit: |
|
154 | 154 | navafter.append({'label': '+%d' % abs(rev - pos), |
|
155 | 155 | 'node': self.hex(rev)}) |
|
156 | 156 | if 0 < rev < pos: |
|
157 | 157 | navbefore.append({'label': '-%d' % abs(rev - pos), |
|
158 | 158 | 'node': self.hex(rev)}) |
|
159 | 159 | |
|
160 | 160 | navafter.append({'label': 'tip', 'node': 'tip'}) |
|
161 | 161 | |
|
162 | 162 | # TODO: maybe this can be a scalar object supporting tomap() |
|
163 | 163 | return templateutil.mappinglist([ |
|
164 | 164 | {'before': templateutil.mappinglist(navbefore), |
|
165 | 165 | 'after': templateutil.mappinglist(navafter)}, |
|
166 | 166 | ]) |
|
167 | 167 | |
|
168 | 168 | class filerevnav(revnav): |
|
169 | 169 | |
|
170 | 170 | def __init__(self, repo, path): |
|
171 | 171 | """Navigation generation object |
|
172 | 172 | |
|
173 | 173 | :repo: repo object we generate nav for |
|
174 | 174 | :path: path of the file we generate nav for |
|
175 | 175 | """ |
|
176 | 176 | # used for iteration |
|
177 | 177 | self._changelog = repo.unfiltered().changelog |
|
178 | 178 | # used for hex generation |
|
179 | 179 | self._revlog = repo.file(path) |
|
180 | 180 | |
|
181 | 181 | def hex(self, rev): |
|
182 | 182 | return hex(self._changelog.node(self._revlog.linkrev(rev))) |
|
183 | 183 | |
|
184 | 184 | # TODO: maybe this can be a wrapper class for changectx/filectx list, which |
|
185 | 185 | # yields {'ctx': ctx} |
|
186 | 186 | def _ctxsgen(context, ctxs): |
|
187 | 187 | for s in ctxs: |
|
188 | 188 | d = { |
|
189 | 189 | 'node': s.hex(), |
|
190 | 190 | 'rev': s.rev(), |
|
191 | 191 | 'user': s.user(), |
|
192 | 192 | 'date': s.date(), |
|
193 | 193 | 'description': s.description(), |
|
194 | 194 | 'branch': s.branch(), |
|
195 | 195 | } |
|
196 | 196 | if util.safehasattr(s, 'path'): |
|
197 | 197 | d['file'] = s.path() |
|
198 | 198 | yield d |
|
199 | 199 | |
|
200 | 200 | def _siblings(siblings=None, hiderev=None): |
|
201 | 201 | if siblings is None: |
|
202 | 202 | siblings = [] |
|
203 | 203 | siblings = [s for s in siblings if s.node() != nullid] |
|
204 | 204 | if len(siblings) == 1 and siblings[0].rev() == hiderev: |
|
205 | 205 | siblings = [] |
|
206 | 206 | return templateutil.mappinggenerator(_ctxsgen, args=(siblings,)) |
|
207 | 207 | |
|
208 | 208 | def difffeatureopts(req, ui, section): |
|
209 | 209 | diffopts = patch.difffeatureopts(ui, untrusted=True, |
|
210 | 210 | section=section, whitespace=True) |
|
211 | 211 | |
|
212 | 212 | for k in ('ignorews', 'ignorewsamount', 'ignorewseol', 'ignoreblanklines'): |
|
213 | 213 | v = req.qsparams.get(k) |
|
214 | 214 | if v is not None: |
|
215 | 215 | v = stringutil.parsebool(v) |
|
216 | 216 | setattr(diffopts, k, v if v is not None else True) |
|
217 | 217 | |
|
218 | 218 | return diffopts |
|
219 | 219 | |
|
220 | 220 | def annotate(req, fctx, ui): |
|
221 | 221 | diffopts = difffeatureopts(req, ui, 'annotate') |
|
222 | 222 | return fctx.annotate(follow=True, diffopts=diffopts) |
|
223 | 223 | |
|
224 | 224 | def parents(ctx, hide=None): |
|
225 | 225 | if isinstance(ctx, context.basefilectx): |
|
226 | 226 | introrev = ctx.introrev() |
|
227 | 227 | if ctx.changectx().rev() != introrev: |
|
228 | 228 | return _siblings([ctx.repo()[introrev]], hide) |
|
229 | 229 | return _siblings(ctx.parents(), hide) |
|
230 | 230 | |
|
231 | 231 | def children(ctx, hide=None): |
|
232 | 232 | return _siblings(ctx.children(), hide) |
|
233 | 233 | |
|
234 | 234 | def renamelink(fctx): |
|
235 | 235 | r = fctx.renamed() |
|
236 | 236 | if r: |
|
237 | 237 | return templateutil.mappinglist([{'file': r[0], 'node': hex(r[1])}]) |
|
238 | 238 | return templateutil.mappinglist([]) |
|
239 | 239 | |
|
240 | 240 | def nodetagsdict(repo, node): |
|
241 | 241 | return templateutil.hybridlist(repo.nodetags(node), name='name') |
|
242 | 242 | |
|
243 | 243 | def nodebookmarksdict(repo, node): |
|
244 | 244 | return templateutil.hybridlist(repo.nodebookmarks(node), name='name') |
|
245 | 245 | |
|
246 | 246 | def nodebranchdict(repo, ctx): |
|
247 | 247 | branches = [] |
|
248 | 248 | branch = ctx.branch() |
|
249 | 249 | # If this is an empty repo, ctx.node() == nullid, |
|
250 | 250 | # ctx.branch() == 'default'. |
|
251 | 251 | try: |
|
252 | 252 | branchnode = repo.branchtip(branch) |
|
253 | 253 | except error.RepoLookupError: |
|
254 | 254 | branchnode = None |
|
255 | 255 | if branchnode == ctx.node(): |
|
256 | 256 | branches.append(branch) |
|
257 | 257 | return templateutil.hybridlist(branches, name='name') |
|
258 | 258 | |
|
259 | 259 | def nodeinbranch(repo, ctx): |
|
260 | 260 | branches = [] |
|
261 | 261 | branch = ctx.branch() |
|
262 | 262 | try: |
|
263 | 263 | branchnode = repo.branchtip(branch) |
|
264 | 264 | except error.RepoLookupError: |
|
265 | 265 | branchnode = None |
|
266 | 266 | if branch != 'default' and branchnode != ctx.node(): |
|
267 | 267 | branches.append(branch) |
|
268 | 268 | return templateutil.hybridlist(branches, name='name') |
|
269 | 269 | |
|
270 | 270 | def nodebranchnodefault(ctx): |
|
271 | 271 | branches = [] |
|
272 | 272 | branch = ctx.branch() |
|
273 | 273 | if branch != 'default': |
|
274 | 274 | branches.append(branch) |
|
275 | 275 | return templateutil.hybridlist(branches, name='name') |
|
276 | 276 | |
|
277 | 277 | def _nodenamesgen(context, f, node, name): |
|
278 | 278 | for t in f(node): |
|
279 | 279 | yield {name: t} |
|
280 | 280 | |
|
281 | 281 | def showtag(repo, t1, node=nullid): |
|
282 | 282 | args = (repo.nodetags, node, 'tag') |
|
283 | 283 | return templateutil.mappinggenerator(_nodenamesgen, args=args, name=t1) |
|
284 | 284 | |
|
285 | 285 | def showbookmark(repo, t1, node=nullid): |
|
286 | 286 | args = (repo.nodebookmarks, node, 'bookmark') |
|
287 | 287 | return templateutil.mappinggenerator(_nodenamesgen, args=args, name=t1) |
|
288 | 288 | |
|
289 | 289 | def branchentries(repo, stripecount, limit=0): |
|
290 | 290 | tips = [] |
|
291 | 291 | heads = repo.heads() |
|
292 | 292 | parity = paritygen(stripecount) |
|
293 | 293 | sortkey = lambda item: (not item[1], item[0].rev()) |
|
294 | 294 | |
|
295 | 295 | def entries(context): |
|
296 | 296 | count = 0 |
|
297 | 297 | if not tips: |
|
298 | 298 | for tag, hs, tip, closed in repo.branchmap().iterbranches(): |
|
299 | 299 | tips.append((repo[tip], closed)) |
|
300 | 300 | for ctx, closed in sorted(tips, key=sortkey, reverse=True): |
|
301 | 301 | if limit > 0 and count >= limit: |
|
302 | 302 | return |
|
303 | 303 | count += 1 |
|
304 | 304 | if closed: |
|
305 | 305 | status = 'closed' |
|
306 | 306 | elif ctx.node() not in heads: |
|
307 | 307 | status = 'inactive' |
|
308 | 308 | else: |
|
309 | 309 | status = 'open' |
|
310 | 310 | yield { |
|
311 | 311 | 'parity': next(parity), |
|
312 | 312 | 'branch': ctx.branch(), |
|
313 | 313 | 'status': status, |
|
314 | 314 | 'node': ctx.hex(), |
|
315 | 315 | 'date': ctx.date() |
|
316 | 316 | } |
|
317 | 317 | |
|
318 | 318 | return templateutil.mappinggenerator(entries) |
|
319 | 319 | |
|
320 | 320 | def cleanpath(repo, path): |
|
321 | 321 | path = path.lstrip('/') |
|
322 | 322 | return pathutil.canonpath(repo.root, '', path) |
|
323 | 323 | |
|
324 | 324 | def changectx(repo, req): |
|
325 | 325 | changeid = "tip" |
|
326 | 326 | if 'node' in req.qsparams: |
|
327 | 327 | changeid = req.qsparams['node'] |
|
328 | 328 | ipos = changeid.find(':') |
|
329 | 329 | if ipos != -1: |
|
330 | 330 | changeid = changeid[(ipos + 1):] |
|
331 | 331 | |
|
332 | 332 | return scmutil.revsymbol(repo, changeid) |
|
333 | 333 | |
|
334 | 334 | def basechangectx(repo, req): |
|
335 | 335 | if 'node' in req.qsparams: |
|
336 | 336 | changeid = req.qsparams['node'] |
|
337 | 337 | ipos = changeid.find(':') |
|
338 | 338 | if ipos != -1: |
|
339 | 339 | changeid = changeid[:ipos] |
|
340 | 340 | return scmutil.revsymbol(repo, changeid) |
|
341 | 341 | |
|
342 | 342 | return None |
|
343 | 343 | |
|
344 | 344 | def filectx(repo, req): |
|
345 | 345 | if 'file' not in req.qsparams: |
|
346 | 346 | raise ErrorResponse(HTTP_NOT_FOUND, 'file not given') |
|
347 | 347 | path = cleanpath(repo, req.qsparams['file']) |
|
348 | 348 | if 'node' in req.qsparams: |
|
349 | 349 | changeid = req.qsparams['node'] |
|
350 | 350 | elif 'filenode' in req.qsparams: |
|
351 | 351 | changeid = req.qsparams['filenode'] |
|
352 | 352 | else: |
|
353 | 353 | raise ErrorResponse(HTTP_NOT_FOUND, 'node or filenode not given') |
|
354 | 354 | try: |
|
355 | 355 | fctx = scmutil.revsymbol(repo, changeid)[path] |
|
356 | 356 | except error.RepoError: |
|
357 | 357 | fctx = repo.filectx(path, fileid=changeid) |
|
358 | 358 | |
|
359 | 359 | return fctx |
|
360 | 360 | |
|
361 | 361 | def linerange(req): |
|
362 | 362 | linerange = req.qsparams.getall('linerange') |
|
363 | 363 | if not linerange: |
|
364 | 364 | return None |
|
365 | 365 | if len(linerange) > 1: |
|
366 | 366 | raise ErrorResponse(HTTP_BAD_REQUEST, |
|
367 | 367 | 'redundant linerange parameter') |
|
368 | 368 | try: |
|
369 | 369 | fromline, toline = map(int, linerange[0].split(':', 1)) |
|
370 | 370 | except ValueError: |
|
371 | 371 | raise ErrorResponse(HTTP_BAD_REQUEST, |
|
372 | 372 | 'invalid linerange parameter') |
|
373 | 373 | try: |
|
374 | 374 | return util.processlinerange(fromline, toline) |
|
375 | 375 | except error.ParseError as exc: |
|
376 | 376 | raise ErrorResponse(HTTP_BAD_REQUEST, pycompat.bytestr(exc)) |
|
377 | 377 | |
|
378 | 378 | def formatlinerange(fromline, toline): |
|
379 | 379 | return '%d:%d' % (fromline + 1, toline) |
|
380 | 380 | |
|
381 | 381 | def _succsandmarkersgen(context, mapping): |
|
382 | 382 | repo = context.resource(mapping, 'repo') |
|
383 | 383 | itemmappings = templatekw.showsuccsandmarkers(context, mapping) |
|
384 | 384 | for item in itemmappings.tovalue(context, mapping): |
|
385 | 385 | item['successors'] = _siblings(repo[successor] |
|
386 | 386 | for successor in item['successors']) |
|
387 | 387 | yield item |
|
388 | 388 | |
|
389 | 389 | def succsandmarkers(context, mapping): |
|
390 | 390 | return templateutil.mappinggenerator(_succsandmarkersgen, args=(mapping,)) |
|
391 | 391 | |
|
392 | 392 | # teach templater succsandmarkers is switched to (context, mapping) API |
|
393 | 393 | succsandmarkers._requires = {'repo', 'ctx'} |
|
394 | 394 | |
|
395 | 395 | def _whyunstablegen(context, mapping): |
|
396 | 396 | repo = context.resource(mapping, 'repo') |
|
397 | 397 | ctx = context.resource(mapping, 'ctx') |
|
398 | 398 | |
|
399 | 399 | entries = obsutil.whyunstable(repo, ctx) |
|
400 | 400 | for entry in entries: |
|
401 | 401 | if entry.get('divergentnodes'): |
|
402 | 402 | entry['divergentnodes'] = _siblings(entry['divergentnodes']) |
|
403 | 403 | yield entry |
|
404 | 404 | |
|
405 | 405 | def whyunstable(context, mapping): |
|
406 | 406 | return templateutil.mappinggenerator(_whyunstablegen, args=(mapping,)) |
|
407 | 407 | |
|
408 | 408 | whyunstable._requires = {'repo', 'ctx'} |
|
409 | 409 | |
|
410 | 410 | def commonentry(repo, ctx): |
|
411 | 411 | node = ctx.node() |
|
412 | 412 | return { |
|
413 | 413 | # TODO: perhaps ctx.changectx() should be assigned if ctx is a |
|
414 | 414 | # filectx, but I'm not pretty sure if that would always work because |
|
415 | 415 | # fctx.parents() != fctx.changectx.parents() for example. |
|
416 | 416 | 'ctx': ctx, |
|
417 | 417 | 'rev': ctx.rev(), |
|
418 | 418 | 'node': hex(node), |
|
419 | 419 | 'author': ctx.user(), |
|
420 | 420 | 'desc': ctx.description(), |
|
421 | 421 | 'date': ctx.date(), |
|
422 | 422 | 'extra': ctx.extra(), |
|
423 | 423 | 'phase': ctx.phasestr(), |
|
424 | 424 | 'obsolete': ctx.obsolete(), |
|
425 | 425 | 'succsandmarkers': succsandmarkers, |
|
426 | 426 | 'instabilities': templateutil.hybridlist(ctx.instabilities(), |
|
427 | 427 | name='instability'), |
|
428 | 428 | 'whyunstable': whyunstable, |
|
429 | 429 | 'branch': nodebranchnodefault(ctx), |
|
430 | 430 | 'inbranch': nodeinbranch(repo, ctx), |
|
431 | 431 | 'branches': nodebranchdict(repo, ctx), |
|
432 | 432 | 'tags': nodetagsdict(repo, node), |
|
433 | 433 | 'bookmarks': nodebookmarksdict(repo, node), |
|
434 | 434 | 'parent': lambda **x: parents(ctx), |
|
435 | 435 | 'child': lambda **x: children(ctx), |
|
436 | 436 | } |
|
437 | 437 | |
|
438 | 438 | def changelistentry(web, ctx): |
|
439 | 439 | '''Obtain a dictionary to be used for entries in a changelist. |
|
440 | 440 | |
|
441 | 441 | This function is called when producing items for the "entries" list passed |
|
442 | 442 | to the "shortlog" and "changelog" templates. |
|
443 | 443 | ''' |
|
444 | 444 | repo = web.repo |
|
445 | 445 | rev = ctx.rev() |
|
446 | 446 | n = ctx.node() |
|
447 | 447 | showtags = showtag(repo, 'changelogtag', n) |
|
448 | 448 | files = listfilediffs(ctx.files(), n, web.maxfiles) |
|
449 | 449 | |
|
450 | 450 | entry = commonentry(repo, ctx) |
|
451 | 451 | entry.update( |
|
452 | 452 | allparents=lambda **x: parents(ctx), |
|
453 | 453 | parent=lambda **x: parents(ctx, rev - 1), |
|
454 | 454 | child=lambda **x: children(ctx, rev + 1), |
|
455 | 455 | changelogtag=showtags, |
|
456 | 456 | files=files, |
|
457 | 457 | ) |
|
458 | 458 | return entry |
|
459 | 459 | |
|
460 | 460 | def symrevorshortnode(req, ctx): |
|
461 | 461 | if 'node' in req.qsparams: |
|
462 | 462 | return templatefilters.revescape(req.qsparams['node']) |
|
463 | 463 | else: |
|
464 | 464 | return short(ctx.node()) |
|
465 | 465 | |
|
466 | 466 | def _listfilesgen(context, ctx, stripecount): |
|
467 | 467 | parity = paritygen(stripecount) |
|
468 | 468 | for blockno, f in enumerate(ctx.files()): |
|
469 | 469 | template = 'filenodelink' if f in ctx else 'filenolink' |
|
470 | 470 | yield context.process(template, { |
|
471 | 471 | 'node': ctx.hex(), |
|
472 | 472 | 'file': f, |
|
473 | 473 | 'blockno': blockno + 1, |
|
474 | 474 | 'parity': next(parity), |
|
475 | 475 | }) |
|
476 | 476 | |
|
477 | 477 | def changesetentry(web, ctx): |
|
478 | 478 | '''Obtain a dictionary to be used to render the "changeset" template.''' |
|
479 | 479 | |
|
480 | 480 | showtags = showtag(web.repo, 'changesettag', ctx.node()) |
|
481 | 481 | showbookmarks = showbookmark(web.repo, 'changesetbookmark', ctx.node()) |
|
482 | 482 | showbranch = nodebranchnodefault(ctx) |
|
483 | 483 | |
|
484 | 484 | basectx = basechangectx(web.repo, web.req) |
|
485 | 485 | if basectx is None: |
|
486 | 486 | basectx = ctx.p1() |
|
487 | 487 | |
|
488 | 488 | style = web.config('web', 'style') |
|
489 | 489 | if 'style' in web.req.qsparams: |
|
490 | 490 | style = web.req.qsparams['style'] |
|
491 | 491 | |
|
492 | 492 | diff = diffs(web, ctx, basectx, None, style) |
|
493 | 493 | |
|
494 | 494 | parity = paritygen(web.stripecount) |
|
495 | 495 | diffstatsgen = diffstatgen(ctx, basectx) |
|
496 | 496 | diffstats = diffstat(web.tmpl, ctx, diffstatsgen, parity) |
|
497 | 497 | |
|
498 | 498 | return dict( |
|
499 | 499 | diff=diff, |
|
500 | 500 | symrev=symrevorshortnode(web.req, ctx), |
|
501 | 501 | basenode=basectx.hex(), |
|
502 | 502 | changesettag=showtags, |
|
503 | 503 | changesetbookmark=showbookmarks, |
|
504 | 504 | changesetbranch=showbranch, |
|
505 | 505 | files=templateutil.mappedgenerator(_listfilesgen, |
|
506 | 506 | args=(ctx, web.stripecount)), |
|
507 | 507 | diffsummary=lambda **x: diffsummary(diffstatsgen), |
|
508 | 508 | diffstat=diffstats, |
|
509 | 509 | archives=web.archivelist(ctx.hex()), |
|
510 | 510 | **pycompat.strkwargs(commonentry(web.repo, ctx))) |
|
511 | 511 | |
|
512 | 512 | def _listfilediffsgen(context, files, node, max): |
|
513 | 513 | for f in files[:max]: |
|
514 | 514 | yield context.process('filedifflink', {'node': hex(node), 'file': f}) |
|
515 | 515 | if len(files) > max: |
|
516 | 516 | yield context.process('fileellipses', {}) |
|
517 | 517 | |
|
518 | 518 | def listfilediffs(files, node, max): |
|
519 | 519 | return templateutil.mappedgenerator(_listfilediffsgen, |
|
520 | 520 | args=(files, node, max)) |
|
521 | 521 | |
|
522 | def diffs(web, ctx, basectx, files, style, linerange=None, | |
|
523 | lineidprefix=''): | |
|
524 | ||
|
525 | def prettyprintlines(lines, blockno): | |
|
522 | def _prettyprintdifflines(tmpl, lines, blockno, lineidprefix): | |
|
526 | 523 |
|
|
527 | 524 |
|
|
528 | 525 |
|
|
529 | 526 |
|
|
530 | 527 |
|
|
531 | 528 |
|
|
532 | 529 |
|
|
533 | 530 |
|
|
534 | 531 |
|
|
535 | 532 |
|
|
536 |
|
|
|
533 | yield tmpl.generate(ltype, { | |
|
537 | 534 |
|
|
538 | 535 |
|
|
539 | 536 |
|
|
540 | 537 |
|
|
541 | 538 |
|
|
542 | 539 | |
|
540 | def diffs(web, ctx, basectx, files, style, linerange=None, | |
|
541 | lineidprefix=''): | |
|
543 | 542 | repo = web.repo |
|
544 | 543 | if files: |
|
545 | 544 | m = match.exact(repo.root, repo.getcwd(), files) |
|
546 | 545 | else: |
|
547 | 546 | m = match.always(repo.root, repo.getcwd()) |
|
548 | 547 | |
|
549 | 548 | diffopts = patch.diffopts(repo.ui, untrusted=True) |
|
550 | 549 | node1 = basectx.node() |
|
551 | 550 | node2 = ctx.node() |
|
552 | 551 | parity = paritygen(web.stripecount) |
|
553 | 552 | |
|
554 | 553 | diffhunks = patch.diffhunks(repo, node1, node2, m, opts=diffopts) |
|
555 | 554 | for blockno, (fctx1, fctx2, header, hunks) in enumerate(diffhunks, 1): |
|
556 | 555 | if style != 'raw': |
|
557 | 556 | header = header[1:] |
|
558 | 557 | lines = [h + '\n' for h in header] |
|
559 | 558 | for hunkrange, hunklines in hunks: |
|
560 | 559 | if linerange is not None and hunkrange is not None: |
|
561 | 560 | s1, l1, s2, l2 = hunkrange |
|
562 | 561 | if not mdiff.hunkinrange((s2, l2), linerange): |
|
563 | 562 | continue |
|
564 | 563 | lines.extend(hunklines) |
|
565 | 564 | if lines: |
|
566 | 565 | yield web.tmpl.generate('diffblock', { |
|
567 | 566 | 'parity': next(parity), |
|
568 | 567 | 'blockno': blockno, |
|
569 |
'lines': prettyprintlines(lines, blockno |
|
|
568 | 'lines': _prettyprintdifflines(web.tmpl, lines, blockno, | |
|
569 | lineidprefix), | |
|
570 | 570 | }) |
|
571 | 571 | |
|
572 | 572 | def compare(tmpl, context, leftlines, rightlines): |
|
573 | 573 | '''Generator function that provides side-by-side comparison data.''' |
|
574 | 574 | |
|
575 | 575 | def compline(type, leftlineno, leftline, rightlineno, rightline): |
|
576 | 576 | lineid = leftlineno and ("l%d" % leftlineno) or '' |
|
577 | 577 | lineid += rightlineno and ("r%d" % rightlineno) or '' |
|
578 | 578 | llno = '%d' % leftlineno if leftlineno else '' |
|
579 | 579 | rlno = '%d' % rightlineno if rightlineno else '' |
|
580 | 580 | return tmpl.generate('comparisonline', { |
|
581 | 581 | 'type': type, |
|
582 | 582 | 'lineid': lineid, |
|
583 | 583 | 'leftlineno': leftlineno, |
|
584 | 584 | 'leftlinenumber': "% 6s" % llno, |
|
585 | 585 | 'leftline': leftline or '', |
|
586 | 586 | 'rightlineno': rightlineno, |
|
587 | 587 | 'rightlinenumber': "% 6s" % rlno, |
|
588 | 588 | 'rightline': rightline or '', |
|
589 | 589 | }) |
|
590 | 590 | |
|
591 | 591 | def getblock(opcodes): |
|
592 | 592 | for type, llo, lhi, rlo, rhi in opcodes: |
|
593 | 593 | len1 = lhi - llo |
|
594 | 594 | len2 = rhi - rlo |
|
595 | 595 | count = min(len1, len2) |
|
596 | 596 | for i in xrange(count): |
|
597 | 597 | yield compline(type=type, |
|
598 | 598 | leftlineno=llo + i + 1, |
|
599 | 599 | leftline=leftlines[llo + i], |
|
600 | 600 | rightlineno=rlo + i + 1, |
|
601 | 601 | rightline=rightlines[rlo + i]) |
|
602 | 602 | if len1 > len2: |
|
603 | 603 | for i in xrange(llo + count, lhi): |
|
604 | 604 | yield compline(type=type, |
|
605 | 605 | leftlineno=i + 1, |
|
606 | 606 | leftline=leftlines[i], |
|
607 | 607 | rightlineno=None, |
|
608 | 608 | rightline=None) |
|
609 | 609 | elif len2 > len1: |
|
610 | 610 | for i in xrange(rlo + count, rhi): |
|
611 | 611 | yield compline(type=type, |
|
612 | 612 | leftlineno=None, |
|
613 | 613 | leftline=None, |
|
614 | 614 | rightlineno=i + 1, |
|
615 | 615 | rightline=rightlines[i]) |
|
616 | 616 | |
|
617 | 617 | s = difflib.SequenceMatcher(None, leftlines, rightlines) |
|
618 | 618 | if context < 0: |
|
619 | 619 | yield tmpl.generate('comparisonblock', |
|
620 | 620 | {'lines': getblock(s.get_opcodes())}) |
|
621 | 621 | else: |
|
622 | 622 | for oc in s.get_grouped_opcodes(n=context): |
|
623 | 623 | yield tmpl.generate('comparisonblock', {'lines': getblock(oc)}) |
|
624 | 624 | |
|
625 | 625 | def diffstatgen(ctx, basectx): |
|
626 | 626 | '''Generator function that provides the diffstat data.''' |
|
627 | 627 | |
|
628 | 628 | stats = patch.diffstatdata( |
|
629 | 629 | util.iterlines(ctx.diff(basectx, noprefix=False))) |
|
630 | 630 | maxname, maxtotal, addtotal, removetotal, binary = patch.diffstatsum(stats) |
|
631 | 631 | while True: |
|
632 | 632 | yield stats, maxname, maxtotal, addtotal, removetotal, binary |
|
633 | 633 | |
|
634 | 634 | def diffsummary(statgen): |
|
635 | 635 | '''Return a short summary of the diff.''' |
|
636 | 636 | |
|
637 | 637 | stats, maxname, maxtotal, addtotal, removetotal, binary = next(statgen) |
|
638 | 638 | return _(' %d files changed, %d insertions(+), %d deletions(-)\n') % ( |
|
639 | 639 | len(stats), addtotal, removetotal) |
|
640 | 640 | |
|
641 | 641 | def diffstat(tmpl, ctx, statgen, parity): |
|
642 | 642 | '''Return a diffstat template for each file in the diff.''' |
|
643 | 643 | |
|
644 | 644 | stats, maxname, maxtotal, addtotal, removetotal, binary = next(statgen) |
|
645 | 645 | files = ctx.files() |
|
646 | 646 | |
|
647 | 647 | def pct(i): |
|
648 | 648 | if maxtotal == 0: |
|
649 | 649 | return 0 |
|
650 | 650 | return (float(i) / maxtotal) * 100 |
|
651 | 651 | |
|
652 | 652 | fileno = 0 |
|
653 | 653 | for filename, adds, removes, isbinary in stats: |
|
654 | 654 | template = 'diffstatlink' if filename in files else 'diffstatnolink' |
|
655 | 655 | total = adds + removes |
|
656 | 656 | fileno += 1 |
|
657 | 657 | yield tmpl.generate(template, { |
|
658 | 658 | 'node': ctx.hex(), |
|
659 | 659 | 'file': filename, |
|
660 | 660 | 'fileno': fileno, |
|
661 | 661 | 'total': total, |
|
662 | 662 | 'addpct': pct(adds), |
|
663 | 663 | 'removepct': pct(removes), |
|
664 | 664 | 'parity': next(parity), |
|
665 | 665 | }) |
|
666 | 666 | |
|
667 | 667 | class sessionvars(templateutil.wrapped): |
|
668 | 668 | def __init__(self, vars, start='?'): |
|
669 | 669 | self._start = start |
|
670 | 670 | self._vars = vars |
|
671 | 671 | |
|
672 | 672 | def __getitem__(self, key): |
|
673 | 673 | return self._vars[key] |
|
674 | 674 | |
|
675 | 675 | def __setitem__(self, key, value): |
|
676 | 676 | self._vars[key] = value |
|
677 | 677 | |
|
678 | 678 | def __copy__(self): |
|
679 | 679 | return sessionvars(copy.copy(self._vars), self._start) |
|
680 | 680 | |
|
681 | 681 | def itermaps(self, context): |
|
682 | 682 | separator = self._start |
|
683 | 683 | for key, value in sorted(self._vars.iteritems()): |
|
684 | 684 | yield {'name': key, |
|
685 | 685 | 'value': pycompat.bytestr(value), |
|
686 | 686 | 'separator': separator, |
|
687 | 687 | } |
|
688 | 688 | separator = '&' |
|
689 | 689 | |
|
690 | 690 | def join(self, context, mapping, sep): |
|
691 | 691 | # could be '{separator}{name}={value|urlescape}' |
|
692 | 692 | raise error.ParseError(_('not displayable without template')) |
|
693 | 693 | |
|
694 | 694 | def show(self, context, mapping): |
|
695 | 695 | return self.join(context, '') |
|
696 | 696 | |
|
697 | 697 | def tovalue(self, context, mapping): |
|
698 | 698 | return self._vars |
|
699 | 699 | |
|
700 | 700 | class wsgiui(uimod.ui): |
|
701 | 701 | # default termwidth breaks under mod_wsgi |
|
702 | 702 | def termwidth(self): |
|
703 | 703 | return 80 |
|
704 | 704 | |
|
705 | 705 | def getwebsubs(repo): |
|
706 | 706 | websubtable = [] |
|
707 | 707 | websubdefs = repo.ui.configitems('websub') |
|
708 | 708 | # we must maintain interhg backwards compatibility |
|
709 | 709 | websubdefs += repo.ui.configitems('interhg') |
|
710 | 710 | for key, pattern in websubdefs: |
|
711 | 711 | # grab the delimiter from the character after the "s" |
|
712 | 712 | unesc = pattern[1:2] |
|
713 | 713 | delim = re.escape(unesc) |
|
714 | 714 | |
|
715 | 715 | # identify portions of the pattern, taking care to avoid escaped |
|
716 | 716 | # delimiters. the replace format and flags are optional, but |
|
717 | 717 | # delimiters are required. |
|
718 | 718 | match = re.match( |
|
719 | 719 | br'^s%s(.+)(?:(?<=\\\\)|(?<!\\))%s(.*)%s([ilmsux])*$' |
|
720 | 720 | % (delim, delim, delim), pattern) |
|
721 | 721 | if not match: |
|
722 | 722 | repo.ui.warn(_("websub: invalid pattern for %s: %s\n") |
|
723 | 723 | % (key, pattern)) |
|
724 | 724 | continue |
|
725 | 725 | |
|
726 | 726 | # we need to unescape the delimiter for regexp and format |
|
727 | 727 | delim_re = re.compile(br'(?<!\\)\\%s' % delim) |
|
728 | 728 | regexp = delim_re.sub(unesc, match.group(1)) |
|
729 | 729 | format = delim_re.sub(unesc, match.group(2)) |
|
730 | 730 | |
|
731 | 731 | # the pattern allows for 6 regexp flags, so set them if necessary |
|
732 | 732 | flagin = match.group(3) |
|
733 | 733 | flags = 0 |
|
734 | 734 | if flagin: |
|
735 | 735 | for flag in flagin.upper(): |
|
736 | 736 | flags |= re.__dict__[flag] |
|
737 | 737 | |
|
738 | 738 | try: |
|
739 | 739 | regexp = re.compile(regexp, flags) |
|
740 | 740 | websubtable.append((regexp, format)) |
|
741 | 741 | except re.error: |
|
742 | 742 | repo.ui.warn(_("websub: invalid regexp for %s: %s\n") |
|
743 | 743 | % (key, regexp)) |
|
744 | 744 | return websubtable |
|
745 | 745 | |
|
746 | 746 | def getgraphnode(repo, ctx): |
|
747 | 747 | return (templatekw.getgraphnodecurrent(repo, ctx) + |
|
748 | 748 | templatekw.getgraphnodesymbol(ctx)) |
General Comments 0
You need to be logged in to leave comments.
Login now