hgweb: stop passing req and tmpl into @webcommand functions (API)
authorGregory Szorc <gregory.szorc@gmail.com>
Sat, 10 Mar 2018 20:51:46 -0800
changeset 36887 4daa22071d5d
parent 36886 563fd95a6efb
child 36888 97e1dda94af8
hgweb: stop passing req and tmpl into @webcommand functions (API) We have effectively removed all consumers of the old wsgirequest type. The templater can be accessed on the requestcontext passed into the @webcommand function. For the most part, these arguments are unused. They only exist to provide backwards compatibility. And in the case of wsgirequest, use of that object could actively interfere with the new request object. So let's stop passing these objects to @webcommand functions. With this commit, wsgirequest is practically dead from the hgweb WSGI application. There are still some uses in hgwebdir though... .. api:: @webcommand functions now only receive a single argument. The request and templater instances can be accessed via the ``req`` and ``templater`` attributes of the first argument. Note that the request object is different from previous Mercurial releases and consumers of the previous ``req`` 2nd argument will need updating to use the new API. Differential Revision: https://phab.mercurial-scm.org/D2803
hgext/highlight/__init__.py
hgext/keyword.py
hgext/largefiles/overrides.py
mercurial/hgweb/hgweb_mod.py
mercurial/hgweb/webcommands.py
tests/hgweberror.py
--- a/hgext/highlight/__init__.py	Sat Mar 10 19:57:08 2018 -0800
+++ b/hgext/highlight/__init__.py	Sat Mar 10 20:51:46 2018 -0800
@@ -58,7 +58,7 @@
         highlight.pygmentize(field, fctx, style, tmpl,
                 guessfilenameonly=filenameonly)
 
-def filerevision_highlight(orig, web, req, fctx):
+def filerevision_highlight(orig, web, fctx):
     mt = ''.join(web.tmpl('mimetype', encoding=encoding.encoding))
     # only pygmentize for mimetype containing 'html' so we both match
     # 'text/html' and possibly 'application/xhtml+xml' in the future
@@ -70,17 +70,17 @@
     if 'html' in mt:
         pygmentize(web, 'fileline', fctx, web.tmpl)
 
-    return orig(web, req, fctx)
+    return orig(web, fctx)
 
-def annotate_highlight(orig, web, req, tmpl):
+def annotate_highlight(orig, web):
     mt = ''.join(web.tmpl('mimetype', encoding=encoding.encoding))
     if 'html' in mt:
         fctx = webutil.filectx(web.repo, web.req)
         pygmentize(web, 'annotateline', fctx, web.tmpl)
 
-    return orig(web, req, web.tmpl)
+    return orig(web)
 
-def generate_css(web, req, tmpl):
+def generate_css(web):
     pg_style = web.config('web', 'pygments_style', 'colorful')
     fmter = highlight.HtmlFormatter(style=pg_style)
     web.res.headers['Content-Type'] = 'text/css'
--- a/hgext/keyword.py	Sat Mar 10 19:57:08 2018 -0800
+++ b/hgext/keyword.py	Sat Mar 10 20:51:46 2018 -0800
@@ -614,14 +614,14 @@
         if kwt:
             kwt.restrict = restrict
 
-def kwweb_skip(orig, web, req, tmpl):
+def kwweb_skip(orig, web):
     '''Wraps webcommands.x turning off keyword expansion.'''
     kwt = getattr(web.repo, '_keywordkwt', None)
     if kwt:
         origmatch = kwt.match
         kwt.match = util.never
     try:
-        for chunk in orig(web, req, tmpl):
+        for chunk in orig(web):
             yield chunk
     finally:
         if kwt:
--- a/hgext/largefiles/overrides.py	Sat Mar 10 19:57:08 2018 -0800
+++ b/hgext/largefiles/overrides.py	Sat Mar 10 20:51:46 2018 -0800
@@ -932,11 +932,11 @@
     finally:
         repo.unfiltered().lfstatus = False
 
