stablesort: use the filtered parents utility
authorPierre-Yves David <pierre-yves.david@octobus.net>
Mon, 18 Dec 2017 07:10:43 +0100
changeset 3311 df399e00c10b
parent 3310 87cb2635352b
child 3312 8e9ea8307cdd
stablesort: use the filtered parents utility
hgext3rd/evolve/stablesort.py
--- a/hgext3rd/evolve/stablesort.py	Mon Dec 18 06:50:57 2017 +0100
+++ b/hgext3rd/evolve/stablesort.py	Mon Dec 18 07:10:43 2017 +0100
@@ -22,8 +22,11 @@
 from . import (
     depthcache,
     exthelper,
+    utility,
 )
 
+filterparents = utility.filterparents
+
 eh = exthelper.exthelper()
 eh.merge(depthcache.eh)
 
@@ -97,10 +100,11 @@
     # * we need to detect branching
     children = collections.defaultdict(list)
     for r in cl.ancestors(revs, inclusive=True):
-        p1, p2 = parents(r)
-        children[p1].append(r)
-        if p2 != nullrev:
-            children[p2].append(r)
+        ps = filterparents(parents(r))
+        if not ps:
+            children[nullrev].append(r)
+        for p in ps:
+            children[p].append(r)
     # step two: walk back up
     # * pick lowest node in case of branching
     # * stack disregarded part of the branching
@@ -108,7 +112,7 @@
 
     # track what changeset has been
     seen = [0] * (max(revs) + 2)
-    seen[-1] = True # nullrev is known
+    seen[nullrev] = True # nullrev is known
     # starts from repository roots
     # reuse the list form the mapping as we won't need it again anyway
     stack = children[nullrev]
@@ -128,8 +132,8 @@
             if seen[current]:
                 current = None
                 continue
-        p1, p2 = parents(current)
-        if not (seen[p1] and seen[p2]):
+        ps = filterparents(parents(current))
+        if not all(seen[p] for p in ps):
             # we can't iterate on this merge yet because other child is not
             # yielded yet (and we are topo sorting) we can discard it for now
             # because it will be reached from the other child.
@@ -138,7 +142,7 @@
         assert not seen[current]
         seen[current] = True
         result.append(current) # could be yield, cf earlier comment
-        if mergecallback is not None and p2 != nullrev:
+        if mergecallback is not None and 2 <= len(ps):
             mergecallback(result, current)
         cs = children[current]
         if not cs:
@@ -199,15 +203,14 @@
     children = collections.defaultdict(set)
     parentmap = {}
     for r in revs:
-        p1, p2 = parents(r)
-        children[p1].add(r)
-        if p2 != nullrev:
-            children[p2].add(r)
-            parentmap[r] = tuple(sorted((p1, p2), key=tiebreaker))
-        elif p1 != nullrev:
-            parentmap[r] = (p1,)
-        else:
-            parentmap[r] = ()
+        ps = filterparents(parents(r))
+        if 2 <= len(ps):
+            ps = tuple(sorted(ps, key=tiebreaker))
+        parentmap[r] = ps
+        for p in ps:
+            children[p].add(r)
+        if not ps:
+            children[nullrev].add(r)
     # step two: walk again,
     stack = [head]
     resultset = set()
@@ -272,10 +275,10 @@
     mid = []
     bottom = []
 
-    ps = [p for p in parents(head) if p is not nodemod.nullrev]
+    ps = filterparents(parents(head))
     while len(ps) == 1:
         top.append(ps[0])
-        ps = [p for p in parents(ps[0]) if p is not nodemod.nullrev]
+        ps = filterparents(parents(ps[0]))
     top.reverse()
     if len(ps) == 2:
         ps.sort(key=tiebreaker)
@@ -319,7 +322,7 @@
         parentsfunc = cl.parentrevs
 
         def parents(rev):
-            return [p for p in parentsfunc(rev) if p is not nodemod.nullrev]
+            return filterparents(parentsfunc(rev))
 
         current = head
         previous_current = None