changeset 27023:0c8ef79b9fd7

webutil: make _siblings into an object with __iter__ and __len__ _siblings is a helper that is used for displaying changeset parents and children in hgweb. Before, when it was a simple generator, it couldn't tell its length without being consumed, and that required a special case when preparing data for changeset template (see 9e1f4c65f5f5). Let's make it into a class (similar to templatekw._hybrid) that allows len(...) without side-effects.
author Anton Shestakov <av6@dwimlabs.net>
date Tue, 10 Nov 2015 17:22:40 +0800
parents 35102876d648
children ceef5fb14872
files mercurial/hgweb/webutil.py
diffstat 1 files changed, 22 insertions(+), 14 deletions(-) [+]
line wrap: on
line diff
--- a/mercurial/hgweb/webutil.py	Wed Nov 18 16:39:30 2015 -0800
+++ b/mercurial/hgweb/webutil.py	Tue Nov 10 17:22:40 2015 +0800
@@ -125,20 +125,28 @@
     def hex(self, rev):
         return hex(self._changelog.node(self._revlog.linkrev(rev)))
 
+class _siblings(object):
+    def __init__(self, siblings=[], hiderev=None):
+        self.siblings = [s for s in siblings if s.node() != nullid]
+        if len(self.siblings) == 1 and self.siblings[0].rev() == hiderev:
+            self.siblings = []
 
-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
-    for s in siblings:
-        d = {'node': s.hex(), 'rev': s.rev()}
-        d['user'] = s.user()
-        d['date'] = s.date()
-        d['description'] = s.description()
-        d['branch'] = s.branch()
-        if util.safehasattr(s, 'path'):
-            d['file'] = s.path()
-        yield d
+    def __iter__(self):
+        for s in self.siblings:
+            d = {
+                'node': s.hex(),
+                'rev': s.rev(),
+                'user': s.user(),
+                'date': s.date(),
+                'description': s.description(),
+                'branch': s.branch(),
+            }
+            if util.safehasattr(s, 'path'):
+                d['file'] = s.path()
+            yield d
+
+    def __len__(self):
+        return len(self.siblings)
 
 def parents(ctx, hide=None):
     if isinstance(ctx, context.basefilectx):
@@ -355,7 +363,7 @@
         rev=ctx.rev(),
         node=ctx.hex(),
         symrev=symrevorshortnode(req, ctx),
-        parent=tuple(parents(ctx)),
+        parent=parents(ctx),
         child=children(ctx),
         basenode=basectx.hex(),
         changesettag=showtags,