revset: factor out linerange processing into a utility function
authorDenis Laxalde <denis.laxalde@logilab.fr>
Fri, 24 Feb 2017 18:39:08 +0100
changeset 31662 080734cd2440
parent 31661 f36dc643ffdc
child 31663 f33c9a5e81cd
revset: factor out linerange processing into a utility function Similar processing will be done in hgweb.webutil in forthcoming changeset.
mercurial/revset.py
mercurial/util.py
--- a/mercurial/revset.py	Mon Mar 13 10:41:13 2017 +0100
+++ b/mercurial/revset.py	Fri Feb 24 18:39:08 2017 +0100
@@ -945,11 +945,7 @@
     lr = getrange(args['lines'][0], _("followlines expects a line range"))
     fromline, toline = [getinteger(a, _("line range bounds must be integers"))
                         for a in lr]
-    if toline - fromline < 0:
-        raise error.ParseError(_("line range must be positive"))
-    if fromline < 1:
-        raise error.ParseError(_("fromline must be strictly positive"))
-    fromline -= 1
+    fromline, toline = util.processlinerange(fromline, toline)
 
     fctx = repo[rev].filectx(fname)
     revs = (c.rev() for c, _linerange
--- a/mercurial/util.py	Mon Mar 13 10:41:13 2017 +0100
+++ b/mercurial/util.py	Fri Feb 24 18:39:08 2017 +0100
@@ -2160,6 +2160,27 @@
 
     return go
 
+def processlinerange(fromline, toline):
+    """Check that linerange <fromline>:<toline> makes sense and return a
+    0-based range.
+
+    >>> processlinerange(10, 20)
+    (9, 20)
+    >>> processlinerange(2, 1)
+    Traceback (most recent call last):
+        ...
+    ParseError: line range must be positive
+    >>> processlinerange(0, 5)
+    Traceback (most recent call last):
+        ...
+    ParseError: fromline must be strictly positive
+    """
+    if toline - fromline < 0:
+        raise error.ParseError(_("line range must be positive"))
+    if fromline < 1:
+        raise error.ParseError(_("fromline must be strictly positive"))
+    return fromline - 1, toline
+
 bytecount = unitcountfn(
     (100, 1 << 30, _('%.0f GB')),
     (10, 1 << 30, _('%.1f GB')),