[PATCH 1 of 7] dagop: unnest inner generator of revancestors()

Yuya Nishihara yuya at tcha.org
Thu Jun 22 11:52:14 EDT 2017


# HG changeset patch
# User Yuya Nishihara <yuya at tcha.org>
# Date 1497706403 -32400
#      Sat Jun 17 22:33:23 2017 +0900
# Node ID a8dd4bd0604af65ee06cd942e9af4b7971725f43
# Parent  c72a5780494d3c6f2850390f6557a6f60e454add
dagop: unnest inner generator of revancestors()

This just moves iterate() to module-level function.

diff --git a/mercurial/dagop.py b/mercurial/dagop.py
--- a/mercurial/dagop.py
+++ b/mercurial/dagop.py
@@ -20,43 +20,43 @@ from . import (
 baseset = smartset.baseset
 generatorset = smartset.generatorset
 
-def revancestors(repo, revs, followfirst):
-    """Like revlog.ancestors(), but supports followfirst."""
+def _genrevancestors(repo, revs, followfirst):
     if followfirst:
         cut = 1
     else:
         cut = None
     cl = repo.changelog
+    revs.sort(reverse=True)
+    irevs = iter(revs)
+    h = []
 
-    def iterate():
-        revs.sort(reverse=True)
-        irevs = iter(revs)
-        h = []
-
-        inputrev = next(irevs, None)
-        if inputrev is not None:
-            heapq.heappush(h, -inputrev)
+    inputrev = next(irevs, None)
+    if inputrev is not None:
+        heapq.heappush(h, -inputrev)
 
-        seen = set()
-        while h:
-            current = -heapq.heappop(h)
-            if current == inputrev:
-                inputrev = next(irevs, None)
-                if inputrev is not None:
-                    heapq.heappush(h, -inputrev)
-            if current not in seen:
-                seen.add(current)
-                yield current
-                try:
-                    for parent in cl.parentrevs(current)[:cut]:
-                        if parent != node.nullrev:
-                            heapq.heappush(h, -parent)
-                except error.WdirUnsupported:
-                    for parent in repo[current].parents()[:cut]:
-                        if parent.rev() != node.nullrev:
-                            heapq.heappush(h, -parent.rev())
+    seen = set()
+    while h:
+        current = -heapq.heappop(h)
+        if current == inputrev:
+            inputrev = next(irevs, None)
+            if inputrev is not None:
+                heapq.heappush(h, -inputrev)
+        if current not in seen:
+            seen.add(current)
+            yield current
+            try:
+                for parent in cl.parentrevs(current)[:cut]:
+                    if parent != node.nullrev:
+                        heapq.heappush(h, -parent)
+            except error.WdirUnsupported:
+                for parent in repo[current].parents()[:cut]:
+                    if parent.rev() != node.nullrev:
+                        heapq.heappush(h, -parent.rev())
 
-    return generatorset(iterate(), iterasc=False)
+def revancestors(repo, revs, followfirst):
+    """Like revlog.ancestors(), but supports followfirst."""
+    gen = _genrevancestors(repo, revs, followfirst)
+    return generatorset(gen, iterasc=False)
 
 def revdescendants(repo, revs, followfirst):
     """Like revlog.descendants() but supports followfirst."""


More information about the Mercurial-devel mailing list