hgext3rd/topic/stack.py
changeset 2914 9897babc1fb5
parent 2839 f9c8c754a528
child 2915 b3abdb3d819e
--- a/hgext3rd/topic/stack.py	Thu Sep 07 16:45:57 2017 +0200
+++ b/hgext3rd/topic/stack.py	Mon Sep 04 12:19:49 2017 +0200
@@ -22,23 +22,45 @@
 if not util.safehasattr(context.basectx, 'isunstable'):
     context.basectx.isunstable = context.basectx.troubled
 
+class stack(object):
+    """object represent a stack and common logic associated to it."""
+
+    def __init__(self, repo, branch=None, topic=None):
+        self._repo = repo
+        self.branch = branch
+        self.topic = topic
+        if topic is not None and branch is not None:
+            raise error.ProgrammingError('both branch and topic specified (not defined yet)')
+        elif topic is not None:
+            trevs = repo.revs("topic(%s) - obsolete()", topic)
+        elif branch is not None:
+            trevs = repo.revs("branch(%s) - public() - obsolete() - topic()", branch)
+        else:
+            raise error.ProgrammingError('neither branch and topic specified (not defined yet)')
+        self._revs = trevs
+
+    def __iter__(self):
+        return iter(self.revs)
+
+    def __getitem__(self, index):
+        return self.revs[index]
+
+    def index(self, item):
+        return self.revs.index(item)
+
+    @util.propertycache
+    def revs(self):
+        revs = _orderrevs(self._repo, self._revs)
+        if revs:
+            pt1 = self._repo[revs[0]].p1()
+            if pt1.obsolete():
+                pt1 = self._repo[_singlesuccessor(self._repo, pt1)]
+            revs.insert(0, pt1.rev())
+        return revs
+
 def getstack(repo, branch=None, topic=None):
     # XXX need sorting
-    if topic is not None and branch is not None:
-        raise error.ProgrammingError('both branch and topic specified (not defined yet)')
-    elif topic is not None:
-        trevs = repo.revs("topic(%s) - obsolete()", topic)
-    elif branch is not None:
-        trevs = repo.revs("branch(%s) - public() - obsolete() - topic()", branch)
-    else:
-        raise error.ProgrammingError('neither branch and topic specified (not defined yet)')
-    revs = _orderrevs(repo, trevs)
-    if revs:
-        pt1 = repo[revs[0]].p1()
-        if pt1.obsolete():
-            pt1 = repo[_singlesuccessor(repo, pt1)]
-        revs.insert(0, pt1.rev())
-    return revs
+    return stack(repo, branch=branch, topic = topic)
 
 def labelsgen(prefix, labelssuffix):
     """ Takes a label prefix and a list of suffixes. Returns a string of the prefix