[PATCH 1 of 8 V2] revset: factor out linerange processing into a utility function

Denis Laxalde denis at laxalde.org
Sat Feb 25 09:05:58 UTC 2017


# HG changeset patch
# User Denis Laxalde <denis.laxalde at logilab.fr>
# Date 1487957948 -3600
#      Fri Feb 24 18:39:08 2017 +0100
# Node ID 17c6195ce42f3cbb47cee53aeb4e6a4e5074878a
# Parent  abb92b3d370e116b29eba4d2e3154e9691c8edbb
# Available At https://hg.logilab.org/users/dlaxalde/hg
#              hg pull https://hg.logilab.org/users/dlaxalde/hg -r 17c6195ce42f
# EXP-Topic linerange-log/hgweb-filelog
revset: factor out linerange processing into a utility function

Similar processing will be done in hgweb.webutil in forthcoming changeset.

diff --git a/mercurial/revset.py b/mercurial/revset.py
--- a/mercurial/revset.py
+++ b/mercurial/revset.py
@@ -945,11 +945,10 @@ def followlines(repo, subset, x):
     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
+    try:
+        fromline, toline = util.processlinerange(fromline, toline)
+    except ValueError as exc:
+        raise error.ParseError(str(exc))
 
     fctx = repo[rev].filectx(fname)
     revs = (c.rev() for c, _linerange
diff --git a/mercurial/util.py b/mercurial/util.py
--- a/mercurial/util.py
+++ b/mercurial/util.py
@@ -2122,6 +2122,27 @@ def unitcountfn(*unittable):
 
     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):
+        ...
+    ValueError: line range must be positive
+    >>> processlinerange(0, 5)
+    Traceback (most recent call last):
+        ...
+    ValueError: fromline must be strictly positive
+    """
+    if toline - fromline < 0:
+        raise ValueError(_("line range must be positive"))
+    if fromline < 1:
+        raise ValueError(_("fromline must be strictly positive"))
+    return fromline - 1, toline
+
 bytecount = unitcountfn(
     (100, 1 << 30, _('%.0f GB')),
     (10, 1 << 30, _('%.1f GB')),


More information about the Mercurial-devel mailing list