hgweb: simplify the handling of empty repo
authorPierre-Yves David <pierre-yves.david@logilab.fr>
Mon, 14 Jan 2013 16:30:06 +0100
changeset 18406 20cf53932b6f
parent 18405 1eaf0d017b2c
child 18407 50dc5b244474
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
mercurial/hgweb/webutil.py
--- a/mercurial/hgweb/webutil.py	Thu Jan 10 18:54:50 2013 +0100
+++ b/mercurial/hgweb/webutil.py	Mon Jan 14 16:30:06 2013 +0100
@@ -48,6 +48,14 @@
         """
         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()
 
@@ -64,6 +72,9 @@
             - 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 = []
@@ -77,10 +88,7 @@
                 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),