Patchwork hgweb: extract changelist entry generation into own function

login
register
mail settings
Submitter Gregory Szorc
Date Jan. 7, 2015, 4:15 a.m.
Message ID <dbe2a9bcd0142fcdad43.1420604110@3.1.168.192.in-addr.arpa>
Download mbox | patch
Permalink /patch/7345/
State Accepted
Commit 513d4790511400f3870c9e8d312da71b09b1c14d
Headers show

Comments

Gregory Szorc - Jan. 7, 2015, 4:15 a.m.
# HG changeset patch
# User Gregory Szorc <gregory.szorc@gmail.com>
# Date 1420604092 28800
#      Tue Jan 06 20:14:52 2015 -0800
# Node ID dbe2a9bcd0142fcdad43589845d89caddb1172ac
# Parent  b9d06fa10ef29c012d48bd4f3c93fd7bf1347d40
hgweb: extract changelist entry generation into own function

I want to supplement changelist entries (used by shortlog and changelog
endpoints) with custom metadata from an extension. i.e. I have extra
per-changeset metadata that I wish to make available to templates so it
can be rendered on hgweb.

To facilitate this, I've extracted the logic for creating a changeset
data structure into its own function, where it can be wrapped by
extensions.

Ideally, hgweb would use the same templater as the command line and have
full access to templatekw.keywords. But that's a lot of work. This patch
gets us some of the benefit without all the work.

Many other hgweb commands could benefit from similar refactorings. I'm
going to wait to see how this patch is received before I go crazy on
extracting inline functions.
Matt Mackall - Jan. 7, 2015, 9:18 p.m.
On Tue, 2015-01-06 at 20:15 -0800, Gregory Szorc wrote:
> # HG changeset patch
> # User Gregory Szorc <gregory.szorc@gmail.com>
> # Date 1420604092 28800
> #      Tue Jan 06 20:14:52 2015 -0800
> # Node ID dbe2a9bcd0142fcdad43589845d89caddb1172ac
> # Parent  b9d06fa10ef29c012d48bd4f3c93fd7bf1347d40
> hgweb: extract changelist entry generation into own function

Queued for default, thanks.

Patch

diff --git a/mercurial/hgweb/webcommands.py b/mercurial/hgweb/webcommands.py
--- a/mercurial/hgweb/webcommands.py
+++ b/mercurial/hgweb/webcommands.py
@@ -281,33 +281,16 @@  def changelog(web, req, tmpl, shortlog=F
         revs = []
         if pos != -1:
             revs = web.repo.changelog.revs(pos, 0)
         curcount = 0
-        for i in revs:
-            ctx = web.repo[i]
-            n = ctx.node()
-            showtags = webutil.showtag(web.repo, tmpl, 'changelogtag', n)
-            files = webutil.listfilediffs(tmpl, ctx.files(), n, web.maxfiles)
-
+        for rev in revs:
             curcount += 1
             if curcount > revcount + 1:
                 break
-            yield {"parity": parity.next(),
-                   "author": ctx.user(),
-                   "parent": webutil.parents(ctx, i - 1),
-                   "child": webutil.children(ctx, i + 1),
-                   "changelogtag": showtags,
-                   "desc": ctx.description(),
-                   "extra": ctx.extra(),
-                   "date": ctx.date(),
-                   "files": files,
-                   "rev": i,
-                   "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.changelistentry(web, web.repo[rev], tmpl)
+            entry['parity'] = parity.next()
+            yield entry
 
     revcount = shortlog and web.maxshortchanges or web.maxchanges
     if 'revcount' in req.form:
         try:
diff --git a/mercurial/hgweb/webutil.py b/mercurial/hgweb/webutil.py
--- a/mercurial/hgweb/webutil.py
+++ b/mercurial/hgweb/webutil.py
@@ -248,8 +248,37 @@  def filectx(repo, req):
         fctx = repo.filectx(path, fileid=changeid)
 
     return fctx
 
+def changelistentry(web, ctx, tmpl):
+    '''Obtain a dictionary to be used for entries in a changelist.
+
+    This function is called when producing items for the "entries" list passed
+    to the "shortlog" and "changelog" templates.
+    '''
+    repo = web.repo
+    rev = ctx.rev()
+    n = ctx.node()
+    showtags = showtag(repo, tmpl, 'changelogtag', n)
+    files = listfilediffs(tmpl, ctx.files(), n, web.maxfiles)
+
+    return {
+        "author": ctx.user(),
+        "parent": parents(ctx, rev - 1),
+        "child": 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)
+    }
+
 def listfilediffs(tmpl, files, node, max):
     for f in files[:max]:
         yield tmpl('filedifflink', node=hex(node), file=f)
     if len(files) > max: