stack: introduce a rich stack object
authorPierre-Yves David <pierre-yves.david@octobus.net>
Mon, 04 Sep 2017 12:19:49 +0200
changeset 2914 9897babc1fb5
parent 2913 6b2ae9f2b9c4
child 2915 b3abdb3d819e
stack: introduce a rich stack object The logic around stack object become more and more complex. We great and object to gather and abstract it.
hgext3rd/topic/stack.py
--- 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