[PATCH 04 of 15 V3] hgweb: move revnavgen into an object

pierre-yves.david at logilab.fr pierre-yves.david at logilab.fr
Wed Jan 16 07:32:10 CST 2013


# HG changeset patch
# User Pierre-Yves David <pierre-yves.david at logilab.fr>
# Date 1358281038 -3600
# Node ID 54a6788dab9ece15d3815d8abedba721fceba32c
# Parent  5f2606fbbe8a0b217870d59f4aecaa95ecd7a650
hgweb: move revnavgen into an object

For later compatibility with changelog filtering some part of the navigation
generation logic will be altered. Those altered part will be different when in
the changelog case and in the filelog case. Moving this into an object will
allow to use inheritance to override just the part of the logic we need.

The aimed logic are for example:

- generation of revision 'hex' (different logic for changelog and filelog)
- revlog emptyness test
- fetching of the first revision of a revlog (may not be 0)

diff --git a/mercurial/hgweb/webcommands.py b/mercurial/hgweb/webcommands.py
--- a/mercurial/hgweb/webcommands.py
+++ b/mercurial/hgweb/webcommands.py
@@ -240,11 +240,11 @@ def changelog(web, req, tmpl, shortlog=F
     start = max(0, pos - revcount + 1)
     end = min(count, start + revcount)
     pos = end - 1
     parity = paritygen(web.stripecount, offset=start - end)
 
-    changenav = webutil.revnavgen(pos, revcount, count, web.repo.changectx)
+    changenav = webutil.revnav().gen(pos, revcount, count, web.repo.changectx)
 
     return tmpl(shortlog and 'shortlog' or 'changelog', changenav=changenav,
                 node=ctx.hex(), rev=pos, changesets=count,
                 entries=lambda **x: changelist(latestonly=False, **x),
                 latestentry=lambda **x: changelist(latestonly=True, **x),
@@ -770,11 +770,11 @@ def filelog(web, req, tmpl):
                       "branches": webutil.nodebranchdict(repo, iterfctx)})
         for e in reversed(l):
             yield e
 
     nodefunc = lambda x: fctx.filectx(fileid=x)
-    nav = webutil.revnavgen(end - 1, revcount, count, nodefunc)
+    nav = webutil.revnav().gen(end - 1, revcount, count, nodefunc)
     return tmpl("filelog", file=f, node=fctx.hex(), nav=nav,
                 entries=lambda **x: entries(latestonly=False, **x),
                 latestentry=lambda **x: entries(latestonly=True, **x),
                 revcount=revcount, morevars=morevars, lessvars=lessvars)
 
@@ -849,11 +849,11 @@ def graph(web, req, tmpl):
     end = min(count, start + revcount)
     pos = end - 1
 
     uprev = min(max(0, count - 1), rev + revcount)
     downrev = max(0, rev - revcount)
-    changenav = webutil.revnavgen(pos, revcount, count, web.repo.changectx)
+    changenav = webutil.revnav().gen(pos, revcount, count, web.repo.changectx)
 
     dag = graphmod.dagwalker(web.repo, range(start, end)[::-1])
     tree = list(graphmod.colored(dag, web.repo))
 
     def getcolumns(tree):
diff --git a/mercurial/hgweb/webutil.py b/mercurial/hgweb/webutil.py
--- a/mercurial/hgweb/webutil.py
+++ b/mercurial/hgweb/webutil.py
@@ -37,45 +37,47 @@ def _navseq(step, firststep=None):
     while True:
         yield 1 * step
         yield 3 * step
         step *= 10
 
-def revnavgen(pos, pagelen, limit, nodefunc):
-    """computes label and revision id for navigation link
+class revnav(object):
 
-    :pos: is the revision relative to which we generate navigation.
-    :pagelen: the size of each navigation page
-    :limit: how far shall we link
-    :nodefun: factory for a changectx from a revision
+    def gen(self, pos, pagelen, limit, nodefunc):
+        """computes label and revision id for navigation link
 
-    The return is:
-        - a single element tuple
-        - containing a dictionary with a `before` and `after` key
-        - values are generator functions taking an arbitrary number of kwargs
-        - yield items are dictionaries with `label` and `node` keys
-    """
+        :pos: is the revision relative to which we generate navigation.
+        :pagelen: the size of each navigation page
+        :limit: how far shall we link
+        :nodefun: factory for a changectx from a revision
 
-    navbefore = []
-    navafter = []
+        The return is:
+            - a single element tuple
+            - containing a dictionary with a `before` and `after` key
+            - values are generator functions taking arbitrary number of kwargs
+            - yield items are dictionaries with `label` and `node` keys
+        """
 
-    for f in _navseq(1, pagelen):
-        if f > limit:
-            break
-        if pos + f < limit:
-            navafter.append(("+%d" % f, hex(nodefunc(pos + f).node())))
-        if pos - f >= 0:
-            navbefore.insert(0, ("-%d" % f, hex(nodefunc(pos - f).node())))
+        navbefore = []
+        navafter = []
 
-    navafter.append(("tip", "tip"))
-    try:
-        navbefore.insert(0, ("(0)", hex(nodefunc(0).node())))
-    except error.RepoError:
-        pass
+        for f in _navseq(1, pagelen):
+            if f > limit:
+                break
+            if pos + f < limit:
+                navafter.append(("+%d" % f, hex(nodefunc(pos + f).node())))
+            if pos - f >= 0:
+                navbefore.insert(0, ("-%d" % f, hex(nodefunc(pos - f).node())))
 
-    data = lambda i: {"label": i[0], "node": i[1]}
-    return ({'before': lambda **map: (data(i) for i in navbefore),
-             'after':  lambda **map: (data(i) for i in navafter)},)
+        navafter.append(("tip", "tip"))
+        try:
+            navbefore.insert(0, ("(0)", hex(nodefunc(0).node())))
+        except error.RepoError:
+            pass
+
+        data = lambda i: {"label": i[0], "node": i[1]}
+        return ({'before': lambda **map: (data(i) for i in navbefore),
+                 'after':  lambda **map: (data(i) for i in navafter)},)
 
 def _siblings(siblings=[], hiderev=None):
     siblings = [s for s in siblings if s.node() != nullid]
     if len(siblings) == 1 and siblings[0].rev() == hiderev:
         return


More information about the Mercurial-devel mailing list