##// END OF EJS Templates
hgweb: port most @webcommand to use modern response type...
Gregory Szorc -
r36887:9fc3d814 default
parent child Browse files
Show More
@@ -30,7 +30,6 b' from __future__ import absolute_import'
30
30
31 from . import highlight
31 from . import highlight
32 from mercurial.hgweb import (
32 from mercurial.hgweb import (
33 common,
34 webcommands,
33 webcommands,
35 webutil,
34 webutil,
36 )
35 )
@@ -84,9 +83,12 b' def annotate_highlight(orig, web, req, t'
84 def generate_css(web, req, tmpl):
83 def generate_css(web, req, tmpl):
85 pg_style = web.config('web', 'pygments_style', 'colorful')
84 pg_style = web.config('web', 'pygments_style', 'colorful')
86 fmter = highlight.HtmlFormatter(style=pg_style)
85 fmter = highlight.HtmlFormatter(style=pg_style)
87 req.respond(common.HTTP_OK, 'text/css')
86 web.res.headers['Content-Type'] = 'text/css'
88 return ['/* pygments_style = %s */\n\n' % pg_style,
87 web.res.setbodybytes(''.join([
89 fmter.get_style_defs('')]
88 '/* pygments_style = %s */\n\n' % pg_style,
89 fmter.get_style_defs(''),
90 ]))
91 return web.res
90
92
91 def extsetup():
93 def extsetup():
92 # monkeypatch in the new version
94 # monkeypatch in the new version
@@ -400,8 +400,11 b' class hgweb(object):'
400 msg = 'no such method: %s' % cmd
400 msg = 'no such method: %s' % cmd
401 raise ErrorResponse(HTTP_BAD_REQUEST, msg)
401 raise ErrorResponse(HTTP_BAD_REQUEST, msg)
402 elif cmd == 'file' and req.qsparams.get('style') == 'raw':
402 elif cmd == 'file' and req.qsparams.get('style') == 'raw':
403 rctx.ctype = ctype
403 res.status = '200 Script output follows'
404 res.headers['Content-Type'] = ctype
404 content = webcommands.rawfile(rctx, wsgireq, tmpl)
405 content = webcommands.rawfile(rctx, wsgireq, tmpl)
406 assert content is res
407 return res.sendresponse()
405 else:
408 else:
406 # Set some globals appropriate for web handlers. Commands can
409 # Set some globals appropriate for web handlers. Commands can
407 # override easily enough.
410 # override easily enough.
@@ -106,17 +106,13 b' def rawfile(web, req, tmpl):'
106
106
107 path = webutil.cleanpath(web.repo, req.req.qsparams.get('file', ''))
107 path = webutil.cleanpath(web.repo, req.req.qsparams.get('file', ''))
108 if not path:
108 if not path:
109 content = manifest(web, req, tmpl)
109 return manifest(web, req, tmpl)
110 req.respond(HTTP_OK, web.ctype)
111 return content
112
110
113 try:
111 try:
114 fctx = webutil.filectx(web.repo, req)
112 fctx = webutil.filectx(web.repo, req)
115 except error.LookupError as inst:
113 except error.LookupError as inst:
116 try:
114 try:
117 content = manifest(web, req, tmpl)
115 return manifest(web, req, tmpl)
118 req.respond(HTTP_OK, web.ctype)
119 return content
120 except ErrorResponse:
116 except ErrorResponse:
121 raise inst
117 raise inst
122
118
@@ -133,8 +129,12 b' def rawfile(web, req, tmpl):'
133 if mt.startswith('text/'):
129 if mt.startswith('text/'):
134 mt += '; charset="%s"' % encoding.encoding
130 mt += '; charset="%s"' % encoding.encoding
135
131
136 req.respond(HTTP_OK, mt, path, body=text)
132 web.res.headers['Content-Type'] = mt
137 return []
133 filename = (path.rpartition('/')[-1]
134 .replace('\\', '\\\\').replace('"', '\\"'))
135 web.res.headers['Content-Disposition'] = 'inline; filename="%s"' % filename
136 web.res.setbodybytes(text)
137 return web.res
138
138
139 def _filerevision(web, req, tmpl, fctx):
139 def _filerevision(web, req, tmpl, fctx):
140 f = fctx.path()
140 f = fctx.path()
@@ -153,7 +153,8 b' def _filerevision(web, req, tmpl, fctx):'
153 "linenumber": "% 6d" % (lineno + 1),
153 "linenumber": "% 6d" % (lineno + 1),
154 "parity": next(parity)}
154 "parity": next(parity)}
155
155
156 return tmpl("filerevision",
156 web.res.setbodygen(tmpl(
157 'filerevision',
157 file=f,
158 file=f,
158 path=webutil.up(f),
159 path=webutil.up(f),
159 text=lines(),
160 text=lines(),
@@ -161,7 +162,9 b' def _filerevision(web, req, tmpl, fctx):'
161 rename=webutil.renamelink(fctx),
162 rename=webutil.renamelink(fctx),
162 permissions=fctx.manifest().flags(f),
163 permissions=fctx.manifest().flags(f),
163 ishead=int(ishead),
164 ishead=int(ishead),
164 **pycompat.strkwargs(webutil.commonentry(web.repo, fctx)))
165 **pycompat.strkwargs(webutil.commonentry(web.repo, fctx))))
166
167 return web.res
165
168
166 @webcommand('file')
169 @webcommand('file')
167 def file(web, req, tmpl):
170 def file(web, req, tmpl):
@@ -335,11 +338,20 b' def _search(web, req, tmpl):'
335 tip = web.repo['tip']
338 tip = web.repo['tip']
336 parity = paritygen(web.stripecount)
339 parity = paritygen(web.stripecount)
337
340
338 return tmpl('search', query=query, node=tip.hex(), symrev='tip',
341 web.res.setbodygen(tmpl(
339 entries=changelist, archives=web.archivelist("tip"),
342 'search',
340 morevars=morevars, lessvars=lessvars,
343 query=query,
344 node=tip.hex(),
345 symrev='tip',
346 entries=changelist,
347 archives=web.archivelist('tip'),
348 morevars=morevars,
349 lessvars=lessvars,
341 modedesc=searchfunc[1],
350 modedesc=searchfunc[1],
342 showforcekw=showforcekw, showunforcekw=showunforcekw)
351 showforcekw=showforcekw,
352 showunforcekw=showunforcekw))
353
354 return web.res
343
355
344 @webcommand('changelog')
356 @webcommand('changelog')
345 def changelog(web, req, tmpl, shortlog=False):
357 def changelog(web, req, tmpl, shortlog=False):
@@ -423,12 +435,23 b' def changelog(web, req, tmpl, shortlog=F'
423 else:
435 else:
424 nextentry = []
436 nextentry = []
425
437
426 return tmpl('shortlog' if shortlog else 'changelog', changenav=changenav,
438 web.res.setbodygen(tmpl(
427 node=ctx.hex(), rev=pos, symrev=symrev, changesets=count,
439 'shortlog' if shortlog else 'changelog',
440 changenav=changenav,
441 node=ctx.hex(),
442 rev=pos,
443 symrev=symrev,
444 changesets=count,
428 entries=entries,
445 entries=entries,
429 latestentry=latestentry, nextentry=nextentry,
446 latestentry=latestentry,
430 archives=web.archivelist("tip"), revcount=revcount,
447 nextentry=nextentry,
431 morevars=morevars, lessvars=lessvars, query=query)
448 archives=web.archivelist('tip'),
449 revcount=revcount,
450 morevars=morevars,
451 lessvars=lessvars,
452 query=query))
453
454 return web.res
432
455
433 @webcommand('shortlog')
456 @webcommand('shortlog')
434 def shortlog(web, req, tmpl):
457 def shortlog(web, req, tmpl):
@@ -461,8 +484,9 b' def changeset(web, req, tmpl):'
461 templates related to diffs may all be used to produce the output.
484 templates related to diffs may all be used to produce the output.
462 """
485 """
463 ctx = webutil.changectx(web.repo, req)
486 ctx = webutil.changectx(web.repo, req)
464
487 web.res.setbodygen(tmpl('changeset',
465 return tmpl('changeset', **webutil.changesetentry(web, req, tmpl, ctx))
488 **webutil.changesetentry(web, req, tmpl, ctx)))
489 return web.res
466
490
467 rev = webcommand('rev')(changeset)
491 rev = webcommand('rev')(changeset)
468
492
@@ -563,7 +587,8 b' def manifest(web, req, tmpl):'
563 "emptydirs": "/".join(emptydirs),
587 "emptydirs": "/".join(emptydirs),
564 "basename": d}
588 "basename": d}
565
589
566 return tmpl("manifest",
590 web.res.setbodygen(tmpl(
591 'manifest',
567 symrev=symrev,
592 symrev=symrev,
568 path=abspath,
593 path=abspath,
569 up=webutil.up(abspath),
594 up=webutil.up(abspath),
@@ -571,7 +596,9 b' def manifest(web, req, tmpl):'
571 fentries=filelist,
596 fentries=filelist,
572 dentries=dirlist,
597 dentries=dirlist,
573 archives=web.archivelist(hex(node)),
598 archives=web.archivelist(hex(node)),
574 **pycompat.strkwargs(webutil.commonentry(web.repo, ctx)))
599 **pycompat.strkwargs(webutil.commonentry(web.repo, ctx))))
600
601 return web.res
575
602
576 @webcommand('tags')
603 @webcommand('tags')
577 def tags(web, req, tmpl):
604 def tags(web, req, tmpl):
@@ -600,11 +627,14 b' def tags(web, req, tmpl):'
600 "date": web.repo[n].date(),
627 "date": web.repo[n].date(),
601 "node": hex(n)}
628 "node": hex(n)}
602
629
603 return tmpl("tags",
630 web.res.setbodygen(tmpl(
631 'tags',
604 node=hex(web.repo.changelog.tip()),
632 node=hex(web.repo.changelog.tip()),
605 entries=lambda **x: entries(False, False, **x),
633 entries=lambda **x: entries(False, False, **x),
606 entriesnotip=lambda **x: entries(True, False, **x),
634 entriesnotip=lambda **x: entries(True, False, **x),
607 latestentry=lambda **x: entries(True, True, **x))
635 latestentry=lambda **x: entries(True, True, **x)))
636
637 return web.res
608
638
609 @webcommand('bookmarks')
639 @webcommand('bookmarks')
610 def bookmarks(web, req, tmpl):
640 def bookmarks(web, req, tmpl):
@@ -638,11 +668,14 b' def bookmarks(web, req, tmpl):'
638 else:
668 else:
639 latestrev = -1
669 latestrev = -1
640
670
641 return tmpl("bookmarks",
671 web.res.setbodygen(tmpl(
672 'bookmarks',
642 node=hex(web.repo.changelog.tip()),
673 node=hex(web.repo.changelog.tip()),
643 lastchange=[{"date": web.repo[latestrev].date()}],
674 lastchange=[{'date': web.repo[latestrev].date()}],
644 entries=lambda **x: entries(latestonly=False, **x),
675 entries=lambda **x: entries(latestonly=False, **x),
645 latestentry=lambda **x: entries(latestonly=True, **x))
676 latestentry=lambda **x: entries(latestonly=True, **x)))
677
678 return web.res
646
679
647 @webcommand('branches')
680 @webcommand('branches')
648 def branches(web, req, tmpl):
681 def branches(web, req, tmpl):
@@ -660,8 +693,14 b' def branches(web, req, tmpl):'
660 """
693 """
661 entries = webutil.branchentries(web.repo, web.stripecount)
694 entries = webutil.branchentries(web.repo, web.stripecount)
662 latestentry = webutil.branchentries(web.repo, web.stripecount, 1)
695 latestentry = webutil.branchentries(web.repo, web.stripecount, 1)
663 return tmpl('branches', node=hex(web.repo.changelog.tip()),
696
664 entries=entries, latestentry=latestentry)
697 web.res.setbodygen(tmpl(
698 'branches',
699 node=hex(web.repo.changelog.tip()),
700 entries=entries,
701 latestentry=latestentry))
702
703 return web.res
665
704
666 @webcommand('summary')
705 @webcommand('summary')
667 def summary(web, req, tmpl):
706 def summary(web, req, tmpl):
@@ -731,9 +770,11 b' def summary(web, req, tmpl):'
731 desc = web.config("web", "description")
770 desc = web.config("web", "description")
732 if not desc:
771 if not desc:
733 desc = 'unknown'
772 desc = 'unknown'
734 return tmpl("summary",
773
774 web.res.setbodygen(tmpl(
775 'summary',
735 desc=desc,
776 desc=desc,
736 owner=get_contact(web.config) or "unknown",
777 owner=get_contact(web.config) or 'unknown',
737 lastchange=tip.date(),
778 lastchange=tip.date(),
738 tags=tagentries,
779 tags=tagentries,
739 bookmarks=bookmarks,
780 bookmarks=bookmarks,
@@ -741,8 +782,10 b' def summary(web, req, tmpl):'
741 shortlog=changelist,
782 shortlog=changelist,
742 node=tip.hex(),
783 node=tip.hex(),
743 symrev='tip',
784 symrev='tip',
744 archives=web.archivelist("tip"),
785 archives=web.archivelist('tip'),
745 labels=web.configlist('web', 'labels'))
786 labels=web.configlist('web', 'labels')))
787
788 return web.res
746
789
747 @webcommand('filediff')
790 @webcommand('filediff')
748 def filediff(web, req, tmpl):
791 def filediff(web, req, tmpl):
@@ -782,12 +825,16 b' def filediff(web, req, tmpl):'
782 else:
825 else:
783 rename = []
826 rename = []
784 ctx = ctx
827 ctx = ctx
785 return tmpl("filediff",
828
829 web.res.setbodygen(tmpl(
830 'filediff',
786 file=path,
831 file=path,
787 symrev=webutil.symrevorshortnode(req, ctx),
832 symrev=webutil.symrevorshortnode(req, ctx),
788 rename=rename,
833 rename=rename,
789 diff=diffs,
834 diff=diffs,
790 **pycompat.strkwargs(webutil.commonentry(web.repo, ctx)))
835 **pycompat.strkwargs(webutil.commonentry(web.repo, ctx))))
836
837 return web.res
791
838
792 diff = webcommand('diff')(filediff)
839 diff = webcommand('diff')(filediff)
793
840
@@ -853,7 +900,9 b' def comparison(web, req, tmpl):'
853 else:
900 else:
854 rename = []
901 rename = []
855 ctx = ctx
902 ctx = ctx
856 return tmpl('filecomparison',
903
904 web.res.setbodygen(tmpl(
905 'filecomparison',
857 file=path,
906 file=path,
858 symrev=webutil.symrevorshortnode(req, ctx),
907 symrev=webutil.symrevorshortnode(req, ctx),
859 rename=rename,
908 rename=rename,
@@ -862,7 +911,9 b' def comparison(web, req, tmpl):'
862 rightrev=rightrev,
911 rightrev=rightrev,
863 rightnode=hex(rightnode),
912 rightnode=hex(rightnode),
864 comparison=comparison,
913 comparison=comparison,
865 **pycompat.strkwargs(webutil.commonentry(web.repo, ctx)))
914 **pycompat.strkwargs(webutil.commonentry(web.repo, ctx))))
915
916 return web.res
866
917
867 @webcommand('annotate')
918 @webcommand('annotate')
868 def annotate(web, req, tmpl):
919 def annotate(web, req, tmpl):
@@ -944,7 +995,8 b' def annotate(web, req, tmpl):'
944 diffopts = webutil.difffeatureopts(req, web.repo.ui, 'annotate')
995 diffopts = webutil.difffeatureopts(req, web.repo.ui, 'annotate')
945 diffopts = {k: getattr(diffopts, k) for k in diffopts.defaults}
996 diffopts = {k: getattr(diffopts, k) for k in diffopts.defaults}
946
997
947 return tmpl("fileannotate",
998 web.res.setbodygen(tmpl(
999 'fileannotate',
948 file=f,
1000 file=f,
949 annotate=annotate,
1001 annotate=annotate,
950 path=webutil.up(f),
1002 path=webutil.up(f),
@@ -953,7 +1005,9 b' def annotate(web, req, tmpl):'
953 permissions=fctx.manifest().flags(f),
1005 permissions=fctx.manifest().flags(f),
954 ishead=int(ishead),
1006 ishead=int(ishead),
955 diffopts=diffopts,
1007 diffopts=diffopts,
956 **pycompat.strkwargs(webutil.commonentry(web.repo, fctx)))
1008 **pycompat.strkwargs(webutil.commonentry(web.repo, fctx))))
1009
1010 return web.res
957
1011
958 @webcommand('filelog')
1012 @webcommand('filelog')
959 def filelog(web, req, tmpl):
1013 def filelog(web, req, tmpl):
@@ -1318,9 +1372,15 b' def graph(web, req, tmpl):'
1318
1372
1319 rows = len(tree)
1373 rows = len(tree)
1320
1374
1321 return tmpl('graph', rev=rev, symrev=symrev, revcount=revcount,
1375 web.res.setbodygen(tmpl(
1376 'graph',
1377 rev=rev,
1378 symrev=symrev,
1379 revcount=revcount,
1322 uprev=uprev,
1380 uprev=uprev,
1323 lessvars=lessvars, morevars=morevars, downrev=downrev,
1381 lessvars=lessvars,
1382 morevars=morevars,
1383 downrev=downrev,
1324 graphvars=graphvars,
1384 graphvars=graphvars,
1325 rows=rows,
1385 rows=rows,
1326 bg_height=bg_height,
1386 bg_height=bg_height,
@@ -1328,7 +1388,10 b' def graph(web, req, tmpl):'
1328 nextentry=nextentry,
1388 nextentry=nextentry,
1329 jsdata=lambda **x: jsdata(),
1389 jsdata=lambda **x: jsdata(),
1330 nodes=lambda **x: nodes(),
1390 nodes=lambda **x: nodes(),
1331 node=ctx.hex(), changenav=changenav)
1391 node=ctx.hex(),
1392 changenav=changenav))
1393
1394 return web.res
1332
1395
1333 def _getdoc(e):
1396 def _getdoc(e):
1334 doc = e[0].__doc__
1397 doc = e[0].__doc__
@@ -1384,8 +1447,13 b' def help(web, req, tmpl):'
1384 for c, doc in other:
1447 for c, doc in other:
1385 yield {'topic': c, 'summary': doc}
1448 yield {'topic': c, 'summary': doc}
1386
1449
1387 return tmpl('helptopics', topics=topics, earlycommands=earlycommands,
1450 web.res.setbodygen(tmpl(
1388 othercommands=othercommands, title='Index')
1451 'helptopics',
1452 topics=topics,
1453 earlycommands=earlycommands,
1454 othercommands=othercommands,
1455 title='Index'))
1456 return web.res
1389
1457
1390 # Render an index of sub-topics.
1458 # Render an index of sub-topics.
1391 if topicname in helpmod.subtopics:
1459 if topicname in helpmod.subtopics:
@@ -1397,8 +1465,12 b' def help(web, req, tmpl):'
1397 'summary': summary,
1465 'summary': summary,
1398 })
1466 })
1399
1467
1400 return tmpl('helptopics', topics=topics, title=topicname,
1468 web.res.setbodygen(tmpl(
1401 subindex=True)
1469 'helptopics',
1470 topics=topics,
1471 title=topicname,
1472 subindex=True))
1473 return web.res
1402
1474
1403 u = webutil.wsgiui.load()
1475 u = webutil.wsgiui.load()
1404 u.verbose = True
1476 u.verbose = True
@@ -1418,7 +1490,13 b' def help(web, req, tmpl):'
1418 doc = helpmod.help_(u, commands, topic, subtopic=subtopic)
1490 doc = helpmod.help_(u, commands, topic, subtopic=subtopic)
1419 except error.Abort:
1491 except error.Abort:
1420 raise ErrorResponse(HTTP_NOT_FOUND)
1492 raise ErrorResponse(HTTP_NOT_FOUND)
1421 return tmpl('help', topic=topicname, doc=doc)
1493
1494 web.res.setbodygen(tmpl(
1495 'help',
1496 topic=topicname,
1497 doc=doc))
1498
1499 return web.res
1422
1500
1423 # tell hggettext to extract docstrings from these functions:
1501 # tell hggettext to extract docstrings from these functions:
1424 i18nfunctions = commands.values()
1502 i18nfunctions = commands.values()
General Comments 0
You need to be logged in to leave comments. Login now