diff --git a/mercurial/ancestor.py b/mercurial/ancestor.py --- a/mercurial/ancestor.py +++ b/mercurial/ancestor.py @@ -9,8 +9,12 @@ import heapq -from .node import nullrev +from .node import ( + nullrev, + wdirrev, +) from . import ( + error, policy, pycompat, ) @@ -306,8 +310,20 @@ heappush(visit, -p2) see(p2) +def parentsfunc(index): + def parentrevs(rev): + try: + entry = index[rev] + except IndexError: + if rev == wdirrev: + raise error.WdirUnsupported + raise + + return entry[5], entry[6] + return parentrevs + class lazyancestors(object): - def __init__(self, pfunc, revs, stoprev=0, inclusive=False): + def __init__(self, index, revs, stoprev=0, inclusive=False): """Create a new object generating ancestors for the given revs. Does not generate revs lower than stoprev. @@ -319,7 +335,7 @@ than stoprev will not be generated. Result does not include the null revision.""" - self._parentrevs = pfunc + self._parentrevs = parentsfunc(index) self._initrevs = revs = [r for r in revs if r >= stoprev] self._stoprev = stoprev self._inclusive = inclusive @@ -330,6 +346,7 @@ self._stoprev, self._inclusive) + def __nonzero__(self): """False if the set is empty, True otherwise.""" try: diff --git a/mercurial/revlog.py b/mercurial/revlog.py --- a/mercurial/revlog.py +++ b/mercurial/revlog.py @@ -675,9 +675,6 @@ return entry[5], entry[6] - # fast parentrevs(rev) where rev isn't filtered - _uncheckedparentrevs = parentrevs - def node(self, rev): try: return self.index[rev][7] @@ -797,14 +794,12 @@ rustext = None if rustext is not None: lazyancestors = rustext.ancestor.lazyancestors - arg = self.index elif util.safehasattr(parsers, 'rustlazyancestors'): lazyancestors = ancestor.rustlazyancestors - arg = self.index else: lazyancestors = ancestor.lazyancestors - arg = self._uncheckedparentrevs - return lazyancestors(arg, revs, stoprev=stoprev, inclusive=inclusive) + return lazyancestors(self.index, revs, + stoprev=stoprev, inclusive=inclusive) def descendants(self, revs): return dagop.descendantrevs(revs, self.revs, self.parentrevs) diff --git a/tests/test-ancestor.py b/tests/test-ancestor.py --- a/tests/test-ancestor.py +++ b/tests/test-ancestor.py @@ -240,10 +240,26 @@ else: print("Ok") +class fakeindex(object): + '''A pseudo index backed by a dict or list graph + + It's only meant for parents lookup, and + all that matters is that self.graph[rev] is the pair of rev's parents. + ''' + def __init__(self, graph): + self.graph = graph + def __getitem__(self, rev): + res = [None] * 5 + try: + res.extend(self.graph[rev]) + except KeyError: + raise IndexError(rev) + return res + def genlazyancestors(revs, stoprev=0, inclusive=False): print(("%% lazy ancestor set for %s, stoprev = %s, inclusive = %s" % (revs, stoprev, inclusive))) - return ancestor.lazyancestors(graph.get, revs, stoprev=stoprev, + return ancestor.lazyancestors(fakeindex(graph), revs, stoprev=stoprev, inclusive=inclusive) def printlazyancestors(s, l):