-def hgwebarchive(orig, web, req, tmpl):
+def hgwebarchive(orig, web):
     web.repo.lfstatus = True
 
     try:
-        return orig(web, req, tmpl)
+        return orig(web)
     finally:
         web.repo.lfstatus = False
 
--- a/mercurial/hgweb/hgweb_mod.py	Sat Mar 10 19:57:08 2018 -0800
+++ b/mercurial/hgweb/hgweb_mod.py	Sat Mar 10 20:51:46 2018 -0800
@@ -404,7 +404,7 @@
                 # override easily enough.
                 res.status = '200 Script output follows'
                 res.headers['Content-Type'] = ctype
-                return getattr(webcommands, cmd)(rctx, wsgireq, rctx.tmpl)
+                return getattr(webcommands, cmd)(rctx)
 
         except (error.LookupError, error.RepoLookupError) as err:
             msg = pycompat.bytestr(err)
--- a/mercurial/hgweb/webcommands.py	Sat Mar 10 19:57:08 2018 -0800
+++ b/mercurial/hgweb/webcommands.py	Sat Mar 10 20:51:46 2018 -0800
@@ -65,7 +65,7 @@
     Usage:
 
     @webcommand('mycommand')
-    def mycommand(web, req, tmpl):
+    def mycommand(web):
         pass
     """
 
@@ -78,7 +78,7 @@
         return func
 
 @webcommand('log')
-def log(web, req, tmpl):
+def log(web):
     """
     /log[/{revision}[/{path}]]
     --------------------------
@@ -95,23 +95,23 @@
     """
 
     if web.req.qsparams.get('file'):
-        return filelog(web, req, None)
+        return filelog(web)
     else:
-        return changelog(web, req, None)
+        return changelog(web)
 
 @webcommand('rawfile')
-def rawfile(web, req, tmpl):
+def rawfile(web):
     guessmime = web.configbool('web', 'guessmime')
 
     path = webutil.cleanpath(web.repo, web.req.qsparams.get('file', ''))
     if not path:
-        return manifest(web, req, None)
+        return manifest(web)
 
     try:
         fctx = webutil.filectx(web.repo, web.req)
     except error.LookupError as inst:
         try:
-            return manifest(web, req, None)
+            return manifest(web)
         except ErrorResponse:
             raise inst
 
@@ -135,7 +135,7 @@
     web.res.setbodybytes(text)
     return web.res.sendresponse()
 
-def _filerevision(web, req, fctx):
+def _filerevision(web, fctx):
     f = fctx.path()
     text = fctx.data()
     parity = paritygen(web.stripecount)
@@ -164,7 +164,7 @@
         **pycompat.strkwargs(webutil.commonentry(web.repo, fctx)))
 
 @webcommand('file')
-def file(web, req, tmpl):
+def file(web):
     """
     /file/{revision}[/{path}]
     -------------------------
@@ -184,16 +184,16 @@
     be rendered.
     """
     if web.req.qsparams.get('style') == 'raw':
-        return rawfile(web, req, None)
+        return rawfile(web)
 
     path = webutil.cleanpath(web.repo, web.req.qsparams.get('file', ''))
     if not path:
-        return manifest(web, req, None)
+        return manifest(web)
     try:
-        return _filerevision(web, req, webutil.filectx(web.repo, web.req))
+        return _filerevision(web, webutil.filectx(web.repo, web.req))
     except error.LookupError as inst:
         try:
-            return manifest(web, req, None)
+            return manifest(web)
         except ErrorResponse:
             raise inst
 
@@ -354,7 +354,7 @@
         showunforcekw=showunforcekw)
 
 @webcommand('changelog')
-def changelog(web, req, tmpl, shortlog=False):
+def changelog(web, shortlog=False):
     """
     /changelog[/{revision}]
     -----------------------
@@ -452,7 +452,7 @@
         query=query)
 
 @webcommand('shortlog')
-def shortlog(web, req, tmpl):
+def shortlog(web):
     """
     /shortlog
     ---------
@@ -463,10 +463,10 @@
     difference is the ``shortlog`` template will be rendered instead of the
     ``changelog`` template.
     """
