changeset 22811:c1fd827e1ae0

generatorset: implement first and last methods
author Pierre-Yves David <pierre-yves.david@fb.com>
date Mon, 06 Oct 2014 12:52:36 -0700
parents 7f97cb12782f
children fcd12b310148
files mercurial/revset.py
diffstat 1 files changed, 28 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- 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