Patchwork [3,of,3] revset: parse variable-length arguments of followlines() by getargsdict()

login
register
mail settings
Submitter Yuya Nishihara
Date Jan. 9, 2017, 2:38 p.m.
Message ID <77a94bdad2684df83e0a.1483972695@mimosa>
Download mbox | patch
Permalink /patch/18144/
State Accepted
Headers show

Comments

Yuya Nishihara - Jan. 9, 2017, 2:38 p.m.
# HG changeset patch
# User Yuya Nishihara <yuya@tcha.org>
# Date 1483945376 -32400
#      Mon Jan 09 16:02:56 2017 +0900
# Node ID 77a94bdad2684df83e0a5fc3646f144e03337c8c
# Parent  dff59b00d9a326d894244add8cf2b1bab99295a6
revset: parse variable-length arguments of followlines() by getargsdict()

Patch

diff --git a/mercurial/revset.py b/mercurial/revset.py
--- a/mercurial/revset.py
+++ b/mercurial/revset.py
@@ -1078,19 +1078,19 @@  def followlines(repo, subset, x):
     """
     from . import context  # avoid circular import issues
 
-    args = getargs(x, 3, 4, _("followlines takes at least three arguments"))
+    args = getargsdict(x, 'followlines', 'file *lines rev')
+    if len(args['lines']) != 2:
+        raise error.ParseError(_("followlines takes at least three arguments"))
 
     rev = '.'
-    if len(args) == 4:
-        revarg = getargsdict(args[3], 'followlines', 'rev')
-        if 'rev' in revarg:
-            revs = getset(repo, fullreposet(repo), revarg['rev'])
-            if len(revs) != 1:
-                raise error.ParseError(
-                    _("followlines expects exactly one revision"))
-            rev = revs.last()
-
-    pat = getstring(args[0], _("followlines requires a pattern"))
+    if 'rev' in args:
+        revs = getset(repo, fullreposet(repo), args['rev'])
+        if len(revs) != 1:
+            raise error.ParseError(
+                _("followlines expects exactly one revision"))
+        rev = revs.last()
+
+    pat = getstring(args['file'], _("followlines requires a pattern"))
     if not matchmod.patkind(pat):
         fname = pathutil.canonpath(repo.root, repo.getcwd(), pat)
     else:
@@ -1101,7 +1101,7 @@  def followlines(repo, subset, x):
         fname = files[0]
 
     try:
-        fromline, toline = [int(getsymbol(a)) for a in args[1:3]]
+        fromline, toline = [int(getsymbol(a)) for a in args['lines']]
     except ValueError:
         raise error.ParseError(_("line range bounds must be integers"))
     if toline - fromline < 0:
diff --git a/tests/test-annotate.t b/tests/test-annotate.t
--- a/tests/test-annotate.t
+++ b/tests/test-annotate.t
@@ -582,6 +582,15 @@  merge
   $ hg up 23 --quiet
 
 check error cases
+  $ hg log -r 'followlines()'
+  hg: parse error: followlines takes at least 1 positional arguments
+  [255]
+  $ hg log -r 'followlines(baz)'
+  hg: parse error: followlines takes at least three arguments
+  [255]
+  $ hg log -r 'followlines(baz, 1)'
+  hg: parse error: followlines takes at least three arguments
+  [255]
   $ hg log -r 'followlines(baz, 1, 2, rev=desc("b"))'
   hg: parse error: followlines expects exactly one revision
   [255]