[PATCH 1 of 3 V2] revset: extract a helper to parse integer range

Yuya Nishihara yuya at tcha.org
Fri Feb 1 08:23:36 EST 2019


# HG changeset patch
# User Yuya Nishihara <yuya at tcha.org>
# Date 1548562733 -32400
#      Sun Jan 27 13:18:53 2019 +0900
# Node ID 1502f467006b95eb15b3f51ab5dc4c1d5a53a90f
# Parent  fe2c826533a7285a38d536b532e0068ca4fb64e1
revset: extract a helper to parse integer range

It's getting common. As a first step, this patch adds getintrange() and
makes followlines() use it.

I wanted to unify the error messages to make the function interface simple,
but I failed to phrase it briefly.

diff --git a/mercurial/revset.py b/mercurial/revset.py
--- a/mercurial/revset.py
+++ b/mercurial/revset.py
@@ -44,6 +44,7 @@ getinteger = revsetlang.getinteger
 getboolean = revsetlang.getboolean
 getlist = revsetlang.getlist
 getrange = revsetlang.getrange
+getintrange = revsetlang.getintrange
 getargs = revsetlang.getargs
 getargsdict = revsetlang.getargsdict
 
@@ -1067,11 +1068,11 @@ def followlines(repo, subset, x):
     # i18n: "followlines" is a keyword
     msg = _("followlines expects exactly one file")
     fname = scmutil.parsefollowlinespattern(repo, rev, pat, msg)
-    # i18n: "followlines" is a keyword
-    lr = getrange(args['lines'][0], _("followlines expects a line range"))
-    fromline, toline = [getinteger(a, _("line range bounds must be integers"))
-                        for a in lr]
-    fromline, toline = util.processlinerange(fromline, toline)
+    fromline, toline = util.processlinerange(
+        *getintrange(args['lines'][0],
+                     # i18n: "followlines" is a keyword
+                     _("followlines expects a line range"),
+                     _("line range bounds must be integers")))
 
     fctx = repo[rev].filectx(fname)
     descend = False
diff --git a/mercurial/revsetlang.py b/mercurial/revsetlang.py
--- a/mercurial/revsetlang.py
+++ b/mercurial/revsetlang.py
@@ -240,6 +240,15 @@ def getrange(x, err):
         return None, None
     raise error.ParseError(err)
 
+def getintrange(x, err1, err2, deffirst=_notset, deflast=_notset):
+    """Get [first, last] integer range (both inclusive) from a parsed tree
+
+    If any of the sides omitted, and if no default provided, ParseError will
+    be raised.
+    """
+    a, b = getrange(x, err1)
+    return getinteger(a, err2, deffirst), getinteger(b, err2, deflast)
+
 def getargs(x, min, max, err):
     l = getlist(x)
     if len(l) < min or (max >= 0 and len(l) > max):


More information about the Mercurial-devel mailing list