Show More
@@ -1,463 +1,461 b'' | |||
|
1 | 1 | # hgweb/hgweb_mod.py - Web interface for a repository. |
|
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 contextlib |
|
12 | 12 | import os |
|
13 | 13 | |
|
14 | 14 | from .common import ( |
|
15 | 15 | ErrorResponse, |
|
16 | 16 | HTTP_BAD_REQUEST, |
|
17 | 17 | cspvalues, |
|
18 | 18 | permhooks, |
|
19 | 19 | statusmessage, |
|
20 | 20 | ) |
|
21 | 21 | |
|
22 | 22 | from .. import ( |
|
23 | 23 | encoding, |
|
24 | 24 | error, |
|
25 | 25 | formatter, |
|
26 | 26 | hg, |
|
27 | 27 | hook, |
|
28 | 28 | profiling, |
|
29 | 29 | pycompat, |
|
30 | 30 | registrar, |
|
31 | 31 | repoview, |
|
32 | 32 | templatefilters, |
|
33 | 33 | templater, |
|
34 | 34 | templateutil, |
|
35 | 35 | ui as uimod, |
|
36 | 36 | util, |
|
37 | 37 | wireprotoserver, |
|
38 | 38 | ) |
|
39 | 39 | |
|
40 | 40 | from . import ( |
|
41 | 41 | request as requestmod, |
|
42 | 42 | webcommands, |
|
43 | 43 | webutil, |
|
44 | 44 | wsgicgi, |
|
45 | 45 | ) |
|
46 | 46 | |
|
47 | 47 | def getstyle(req, configfn, templatepath): |
|
48 | 48 | styles = ( |
|
49 | 49 | req.qsparams.get('style', None), |
|
50 | 50 | configfn('web', 'style'), |
|
51 | 51 | 'paper', |
|
52 | 52 | ) |
|
53 | 53 | return styles, templater.stylemap(styles, templatepath) |
|
54 | 54 | |
|
55 | 55 | def makebreadcrumb(url, prefix=''): |
|
56 | 56 | '''Return a 'URL breadcrumb' list |
|
57 | 57 | |
|
58 | 58 | A 'URL breadcrumb' is a list of URL-name pairs, |
|
59 | 59 | corresponding to each of the path items on a URL. |
|
60 | 60 | This can be used to create path navigation entries. |
|
61 | 61 | ''' |
|
62 | 62 | if url.endswith('/'): |
|
63 | 63 | url = url[:-1] |
|
64 | 64 | if prefix: |
|
65 | 65 | url = '/' + prefix + url |
|
66 | 66 | relpath = url |
|
67 | 67 | if relpath.startswith('/'): |
|
68 | 68 | relpath = relpath[1:] |
|
69 | 69 | |
|
70 | 70 | breadcrumb = [] |
|
71 | 71 | urlel = url |
|
72 | 72 | pathitems = [''] + relpath.split('/') |
|
73 | 73 | for pathel in reversed(pathitems): |
|
74 | 74 | if not pathel or not urlel: |
|
75 | 75 | break |
|
76 | 76 | breadcrumb.append({'url': urlel, 'name': pathel}) |
|
77 | 77 | urlel = os.path.dirname(urlel) |
|
78 | 78 | return templateutil.mappinglist(reversed(breadcrumb)) |
|
79 | 79 | |
|
80 | 80 | class requestcontext(object): |
|
81 | 81 | """Holds state/context for an individual request. |
|
82 | 82 | |
|
83 | 83 | Servers can be multi-threaded. Holding state on the WSGI application |
|
84 | 84 | is prone to race conditions. Instances of this class exist to hold |
|
85 | 85 | mutable and race-free state for requests. |
|
86 | 86 | """ |
|
87 | 87 | def __init__(self, app, repo, req, res): |
|
88 | 88 | self.repo = repo |
|
89 | 89 | self.reponame = app.reponame |
|
90 | 90 | self.req = req |
|
91 | 91 | self.res = res |
|
92 | 92 | |
|
93 | self.archivespecs = webutil.archivespecs | |
|
94 | ||
|
95 | 93 | self.maxchanges = self.configint('web', 'maxchanges') |
|
96 | 94 | self.stripecount = self.configint('web', 'stripes') |
|
97 | 95 | self.maxshortchanges = self.configint('web', 'maxshortchanges') |
|
98 | 96 | self.maxfiles = self.configint('web', 'maxfiles') |
|
99 | 97 | self.allowpull = self.configbool('web', 'allow-pull') |
|
100 | 98 | |
|
101 | 99 | # we use untrusted=False to prevent a repo owner from using |
|
102 | 100 | # web.templates in .hg/hgrc to get access to any file readable |
|
103 | 101 | # by the user running the CGI script |
|
104 | 102 | self.templatepath = self.config('web', 'templates', untrusted=False) |
|
105 | 103 | |
|
106 | 104 | # This object is more expensive to build than simple config values. |
|
107 | 105 | # It is shared across requests. The app will replace the object |
|
108 | 106 | # if it is updated. Since this is a reference and nothing should |
|
109 | 107 | # modify the underlying object, it should be constant for the lifetime |
|
110 | 108 | # of the request. |
|
111 | 109 | self.websubtable = app.websubtable |
|
112 | 110 | |
|
113 | 111 | self.csp, self.nonce = cspvalues(self.repo.ui) |
|
114 | 112 | |
|
115 | 113 | # Trust the settings from the .hg/hgrc files by default. |
|
116 | 114 | def config(self, section, name, default=uimod._unset, untrusted=True): |
|
117 | 115 | return self.repo.ui.config(section, name, default, |
|
118 | 116 | untrusted=untrusted) |
|
119 | 117 | |
|
120 | 118 | def configbool(self, section, name, default=uimod._unset, untrusted=True): |
|
121 | 119 | return self.repo.ui.configbool(section, name, default, |
|
122 | 120 | untrusted=untrusted) |
|
123 | 121 | |
|
124 | 122 | def configint(self, section, name, default=uimod._unset, untrusted=True): |
|
125 | 123 | return self.repo.ui.configint(section, name, default, |
|
126 | 124 | untrusted=untrusted) |
|
127 | 125 | |
|
128 | 126 | def configlist(self, section, name, default=uimod._unset, untrusted=True): |
|
129 | 127 | return self.repo.ui.configlist(section, name, default, |
|
130 | 128 | untrusted=untrusted) |
|
131 | 129 | |
|
132 | 130 | def archivelist(self, nodeid): |
|
133 | 131 | allowed = self.configlist('web', 'allow_archive') |
|
134 |
for typ, spec in |
|
|
132 | for typ, spec in webutil.archivespecs.iteritems(): | |
|
135 | 133 | if typ in allowed or self.configbool('web', 'allow%s' % typ): |
|
136 | 134 | yield {'type': typ, 'extension': spec[2], 'node': nodeid} |
|
137 | 135 | |
|
138 | 136 | def templater(self, req): |
|
139 | 137 | # determine scheme, port and server name |
|
140 | 138 | # this is needed to create absolute urls |
|
141 | 139 | logourl = self.config('web', 'logourl') |
|
142 | 140 | logoimg = self.config('web', 'logoimg') |
|
143 | 141 | staticurl = (self.config('web', 'staticurl') |
|
144 | 142 | or req.apppath + '/static/') |
|
145 | 143 | if not staticurl.endswith('/'): |
|
146 | 144 | staticurl += '/' |
|
147 | 145 | |
|
148 | 146 | # some functions for the templater |
|
149 | 147 | |
|
150 | 148 | def motd(**map): |
|
151 | 149 | yield self.config('web', 'motd') |
|
152 | 150 | |
|
153 | 151 | # figure out which style to use |
|
154 | 152 | |
|
155 | 153 | vars = {} |
|
156 | 154 | styles, (style, mapfile) = getstyle(req, self.config, |
|
157 | 155 | self.templatepath) |
|
158 | 156 | if style == styles[0]: |
|
159 | 157 | vars['style'] = style |
|
160 | 158 | |
|
161 | 159 | sessionvars = webutil.sessionvars(vars, '?') |
|
162 | 160 | |
|
163 | 161 | if not self.reponame: |
|
164 | 162 | self.reponame = (self.config('web', 'name', '') |
|
165 | 163 | or req.reponame |
|
166 | 164 | or req.apppath |
|
167 | 165 | or self.repo.root) |
|
168 | 166 | |
|
169 | 167 | filters = {} |
|
170 | 168 | templatefilter = registrar.templatefilter(filters) |
|
171 | 169 | @templatefilter('websub', intype=bytes) |
|
172 | 170 | def websubfilter(text): |
|
173 | 171 | return templatefilters.websub(text, self.websubtable) |
|
174 | 172 | |
|
175 | 173 | # create the templater |
|
176 | 174 | # TODO: export all keywords: defaults = templatekw.keywords.copy() |
|
177 | 175 | defaults = { |
|
178 | 176 | 'url': req.apppath + '/', |
|
179 | 177 | 'logourl': logourl, |
|
180 | 178 | 'logoimg': logoimg, |
|
181 | 179 | 'staticurl': staticurl, |
|
182 | 180 | 'urlbase': req.advertisedbaseurl, |
|
183 | 181 | 'repo': self.reponame, |
|
184 | 182 | 'encoding': encoding.encoding, |
|
185 | 183 | 'motd': motd, |
|
186 | 184 | 'sessionvars': sessionvars, |
|
187 | 185 | 'pathdef': makebreadcrumb(req.apppath), |
|
188 | 186 | 'style': style, |
|
189 | 187 | 'nonce': self.nonce, |
|
190 | 188 | } |
|
191 | 189 | tres = formatter.templateresources(self.repo.ui, self.repo) |
|
192 | 190 | tmpl = templater.templater.frommapfile(mapfile, |
|
193 | 191 | filters=filters, |
|
194 | 192 | defaults=defaults, |
|
195 | 193 | resources=tres) |
|
196 | 194 | return tmpl |
|
197 | 195 | |
|
198 | 196 | def sendtemplate(self, name, **kwargs): |
|
199 | 197 | """Helper function to send a response generated from a template.""" |
|
200 | 198 | kwargs = pycompat.byteskwargs(kwargs) |
|
201 | 199 | self.res.setbodygen(self.tmpl.generate(name, kwargs)) |
|
202 | 200 | return self.res.sendresponse() |
|
203 | 201 | |
|
204 | 202 | class hgweb(object): |
|
205 | 203 | """HTTP server for individual repositories. |
|
206 | 204 | |
|
207 | 205 | Instances of this class serve HTTP responses for a particular |
|
208 | 206 | repository. |
|
209 | 207 | |
|
210 | 208 | Instances are typically used as WSGI applications. |
|
211 | 209 | |
|
212 | 210 | Some servers are multi-threaded. On these servers, there may |
|
213 | 211 | be multiple active threads inside __call__. |
|
214 | 212 | """ |
|
215 | 213 | def __init__(self, repo, name=None, baseui=None): |
|
216 | 214 | if isinstance(repo, str): |
|
217 | 215 | if baseui: |
|
218 | 216 | u = baseui.copy() |
|
219 | 217 | else: |
|
220 | 218 | u = uimod.ui.load() |
|
221 | 219 | r = hg.repository(u, repo) |
|
222 | 220 | else: |
|
223 | 221 | # we trust caller to give us a private copy |
|
224 | 222 | r = repo |
|
225 | 223 | |
|
226 | 224 | r.ui.setconfig('ui', 'report_untrusted', 'off', 'hgweb') |
|
227 | 225 | r.baseui.setconfig('ui', 'report_untrusted', 'off', 'hgweb') |
|
228 | 226 | r.ui.setconfig('ui', 'nontty', 'true', 'hgweb') |
|
229 | 227 | r.baseui.setconfig('ui', 'nontty', 'true', 'hgweb') |
|
230 | 228 | # resolve file patterns relative to repo root |
|
231 | 229 | r.ui.setconfig('ui', 'forcecwd', r.root, 'hgweb') |
|
232 | 230 | r.baseui.setconfig('ui', 'forcecwd', r.root, 'hgweb') |
|
233 | 231 | # displaying bundling progress bar while serving feel wrong and may |
|
234 | 232 | # break some wsgi implementation. |
|
235 | 233 | r.ui.setconfig('progress', 'disable', 'true', 'hgweb') |
|
236 | 234 | r.baseui.setconfig('progress', 'disable', 'true', 'hgweb') |
|
237 | 235 | self._repos = [hg.cachedlocalrepo(self._webifyrepo(r))] |
|
238 | 236 | self._lastrepo = self._repos[0] |
|
239 | 237 | hook.redirect(True) |
|
240 | 238 | self.reponame = name |
|
241 | 239 | |
|
242 | 240 | def _webifyrepo(self, repo): |
|
243 | 241 | repo = getwebview(repo) |
|
244 | 242 | self.websubtable = webutil.getwebsubs(repo) |
|
245 | 243 | return repo |
|
246 | 244 | |
|
247 | 245 | @contextlib.contextmanager |
|
248 | 246 | def _obtainrepo(self): |
|
249 | 247 | """Obtain a repo unique to the caller. |
|
250 | 248 | |
|
251 | 249 | Internally we maintain a stack of cachedlocalrepo instances |
|
252 | 250 | to be handed out. If one is available, we pop it and return it, |
|
253 | 251 | ensuring it is up to date in the process. If one is not available, |
|
254 | 252 | we clone the most recently used repo instance and return it. |
|
255 | 253 | |
|
256 | 254 | It is currently possible for the stack to grow without bounds |
|
257 | 255 | if the server allows infinite threads. However, servers should |
|
258 | 256 | have a thread limit, thus establishing our limit. |
|
259 | 257 | """ |
|
260 | 258 | if self._repos: |
|
261 | 259 | cached = self._repos.pop() |
|
262 | 260 | r, created = cached.fetch() |
|
263 | 261 | else: |
|
264 | 262 | cached = self._lastrepo.copy() |
|
265 | 263 | r, created = cached.fetch() |
|
266 | 264 | if created: |
|
267 | 265 | r = self._webifyrepo(r) |
|
268 | 266 | |
|
269 | 267 | self._lastrepo = cached |
|
270 | 268 | self.mtime = cached.mtime |
|
271 | 269 | try: |
|
272 | 270 | yield r |
|
273 | 271 | finally: |
|
274 | 272 | self._repos.append(cached) |
|
275 | 273 | |
|
276 | 274 | def run(self): |
|
277 | 275 | """Start a server from CGI environment. |
|
278 | 276 | |
|
279 | 277 | Modern servers should be using WSGI and should avoid this |
|
280 | 278 | method, if possible. |
|
281 | 279 | """ |
|
282 | 280 | if not encoding.environ.get('GATEWAY_INTERFACE', |
|
283 | 281 | '').startswith("CGI/1."): |
|
284 | 282 | raise RuntimeError("This function is only intended to be " |
|
285 | 283 | "called while running as a CGI script.") |
|
286 | 284 | wsgicgi.launch(self) |
|
287 | 285 | |
|
288 | 286 | def __call__(self, env, respond): |
|
289 | 287 | """Run the WSGI application. |
|
290 | 288 | |
|
291 | 289 | This may be called by multiple threads. |
|
292 | 290 | """ |
|
293 | 291 | req = requestmod.parserequestfromenv(env) |
|
294 | 292 | res = requestmod.wsgiresponse(req, respond) |
|
295 | 293 | |
|
296 | 294 | return self.run_wsgi(req, res) |
|
297 | 295 | |
|
298 | 296 | def run_wsgi(self, req, res): |
|
299 | 297 | """Internal method to run the WSGI application. |
|
300 | 298 | |
|
301 | 299 | This is typically only called by Mercurial. External consumers |
|
302 | 300 | should be using instances of this class as the WSGI application. |
|
303 | 301 | """ |
|
304 | 302 | with self._obtainrepo() as repo: |
|
305 | 303 | profile = repo.ui.configbool('profiling', 'enabled') |
|
306 | 304 | with profiling.profile(repo.ui, enabled=profile): |
|
307 | 305 | for r in self._runwsgi(req, res, repo): |
|
308 | 306 | yield r |
|
309 | 307 | |
|
310 | 308 | def _runwsgi(self, req, res, repo): |
|
311 | 309 | rctx = requestcontext(self, repo, req, res) |
|
312 | 310 | |
|
313 | 311 | # This state is global across all threads. |
|
314 | 312 | encoding.encoding = rctx.config('web', 'encoding') |
|
315 | 313 | rctx.repo.ui.environ = req.rawenv |
|
316 | 314 | |
|
317 | 315 | if rctx.csp: |
|
318 | 316 | # hgwebdir may have added CSP header. Since we generate our own, |
|
319 | 317 | # replace it. |
|
320 | 318 | res.headers['Content-Security-Policy'] = rctx.csp |
|
321 | 319 | |
|
322 | 320 | # /api/* is reserved for various API implementations. Dispatch |
|
323 | 321 | # accordingly. But URL paths can conflict with subrepos and virtual |
|
324 | 322 | # repos in hgwebdir. So until we have a workaround for this, only |
|
325 | 323 | # expose the URLs if the feature is enabled. |
|
326 | 324 | apienabled = rctx.repo.ui.configbool('experimental', 'web.apiserver') |
|
327 | 325 | if apienabled and req.dispatchparts and req.dispatchparts[0] == b'api': |
|
328 | 326 | wireprotoserver.handlewsgiapirequest(rctx, req, res, |
|
329 | 327 | self.check_perm) |
|
330 | 328 | return res.sendresponse() |
|
331 | 329 | |
|
332 | 330 | handled = wireprotoserver.handlewsgirequest( |
|
333 | 331 | rctx, req, res, self.check_perm) |
|
334 | 332 | if handled: |
|
335 | 333 | return res.sendresponse() |
|
336 | 334 | |
|
337 | 335 | # Old implementations of hgweb supported dispatching the request via |
|
338 | 336 | # the initial query string parameter instead of using PATH_INFO. |
|
339 | 337 | # If PATH_INFO is present (signaled by ``req.dispatchpath`` having |
|
340 | 338 | # a value), we use it. Otherwise fall back to the query string. |
|
341 | 339 | if req.dispatchpath is not None: |
|
342 | 340 | query = req.dispatchpath |
|
343 | 341 | else: |
|
344 | 342 | query = req.querystring.partition('&')[0].partition(';')[0] |
|
345 | 343 | |
|
346 | 344 | # translate user-visible url structure to internal structure |
|
347 | 345 | |
|
348 | 346 | args = query.split('/', 2) |
|
349 | 347 | if 'cmd' not in req.qsparams and args and args[0]: |
|
350 | 348 | cmd = args.pop(0) |
|
351 | 349 | style = cmd.rfind('-') |
|
352 | 350 | if style != -1: |
|
353 | 351 | req.qsparams['style'] = cmd[:style] |
|
354 | 352 | cmd = cmd[style + 1:] |
|
355 | 353 | |
|
356 | 354 | # avoid accepting e.g. style parameter as command |
|
357 | 355 | if util.safehasattr(webcommands, cmd): |
|
358 | 356 | req.qsparams['cmd'] = cmd |
|
359 | 357 | |
|
360 | 358 | if cmd == 'static': |
|
361 | 359 | req.qsparams['file'] = '/'.join(args) |
|
362 | 360 | else: |
|
363 | 361 | if args and args[0]: |
|
364 | 362 | node = args.pop(0).replace('%2F', '/') |
|
365 | 363 | req.qsparams['node'] = node |
|
366 | 364 | if args: |
|
367 | 365 | if 'file' in req.qsparams: |
|
368 | 366 | del req.qsparams['file'] |
|
369 | 367 | for a in args: |
|
370 | 368 | req.qsparams.add('file', a) |
|
371 | 369 | |
|
372 | 370 | ua = req.headers.get('User-Agent', '') |
|
373 | 371 | if cmd == 'rev' and 'mercurial' in ua: |
|
374 | 372 | req.qsparams['style'] = 'raw' |
|
375 | 373 | |
|
376 | 374 | if cmd == 'archive': |
|
377 | 375 | fn = req.qsparams['node'] |
|
378 |
for type_, spec in |
|
|
376 | for type_, spec in webutil.archivespecs.iteritems(): | |
|
379 | 377 | ext = spec[2] |
|
380 | 378 | if fn.endswith(ext): |
|
381 | 379 | req.qsparams['node'] = fn[:-len(ext)] |
|
382 | 380 | req.qsparams['type'] = type_ |
|
383 | 381 | else: |
|
384 | 382 | cmd = req.qsparams.get('cmd', '') |
|
385 | 383 | |
|
386 | 384 | # process the web interface request |
|
387 | 385 | |
|
388 | 386 | try: |
|
389 | 387 | rctx.tmpl = rctx.templater(req) |
|
390 | 388 | ctype = rctx.tmpl.render('mimetype', |
|
391 | 389 | {'encoding': encoding.encoding}) |
|
392 | 390 | |
|
393 | 391 | # check read permissions non-static content |
|
394 | 392 | if cmd != 'static': |
|
395 | 393 | self.check_perm(rctx, req, None) |
|
396 | 394 | |
|
397 | 395 | if cmd == '': |
|
398 | 396 | req.qsparams['cmd'] = rctx.tmpl.render('default', {}) |
|
399 | 397 | cmd = req.qsparams['cmd'] |
|
400 | 398 | |
|
401 | 399 | # Don't enable caching if using a CSP nonce because then it wouldn't |
|
402 | 400 | # be a nonce. |
|
403 | 401 | if rctx.configbool('web', 'cache') and not rctx.nonce: |
|
404 | 402 | tag = 'W/"%d"' % self.mtime |
|
405 | 403 | if req.headers.get('If-None-Match') == tag: |
|
406 | 404 | res.status = '304 Not Modified' |
|
407 | 405 | # Response body not allowed on 304. |
|
408 | 406 | res.setbodybytes('') |
|
409 | 407 | return res.sendresponse() |
|
410 | 408 | |
|
411 | 409 | res.headers['ETag'] = tag |
|
412 | 410 | |
|
413 | 411 | if cmd not in webcommands.__all__: |
|
414 | 412 | msg = 'no such method: %s' % cmd |
|
415 | 413 | raise ErrorResponse(HTTP_BAD_REQUEST, msg) |
|
416 | 414 | else: |
|
417 | 415 | # Set some globals appropriate for web handlers. Commands can |
|
418 | 416 | # override easily enough. |
|
419 | 417 | res.status = '200 Script output follows' |
|
420 | 418 | res.headers['Content-Type'] = ctype |
|
421 | 419 | return getattr(webcommands, cmd)(rctx) |
|
422 | 420 | |
|
423 | 421 | except (error.LookupError, error.RepoLookupError) as err: |
|
424 | 422 | msg = pycompat.bytestr(err) |
|
425 | 423 | if (util.safehasattr(err, 'name') and |
|
426 | 424 | not isinstance(err, error.ManifestLookupError)): |
|
427 | 425 | msg = 'revision not found: %s' % err.name |
|
428 | 426 | |
|
429 | 427 | res.status = '404 Not Found' |
|
430 | 428 | res.headers['Content-Type'] = ctype |
|
431 | 429 | return rctx.sendtemplate('error', error=msg) |
|
432 | 430 | except (error.RepoError, error.RevlogError) as e: |
|
433 | 431 | res.status = '500 Internal Server Error' |
|
434 | 432 | res.headers['Content-Type'] = ctype |
|
435 | 433 | return rctx.sendtemplate('error', error=pycompat.bytestr(e)) |
|
436 | 434 | except ErrorResponse as e: |
|
437 | 435 | res.status = statusmessage(e.code, pycompat.bytestr(e)) |
|
438 | 436 | res.headers['Content-Type'] = ctype |
|
439 | 437 | return rctx.sendtemplate('error', error=pycompat.bytestr(e)) |
|
440 | 438 | |
|
441 | 439 | def check_perm(self, rctx, req, op): |
|
442 | 440 | for permhook in permhooks: |
|
443 | 441 | permhook(rctx, req, op) |
|
444 | 442 | |
|
445 | 443 | def getwebview(repo): |
|
446 | 444 | """The 'web.view' config controls changeset filter to hgweb. Possible |
|
447 | 445 | values are ``served``, ``visible`` and ``all``. Default is ``served``. |
|
448 | 446 | The ``served`` filter only shows changesets that can be pulled from the |
|
449 | 447 | hgweb instance. The``visible`` filter includes secret changesets but |
|
450 | 448 | still excludes "hidden" one. |
|
451 | 449 | |
|
452 | 450 | See the repoview module for details. |
|
453 | 451 | |
|
454 | 452 | The option has been around undocumented since Mercurial 2.5, but no |
|
455 | 453 | user ever asked about it. So we better keep it undocumented for now.""" |
|
456 | 454 | # experimental config: web.view |
|
457 | 455 | viewconfig = repo.ui.config('web', 'view', untrusted=True) |
|
458 | 456 | if viewconfig == 'all': |
|
459 | 457 | return repo.unfiltered() |
|
460 | 458 | elif viewconfig in repoview.filtertable: |
|
461 | 459 | return repo.filtered(viewconfig) |
|
462 | 460 | else: |
|
463 | 461 | return repo.filtered('served') |
@@ -1,1494 +1,1494 b'' | |||
|
1 | 1 | # |
|
2 | 2 | # Copyright 21 May 2005 - (c) 2005 Jake Edge <jake@edge2.net> |
|
3 | 3 | # Copyright 2005-2007 Matt Mackall <mpm@selenic.com> |
|
4 | 4 | # |
|
5 | 5 | # This software may be used and distributed according to the terms of the |
|
6 | 6 | # GNU General Public License version 2 or any later version. |
|
7 | 7 | |
|
8 | 8 | from __future__ import absolute_import |
|
9 | 9 | |
|
10 | 10 | import copy |
|
11 | 11 | import mimetypes |
|
12 | 12 | import os |
|
13 | 13 | import re |
|
14 | 14 | |
|
15 | 15 | from ..i18n import _ |
|
16 | 16 | from ..node import hex, nullid, short |
|
17 | 17 | |
|
18 | 18 | from .common import ( |
|
19 | 19 | ErrorResponse, |
|
20 | 20 | HTTP_FORBIDDEN, |
|
21 | 21 | HTTP_NOT_FOUND, |
|
22 | 22 | get_contact, |
|
23 | 23 | paritygen, |
|
24 | 24 | staticfile, |
|
25 | 25 | ) |
|
26 | 26 | |
|
27 | 27 | from .. import ( |
|
28 | 28 | archival, |
|
29 | 29 | dagop, |
|
30 | 30 | encoding, |
|
31 | 31 | error, |
|
32 | 32 | graphmod, |
|
33 | 33 | pycompat, |
|
34 | 34 | revset, |
|
35 | 35 | revsetlang, |
|
36 | 36 | scmutil, |
|
37 | 37 | smartset, |
|
38 | 38 | templater, |
|
39 | 39 | templateutil, |
|
40 | 40 | ) |
|
41 | 41 | |
|
42 | 42 | from ..utils import ( |
|
43 | 43 | stringutil, |
|
44 | 44 | ) |
|
45 | 45 | |
|
46 | 46 | from . import ( |
|
47 | 47 | webutil, |
|
48 | 48 | ) |
|
49 | 49 | |
|
50 | 50 | __all__ = [] |
|
51 | 51 | commands = {} |
|
52 | 52 | |
|
53 | 53 | class webcommand(object): |
|
54 | 54 | """Decorator used to register a web command handler. |
|
55 | 55 | |
|
56 | 56 | The decorator takes as its positional arguments the name/path the |
|
57 | 57 | command should be accessible under. |
|
58 | 58 | |
|
59 | 59 | When called, functions receive as arguments a ``requestcontext``, |
|
60 | 60 | ``wsgirequest``, and a templater instance for generatoring output. |
|
61 | 61 | The functions should populate the ``rctx.res`` object with details |
|
62 | 62 | about the HTTP response. |
|
63 | 63 | |
|
64 | 64 | The function returns a generator to be consumed by the WSGI application. |
|
65 | 65 | For most commands, this should be the result from |
|
66 | 66 | ``web.res.sendresponse()``. Many commands will call ``web.sendtemplate()`` |
|
67 | 67 | to render a template. |
|
68 | 68 | |
|
69 | 69 | Usage: |
|
70 | 70 | |
|
71 | 71 | @webcommand('mycommand') |
|
72 | 72 | def mycommand(web): |
|
73 | 73 | pass |
|
74 | 74 | """ |
|
75 | 75 | |
|
76 | 76 | def __init__(self, name): |
|
77 | 77 | self.name = name |
|
78 | 78 | |
|
79 | 79 | def __call__(self, func): |
|
80 | 80 | __all__.append(self.name) |
|
81 | 81 | commands[self.name] = func |
|
82 | 82 | return func |
|
83 | 83 | |
|
84 | 84 | @webcommand('log') |
|
85 | 85 | def log(web): |
|
86 | 86 | """ |
|
87 | 87 | /log[/{revision}[/{path}]] |
|
88 | 88 | -------------------------- |
|
89 | 89 | |
|
90 | 90 | Show repository or file history. |
|
91 | 91 | |
|
92 | 92 | For URLs of the form ``/log/{revision}``, a list of changesets starting at |
|
93 | 93 | the specified changeset identifier is shown. If ``{revision}`` is not |
|
94 | 94 | defined, the default is ``tip``. This form is equivalent to the |
|
95 | 95 | ``changelog`` handler. |
|
96 | 96 | |
|
97 | 97 | For URLs of the form ``/log/{revision}/{file}``, the history for a specific |
|
98 | 98 | file will be shown. This form is equivalent to the ``filelog`` handler. |
|
99 | 99 | """ |
|
100 | 100 | |
|
101 | 101 | if web.req.qsparams.get('file'): |
|
102 | 102 | return filelog(web) |
|
103 | 103 | else: |
|
104 | 104 | return changelog(web) |
|
105 | 105 | |
|
106 | 106 | @webcommand('rawfile') |
|
107 | 107 | def rawfile(web): |
|
108 | 108 | guessmime = web.configbool('web', 'guessmime') |
|
109 | 109 | |
|
110 | 110 | path = webutil.cleanpath(web.repo, web.req.qsparams.get('file', '')) |
|
111 | 111 | if not path: |
|
112 | 112 | return manifest(web) |
|
113 | 113 | |
|
114 | 114 | try: |
|
115 | 115 | fctx = webutil.filectx(web.repo, web.req) |
|
116 | 116 | except error.LookupError as inst: |
|
117 | 117 | try: |
|
118 | 118 | return manifest(web) |
|
119 | 119 | except ErrorResponse: |
|
120 | 120 | raise inst |
|
121 | 121 | |
|
122 | 122 | path = fctx.path() |
|
123 | 123 | text = fctx.data() |
|
124 | 124 | mt = 'application/binary' |
|
125 | 125 | if guessmime: |
|
126 | 126 | mt = mimetypes.guess_type(path)[0] |
|
127 | 127 | if mt is None: |
|
128 | 128 | if stringutil.binary(text): |
|
129 | 129 | mt = 'application/binary' |
|
130 | 130 | else: |
|
131 | 131 | mt = 'text/plain' |
|
132 | 132 | if mt.startswith('text/'): |
|
133 | 133 | mt += '; charset="%s"' % encoding.encoding |
|
134 | 134 | |
|
135 | 135 | web.res.headers['Content-Type'] = mt |
|
136 | 136 | filename = (path.rpartition('/')[-1] |
|
137 | 137 | .replace('\\', '\\\\').replace('"', '\\"')) |
|
138 | 138 | web.res.headers['Content-Disposition'] = 'inline; filename="%s"' % filename |
|
139 | 139 | web.res.setbodybytes(text) |
|
140 | 140 | return web.res.sendresponse() |
|
141 | 141 | |
|
142 | 142 | def _filerevision(web, fctx): |
|
143 | 143 | f = fctx.path() |
|
144 | 144 | text = fctx.data() |
|
145 | 145 | parity = paritygen(web.stripecount) |
|
146 | 146 | ishead = fctx.filerev() in fctx.filelog().headrevs() |
|
147 | 147 | |
|
148 | 148 | if stringutil.binary(text): |
|
149 | 149 | mt = mimetypes.guess_type(f)[0] or 'application/octet-stream' |
|
150 | 150 | text = '(binary:%s)' % mt |
|
151 | 151 | |
|
152 | 152 | def lines(): |
|
153 | 153 | for lineno, t in enumerate(text.splitlines(True)): |
|
154 | 154 | yield {"line": t, |
|
155 | 155 | "lineid": "l%d" % (lineno + 1), |
|
156 | 156 | "linenumber": "% 6d" % (lineno + 1), |
|
157 | 157 | "parity": next(parity)} |
|
158 | 158 | |
|
159 | 159 | return web.sendtemplate( |
|
160 | 160 | 'filerevision', |
|
161 | 161 | file=f, |
|
162 | 162 | path=webutil.up(f), |
|
163 | 163 | text=lines(), |
|
164 | 164 | symrev=webutil.symrevorshortnode(web.req, fctx), |
|
165 | 165 | rename=webutil.renamelink(fctx), |
|
166 | 166 | permissions=fctx.manifest().flags(f), |
|
167 | 167 | ishead=int(ishead), |
|
168 | 168 | **pycompat.strkwargs(webutil.commonentry(web.repo, fctx))) |
|
169 | 169 | |
|
170 | 170 | @webcommand('file') |
|
171 | 171 | def file(web): |
|
172 | 172 | """ |
|
173 | 173 | /file/{revision}[/{path}] |
|
174 | 174 | ------------------------- |
|
175 | 175 | |
|
176 | 176 | Show information about a directory or file in the repository. |
|
177 | 177 | |
|
178 | 178 | Info about the ``path`` given as a URL parameter will be rendered. |
|
179 | 179 | |
|
180 | 180 | If ``path`` is a directory, information about the entries in that |
|
181 | 181 | directory will be rendered. This form is equivalent to the ``manifest`` |
|
182 | 182 | handler. |
|
183 | 183 | |
|
184 | 184 | If ``path`` is a file, information about that file will be shown via |
|
185 | 185 | the ``filerevision`` template. |
|
186 | 186 | |
|
187 | 187 | If ``path`` is not defined, information about the root directory will |
|
188 | 188 | be rendered. |
|
189 | 189 | """ |
|
190 | 190 | if web.req.qsparams.get('style') == 'raw': |
|
191 | 191 | return rawfile(web) |
|
192 | 192 | |
|
193 | 193 | path = webutil.cleanpath(web.repo, web.req.qsparams.get('file', '')) |
|
194 | 194 | if not path: |
|
195 | 195 | return manifest(web) |
|
196 | 196 | try: |
|
197 | 197 | return _filerevision(web, webutil.filectx(web.repo, web.req)) |
|
198 | 198 | except error.LookupError as inst: |
|
199 | 199 | try: |
|
200 | 200 | return manifest(web) |
|
201 | 201 | except ErrorResponse: |
|
202 | 202 | raise inst |
|
203 | 203 | |
|
204 | 204 | def _search(web): |
|
205 | 205 | MODE_REVISION = 'rev' |
|
206 | 206 | MODE_KEYWORD = 'keyword' |
|
207 | 207 | MODE_REVSET = 'revset' |
|
208 | 208 | |
|
209 | 209 | def revsearch(ctx): |
|
210 | 210 | yield ctx |
|
211 | 211 | |
|
212 | 212 | def keywordsearch(query): |
|
213 | 213 | lower = encoding.lower |
|
214 | 214 | qw = lower(query).split() |
|
215 | 215 | |
|
216 | 216 | def revgen(): |
|
217 | 217 | cl = web.repo.changelog |
|
218 | 218 | for i in xrange(len(web.repo) - 1, 0, -100): |
|
219 | 219 | l = [] |
|
220 | 220 | for j in cl.revs(max(0, i - 99), i): |
|
221 | 221 | ctx = web.repo[j] |
|
222 | 222 | l.append(ctx) |
|
223 | 223 | l.reverse() |
|
224 | 224 | for e in l: |
|
225 | 225 | yield e |
|
226 | 226 | |
|
227 | 227 | for ctx in revgen(): |
|
228 | 228 | miss = 0 |
|
229 | 229 | for q in qw: |
|
230 | 230 | if not (q in lower(ctx.user()) or |
|
231 | 231 | q in lower(ctx.description()) or |
|
232 | 232 | q in lower(" ".join(ctx.files()))): |
|
233 | 233 | miss = 1 |
|
234 | 234 | break |
|
235 | 235 | if miss: |
|
236 | 236 | continue |
|
237 | 237 | |
|
238 | 238 | yield ctx |
|
239 | 239 | |
|
240 | 240 | def revsetsearch(revs): |
|
241 | 241 | for r in revs: |
|
242 | 242 | yield web.repo[r] |
|
243 | 243 | |
|
244 | 244 | searchfuncs = { |
|
245 | 245 | MODE_REVISION: (revsearch, 'exact revision search'), |
|
246 | 246 | MODE_KEYWORD: (keywordsearch, 'literal keyword search'), |
|
247 | 247 | MODE_REVSET: (revsetsearch, 'revset expression search'), |
|
248 | 248 | } |
|
249 | 249 | |
|
250 | 250 | def getsearchmode(query): |
|
251 | 251 | try: |
|
252 | 252 | ctx = scmutil.revsymbol(web.repo, query) |
|
253 | 253 | except (error.RepoError, error.LookupError): |
|
254 | 254 | # query is not an exact revision pointer, need to |
|
255 | 255 | # decide if it's a revset expression or keywords |
|
256 | 256 | pass |
|
257 | 257 | else: |
|
258 | 258 | return MODE_REVISION, ctx |
|
259 | 259 | |
|
260 | 260 | revdef = 'reverse(%s)' % query |
|
261 | 261 | try: |
|
262 | 262 | tree = revsetlang.parse(revdef) |
|
263 | 263 | except error.ParseError: |
|
264 | 264 | # can't parse to a revset tree |
|
265 | 265 | return MODE_KEYWORD, query |
|
266 | 266 | |
|
267 | 267 | if revsetlang.depth(tree) <= 2: |
|
268 | 268 | # no revset syntax used |
|
269 | 269 | return MODE_KEYWORD, query |
|
270 | 270 | |
|
271 | 271 | if any((token, (value or '')[:3]) == ('string', 're:') |
|
272 | 272 | for token, value, pos in revsetlang.tokenize(revdef)): |
|
273 | 273 | return MODE_KEYWORD, query |
|
274 | 274 | |
|
275 | 275 | funcsused = revsetlang.funcsused(tree) |
|
276 | 276 | if not funcsused.issubset(revset.safesymbols): |
|
277 | 277 | return MODE_KEYWORD, query |
|
278 | 278 | |
|
279 | 279 | mfunc = revset.match(web.repo.ui, revdef, repo=web.repo) |
|
280 | 280 | try: |
|
281 | 281 | revs = mfunc(web.repo) |
|
282 | 282 | return MODE_REVSET, revs |
|
283 | 283 | # ParseError: wrongly placed tokens, wrongs arguments, etc |
|
284 | 284 | # RepoLookupError: no such revision, e.g. in 'revision:' |
|
285 | 285 | # Abort: bookmark/tag not exists |
|
286 | 286 | # LookupError: ambiguous identifier, e.g. in '(bc)' on a large repo |
|
287 | 287 | except (error.ParseError, error.RepoLookupError, error.Abort, |
|
288 | 288 | LookupError): |
|
289 | 289 | return MODE_KEYWORD, query |
|
290 | 290 | |
|
291 | 291 | def changelist(context): |
|
292 | 292 | count = 0 |
|
293 | 293 | |
|
294 | 294 | for ctx in searchfunc[0](funcarg): |
|
295 | 295 | count += 1 |
|
296 | 296 | n = ctx.node() |
|
297 | 297 | showtags = webutil.showtag(web.repo, web.tmpl, 'changelogtag', n) |
|
298 | 298 | files = webutil.listfilediffs(web.tmpl, ctx.files(), n, |
|
299 | 299 | web.maxfiles) |
|
300 | 300 | |
|
301 | 301 | lm = webutil.commonentry(web.repo, ctx) |
|
302 | 302 | lm.update({ |
|
303 | 303 | 'parity': next(parity), |
|
304 | 304 | 'changelogtag': showtags, |
|
305 | 305 | 'files': files, |
|
306 | 306 | }) |
|
307 | 307 | yield lm |
|
308 | 308 | |
|
309 | 309 | if count >= revcount: |
|
310 | 310 | break |
|
311 | 311 | |
|
312 | 312 | query = web.req.qsparams['rev'] |
|
313 | 313 | revcount = web.maxchanges |
|
314 | 314 | if 'revcount' in web.req.qsparams: |
|
315 | 315 | try: |
|
316 | 316 | revcount = int(web.req.qsparams.get('revcount', revcount)) |
|
317 | 317 | revcount = max(revcount, 1) |
|
318 | 318 | web.tmpl.defaults['sessionvars']['revcount'] = revcount |
|
319 | 319 | except ValueError: |
|
320 | 320 | pass |
|
321 | 321 | |
|
322 | 322 | lessvars = copy.copy(web.tmpl.defaults['sessionvars']) |
|
323 | 323 | lessvars['revcount'] = max(revcount // 2, 1) |
|
324 | 324 | lessvars['rev'] = query |
|
325 | 325 | morevars = copy.copy(web.tmpl.defaults['sessionvars']) |
|
326 | 326 | morevars['revcount'] = revcount * 2 |
|
327 | 327 | morevars['rev'] = query |
|
328 | 328 | |
|
329 | 329 | mode, funcarg = getsearchmode(query) |
|
330 | 330 | |
|
331 | 331 | if 'forcekw' in web.req.qsparams: |
|
332 | 332 | showforcekw = '' |
|
333 | 333 | showunforcekw = searchfuncs[mode][1] |
|
334 | 334 | mode = MODE_KEYWORD |
|
335 | 335 | funcarg = query |
|
336 | 336 | else: |
|
337 | 337 | if mode != MODE_KEYWORD: |
|
338 | 338 | showforcekw = searchfuncs[MODE_KEYWORD][1] |
|
339 | 339 | else: |
|
340 | 340 | showforcekw = '' |
|
341 | 341 | showunforcekw = '' |
|
342 | 342 | |
|
343 | 343 | searchfunc = searchfuncs[mode] |
|
344 | 344 | |
|
345 | 345 | tip = web.repo['tip'] |
|
346 | 346 | parity = paritygen(web.stripecount) |
|
347 | 347 | |
|
348 | 348 | return web.sendtemplate( |
|
349 | 349 | 'search', |
|
350 | 350 | query=query, |
|
351 | 351 | node=tip.hex(), |
|
352 | 352 | symrev='tip', |
|
353 | 353 | entries=templateutil.mappinggenerator(changelist, name='searchentry'), |
|
354 | 354 | archives=web.archivelist('tip'), |
|
355 | 355 | morevars=morevars, |
|
356 | 356 | lessvars=lessvars, |
|
357 | 357 | modedesc=searchfunc[1], |
|
358 | 358 | showforcekw=showforcekw, |
|
359 | 359 | showunforcekw=showunforcekw) |
|
360 | 360 | |
|
361 | 361 | @webcommand('changelog') |
|
362 | 362 | def changelog(web, shortlog=False): |
|
363 | 363 | """ |
|
364 | 364 | /changelog[/{revision}] |
|
365 | 365 | ----------------------- |
|
366 | 366 | |
|
367 | 367 | Show information about multiple changesets. |
|
368 | 368 | |
|
369 | 369 | If the optional ``revision`` URL argument is absent, information about |
|
370 | 370 | all changesets starting at ``tip`` will be rendered. If the ``revision`` |
|
371 | 371 | argument is present, changesets will be shown starting from the specified |
|
372 | 372 | revision. |
|
373 | 373 | |
|
374 | 374 | If ``revision`` is absent, the ``rev`` query string argument may be |
|
375 | 375 | defined. This will perform a search for changesets. |
|
376 | 376 | |
|
377 | 377 | The argument for ``rev`` can be a single revision, a revision set, |
|
378 | 378 | or a literal keyword to search for in changeset data (equivalent to |
|
379 | 379 | :hg:`log -k`). |
|
380 | 380 | |
|
381 | 381 | The ``revcount`` query string argument defines the maximum numbers of |
|
382 | 382 | changesets to render. |
|
383 | 383 | |
|
384 | 384 | For non-searches, the ``changelog`` template will be rendered. |
|
385 | 385 | """ |
|
386 | 386 | |
|
387 | 387 | query = '' |
|
388 | 388 | if 'node' in web.req.qsparams: |
|
389 | 389 | ctx = webutil.changectx(web.repo, web.req) |
|
390 | 390 | symrev = webutil.symrevorshortnode(web.req, ctx) |
|
391 | 391 | elif 'rev' in web.req.qsparams: |
|
392 | 392 | return _search(web) |
|
393 | 393 | else: |
|
394 | 394 | ctx = web.repo['tip'] |
|
395 | 395 | symrev = 'tip' |
|
396 | 396 | |
|
397 | 397 | def changelist(): |
|
398 | 398 | revs = [] |
|
399 | 399 | if pos != -1: |
|
400 | 400 | revs = web.repo.changelog.revs(pos, 0) |
|
401 | 401 | curcount = 0 |
|
402 | 402 | for rev in revs: |
|
403 | 403 | curcount += 1 |
|
404 | 404 | if curcount > revcount + 1: |
|
405 | 405 | break |
|
406 | 406 | |
|
407 | 407 | entry = webutil.changelistentry(web, web.repo[rev]) |
|
408 | 408 | entry['parity'] = next(parity) |
|
409 | 409 | yield entry |
|
410 | 410 | |
|
411 | 411 | if shortlog: |
|
412 | 412 | revcount = web.maxshortchanges |
|
413 | 413 | else: |
|
414 | 414 | revcount = web.maxchanges |
|
415 | 415 | |
|
416 | 416 | if 'revcount' in web.req.qsparams: |
|
417 | 417 | try: |
|
418 | 418 | revcount = int(web.req.qsparams.get('revcount', revcount)) |
|
419 | 419 | revcount = max(revcount, 1) |
|
420 | 420 | web.tmpl.defaults['sessionvars']['revcount'] = revcount |
|
421 | 421 | except ValueError: |
|
422 | 422 | pass |
|
423 | 423 | |
|
424 | 424 | lessvars = copy.copy(web.tmpl.defaults['sessionvars']) |
|
425 | 425 | lessvars['revcount'] = max(revcount // 2, 1) |
|
426 | 426 | morevars = copy.copy(web.tmpl.defaults['sessionvars']) |
|
427 | 427 | morevars['revcount'] = revcount * 2 |
|
428 | 428 | |
|
429 | 429 | count = len(web.repo) |
|
430 | 430 | pos = ctx.rev() |
|
431 | 431 | parity = paritygen(web.stripecount) |
|
432 | 432 | |
|
433 | 433 | changenav = webutil.revnav(web.repo).gen(pos, revcount, count) |
|
434 | 434 | |
|
435 | 435 | entries = list(changelist()) |
|
436 | 436 | latestentry = entries[:1] |
|
437 | 437 | if len(entries) > revcount: |
|
438 | 438 | nextentry = entries[-1:] |
|
439 | 439 | entries = entries[:-1] |
|
440 | 440 | else: |
|
441 | 441 | nextentry = [] |
|
442 | 442 | |
|
443 | 443 | return web.sendtemplate( |
|
444 | 444 | 'shortlog' if shortlog else 'changelog', |
|
445 | 445 | changenav=changenav, |
|
446 | 446 | node=ctx.hex(), |
|
447 | 447 | rev=pos, |
|
448 | 448 | symrev=symrev, |
|
449 | 449 | changesets=count, |
|
450 | 450 | entries=entries, |
|
451 | 451 | latestentry=latestentry, |
|
452 | 452 | nextentry=nextentry, |
|
453 | 453 | archives=web.archivelist('tip'), |
|
454 | 454 | revcount=revcount, |
|
455 | 455 | morevars=morevars, |
|
456 | 456 | lessvars=lessvars, |
|
457 | 457 | query=query) |
|
458 | 458 | |
|
459 | 459 | @webcommand('shortlog') |
|
460 | 460 | def shortlog(web): |
|
461 | 461 | """ |
|
462 | 462 | /shortlog |
|
463 | 463 | --------- |
|
464 | 464 | |
|
465 | 465 | Show basic information about a set of changesets. |
|
466 | 466 | |
|
467 | 467 | This accepts the same parameters as the ``changelog`` handler. The only |
|
468 | 468 | difference is the ``shortlog`` template will be rendered instead of the |
|
469 | 469 | ``changelog`` template. |
|
470 | 470 | """ |
|
471 | 471 | return changelog(web, shortlog=True) |
|
472 | 472 | |
|
473 | 473 | @webcommand('changeset') |
|
474 | 474 | def changeset(web): |
|
475 | 475 | """ |
|
476 | 476 | /changeset[/{revision}] |
|
477 | 477 | ----------------------- |
|
478 | 478 | |
|
479 | 479 | Show information about a single changeset. |
|
480 | 480 | |
|
481 | 481 | A URL path argument is the changeset identifier to show. See ``hg help |
|
482 | 482 | revisions`` for possible values. If not defined, the ``tip`` changeset |
|
483 | 483 | will be shown. |
|
484 | 484 | |
|
485 | 485 | The ``changeset`` template is rendered. Contents of the ``changesettag``, |
|
486 | 486 | ``changesetbookmark``, ``filenodelink``, ``filenolink``, and the many |
|
487 | 487 | templates related to diffs may all be used to produce the output. |
|
488 | 488 | """ |
|
489 | 489 | ctx = webutil.changectx(web.repo, web.req) |
|
490 | 490 | |
|
491 | 491 | return web.sendtemplate( |
|
492 | 492 | 'changeset', |
|
493 | 493 | **webutil.changesetentry(web, ctx)) |
|
494 | 494 | |
|
495 | 495 | rev = webcommand('rev')(changeset) |
|
496 | 496 | |
|
497 | 497 | def decodepath(path): |
|
498 | 498 | """Hook for mapping a path in the repository to a path in the |
|
499 | 499 | working copy. |
|
500 | 500 | |
|
501 | 501 | Extensions (e.g., largefiles) can override this to remap files in |
|
502 | 502 | the virtual file system presented by the manifest command below.""" |
|
503 | 503 | return path |
|
504 | 504 | |
|
505 | 505 | @webcommand('manifest') |
|
506 | 506 | def manifest(web): |
|
507 | 507 | """ |
|
508 | 508 | /manifest[/{revision}[/{path}]] |
|
509 | 509 | ------------------------------- |
|
510 | 510 | |
|
511 | 511 | Show information about a directory. |
|
512 | 512 | |
|
513 | 513 | If the URL path arguments are omitted, information about the root |
|
514 | 514 | directory for the ``tip`` changeset will be shown. |
|
515 | 515 | |
|
516 | 516 | Because this handler can only show information for directories, it |
|
517 | 517 | is recommended to use the ``file`` handler instead, as it can handle both |
|
518 | 518 | directories and files. |
|
519 | 519 | |
|
520 | 520 | The ``manifest`` template will be rendered for this handler. |
|
521 | 521 | """ |
|
522 | 522 | if 'node' in web.req.qsparams: |
|
523 | 523 | ctx = webutil.changectx(web.repo, web.req) |
|
524 | 524 | symrev = webutil.symrevorshortnode(web.req, ctx) |
|
525 | 525 | else: |
|
526 | 526 | ctx = web.repo['tip'] |
|
527 | 527 | symrev = 'tip' |
|
528 | 528 | path = webutil.cleanpath(web.repo, web.req.qsparams.get('file', '')) |
|
529 | 529 | mf = ctx.manifest() |
|
530 | 530 | node = ctx.node() |
|
531 | 531 | |
|
532 | 532 | files = {} |
|
533 | 533 | dirs = {} |
|
534 | 534 | parity = paritygen(web.stripecount) |
|
535 | 535 | |
|
536 | 536 | if path and path[-1:] != "/": |
|
537 | 537 | path += "/" |
|
538 | 538 | l = len(path) |
|
539 | 539 | abspath = "/" + path |
|
540 | 540 | |
|
541 | 541 | for full, n in mf.iteritems(): |
|
542 | 542 | # the virtual path (working copy path) used for the full |
|
543 | 543 | # (repository) path |
|
544 | 544 | f = decodepath(full) |
|
545 | 545 | |
|
546 | 546 | if f[:l] != path: |
|
547 | 547 | continue |
|
548 | 548 | remain = f[l:] |
|
549 | 549 | elements = remain.split('/') |
|
550 | 550 | if len(elements) == 1: |
|
551 | 551 | files[remain] = full |
|
552 | 552 | else: |
|
553 | 553 | h = dirs # need to retain ref to dirs (root) |
|
554 | 554 | for elem in elements[0:-1]: |
|
555 | 555 | if elem not in h: |
|
556 | 556 | h[elem] = {} |
|
557 | 557 | h = h[elem] |
|
558 | 558 | if len(h) > 1: |
|
559 | 559 | break |
|
560 | 560 | h[None] = None # denotes files present |
|
561 | 561 | |
|
562 | 562 | if mf and not files and not dirs: |
|
563 | 563 | raise ErrorResponse(HTTP_NOT_FOUND, 'path not found: ' + path) |
|
564 | 564 | |
|
565 | 565 | def filelist(**map): |
|
566 | 566 | for f in sorted(files): |
|
567 | 567 | full = files[f] |
|
568 | 568 | |
|
569 | 569 | fctx = ctx.filectx(full) |
|
570 | 570 | yield {"file": full, |
|
571 | 571 | "parity": next(parity), |
|
572 | 572 | "basename": f, |
|
573 | 573 | "date": fctx.date(), |
|
574 | 574 | "size": fctx.size(), |
|
575 | 575 | "permissions": mf.flags(full)} |
|
576 | 576 | |
|
577 | 577 | def dirlist(**map): |
|
578 | 578 | for d in sorted(dirs): |
|
579 | 579 | |
|
580 | 580 | emptydirs = [] |
|
581 | 581 | h = dirs[d] |
|
582 | 582 | while isinstance(h, dict) and len(h) == 1: |
|
583 | 583 | k, v = next(iter(h.items())) |
|
584 | 584 | if v: |
|
585 | 585 | emptydirs.append(k) |
|
586 | 586 | h = v |
|
587 | 587 | |
|
588 | 588 | path = "%s%s" % (abspath, d) |
|
589 | 589 | yield {"parity": next(parity), |
|
590 | 590 | "path": path, |
|
591 | 591 | "emptydirs": "/".join(emptydirs), |
|
592 | 592 | "basename": d} |
|
593 | 593 | |
|
594 | 594 | return web.sendtemplate( |
|
595 | 595 | 'manifest', |
|
596 | 596 | symrev=symrev, |
|
597 | 597 | path=abspath, |
|
598 | 598 | up=webutil.up(abspath), |
|
599 | 599 | upparity=next(parity), |
|
600 | 600 | fentries=filelist, |
|
601 | 601 | dentries=dirlist, |
|
602 | 602 | archives=web.archivelist(hex(node)), |
|
603 | 603 | **pycompat.strkwargs(webutil.commonentry(web.repo, ctx))) |
|
604 | 604 | |
|
605 | 605 | @webcommand('tags') |
|
606 | 606 | def tags(web): |
|
607 | 607 | """ |
|
608 | 608 | /tags |
|
609 | 609 | ----- |
|
610 | 610 | |
|
611 | 611 | Show information about tags. |
|
612 | 612 | |
|
613 | 613 | No arguments are accepted. |
|
614 | 614 | |
|
615 | 615 | The ``tags`` template is rendered. |
|
616 | 616 | """ |
|
617 | 617 | i = list(reversed(web.repo.tagslist())) |
|
618 | 618 | parity = paritygen(web.stripecount) |
|
619 | 619 | |
|
620 | 620 | def entries(notip, latestonly, **map): |
|
621 | 621 | t = i |
|
622 | 622 | if notip: |
|
623 | 623 | t = [(k, n) for k, n in i if k != "tip"] |
|
624 | 624 | if latestonly: |
|
625 | 625 | t = t[:1] |
|
626 | 626 | for k, n in t: |
|
627 | 627 | yield {"parity": next(parity), |
|
628 | 628 | "tag": k, |
|
629 | 629 | "date": web.repo[n].date(), |
|
630 | 630 | "node": hex(n)} |
|
631 | 631 | |
|
632 | 632 | return web.sendtemplate( |
|
633 | 633 | 'tags', |
|
634 | 634 | node=hex(web.repo.changelog.tip()), |
|
635 | 635 | entries=lambda **x: entries(False, False, **x), |
|
636 | 636 | entriesnotip=lambda **x: entries(True, False, **x), |
|
637 | 637 | latestentry=lambda **x: entries(True, True, **x)) |
|
638 | 638 | |
|
639 | 639 | @webcommand('bookmarks') |
|
640 | 640 | def bookmarks(web): |
|
641 | 641 | """ |
|
642 | 642 | /bookmarks |
|
643 | 643 | ---------- |
|
644 | 644 | |
|
645 | 645 | Show information about bookmarks. |
|
646 | 646 | |
|
647 | 647 | No arguments are accepted. |
|
648 | 648 | |
|
649 | 649 | The ``bookmarks`` template is rendered. |
|
650 | 650 | """ |
|
651 | 651 | i = [b for b in web.repo._bookmarks.items() if b[1] in web.repo] |
|
652 | 652 | sortkey = lambda b: (web.repo[b[1]].rev(), b[0]) |
|
653 | 653 | i = sorted(i, key=sortkey, reverse=True) |
|
654 | 654 | parity = paritygen(web.stripecount) |
|
655 | 655 | |
|
656 | 656 | def entries(latestonly, **map): |
|
657 | 657 | t = i |
|
658 | 658 | if latestonly: |
|
659 | 659 | t = i[:1] |
|
660 | 660 | for k, n in t: |
|
661 | 661 | yield {"parity": next(parity), |
|
662 | 662 | "bookmark": k, |
|
663 | 663 | "date": web.repo[n].date(), |
|
664 | 664 | "node": hex(n)} |
|
665 | 665 | |
|
666 | 666 | if i: |
|
667 | 667 | latestrev = i[0][1] |
|
668 | 668 | else: |
|
669 | 669 | latestrev = -1 |
|
670 | 670 | |
|
671 | 671 | return web.sendtemplate( |
|
672 | 672 | 'bookmarks', |
|
673 | 673 | node=hex(web.repo.changelog.tip()), |
|
674 | 674 | lastchange=[{'date': web.repo[latestrev].date()}], |
|
675 | 675 | entries=lambda **x: entries(latestonly=False, **x), |
|
676 | 676 | latestentry=lambda **x: entries(latestonly=True, **x)) |
|
677 | 677 | |
|
678 | 678 | @webcommand('branches') |
|
679 | 679 | def branches(web): |
|
680 | 680 | """ |
|
681 | 681 | /branches |
|
682 | 682 | --------- |
|
683 | 683 | |
|
684 | 684 | Show information about branches. |
|
685 | 685 | |
|
686 | 686 | All known branches are contained in the output, even closed branches. |
|
687 | 687 | |
|
688 | 688 | No arguments are accepted. |
|
689 | 689 | |
|
690 | 690 | The ``branches`` template is rendered. |
|
691 | 691 | """ |
|
692 | 692 | entries = webutil.branchentries(web.repo, web.stripecount) |
|
693 | 693 | latestentry = webutil.branchentries(web.repo, web.stripecount, 1) |
|
694 | 694 | |
|
695 | 695 | return web.sendtemplate( |
|
696 | 696 | 'branches', |
|
697 | 697 | node=hex(web.repo.changelog.tip()), |
|
698 | 698 | entries=entries, |
|
699 | 699 | latestentry=latestentry) |
|
700 | 700 | |
|
701 | 701 | @webcommand('summary') |
|
702 | 702 | def summary(web): |
|
703 | 703 | """ |
|
704 | 704 | /summary |
|
705 | 705 | -------- |
|
706 | 706 | |
|
707 | 707 | Show a summary of repository state. |
|
708 | 708 | |
|
709 | 709 | Information about the latest changesets, bookmarks, tags, and branches |
|
710 | 710 | is captured by this handler. |
|
711 | 711 | |
|
712 | 712 | The ``summary`` template is rendered. |
|
713 | 713 | """ |
|
714 | 714 | i = reversed(web.repo.tagslist()) |
|
715 | 715 | |
|
716 | 716 | def tagentries(context): |
|
717 | 717 | parity = paritygen(web.stripecount) |
|
718 | 718 | count = 0 |
|
719 | 719 | for k, n in i: |
|
720 | 720 | if k == "tip": # skip tip |
|
721 | 721 | continue |
|
722 | 722 | |
|
723 | 723 | count += 1 |
|
724 | 724 | if count > 10: # limit to 10 tags |
|
725 | 725 | break |
|
726 | 726 | |
|
727 | 727 | yield { |
|
728 | 728 | 'parity': next(parity), |
|
729 | 729 | 'tag': k, |
|
730 | 730 | 'node': hex(n), |
|
731 | 731 | 'date': web.repo[n].date(), |
|
732 | 732 | } |
|
733 | 733 | |
|
734 | 734 | def bookmarks(**map): |
|
735 | 735 | parity = paritygen(web.stripecount) |
|
736 | 736 | marks = [b for b in web.repo._bookmarks.items() if b[1] in web.repo] |
|
737 | 737 | sortkey = lambda b: (web.repo[b[1]].rev(), b[0]) |
|
738 | 738 | marks = sorted(marks, key=sortkey, reverse=True) |
|
739 | 739 | for k, n in marks[:10]: # limit to 10 bookmarks |
|
740 | 740 | yield {'parity': next(parity), |
|
741 | 741 | 'bookmark': k, |
|
742 | 742 | 'date': web.repo[n].date(), |
|
743 | 743 | 'node': hex(n)} |
|
744 | 744 | |
|
745 | 745 | def changelist(context): |
|
746 | 746 | parity = paritygen(web.stripecount, offset=start - end) |
|
747 | 747 | l = [] # build a list in forward order for efficiency |
|
748 | 748 | revs = [] |
|
749 | 749 | if start < end: |
|
750 | 750 | revs = web.repo.changelog.revs(start, end - 1) |
|
751 | 751 | for i in revs: |
|
752 | 752 | ctx = web.repo[i] |
|
753 | 753 | lm = webutil.commonentry(web.repo, ctx) |
|
754 | 754 | lm['parity'] = next(parity) |
|
755 | 755 | l.append(lm) |
|
756 | 756 | |
|
757 | 757 | for entry in reversed(l): |
|
758 | 758 | yield entry |
|
759 | 759 | |
|
760 | 760 | tip = web.repo['tip'] |
|
761 | 761 | count = len(web.repo) |
|
762 | 762 | start = max(0, count - web.maxchanges) |
|
763 | 763 | end = min(count, start + web.maxchanges) |
|
764 | 764 | |
|
765 | 765 | desc = web.config("web", "description") |
|
766 | 766 | if not desc: |
|
767 | 767 | desc = 'unknown' |
|
768 | 768 | labels = web.configlist('web', 'labels') |
|
769 | 769 | |
|
770 | 770 | return web.sendtemplate( |
|
771 | 771 | 'summary', |
|
772 | 772 | desc=desc, |
|
773 | 773 | owner=get_contact(web.config) or 'unknown', |
|
774 | 774 | lastchange=tip.date(), |
|
775 | 775 | tags=templateutil.mappinggenerator(tagentries, name='tagentry'), |
|
776 | 776 | bookmarks=bookmarks, |
|
777 | 777 | branches=webutil.branchentries(web.repo, web.stripecount, 10), |
|
778 | 778 | shortlog=templateutil.mappinggenerator(changelist, |
|
779 | 779 | name='shortlogentry'), |
|
780 | 780 | node=tip.hex(), |
|
781 | 781 | symrev='tip', |
|
782 | 782 | archives=web.archivelist('tip'), |
|
783 | 783 | labels=templateutil.hybridlist(labels, name='label')) |
|
784 | 784 | |
|
785 | 785 | @webcommand('filediff') |
|
786 | 786 | def filediff(web): |
|
787 | 787 | """ |
|
788 | 788 | /diff/{revision}/{path} |
|
789 | 789 | ----------------------- |
|
790 | 790 | |
|
791 | 791 | Show how a file changed in a particular commit. |
|
792 | 792 | |
|
793 | 793 | The ``filediff`` template is rendered. |
|
794 | 794 | |
|
795 | 795 | This handler is registered under both the ``/diff`` and ``/filediff`` |
|
796 | 796 | paths. ``/diff`` is used in modern code. |
|
797 | 797 | """ |
|
798 | 798 | fctx, ctx = None, None |
|
799 | 799 | try: |
|
800 | 800 | fctx = webutil.filectx(web.repo, web.req) |
|
801 | 801 | except LookupError: |
|
802 | 802 | ctx = webutil.changectx(web.repo, web.req) |
|
803 | 803 | path = webutil.cleanpath(web.repo, web.req.qsparams['file']) |
|
804 | 804 | if path not in ctx.files(): |
|
805 | 805 | raise |
|
806 | 806 | |
|
807 | 807 | if fctx is not None: |
|
808 | 808 | path = fctx.path() |
|
809 | 809 | ctx = fctx.changectx() |
|
810 | 810 | basectx = ctx.p1() |
|
811 | 811 | |
|
812 | 812 | style = web.config('web', 'style') |
|
813 | 813 | if 'style' in web.req.qsparams: |
|
814 | 814 | style = web.req.qsparams['style'] |
|
815 | 815 | |
|
816 | 816 | diffs = webutil.diffs(web, ctx, basectx, [path], style) |
|
817 | 817 | if fctx is not None: |
|
818 | 818 | rename = webutil.renamelink(fctx) |
|
819 | 819 | ctx = fctx |
|
820 | 820 | else: |
|
821 | 821 | rename = [] |
|
822 | 822 | ctx = ctx |
|
823 | 823 | |
|
824 | 824 | return web.sendtemplate( |
|
825 | 825 | 'filediff', |
|
826 | 826 | file=path, |
|
827 | 827 | symrev=webutil.symrevorshortnode(web.req, ctx), |
|
828 | 828 | rename=rename, |
|
829 | 829 | diff=diffs, |
|
830 | 830 | **pycompat.strkwargs(webutil.commonentry(web.repo, ctx))) |
|
831 | 831 | |
|
832 | 832 | diff = webcommand('diff')(filediff) |
|
833 | 833 | |
|
834 | 834 | @webcommand('comparison') |
|
835 | 835 | def comparison(web): |
|
836 | 836 | """ |
|
837 | 837 | /comparison/{revision}/{path} |
|
838 | 838 | ----------------------------- |
|
839 | 839 | |
|
840 | 840 | Show a comparison between the old and new versions of a file from changes |
|
841 | 841 | made on a particular revision. |
|
842 | 842 | |
|
843 | 843 | This is similar to the ``diff`` handler. However, this form features |
|
844 | 844 | a split or side-by-side diff rather than a unified diff. |
|
845 | 845 | |
|
846 | 846 | The ``context`` query string argument can be used to control the lines of |
|
847 | 847 | context in the diff. |
|
848 | 848 | |
|
849 | 849 | The ``filecomparison`` template is rendered. |
|
850 | 850 | """ |
|
851 | 851 | ctx = webutil.changectx(web.repo, web.req) |
|
852 | 852 | if 'file' not in web.req.qsparams: |
|
853 | 853 | raise ErrorResponse(HTTP_NOT_FOUND, 'file not given') |
|
854 | 854 | path = webutil.cleanpath(web.repo, web.req.qsparams['file']) |
|
855 | 855 | |
|
856 | 856 | parsecontext = lambda v: v == 'full' and -1 or int(v) |
|
857 | 857 | if 'context' in web.req.qsparams: |
|
858 | 858 | context = parsecontext(web.req.qsparams['context']) |
|
859 | 859 | else: |
|
860 | 860 | context = parsecontext(web.config('web', 'comparisoncontext', '5')) |
|
861 | 861 | |
|
862 | 862 | def filelines(f): |
|
863 | 863 | if f.isbinary(): |
|
864 | 864 | mt = mimetypes.guess_type(f.path())[0] |
|
865 | 865 | if not mt: |
|
866 | 866 | mt = 'application/octet-stream' |
|
867 | 867 | return [_('(binary file %s, hash: %s)') % (mt, hex(f.filenode()))] |
|
868 | 868 | return f.data().splitlines() |
|
869 | 869 | |
|
870 | 870 | fctx = None |
|
871 | 871 | parent = ctx.p1() |
|
872 | 872 | leftrev = parent.rev() |
|
873 | 873 | leftnode = parent.node() |
|
874 | 874 | rightrev = ctx.rev() |
|
875 | 875 | rightnode = ctx.node() |
|
876 | 876 | if path in ctx: |
|
877 | 877 | fctx = ctx[path] |
|
878 | 878 | rightlines = filelines(fctx) |
|
879 | 879 | if path not in parent: |
|
880 | 880 | leftlines = () |
|
881 | 881 | else: |
|
882 | 882 | pfctx = parent[path] |
|
883 | 883 | leftlines = filelines(pfctx) |
|
884 | 884 | else: |
|
885 | 885 | rightlines = () |
|
886 | 886 | pfctx = ctx.parents()[0][path] |
|
887 | 887 | leftlines = filelines(pfctx) |
|
888 | 888 | |
|
889 | 889 | comparison = webutil.compare(web.tmpl, context, leftlines, rightlines) |
|
890 | 890 | if fctx is not None: |
|
891 | 891 | rename = webutil.renamelink(fctx) |
|
892 | 892 | ctx = fctx |
|
893 | 893 | else: |
|
894 | 894 | rename = [] |
|
895 | 895 | ctx = ctx |
|
896 | 896 | |
|
897 | 897 | return web.sendtemplate( |
|
898 | 898 | 'filecomparison', |
|
899 | 899 | file=path, |
|
900 | 900 | symrev=webutil.symrevorshortnode(web.req, ctx), |
|
901 | 901 | rename=rename, |
|
902 | 902 | leftrev=leftrev, |
|
903 | 903 | leftnode=hex(leftnode), |
|
904 | 904 | rightrev=rightrev, |
|
905 | 905 | rightnode=hex(rightnode), |
|
906 | 906 | comparison=comparison, |
|
907 | 907 | **pycompat.strkwargs(webutil.commonentry(web.repo, ctx))) |
|
908 | 908 | |
|
909 | 909 | @webcommand('annotate') |
|
910 | 910 | def annotate(web): |
|
911 | 911 | """ |
|
912 | 912 | /annotate/{revision}/{path} |
|
913 | 913 | --------------------------- |
|
914 | 914 | |
|
915 | 915 | Show changeset information for each line in a file. |
|
916 | 916 | |
|
917 | 917 | The ``ignorews``, ``ignorewsamount``, ``ignorewseol``, and |
|
918 | 918 | ``ignoreblanklines`` query string arguments have the same meaning as |
|
919 | 919 | their ``[annotate]`` config equivalents. It uses the hgrc boolean |
|
920 | 920 | parsing logic to interpret the value. e.g. ``0`` and ``false`` are |
|
921 | 921 | false and ``1`` and ``true`` are true. If not defined, the server |
|
922 | 922 | default settings are used. |
|
923 | 923 | |
|
924 | 924 | The ``fileannotate`` template is rendered. |
|
925 | 925 | """ |
|
926 | 926 | fctx = webutil.filectx(web.repo, web.req) |
|
927 | 927 | f = fctx.path() |
|
928 | 928 | parity = paritygen(web.stripecount) |
|
929 | 929 | ishead = fctx.filerev() in fctx.filelog().headrevs() |
|
930 | 930 | |
|
931 | 931 | # parents() is called once per line and several lines likely belong to |
|
932 | 932 | # same revision. So it is worth caching. |
|
933 | 933 | # TODO there are still redundant operations within basefilectx.parents() |
|
934 | 934 | # and from the fctx.annotate() call itself that could be cached. |
|
935 | 935 | parentscache = {} |
|
936 | 936 | def parents(f): |
|
937 | 937 | rev = f.rev() |
|
938 | 938 | if rev not in parentscache: |
|
939 | 939 | parentscache[rev] = [] |
|
940 | 940 | for p in f.parents(): |
|
941 | 941 | entry = { |
|
942 | 942 | 'node': p.hex(), |
|
943 | 943 | 'rev': p.rev(), |
|
944 | 944 | } |
|
945 | 945 | parentscache[rev].append(entry) |
|
946 | 946 | |
|
947 | 947 | for p in parentscache[rev]: |
|
948 | 948 | yield p |
|
949 | 949 | |
|
950 | 950 | def annotate(**map): |
|
951 | 951 | if fctx.isbinary(): |
|
952 | 952 | mt = (mimetypes.guess_type(fctx.path())[0] |
|
953 | 953 | or 'application/octet-stream') |
|
954 | 954 | lines = [dagop.annotateline(fctx=fctx.filectx(fctx.filerev()), |
|
955 | 955 | lineno=1, text='(binary:%s)' % mt)] |
|
956 | 956 | else: |
|
957 | 957 | lines = webutil.annotate(web.req, fctx, web.repo.ui) |
|
958 | 958 | |
|
959 | 959 | previousrev = None |
|
960 | 960 | blockparitygen = paritygen(1) |
|
961 | 961 | for lineno, aline in enumerate(lines): |
|
962 | 962 | f = aline.fctx |
|
963 | 963 | rev = f.rev() |
|
964 | 964 | if rev != previousrev: |
|
965 | 965 | blockhead = True |
|
966 | 966 | blockparity = next(blockparitygen) |
|
967 | 967 | else: |
|
968 | 968 | blockhead = None |
|
969 | 969 | previousrev = rev |
|
970 | 970 | yield {"parity": next(parity), |
|
971 | 971 | "node": f.hex(), |
|
972 | 972 | "rev": rev, |
|
973 | 973 | "author": f.user(), |
|
974 | 974 | "parents": parents(f), |
|
975 | 975 | "desc": f.description(), |
|
976 | 976 | "extra": f.extra(), |
|
977 | 977 | "file": f.path(), |
|
978 | 978 | "blockhead": blockhead, |
|
979 | 979 | "blockparity": blockparity, |
|
980 | 980 | "targetline": aline.lineno, |
|
981 | 981 | "line": aline.text, |
|
982 | 982 | "lineno": lineno + 1, |
|
983 | 983 | "lineid": "l%d" % (lineno + 1), |
|
984 | 984 | "linenumber": "% 6d" % (lineno + 1), |
|
985 | 985 | "revdate": f.date()} |
|
986 | 986 | |
|
987 | 987 | diffopts = webutil.difffeatureopts(web.req, web.repo.ui, 'annotate') |
|
988 | 988 | diffopts = {k: getattr(diffopts, k) for k in diffopts.defaults} |
|
989 | 989 | |
|
990 | 990 | return web.sendtemplate( |
|
991 | 991 | 'fileannotate', |
|
992 | 992 | file=f, |
|
993 | 993 | annotate=annotate, |
|
994 | 994 | path=webutil.up(f), |
|
995 | 995 | symrev=webutil.symrevorshortnode(web.req, fctx), |
|
996 | 996 | rename=webutil.renamelink(fctx), |
|
997 | 997 | permissions=fctx.manifest().flags(f), |
|
998 | 998 | ishead=int(ishead), |
|
999 | 999 | diffopts=diffopts, |
|
1000 | 1000 | **pycompat.strkwargs(webutil.commonentry(web.repo, fctx))) |
|
1001 | 1001 | |
|
1002 | 1002 | @webcommand('filelog') |
|
1003 | 1003 | def filelog(web): |
|
1004 | 1004 | """ |
|
1005 | 1005 | /filelog/{revision}/{path} |
|
1006 | 1006 | -------------------------- |
|
1007 | 1007 | |
|
1008 | 1008 | Show information about the history of a file in the repository. |
|
1009 | 1009 | |
|
1010 | 1010 | The ``revcount`` query string argument can be defined to control the |
|
1011 | 1011 | maximum number of entries to show. |
|
1012 | 1012 | |
|
1013 | 1013 | The ``filelog`` template will be rendered. |
|
1014 | 1014 | """ |
|
1015 | 1015 | |
|
1016 | 1016 | try: |
|
1017 | 1017 | fctx = webutil.filectx(web.repo, web.req) |
|
1018 | 1018 | f = fctx.path() |
|
1019 | 1019 | fl = fctx.filelog() |
|
1020 | 1020 | except error.LookupError: |
|
1021 | 1021 | f = webutil.cleanpath(web.repo, web.req.qsparams['file']) |
|
1022 | 1022 | fl = web.repo.file(f) |
|
1023 | 1023 | numrevs = len(fl) |
|
1024 | 1024 | if not numrevs: # file doesn't exist at all |
|
1025 | 1025 | raise |
|
1026 | 1026 | rev = webutil.changectx(web.repo, web.req).rev() |
|
1027 | 1027 | first = fl.linkrev(0) |
|
1028 | 1028 | if rev < first: # current rev is from before file existed |
|
1029 | 1029 | raise |
|
1030 | 1030 | frev = numrevs - 1 |
|
1031 | 1031 | while fl.linkrev(frev) > rev: |
|
1032 | 1032 | frev -= 1 |
|
1033 | 1033 | fctx = web.repo.filectx(f, fl.linkrev(frev)) |
|
1034 | 1034 | |
|
1035 | 1035 | revcount = web.maxshortchanges |
|
1036 | 1036 | if 'revcount' in web.req.qsparams: |
|
1037 | 1037 | try: |
|
1038 | 1038 | revcount = int(web.req.qsparams.get('revcount', revcount)) |
|
1039 | 1039 | revcount = max(revcount, 1) |
|
1040 | 1040 | web.tmpl.defaults['sessionvars']['revcount'] = revcount |
|
1041 | 1041 | except ValueError: |
|
1042 | 1042 | pass |
|
1043 | 1043 | |
|
1044 | 1044 | lrange = webutil.linerange(web.req) |
|
1045 | 1045 | |
|
1046 | 1046 | lessvars = copy.copy(web.tmpl.defaults['sessionvars']) |
|
1047 | 1047 | lessvars['revcount'] = max(revcount // 2, 1) |
|
1048 | 1048 | morevars = copy.copy(web.tmpl.defaults['sessionvars']) |
|
1049 | 1049 | morevars['revcount'] = revcount * 2 |
|
1050 | 1050 | |
|
1051 | 1051 | patch = 'patch' in web.req.qsparams |
|
1052 | 1052 | if patch: |
|
1053 | 1053 | lessvars['patch'] = morevars['patch'] = web.req.qsparams['patch'] |
|
1054 | 1054 | descend = 'descend' in web.req.qsparams |
|
1055 | 1055 | if descend: |
|
1056 | 1056 | lessvars['descend'] = morevars['descend'] = web.req.qsparams['descend'] |
|
1057 | 1057 | |
|
1058 | 1058 | count = fctx.filerev() + 1 |
|
1059 | 1059 | start = max(0, count - revcount) # first rev on this page |
|
1060 | 1060 | end = min(count, start + revcount) # last rev on this page |
|
1061 | 1061 | parity = paritygen(web.stripecount, offset=start - end) |
|
1062 | 1062 | |
|
1063 | 1063 | repo = web.repo |
|
1064 | 1064 | filelog = fctx.filelog() |
|
1065 | 1065 | revs = [filerev for filerev in filelog.revs(start, end - 1) |
|
1066 | 1066 | if filelog.linkrev(filerev) in repo] |
|
1067 | 1067 | entries = [] |
|
1068 | 1068 | |
|
1069 | 1069 | diffstyle = web.config('web', 'style') |
|
1070 | 1070 | if 'style' in web.req.qsparams: |
|
1071 | 1071 | diffstyle = web.req.qsparams['style'] |
|
1072 | 1072 | |
|
1073 | 1073 | def diff(fctx, linerange=None): |
|
1074 | 1074 | ctx = fctx.changectx() |
|
1075 | 1075 | basectx = ctx.p1() |
|
1076 | 1076 | path = fctx.path() |
|
1077 | 1077 | return webutil.diffs(web, ctx, basectx, [path], diffstyle, |
|
1078 | 1078 | linerange=linerange, |
|
1079 | 1079 | lineidprefix='%s-' % ctx.hex()[:12]) |
|
1080 | 1080 | |
|
1081 | 1081 | linerange = None |
|
1082 | 1082 | if lrange is not None: |
|
1083 | 1083 | linerange = webutil.formatlinerange(*lrange) |
|
1084 | 1084 | # deactivate numeric nav links when linerange is specified as this |
|
1085 | 1085 | # would required a dedicated "revnav" class |
|
1086 | 1086 | nav = [] |
|
1087 | 1087 | if descend: |
|
1088 | 1088 | it = dagop.blockdescendants(fctx, *lrange) |
|
1089 | 1089 | else: |
|
1090 | 1090 | it = dagop.blockancestors(fctx, *lrange) |
|
1091 | 1091 | for i, (c, lr) in enumerate(it, 1): |
|
1092 | 1092 | diffs = None |
|
1093 | 1093 | if patch: |
|
1094 | 1094 | diffs = diff(c, linerange=lr) |
|
1095 | 1095 | # follow renames accross filtered (not in range) revisions |
|
1096 | 1096 | path = c.path() |
|
1097 | 1097 | entries.append(dict( |
|
1098 | 1098 | parity=next(parity), |
|
1099 | 1099 | filerev=c.rev(), |
|
1100 | 1100 | file=path, |
|
1101 | 1101 | diff=diffs, |
|
1102 | 1102 | linerange=webutil.formatlinerange(*lr), |
|
1103 | 1103 | **pycompat.strkwargs(webutil.commonentry(repo, c)))) |
|
1104 | 1104 | if i == revcount: |
|
1105 | 1105 | break |
|
1106 | 1106 | lessvars['linerange'] = webutil.formatlinerange(*lrange) |
|
1107 | 1107 | morevars['linerange'] = lessvars['linerange'] |
|
1108 | 1108 | else: |
|
1109 | 1109 | for i in revs: |
|
1110 | 1110 | iterfctx = fctx.filectx(i) |
|
1111 | 1111 | diffs = None |
|
1112 | 1112 | if patch: |
|
1113 | 1113 | diffs = diff(iterfctx) |
|
1114 | 1114 | entries.append(dict( |
|
1115 | 1115 | parity=next(parity), |
|
1116 | 1116 | filerev=i, |
|
1117 | 1117 | file=f, |
|
1118 | 1118 | diff=diffs, |
|
1119 | 1119 | rename=webutil.renamelink(iterfctx), |
|
1120 | 1120 | **pycompat.strkwargs(webutil.commonentry(repo, iterfctx)))) |
|
1121 | 1121 | entries.reverse() |
|
1122 | 1122 | revnav = webutil.filerevnav(web.repo, fctx.path()) |
|
1123 | 1123 | nav = revnav.gen(end - 1, revcount, count) |
|
1124 | 1124 | |
|
1125 | 1125 | latestentry = entries[:1] |
|
1126 | 1126 | |
|
1127 | 1127 | return web.sendtemplate( |
|
1128 | 1128 | 'filelog', |
|
1129 | 1129 | file=f, |
|
1130 | 1130 | nav=nav, |
|
1131 | 1131 | symrev=webutil.symrevorshortnode(web.req, fctx), |
|
1132 | 1132 | entries=entries, |
|
1133 | 1133 | descend=descend, |
|
1134 | 1134 | patch=patch, |
|
1135 | 1135 | latestentry=latestentry, |
|
1136 | 1136 | linerange=linerange, |
|
1137 | 1137 | revcount=revcount, |
|
1138 | 1138 | morevars=morevars, |
|
1139 | 1139 | lessvars=lessvars, |
|
1140 | 1140 | **pycompat.strkwargs(webutil.commonentry(web.repo, fctx))) |
|
1141 | 1141 | |
|
1142 | 1142 | @webcommand('archive') |
|
1143 | 1143 | def archive(web): |
|
1144 | 1144 | """ |
|
1145 | 1145 | /archive/{revision}.{format}[/{path}] |
|
1146 | 1146 | ------------------------------------- |
|
1147 | 1147 | |
|
1148 | 1148 | Obtain an archive of repository content. |
|
1149 | 1149 | |
|
1150 | 1150 | The content and type of the archive is defined by a URL path parameter. |
|
1151 | 1151 | ``format`` is the file extension of the archive type to be generated. e.g. |
|
1152 | 1152 | ``zip`` or ``tar.bz2``. Not all archive types may be allowed by your |
|
1153 | 1153 | server configuration. |
|
1154 | 1154 | |
|
1155 | 1155 | The optional ``path`` URL parameter controls content to include in the |
|
1156 | 1156 | archive. If omitted, every file in the specified revision is present in the |
|
1157 | 1157 | archive. If included, only the specified file or contents of the specified |
|
1158 | 1158 | directory will be included in the archive. |
|
1159 | 1159 | |
|
1160 | 1160 | No template is used for this handler. Raw, binary content is generated. |
|
1161 | 1161 | """ |
|
1162 | 1162 | |
|
1163 | 1163 | type_ = web.req.qsparams.get('type') |
|
1164 | 1164 | allowed = web.configlist("web", "allow_archive") |
|
1165 | 1165 | key = web.req.qsparams['node'] |
|
1166 | 1166 | |
|
1167 | if type_ not in web.archivespecs: | |
|
1167 | if type_ not in webutil.archivespecs: | |
|
1168 | 1168 | msg = 'Unsupported archive type: %s' % type_ |
|
1169 | 1169 | raise ErrorResponse(HTTP_NOT_FOUND, msg) |
|
1170 | 1170 | |
|
1171 | 1171 | if not ((type_ in allowed or |
|
1172 | 1172 | web.configbool("web", "allow" + type_))): |
|
1173 | 1173 | msg = 'Archive type not allowed: %s' % type_ |
|
1174 | 1174 | raise ErrorResponse(HTTP_FORBIDDEN, msg) |
|
1175 | 1175 | |
|
1176 | 1176 | reponame = re.sub(br"\W+", "-", os.path.basename(web.reponame)) |
|
1177 | 1177 | cnode = web.repo.lookup(key) |
|
1178 | 1178 | arch_version = key |
|
1179 | 1179 | if cnode == key or key == 'tip': |
|
1180 | 1180 | arch_version = short(cnode) |
|
1181 | 1181 | name = "%s-%s" % (reponame, arch_version) |
|
1182 | 1182 | |
|
1183 | 1183 | ctx = webutil.changectx(web.repo, web.req) |
|
1184 | 1184 | pats = [] |
|
1185 | 1185 | match = scmutil.match(ctx, []) |
|
1186 | 1186 | file = web.req.qsparams.get('file') |
|
1187 | 1187 | if file: |
|
1188 | 1188 | pats = ['path:' + file] |
|
1189 | 1189 | match = scmutil.match(ctx, pats, default='path') |
|
1190 | 1190 | if pats: |
|
1191 | 1191 | files = [f for f in ctx.manifest().keys() if match(f)] |
|
1192 | 1192 | if not files: |
|
1193 | 1193 | raise ErrorResponse(HTTP_NOT_FOUND, |
|
1194 | 1194 | 'file(s) not found: %s' % file) |
|
1195 | 1195 | |
|
1196 | mimetype, artype, extension, encoding = web.archivespecs[type_] | |
|
1196 | mimetype, artype, extension, encoding = webutil.archivespecs[type_] | |
|
1197 | 1197 | |
|
1198 | 1198 | web.res.headers['Content-Type'] = mimetype |
|
1199 | 1199 | web.res.headers['Content-Disposition'] = 'attachment; filename=%s%s' % ( |
|
1200 | 1200 | name, extension) |
|
1201 | 1201 | |
|
1202 | 1202 | if encoding: |
|
1203 | 1203 | web.res.headers['Content-Encoding'] = encoding |
|
1204 | 1204 | |
|
1205 | 1205 | web.res.setbodywillwrite() |
|
1206 | 1206 | if list(web.res.sendresponse()): |
|
1207 | 1207 | raise error.ProgrammingError('sendresponse() should not emit data ' |
|
1208 | 1208 | 'if writing later') |
|
1209 | 1209 | |
|
1210 | 1210 | bodyfh = web.res.getbodyfile() |
|
1211 | 1211 | |
|
1212 | 1212 | archival.archive(web.repo, bodyfh, cnode, artype, prefix=name, |
|
1213 | 1213 | matchfn=match, |
|
1214 | 1214 | subrepos=web.configbool("web", "archivesubrepos")) |
|
1215 | 1215 | |
|
1216 | 1216 | return [] |
|
1217 | 1217 | |
|
1218 | 1218 | @webcommand('static') |
|
1219 | 1219 | def static(web): |
|
1220 | 1220 | fname = web.req.qsparams['file'] |
|
1221 | 1221 | # a repo owner may set web.static in .hg/hgrc to get any file |
|
1222 | 1222 | # readable by the user running the CGI script |
|
1223 | 1223 | static = web.config("web", "static", None, untrusted=False) |
|
1224 | 1224 | if not static: |
|
1225 | 1225 | tp = web.templatepath or templater.templatepaths() |
|
1226 | 1226 | if isinstance(tp, str): |
|
1227 | 1227 | tp = [tp] |
|
1228 | 1228 | static = [os.path.join(p, 'static') for p in tp] |
|
1229 | 1229 | |
|
1230 | 1230 | staticfile(static, fname, web.res) |
|
1231 | 1231 | return web.res.sendresponse() |
|
1232 | 1232 | |
|
1233 | 1233 | @webcommand('graph') |
|
1234 | 1234 | def graph(web): |
|
1235 | 1235 | """ |
|
1236 | 1236 | /graph[/{revision}] |
|
1237 | 1237 | ------------------- |
|
1238 | 1238 | |
|
1239 | 1239 | Show information about the graphical topology of the repository. |
|
1240 | 1240 | |
|
1241 | 1241 | Information rendered by this handler can be used to create visual |
|
1242 | 1242 | representations of repository topology. |
|
1243 | 1243 | |
|
1244 | 1244 | The ``revision`` URL parameter controls the starting changeset. If it's |
|
1245 | 1245 | absent, the default is ``tip``. |
|
1246 | 1246 | |
|
1247 | 1247 | The ``revcount`` query string argument can define the number of changesets |
|
1248 | 1248 | to show information for. |
|
1249 | 1249 | |
|
1250 | 1250 | The ``graphtop`` query string argument can specify the starting changeset |
|
1251 | 1251 | for producing ``jsdata`` variable that is used for rendering graph in |
|
1252 | 1252 | JavaScript. By default it has the same value as ``revision``. |
|
1253 | 1253 | |
|
1254 | 1254 | This handler will render the ``graph`` template. |
|
1255 | 1255 | """ |
|
1256 | 1256 | |
|
1257 | 1257 | if 'node' in web.req.qsparams: |
|
1258 | 1258 | ctx = webutil.changectx(web.repo, web.req) |
|
1259 | 1259 | symrev = webutil.symrevorshortnode(web.req, ctx) |
|
1260 | 1260 | else: |
|
1261 | 1261 | ctx = web.repo['tip'] |
|
1262 | 1262 | symrev = 'tip' |
|
1263 | 1263 | rev = ctx.rev() |
|
1264 | 1264 | |
|
1265 | 1265 | bg_height = 39 |
|
1266 | 1266 | revcount = web.maxshortchanges |
|
1267 | 1267 | if 'revcount' in web.req.qsparams: |
|
1268 | 1268 | try: |
|
1269 | 1269 | revcount = int(web.req.qsparams.get('revcount', revcount)) |
|
1270 | 1270 | revcount = max(revcount, 1) |
|
1271 | 1271 | web.tmpl.defaults['sessionvars']['revcount'] = revcount |
|
1272 | 1272 | except ValueError: |
|
1273 | 1273 | pass |
|
1274 | 1274 | |
|
1275 | 1275 | lessvars = copy.copy(web.tmpl.defaults['sessionvars']) |
|
1276 | 1276 | lessvars['revcount'] = max(revcount // 2, 1) |
|
1277 | 1277 | morevars = copy.copy(web.tmpl.defaults['sessionvars']) |
|
1278 | 1278 | morevars['revcount'] = revcount * 2 |
|
1279 | 1279 | |
|
1280 | 1280 | graphtop = web.req.qsparams.get('graphtop', ctx.hex()) |
|
1281 | 1281 | graphvars = copy.copy(web.tmpl.defaults['sessionvars']) |
|
1282 | 1282 | graphvars['graphtop'] = graphtop |
|
1283 | 1283 | |
|
1284 | 1284 | count = len(web.repo) |
|
1285 | 1285 | pos = rev |
|
1286 | 1286 | |
|
1287 | 1287 | uprev = min(max(0, count - 1), rev + revcount) |
|
1288 | 1288 | downrev = max(0, rev - revcount) |
|
1289 | 1289 | changenav = webutil.revnav(web.repo).gen(pos, revcount, count) |
|
1290 | 1290 | |
|
1291 | 1291 | tree = [] |
|
1292 | 1292 | nextentry = [] |
|
1293 | 1293 | lastrev = 0 |
|
1294 | 1294 | if pos != -1: |
|
1295 | 1295 | allrevs = web.repo.changelog.revs(pos, 0) |
|
1296 | 1296 | revs = [] |
|
1297 | 1297 | for i in allrevs: |
|
1298 | 1298 | revs.append(i) |
|
1299 | 1299 | if len(revs) >= revcount + 1: |
|
1300 | 1300 | break |
|
1301 | 1301 | |
|
1302 | 1302 | if len(revs) > revcount: |
|
1303 | 1303 | nextentry = [webutil.commonentry(web.repo, web.repo[revs[-1]])] |
|
1304 | 1304 | revs = revs[:-1] |
|
1305 | 1305 | |
|
1306 | 1306 | lastrev = revs[-1] |
|
1307 | 1307 | |
|
1308 | 1308 | # We have to feed a baseset to dagwalker as it is expecting smartset |
|
1309 | 1309 | # object. This does not have a big impact on hgweb performance itself |
|
1310 | 1310 | # since hgweb graphing code is not itself lazy yet. |
|
1311 | 1311 | dag = graphmod.dagwalker(web.repo, smartset.baseset(revs)) |
|
1312 | 1312 | # As we said one line above... not lazy. |
|
1313 | 1313 | tree = list(item for item in graphmod.colored(dag, web.repo) |
|
1314 | 1314 | if item[1] == graphmod.CHANGESET) |
|
1315 | 1315 | |
|
1316 | 1316 | def nodecurrent(ctx): |
|
1317 | 1317 | wpnodes = web.repo.dirstate.parents() |
|
1318 | 1318 | if wpnodes[1] == nullid: |
|
1319 | 1319 | wpnodes = wpnodes[:1] |
|
1320 | 1320 | if ctx.node() in wpnodes: |
|
1321 | 1321 | return '@' |
|
1322 | 1322 | return '' |
|
1323 | 1323 | |
|
1324 | 1324 | def nodesymbol(ctx): |
|
1325 | 1325 | if ctx.obsolete(): |
|
1326 | 1326 | return 'x' |
|
1327 | 1327 | elif ctx.isunstable(): |
|
1328 | 1328 | return '*' |
|
1329 | 1329 | elif ctx.closesbranch(): |
|
1330 | 1330 | return '_' |
|
1331 | 1331 | else: |
|
1332 | 1332 | return 'o' |
|
1333 | 1333 | |
|
1334 | 1334 | def fulltree(): |
|
1335 | 1335 | pos = web.repo[graphtop].rev() |
|
1336 | 1336 | tree = [] |
|
1337 | 1337 | if pos != -1: |
|
1338 | 1338 | revs = web.repo.changelog.revs(pos, lastrev) |
|
1339 | 1339 | dag = graphmod.dagwalker(web.repo, smartset.baseset(revs)) |
|
1340 | 1340 | tree = list(item for item in graphmod.colored(dag, web.repo) |
|
1341 | 1341 | if item[1] == graphmod.CHANGESET) |
|
1342 | 1342 | return tree |
|
1343 | 1343 | |
|
1344 | 1344 | def jsdata(): |
|
1345 | 1345 | return [{'node': pycompat.bytestr(ctx), |
|
1346 | 1346 | 'graphnode': nodecurrent(ctx) + nodesymbol(ctx), |
|
1347 | 1347 | 'vertex': vtx, |
|
1348 | 1348 | 'edges': edges} |
|
1349 | 1349 | for (id, type, ctx, vtx, edges) in fulltree()] |
|
1350 | 1350 | |
|
1351 | 1351 | def nodes(): |
|
1352 | 1352 | parity = paritygen(web.stripecount) |
|
1353 | 1353 | for row, (id, type, ctx, vtx, edges) in enumerate(tree): |
|
1354 | 1354 | entry = webutil.commonentry(web.repo, ctx) |
|
1355 | 1355 | edgedata = [{'col': edge[0], |
|
1356 | 1356 | 'nextcol': edge[1], |
|
1357 | 1357 | 'color': (edge[2] - 1) % 6 + 1, |
|
1358 | 1358 | 'width': edge[3], |
|
1359 | 1359 | 'bcolor': edge[4]} |
|
1360 | 1360 | for edge in edges] |
|
1361 | 1361 | |
|
1362 | 1362 | entry.update({'col': vtx[0], |
|
1363 | 1363 | 'color': (vtx[1] - 1) % 6 + 1, |
|
1364 | 1364 | 'parity': next(parity), |
|
1365 | 1365 | 'edges': edgedata, |
|
1366 | 1366 | 'row': row, |
|
1367 | 1367 | 'nextrow': row + 1}) |
|
1368 | 1368 | |
|
1369 | 1369 | yield entry |
|
1370 | 1370 | |
|
1371 | 1371 | rows = len(tree) |
|
1372 | 1372 | |
|
1373 | 1373 | return web.sendtemplate( |
|
1374 | 1374 | 'graph', |
|
1375 | 1375 | rev=rev, |
|
1376 | 1376 | symrev=symrev, |
|
1377 | 1377 | revcount=revcount, |
|
1378 | 1378 | uprev=uprev, |
|
1379 | 1379 | lessvars=lessvars, |
|
1380 | 1380 | morevars=morevars, |
|
1381 | 1381 | downrev=downrev, |
|
1382 | 1382 | graphvars=graphvars, |
|
1383 | 1383 | rows=rows, |
|
1384 | 1384 | bg_height=bg_height, |
|
1385 | 1385 | changesets=count, |
|
1386 | 1386 | nextentry=nextentry, |
|
1387 | 1387 | jsdata=lambda **x: jsdata(), |
|
1388 | 1388 | nodes=lambda **x: nodes(), |
|
1389 | 1389 | node=ctx.hex(), |
|
1390 | 1390 | changenav=changenav) |
|
1391 | 1391 | |
|
1392 | 1392 | def _getdoc(e): |
|
1393 | 1393 | doc = e[0].__doc__ |
|
1394 | 1394 | if doc: |
|
1395 | 1395 | doc = _(doc).partition('\n')[0] |
|
1396 | 1396 | else: |
|
1397 | 1397 | doc = _('(no help text available)') |
|
1398 | 1398 | return doc |
|
1399 | 1399 | |
|
1400 | 1400 | @webcommand('help') |
|
1401 | 1401 | def help(web): |
|
1402 | 1402 | """ |
|
1403 | 1403 | /help[/{topic}] |
|
1404 | 1404 | --------------- |
|
1405 | 1405 | |
|
1406 | 1406 | Render help documentation. |
|
1407 | 1407 | |
|
1408 | 1408 | This web command is roughly equivalent to :hg:`help`. If a ``topic`` |
|
1409 | 1409 | is defined, that help topic will be rendered. If not, an index of |
|
1410 | 1410 | available help topics will be rendered. |
|
1411 | 1411 | |
|
1412 | 1412 | The ``help`` template will be rendered when requesting help for a topic. |
|
1413 | 1413 | ``helptopics`` will be rendered for the index of help topics. |
|
1414 | 1414 | """ |
|
1415 | 1415 | from .. import commands, help as helpmod # avoid cycle |
|
1416 | 1416 | |
|
1417 | 1417 | topicname = web.req.qsparams.get('node') |
|
1418 | 1418 | if not topicname: |
|
1419 | 1419 | def topics(**map): |
|
1420 | 1420 | for entries, summary, _doc in helpmod.helptable: |
|
1421 | 1421 | yield {'topic': entries[0], 'summary': summary} |
|
1422 | 1422 | |
|
1423 | 1423 | early, other = [], [] |
|
1424 | 1424 | primary = lambda s: s.partition('|')[0] |
|
1425 | 1425 | for c, e in commands.table.iteritems(): |
|
1426 | 1426 | doc = _getdoc(e) |
|
1427 | 1427 | if 'DEPRECATED' in doc or c.startswith('debug'): |
|
1428 | 1428 | continue |
|
1429 | 1429 | cmd = primary(c) |
|
1430 | 1430 | if cmd.startswith('^'): |
|
1431 | 1431 | early.append((cmd[1:], doc)) |
|
1432 | 1432 | else: |
|
1433 | 1433 | other.append((cmd, doc)) |
|
1434 | 1434 | |
|
1435 | 1435 | early.sort() |
|
1436 | 1436 | other.sort() |
|
1437 | 1437 | |
|
1438 | 1438 | def earlycommands(**map): |
|
1439 | 1439 | for c, doc in early: |
|
1440 | 1440 | yield {'topic': c, 'summary': doc} |
|
1441 | 1441 | |
|
1442 | 1442 | def othercommands(**map): |
|
1443 | 1443 | for c, doc in other: |
|
1444 | 1444 | yield {'topic': c, 'summary': doc} |
|
1445 | 1445 | |
|
1446 | 1446 | return web.sendtemplate( |
|
1447 | 1447 | 'helptopics', |
|
1448 | 1448 | topics=topics, |
|
1449 | 1449 | earlycommands=earlycommands, |
|
1450 | 1450 | othercommands=othercommands, |
|
1451 | 1451 | title='Index') |
|
1452 | 1452 | |
|
1453 | 1453 | # Render an index of sub-topics. |
|
1454 | 1454 | if topicname in helpmod.subtopics: |
|
1455 | 1455 | topics = [] |
|
1456 | 1456 | for entries, summary, _doc in helpmod.subtopics[topicname]: |
|
1457 | 1457 | topics.append({ |
|
1458 | 1458 | 'topic': '%s.%s' % (topicname, entries[0]), |
|
1459 | 1459 | 'basename': entries[0], |
|
1460 | 1460 | 'summary': summary, |
|
1461 | 1461 | }) |
|
1462 | 1462 | |
|
1463 | 1463 | return web.sendtemplate( |
|
1464 | 1464 | 'helptopics', |
|
1465 | 1465 | topics=topics, |
|
1466 | 1466 | title=topicname, |
|
1467 | 1467 | subindex=True) |
|
1468 | 1468 | |
|
1469 | 1469 | u = webutil.wsgiui.load() |
|
1470 | 1470 | u.verbose = True |
|
1471 | 1471 | |
|
1472 | 1472 | # Render a page from a sub-topic. |
|
1473 | 1473 | if '.' in topicname: |
|
1474 | 1474 | # TODO implement support for rendering sections, like |
|
1475 | 1475 | # `hg help` works. |
|
1476 | 1476 | topic, subtopic = topicname.split('.', 1) |
|
1477 | 1477 | if topic not in helpmod.subtopics: |
|
1478 | 1478 | raise ErrorResponse(HTTP_NOT_FOUND) |
|
1479 | 1479 | else: |
|
1480 | 1480 | topic = topicname |
|
1481 | 1481 | subtopic = None |
|
1482 | 1482 | |
|
1483 | 1483 | try: |
|
1484 | 1484 | doc = helpmod.help_(u, commands, topic, subtopic=subtopic) |
|
1485 | 1485 | except error.Abort: |
|
1486 | 1486 | raise ErrorResponse(HTTP_NOT_FOUND) |
|
1487 | 1487 | |
|
1488 | 1488 | return web.sendtemplate( |
|
1489 | 1489 | 'help', |
|
1490 | 1490 | topic=topicname, |
|
1491 | 1491 | doc=doc) |
|
1492 | 1492 | |
|
1493 | 1493 | # tell hggettext to extract docstrings from these functions: |
|
1494 | 1494 | i18nfunctions = commands.values() |
General Comments 0
You need to be logged in to leave comments.
Login now