revset: extract a helper to parse integer range
authorYuya Nishihara <yuya@tcha.org>
Sun, 27 Jan 2019 13:18:53 +0900
changeset 41561 59638c6fcb70
parent 41560 66399f2e92aa
child 41562 1c04894e8fe1
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.
mercurial/revset.py
mercurial/revsetlang.py
--- a/mercurial/revset.py	Thu Jan 31 14:47:34 2019 -0800
+++ b/mercurial/revset.py	Sun Jan 27 13:18:53 2019 +0900
@@ -44,6 +44,7 @@
 getboolean = revsetlang.getboolean
 getlist = revsetlang.getlist
 getrange = revsetlang.getrange
+getintrange = revsetlang.getintrange
 getargs = revsetlang.getargs
 getargsdict = revsetlang.getargsdict
 
@@ -1067,11 +1068,11 @@
     # 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
--- a/mercurial/revsetlang.py	Thu Jan 31 14:47:34 2019 -0800
+++ b/mercurial/revsetlang.py	Sun Jan 27 13:18:53 2019 +0900
@@ -240,6 +240,15 @@
         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):