-    return changelog(web, req, None, shortlog=True)
+    return changelog(web, shortlog=True)
 
 @webcommand('changeset')
-def changeset(web, req, tmpl):
+def changeset(web):
     """
     /changeset[/{revision}]
     -----------------------
@@ -498,7 +498,7 @@
     return path
 
 @webcommand('manifest')
-def manifest(web, req, tmpl):
+def manifest(web):
     """
     /manifest[/{revision}[/{path}]]
     -------------------------------
@@ -598,7 +598,7 @@
         **pycompat.strkwargs(webutil.commonentry(web.repo, ctx)))
 
 @webcommand('tags')
-def tags(web, req, tmpl):
+def tags(web):
     """
     /tags
     -----
@@ -632,7 +632,7 @@
         latestentry=lambda **x: entries(True, True, **x))
 
 @webcommand('bookmarks')
-def bookmarks(web, req, tmpl):
+def bookmarks(web):
     """
     /bookmarks
     ----------
@@ -671,7 +671,7 @@
         latestentry=lambda **x: entries(latestonly=True, **x))
 
 @webcommand('branches')
-def branches(web, req, tmpl):
+def branches(web):
     """
     /branches
     ---------
@@ -694,7 +694,7 @@
         latestentry=latestentry)
 
 @webcommand('summary')
-def summary(web, req, tmpl):
+def summary(web):
     """
     /summary
     --------
@@ -778,7 +778,7 @@
         labels=web.configlist('web', 'labels'))
 
 @webcommand('filediff')
-def filediff(web, req, tmpl):
+def filediff(web):
     """
     /diff/{revision}/{path}
     -----------------------
@@ -827,7 +827,7 @@
 diff = webcommand('diff')(filediff)
 
 @webcommand('comparison')
-def comparison(web, req, tmpl):
+def comparison(web):
     """
     /comparison/{revision}/{path}
     -----------------------------
@@ -902,7 +902,7 @@
         **pycompat.strkwargs(webutil.commonentry(web.repo, ctx)))
 
 @webcommand('annotate')
-def annotate(web, req, tmpl):
+def annotate(web):
     """
     /annotate/{revision}/{path}
     ---------------------------
@@ -994,7 +994,7 @@
         **pycompat.strkwargs(webutil.commonentry(web.repo, fctx)))
 
 @webcommand('filelog')
-def filelog(web, req, tmpl):
+def filelog(web):
     """
     /filelog/{revision}/{path}
     --------------------------
@@ -1132,7 +1132,7 @@
         **pycompat.strkwargs(webutil.commonentry(web.repo, fctx)))
 
 @webcommand('archive')
-def archive(web, req, tmpl):
+def archive(web):
     """
     /archive/{revision}.{format}[/{path}]
     -------------------------------------
@@ -1206,7 +1206,7 @@
     return []
 
 @webcommand('static')
-def static(web, req, tmpl):
+def static(web):
     fname = web.req.qsparams['file']
     # a repo owner may set web.static in .hg/hgrc to get any file
     # readable by the user running the CGI script
@@ -1221,7 +1221,7 @@
     return web.res.sendresponse()
 
 @webcommand('graph')
-def graph(web, req, tmpl):
+def graph(web):
     """
     /graph[/{revision}]
     -------------------
@@ -1388,7 +1388,7 @@
     return doc
 
 @webcommand('help')
-def help(web, req, tmpl):
+def help(web):
     """
     /help[/{topic}]
     ---------------
--- a/tests/hgweberror.py	Sat Mar 10 19:57:08 2018 -0800
+++ b/tests/hgweberror.py	Sat Mar 10 20:51:46 2018 -0800
@@ -6,7 +6,7 @@
     webcommands,
 )
 
-def raiseerror(web, req, tmpl):
+def raiseerror(web):
     '''Dummy web command that raises an uncaught Exception.'''
 
     # Simulate an error after partial response.