Patchwork [hglib] client: for loop over client (issue4511)

login
register
mail settings
Submitter Axel Hecht
Date April 6, 2016, 1:50 p.m.
Message ID <3478adf2394ad27a4b50.1459950654@fuchsia.local>
Download mbox | patch
Permalink /patch/14392/
State Changes Requested
Delegated to: Yuya Nishihara
Headers show

Comments

Axel Hecht - April 6, 2016, 1:50 p.m.
# HG changeset patch
# User Axel Hecht <axel@pike.org>
# Date 1442330828 -7200
#      Tue Sep 15 17:27:08 2015 +0200
# Node ID 3478adf2394ad27a4b501c91bbdae8157f06dd10
# Parent  674bd25c2339aa4b2be714b8d763c93c8eb07b36
client: for loop over client (issue4511)

By mapping ValueError to IndexError in __getitem__, one can do
for changeset in repo:
  pass
Yuya Nishihara - April 8, 2016, 2:16 p.m.
On Wed, 06 Apr 2016 15:50:54 +0200, Axel Hecht wrote:
> # HG changeset patch
> # User Axel Hecht <axel@pike.org>
> # Date 1442330828 -7200
> #      Tue Sep 15 17:27:08 2015 +0200
> # Node ID 3478adf2394ad27a4b501c91bbdae8157f06dd10
> # Parent  674bd25c2339aa4b2be714b8d763c93c8eb07b36
> client: for loop over client (issue4511)
> 
> By mapping ValueError to IndexError in __getitem__, one can do
> for changeset in repo:
>   pass
> 
> diff --git a/hglib/client.py b/hglib/client.py
> --- a/hglib/client.py
> +++ b/hglib/client.py
> @@ -1,4 +1,4 @@
> -import subprocess, os, struct, re, datetime
> +import subprocess, os, struct, sys, re, datetime

unused import?

> @@ -1631,7 +1631,10 @@
>          return self._version
>  
>      def __getitem__(self, changeid):
> -        return context.changectx(self, changeid)
> +        try:
> +            return context.changectx(self, changeid)
> +        except ValueError as e:
> +            raise IndexError(*e.args)

It seems legit to raise an exception of LookupError family, but I'm not
sure it should be IndexError because changeid isn't always an index.

> +    def __iter__(self):
> +        changeid = 0
> +        while True:
> +            try:
> +                yield self[changeid]
> +            except IndexError:
> +                break
> +            changeid += 1

You can't assume revision numbers are contiguous if evolve is enabled.

https://www.mercurial-scm.org/wiki/EvolveExtension

Patch

diff --git a/hglib/client.py b/hglib/client.py
--- a/hglib/client.py
+++ b/hglib/client.py
@@ -1,4 +1,4 @@ 
-import subprocess, os, struct, re, datetime
+import subprocess, os, struct, sys, re, datetime
 import hglib
 from hglib import error, util, templates, merge, context
 
@@ -1631,7 +1631,10 @@ 
         return self._version
 
     def __getitem__(self, changeid):
-        return context.changectx(self, changeid)
+        try:
+            return context.changectx(self, changeid)
+        except ValueError as e:
+            raise IndexError(*e.args)
 
     def __contains__(self, changeid):
         """
@@ -1643,3 +1646,12 @@ 
             return True
         except ValueError:
             return False
+
+    def __iter__(self):
+        changeid = 0
+        while True:
+            try:
+                yield self[changeid]
+            except IndexError:
+                break
+            changeid += 1
diff --git a/tests/test-log.py b/tests/test-log.py
--- a/tests/test-log.py
+++ b/tests/test-log.py
@@ -20,6 +20,19 @@ 
 
         self.assertEquals(self.client.log(), self.client.log(hidden=True))
 
+    def test_iterate(self):
+        self.append('a', 'a')
+        rev0, node0 = self.client.commit('first', addremove=True)
+        self.append('a', 'a')
+        rev1, node1 = self.client.commit('second')
+
+        revs = [commit for commit in self.client]
+
+        self.assertTrue(len(revs) == 2)
+        self.assertEquals(revs[1].node(), node1)
+
+        self.assertEquals(revs[0].node(), node0)
+
     # def test_errors(self):
     #     self.assertRaisesRegexp(CommandError, 'abort: unknown revision',
     #                             self.client.log, 'foo')