context: add a blockdescendants function
authorDenis Laxalde <denis.laxalde@logilab.fr>
Mon, 10 Apr 2017 15:11:36 +0200
changeset 31937 826e600605f6
parent 31936 806f9a883b4f
child 31938 5e3b49defbff
context: add a blockdescendants function This is symmetrical with blockancestors() and yields descendants of a filectx with changes in the given line range. The noticeable difference is that the algorithm does not follow renames (probably because filelog.descendants() does not), so we are missing branches with renames.
mercurial/context.py
--- a/mercurial/context.py	Thu Mar 09 22:40:52 2017 -0800
+++ b/mercurial/context.py	Mon Apr 10 15:11:36 2017 +0200
@@ -1208,6 +1208,26 @@
         if inrange:
             yield c, linerange2
 
+def blockdescendants(fctx, fromline, toline):
+    """Yield descendants of `fctx` with respect to the block of lines within
+    `fromline`-`toline` range.
+    """
+    diffopts = patch.diffopts(fctx._repo.ui)
+    fl = fctx.filelog()
+    seen = {fctx.filerev(): (fctx, (fromline, toline))}
+    for i in fl.descendants([fctx.filerev()]):
+        c = fctx.filectx(i)
+        for x in fl.parentrevs(i):
+            try:
+                p, linerange2 = seen.pop(x)
+            except KeyError:
+                # nullrev or other branch
+                continue
+            inrange, linerange1 = _changesrange(c, p, linerange2, diffopts)
+            if inrange:
+                yield c, linerange1
+            seen[i] = c, linerange1
+
 class committablectx(basectx):
     """A committablectx object provides common functionality for a context that
     wants the ability to commit, e.g. workingctx or memctx."""