diff mercurial/ancestor.py @ 23342:f710644e1ce9

ancestor: add a way to remove ancestors of bases from a given set This and missingancestors can share state, which will turn out to be perfect for set discovery.
author Siddharth Agarwal <sid0@fb.com>
date Fri, 14 Nov 2014 19:40:30 -0800
parents bcc3012f8477
children 0ca8410ea345
line wrap: on
line diff
--- a/mercurial/ancestor.py	Fri Nov 14 17:21:00 2014 -0800
+++ b/mercurial/ancestor.py	Fri Nov 14 19:40:30 2014 -0800
@@ -154,6 +154,32 @@
         '''grow the ancestor set by adding new bases'''
         self.bases.update(newbases)
 
+    def removeancestorsfrom(self, revs):
+        '''remove all ancestors of bases from the set revs (in place)'''
+        bases = self.bases
+        pfunc = self.pfunc
+        revs.difference_update(bases)
+        # nullrev is always an ancestor
+        revs.discard(nullrev)
+        if not revs:
+            return
+        # anything in revs > start is definitely not an ancestor of bases
+        # revs <= start needs to be investigated
+        start = max(bases)
+        keepcount = sum(1 for r in revs if r > start)
+        if len(revs) == keepcount:
+            # no revs to consider
+            return
+
+        for curr in xrange(start, min(revs) - 1, -1):
+            if curr not in bases:
+                continue
+            revs.discard(curr)
+            bases.update(pfunc(curr))
+            if len(revs) == keepcount:
+                # no more potential revs to discard
+                break
+
     def missingancestors(self, revs):
         '''return all the ancestors of revs that are not ancestors of self.bases