# HG changeset patch # User Pierre-Yves David # Date 1504520389 -7200 # Node ID 9897babc1fb513871af3ba436c35dc846d88073c # Parent 6b2ae9f2b9c43e994039920cd31531b4f32d5743 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. diff -r 6b2ae9f2b9c4 -r 9897babc1fb5 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