[PATCH 3 of 7 V3] revset: factor out linerange processing into a utility function

Denis Laxalde denis at laxalde.org
Wed Mar 22 06:37:28 EDT 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 b93ba42f47ac861922b483ce9870fbe6a5cc412c
# Parent  613e7e42d14311059bdaf4c500147f9e35fb31fa
# Available At http://hg.logilab.org/users/dlaxalde/hg
#              hg pull http://hg.logilab.org/users/dlaxalde/hg -r b93ba42f47ac
# 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,7 @@ 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
+    fromline, toline = util.processlinerange(fromline, toline)
 
     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
@@ -2119,6 +2119,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):
+        ...
+    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')),


More information about the Mercurial-devel mailing list