D5443: ancestor: uniformity of calling lazyancestors classes

gracinet (Georges Racinet) phabricator at mercurial-scm.org
Sat Dec 15 11:43:27 UTC 2018


gracinet created this revision.
Herald added a reviewer: indygreg.
Herald added a subscriber: mercurial-devel.
Herald added a reviewer: hg-reviewers.

REVISION SUMMARY
  Up to now, the pure Python lazyancestors had been taking the parents
  function in its constructor, whereas Rust-backed variants would have needed
  a revlog index.
  
  With this change, instantiation of all lazyancestors work uniformely with
  an index. This requires only minor wrapping in test-ancestor.py.
  
  It could be argued that this introduces a new duplication, and that
  therefore, parentsfunc(index) should be provided in some utility module and
  used both from revlog and ancestor modules.

REPOSITORY
  rHG Mercurial

REVISION DETAIL
  https://phab.mercurial-scm.org/D5443

AFFECTED FILES
  mercurial/ancestor.py
  mercurial/revlog.py
  tests/test-ancestor.py

CHANGE DETAILS

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):
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/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:



To: gracinet, indygreg, #hg-reviewers
Cc: mercurial-devel


More information about the Mercurial-devel mailing list