changeset 27294:5aa2afb4f81a

hgweb: move entry-preparing code from webcommands to webutils.commonentry() The new function is used to fill 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.
author Anton Shestakov <av6@dwimlabs.net>
date Fri, 13 Nov 2015 14:35:36 +0800
parents 9e06e7fb037d
children a327a24acfea
files mercurial/hgweb/webcommands.py mercurial/hgweb/webutil.py
diffstat 2 files changed, 53 insertions(+), 137 deletions(-) [+]
line wrap: on
line diff
--- a/mercurial/hgweb/webcommands.py	Mon Nov 30 19:30:16 2015 +0000
+++ b/mercurial/hgweb/webcommands.py	Fri Nov 13 14:35:36 2015 +0800
@@ -145,20 +145,10 @@
                 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))
+                permissions=fctx.manifest().flags(f),
+                **webutil.commonentry(web.repo, fctx))
 
 @webcommand('file')
 def file(web, req, tmpl):
@@ -289,20 +279,9 @@
 
             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))
+                       **webutil.commonentry(web.repo, ctx))
 
             if count >= revcount:
                 break
@@ -572,20 +551,14 @@
                    "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))
+                **webutil.commonentry(web.repo, ctx))
 
 @webcommand('tags')
 def tags(web, req, tmpl):
@@ -719,22 +692,11 @@
             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',
+                '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)))
+                **webutil.commonentry(web.repo, ctx)))
 
         l.reverse()
         yield l
@@ -779,12 +741,8 @@
             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')
@@ -800,20 +758,10 @@
         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)
+                diff=diffs,
+                **webutil.commonentry(web.repo, ctx))
 
 diff = webcommand('diff')(filediff)
 
@@ -881,24 +829,14 @@
         ctx = ctx
     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(ctx),
-                child=webutil.children(ctx),
-                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)
+                comparison=comparison,
+                **webutil.commonentry(web.repo, ctx))
 
 @webcommand('annotate')
 def annotate(web, req, tmpl):
@@ -950,20 +888,10 @@
                 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))
+                permissions=fctx.manifest().flags(f),
+                **webutil.commonentry(web.repo, fctx))
 
 @webcommand('filelog')
 def filelog(web, req, tmpl):
@@ -1025,23 +953,12 @@
         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)})
+            l.append(dict(
+                parity=parity.next(),
+                filerev=i,
+                file=f,
+                rename=webutil.renamelink(iterfctx),
+                **webutil.commonentry(repo, iterfctx)))
         for e in reversed(l):
             yield e
 
@@ -1050,15 +967,16 @@
 
     revnav = webutil.filerevnav(web.repo, fctx.path())
     nav = revnav.gen(end - 1, revcount, count)
-    return tmpl("filelog", file=f, node=fctx.hex(), nav=nav,
-                rev=fctx.rev(),
+    return tmpl("filelog",
+                file=f,
+                nav=nav,
                 symrev=webutil.symrevorshortnode(req, fctx),
-                branch=webutil.nodebranchnodefault(fctx),
-                tags=webutil.nodetagsdict(web.repo, fctx.node()),
-                bookmarks=webutil.nodebookmarksdict(web.repo, fctx.node()),
                 entries=entries,
                 latestentry=latestentry,
-                revcount=revcount, morevars=morevars, lessvars=lessvars)
+                revcount=revcount,
+                morevars=morevars,
+                lessvars=lessvars,
+                **webutil.commonentry(web.repo, fctx))
 
 @webcommand('archive')
 def archive(web, req, tmpl):
--- a/mercurial/hgweb/webutil.py	Mon Nov 30 19:30:16 2015 +0000
+++ b/mercurial/hgweb/webutil.py	Fri Nov 13 14:35:36 2015 +0800
@@ -308,6 +308,25 @@
 
     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 @@
     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:
@@ -376,29 +387,16 @@
 
     return dict(
         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))
+        **commonentry(web.repo, ctx))
 
 def listfilediffs(tmpl, files, node, max):
     for f in files[:max]: