generatorset: implement first and last methods
authorPierre-Yves David <pierre-yves.david@fb.com>
Mon, 06 Oct 2014 12:52:36 -0700
changeset 22811 c1fd827e1ae0
parent 22810 7f97cb12782f
child 22812 fcd12b310148
generatorset: implement first and last methods
mercurial/revset.py
--- a/mercurial/revset.py	Mon Oct 06 11:57:59 2014 -0700
+++ b/mercurial/revset.py	Mon Oct 06 12:52:36 2014 -0700
@@ -2767,6 +2767,34 @@
     def isdescending(self):
         return not self._ascending
 
+    def first(self):
+        if self._ascending:
+            it = self.fastasc
+        else:
+            it = self.fastdesc
+        if it is None:
+            # we need to consume all and try again
+            for x in self._consumegen():
+                pass
+            return self.first()
+        if self:
+            return it.next()
+        return None
+
+    def last(self):
+        if self._ascending:
+            it = self.fastdesc
+        else:
+            it = self.fastasc
+        if it is None:
+            # we need to consume all and try again
+            for x in self._consumegen():
+                pass
+            return self.first()
+        if self:
+            return it.next()
+        return None
+
 def spanset(repo, start=None, end=None):
     """factory function to dispatch between fullreposet and actual spanset