[PATCH 1 of 2 V2] hgweb: move entry-preparing code from webcommands to webutils.commonentry()

Anton Shestakov av6 at dwimlabs.net
Sun Nov 22 07:13:50 UTC 2015


# HG changeset patch
# User Anton Shestakov <av6 at dwimlabs.net>
# Date 1447396536 -28800
#      Fri Nov 13 14:35:36 2015 +0800
# Node ID b4c5ca5544c5b51cbe9f616d3821d07ff29f1073
# Parent  df9b73d2d444ae82fe8d3fe6cf682a93b2c4a7ef
hgweb: move entry-preparing code from webcommands to webutils.commonentry()

The new function is used to prefill basic information about a ctx, such as
revision number and hash, author, commit message, etc. Before, every webcommand
used to get this basic information on its own using some boilerplate code, and
some things in some places just weren't available (e.g. branch/tag/bookmark
info for the current hgweb revision in filelog).

diff --git a/mercurial/hgweb/webcommands.py b/mercurial/hgweb/webcommands.py
--- a/mercurial/hgweb/webcommands.py
+++ b/mercurial/hgweb/webcommands.py
@@ -141,24 +141,16 @@ def _filerevision(web, req, tmpl, fctx):
                    "linenumber": "% 6d" % (lineno + 1),
                    "parity": parity.next()}
 
-    return tmpl("filerevision",
-                file=f,
-                path=webutil.up(f),
-                text=lines(),
-                rev=fctx.rev(),
-                symrev=webutil.symrevorshortnode(req, fctx),
-                node=fctx.hex(),
-                author=fctx.user(),
-                date=fctx.date(),
-                desc=fctx.description(),
-                extra=fctx.extra(),
-                branch=webutil.nodebranchnodefault(fctx),
-                parent=webutil.parents(fctx),
-                child=webutil.children(fctx),
-                rename=webutil.renamelink(fctx),
-                tags=webutil.nodetagsdict(web.repo, fctx.node()),
-                bookmarks=webutil.nodebookmarksdict(web.repo, fctx.node()),
-                permissions=fctx.manifest().flags(f))
+    entry = webutil.commonentry(web.repo, fctx)
+    entry.update(
+        file=f,
+        path=webutil.up(f),
+        text=lines(),
+        symrev=webutil.symrevorshortnode(req, fctx),
+        rename=webutil.renamelink(fctx),
+        permissions=fctx.manifest().flags(f),
+    )
+    return tmpl("filerevision", **entry)
 
 @webcommand('file')
 def file(web, req, tmpl):
@@ -287,22 +279,13 @@ def _search(web, req, tmpl):
             showtags = webutil.showtag(web.repo, tmpl, 'changelogtag', n)
             files = webutil.listfilediffs(tmpl, ctx.files(), n, web.maxfiles)
 
-            yield tmpl('searchentry',
-                       parity=parity.next(),
-                       author=ctx.user(),
-                       parent=lambda **x: webutil.parents(ctx),
-                       child=lambda **x: webutil.children(ctx),
-                       changelogtag=showtags,
-                       desc=ctx.description(),
-                       extra=ctx.extra(),
-                       date=ctx.date(),
-                       files=files,
-                       rev=ctx.rev(),
-                       node=hex(n),
-                       tags=webutil.nodetagsdict(web.repo, n),
-                       bookmarks=webutil.nodebookmarksdict(web.repo, n),
-                       inbranch=webutil.nodeinbranch(web.repo, ctx),
-                       branches=webutil.nodebranchdict(web.repo, ctx))
+            entry = webutil.commonentry(web.repo, ctx)
+            entry.update(
+                parity=parity.next(),
+                changelogtag=showtags,
+                files=files,
+            )
+            yield tmpl('searchentry', **entry)
 
             if count >= revcount:
                 break
@@ -571,21 +554,17 @@ def manifest(web, req, tmpl):
                    "emptydirs": "/".join(emptydirs),
                    "basename": d}
 
