Patchwork [3,of,7,V4] revset: factor out linerange processing into a utility function

login
register
mail settings
Submitter Denis Laxalde
Date March 24, 2017, 7:57 a.m.
Message ID <3b234555196d4df1881b.1490342256@sh77.tls.logilab.fr>
Download mbox | patch
Permalink /patch/19628/
State Superseded
Headers show

Comments

Denis Laxalde - March 24, 2017, 7:57 a.m.
# HG changeset patch
# User Denis Laxalde <denis.laxalde@logilab.fr>
# Date 1487957948 -3600
#      Fri Feb 24 18:39:08 2017 +0100
# Node ID 3b234555196d4df1881b59c0e8c9a2531a53eed4
# Parent  bc1ca69d5b73f43fb2c93c4f075c909eaf32e531
# Available At http://hg.logilab.org/users/dlaxalde/hg
#              hg pull http://hg.logilab.org/users/dlaxalde/hg -r 3b234555196d
# 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.

Patch

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')),