[PATCH 07 of 15 V3] hgweb: simplify the handling of empty repo

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


# HG changeset patch
# User Pierre-Yves David <pierre-yves.david at logilab.fr>
# Date 1358177406 -3600
# Node ID c3821d05e8b7bb9158998d43024e12723025e270
# Parent  56d66eca9de40804a165128a45f0815f50acb7af
hgweb: simplify the handling of empty repo

This abstraction have two advantages:

- If the revlog is empty, None of the code bellow is relevant,
  early returns seems a win.

- Abtraction of the 'emptiness' check will help later when we stop relying on
  nodefunc.

A bonus, with filtering, a non-empty revlog may not have '0' revision
accessible. It'll be easier to handle with the emptiness test in a dedicated
function

diff --git a/mercurial/hgweb/webutil.py b/mercurial/hgweb/webutil.py
--- a/mercurial/hgweb/webutil.py
+++ b/mercurial/hgweb/webutil.py
@@ -46,10 +46,18 @@ class revnav(object):
 
         :nodefun: factory for a changectx from a revision
         """
         self.nodefunc = nodefunc
 
+    def __nonzero__(self):
+        """return True if any revision to navigate over"""
+        try:
+            self.nodefunc(0)
+            return True
+        except error.RepoError:
+            return False
+
     def hex(self, rev):
         return self.nodefunc(rev).hex()
 
     def gen(self, pos, pagelen, limit):
         """computes label and revision id for navigation link
@@ -62,10 +70,13 @@ class revnav(object):
             - 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
         """
+        if not self:
+            # empty repo
+            return ({'before': (), 'after': ()},)
 
         navbefore = []
         navafter = []
 
         for f in _navseq(1, pagelen):
@@ -75,14 +86,11 @@ class revnav(object):
                 navafter.append(("+%d" % f, self.hex(pos + f)))
             if pos - f >= 0:
                 navbefore.insert(0, ("-%d" % f, self.hex(pos - f)))
 
         navafter.append(("tip", "tip"))
-        try:
-            navbefore.insert(0, ("(0)", self.hex(0)))
-        except error.RepoError:
-            pass
+        navbefore.insert(0, ("(0)", self.hex(0)))
 
         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)},)
 


More information about the Mercurial-devel mailing list