-    return tmpl("manifest",
-                rev=ctx.rev(),
-                symrev=symrev,
-                node=hex(node),
-                path=abspath,
-                up=webutil.up(abspath),
-                upparity=parity.next(),
-                fentries=filelist,
-                dentries=dirlist,
-                archives=web.archivelist(hex(node)),
-                tags=webutil.nodetagsdict(web.repo, node),
-                bookmarks=webutil.nodebookmarksdict(web.repo, node),
-                branch=webutil.nodebranchnodefault(ctx),
-                inbranch=webutil.nodeinbranch(web.repo, ctx),
-                branches=webutil.nodebranchdict(web.repo, ctx))
+    entry = webutil.commonentry(web.repo, ctx)
+    entry.update(
+        symrev=symrev,
+        path=abspath,
+        up=webutil.up(abspath),
+        upparity=parity.next(),
+        fentries=filelist,
+        dentries=dirlist,
+        archives=web.archivelist(hex(node)),
+    )
+    return tmpl("manifest", **entry)
 
 @webcommand('tags')
 def tags(web, req, tmpl):
@@ -719,22 +698,10 @@ def summary(web, req, tmpl):
             revs = web.repo.changelog.revs(start, end - 1)
         for i in revs:
             ctx = web.repo[i]
-            n = ctx.node()
-            hn = hex(n)
 
-            l.append(tmpl(
-               'shortlogentry',
-                parity=parity.next(),
-                author=ctx.user(),
-                desc=ctx.description(),
-                extra=ctx.extra(),
-                date=ctx.date(),
-                rev=i,
-                node=hn,
-                tags=webutil.nodetagsdict(web.repo, n),
-                bookmarks=webutil.nodebookmarksdict(web.repo, n),
-                inbranch=webutil.nodeinbranch(web.repo, ctx),
-                branches=webutil.nodebranchdict(web.repo, ctx)))
+            entry = webutil.commonentry(web.repo, ctx)
+            entry['parity'] = parity.next()
+            l.append(tmpl('shortlogentry', **entry))
 
         l.reverse()
         yield l
@@ -779,12 +746,8 @@ def filediff(web, req, tmpl):
             raise
 
     if fctx is not None:
-        n = fctx.node()
         path = fctx.path()
         ctx = fctx.changectx()
-    else:
-        n = ctx.node()
-        # path already defined in except clause
 
     parity = paritygen(web.stripecount)
     style = web.config('web', 'style', 'paper')
@@ -798,22 +761,14 @@ def filediff(web, req, tmpl):
     else:
         rename = []
         ctx = ctx
-    return tmpl("filediff",
-                file=path,
-                node=hex(n),
-                rev=ctx.rev(),
-                symrev=webutil.symrevorshortnode(req, ctx),
-                date=ctx.date(),
-                desc=ctx.description(),
-                extra=ctx.extra(),
-                author=ctx.user(),
-                rename=rename,
-                branch=webutil.nodebranchnodefault(ctx),
-                parent=webutil.parents(ctx),
-                child=webutil.children(ctx),
-                tags=webutil.nodetagsdict(web.repo, n),
-                bookmarks=webutil.nodebookmarksdict(web.repo, n),
-                diff=diffs)
+    entry = webutil.commonentry(web.repo, ctx)
+    entry.update(
+        file=path,
+        symrev=webutil.symrevorshortnode(req, ctx),
+        rename=rename,
+        diff=diffs,
+    )
+    return tmpl("filediff", **entry)
 
 diff = webcommand('diff')(filediff)
 
@@ -873,26 +828,20 @@ def comparison(web, req, tmpl):
         leftlines = filelines(fctx)
 
     comparison = webutil.compare(tmpl, context, leftlines, rightlines)
-    return tmpl('filecomparison',
-                file=path,
-                node=hex(ctx.node()),
-                rev=ctx.rev(),
-                symrev=webutil.symrevorshortnode(req, ctx),
-                date=ctx.date(),
-                desc=ctx.description(),
-                extra=ctx.extra(),
-                author=ctx.user(),
-                rename=rename,
-                branch=webutil.nodebranchnodefault(ctx),
-                parent=webutil.parents(fctx),
-                child=webutil.children(fctx),
-                tags=webutil.nodetagsdict(web.repo, ctx.node()),
-                bookmarks=webutil.nodebookmarksdict(web.repo, ctx.node()),
-                leftrev=leftrev,
-                leftnode=hex(leftnode),
-                rightrev=rightrev,
-                rightnode=hex(rightnode),
-                comparison=comparison)
+    entry = webutil.commonentry(web.repo, ctx)
+    entry.update(
+        file=path,
+        symrev=webutil.symrevorshortnode(req, ctx),
+        rename=rename,
+        parent=webutil.parents(fctx),
+        child=webutil.children(fctx),
+        leftrev=leftrev,
+        leftnode=hex(leftnode),
+        rightrev=rightrev,
+        rightnode=hex(rightnode),
+        comparison=comparison,
+    )
+    return tmpl('filecomparison', **entry)
 
 @webcommand('annotate')
 def annotate(web, req, tmpl):
@@ -940,24 +889,16 @@ def annotate(web, req, tmpl):
                    "linenumber": "% 6d" % (lineno + 1),
                    "revdate": f.date()}
 
-    return tmpl("fileannotate",
-                file=f,
-                annotate=annotate,
-                path=webutil.up(f),
-                rev=fctx.rev(),
-                symrev=webutil.symrevorshortnode(req, fctx),
-                node=fctx.hex(),
-                author=fctx.user(),
-                date=fctx.date(),
-                desc=fctx.description(),
-                extra=fctx.extra(),
-                rename=webutil.renamelink(fctx),
-                branch=webutil.nodebranchnodefault(fctx),
-                parent=webutil.parents(fctx),
-                child=webutil.children(fctx),
-                tags=webutil.nodetagsdict(web.repo, fctx.node()),
-                bookmarks=webutil.nodebookmarksdict(web.repo, fctx.node()),
-                permissions=fctx.manifest().flags(f))
+    entry = webutil.commonentry(web.repo, fctx)
+    entry.update(
+        file=f,
+        annotate=annotate,
+        path=webutil.up(f),
+        symrev=webutil.symrevorshortnode(req, fctx),
+        rename=webutil.renamelink(fctx),
+        permissions=fctx.manifest().flags(f),
+    )
+    return tmpl("fileannotate", **entry)
 
 @webcommand('filelog')
 def filelog(web, req, tmpl):
@@ -1019,23 +960,14 @@ def filelog(web, req, tmpl):
         for i in revs:
             iterfctx = fctx.filectx(i)
 
-            l.append({"parity": parity.next(),
-                      "filerev": i,
-                      "file": f,
-                      "node": iterfctx.hex(),
-                      "author": iterfctx.user(),
-                      "date": iterfctx.date(),
-                      "rename": webutil.renamelink(iterfctx),
-                      "parent": lambda **x: webutil.parents(iterfctx),
-                      "child": lambda **x: webutil.children(iterfctx),
-                      "desc": iterfctx.description(),
-                      "extra": iterfctx.extra(),
-                      "tags": webutil.nodetagsdict(repo, iterfctx.node()),
-                      "bookmarks": webutil.nodebookmarksdict(
-                          repo, iterfctx.node()),
-                      "branch": webutil.nodebranchnodefault(iterfctx),
-                      "inbranch": webutil.nodeinbranch(repo, iterfctx),
-                      "branches": webutil.nodebranchdict(repo, iterfctx)})
+            entry = webutil.commonentry(repo, iterfctx)
+            entry.update(
+                parity=parity.next(),
+                filerev=i,
+                file=f,
+                rename=webutil.renamelink(iterfctx),
+            )
+            l.append(entry)
         for e in reversed(l):
             yield e
 
@@ -1044,11 +976,18 @@ def filelog(web, req, tmpl):
 
     revnav = webutil.filerevnav(web.repo, fctx.path())
     nav = revnav.gen(end - 1, revcount, count)
-    return tmpl("filelog", file=f, node=fctx.hex(), nav=nav,
-                symrev=webutil.symrevorshortnode(req, fctx),
-                entries=entries,
-                latestentry=latestentry,
-                revcount=revcount, morevars=morevars, lessvars=lessvars)
+    entry = webutil.commonentry(web.repo, fctx)
+    entry.update(
+        file=f,
+        nav=nav,
+        symrev=webutil.symrevorshortnode(req, fctx),
+        entries=entries,
+        latestentry=latestentry,
+        revcount=revcount,
+        morevars=morevars,
+        lessvars=lessvars,
+    )
+    return tmpl("filelog", **entry)
 
 @webcommand('archive')
 def archive(web, req, tmpl):
diff --git a/mercurial/hgweb/webutil.py b/mercurial/hgweb/webutil.py
--- a/mercurial/hgweb/webutil.py
+++ b/mercurial/hgweb/webutil.py
@@ -308,6 +308,25 @@ def filectx(repo, req):
 
     return fctx
 
+def commonentry(repo, ctx):
+    node = ctx.node()
+    return {
+        'rev': ctx.rev(),
+        'node': hex(node),
+        'author': ctx.user(),
+        'desc': ctx.description(),
+        'date': ctx.date(),
+        'extra': ctx.extra(),
+        'phase': ctx.phasestr(),
+        'branch': nodebranchnodefault(ctx),
+        'inbranch': nodeinbranch(repo, ctx),
+        'branches': nodebranchdict(repo, ctx),
+        'tags': nodetagsdict(repo, node),
+        'bookmarks': nodebookmarksdict(repo, node),
+        'parent': lambda **x: parents(ctx),
+        'child': lambda **x: children(ctx),
+    }
+
 def changelistentry(web, ctx, tmpl):
     '''Obtain a dictionary to be used for entries in a changelist.
 
@@ -320,22 +339,14 @@ def changelistentry(web, ctx, tmpl):
     showtags = showtag(repo, tmpl, 'changelogtag', n)
     files = listfilediffs(tmpl, ctx.files(), n, web.maxfiles)
 
-    return {
-        "author": ctx.user(),
-        "parent": lambda **x: parents(ctx, rev - 1),
-        "child": lambda **x: children(ctx, rev + 1),
-        "changelogtag": showtags,
-        "desc": ctx.description(),
-        "extra": ctx.extra(),
-        "date": ctx.date(),
-        "files": files,
-        "rev": rev,
-        "node": hex(n),
-        "tags": nodetagsdict(repo, n),
-        "bookmarks": nodebookmarksdict(repo, n),
-        "inbranch": nodeinbranch(repo, ctx),
-        "branches": nodebranchdict(repo, ctx)
-    }
+    entry = commonentry(repo, ctx)
+    entry.update(
+        parent=lambda **x: parents(ctx, rev - 1),
+        child=lambda **x: children(ctx, rev + 1),
+        changelogtag=showtags,
+        files=files,
+    )
+    return entry
 
 def symrevorshortnode(req, ctx):
     if 'node' in req.form:
@@ -374,31 +385,21 @@ def changesetentry(web, req, tmpl, ctx):
     diffstatsgen = diffstatgen(ctx, basectx)
     diffstats = diffstat(tmpl, ctx, diffstatsgen, parity)
 
-    return dict(
+    entry = commonentry(web.repo, ctx)
+    entry.update(
         diff=diff,
-        rev=ctx.rev(),
-        node=ctx.hex(),
         symrev=symrevorshortnode(req, ctx),
-        parent=parents(ctx),
-        child=children(ctx),
         basenode=basectx.hex(),
         changesettag=showtags,
         changesetbookmark=showbookmarks,
         changesetbranch=showbranch,
-        author=ctx.user(),
-        desc=ctx.description(),
-        extra=ctx.extra(),
-        date=ctx.date(),
-        phase=ctx.phasestr(),
         files=files,
         diffsummary=lambda **x: diffsummary(diffstatsgen),
         diffstat=diffstats,
         archives=web.archivelist(ctx.hex()),
-        tags=nodetagsdict(web.repo, ctx.node()),
-        bookmarks=nodebookmarksdict(web.repo, ctx.node()),
         branch=showbranch,
-        inbranch=nodeinbranch(web.repo, ctx),
-        branches=nodebranchdict(web.repo, ctx))
+    )
+    return entry
 
 def listfilediffs(tmpl, files, node, max):
     for f in files[:max]:


More information about the Mercurial-devel mailing list