# Patchwork [4,of,6] revset: reject negative number to select first/last n members

Submitter Yuya Nishihara June 11, 2017, 5:36 a.m. mbox | patch /patch/21327/ Accepted show

Yuya Nishihara - June 11, 2017, 5:36 a.m.
```# HG changeset patch
# User Yuya Nishihara <yuya@tcha.org>
# Date 1497087311 -32400
#      Sat Jun 10 18:35:11 2017 +0900
# Parent  1d108aa3519ed6318b498171e0e52f9efabcb19f
revset: reject negative number to select first/last n members

Negative 'lim' doesn't make sense here, and it makes things complicated
when using list[:lim].
```

## Patch

```diff --git a/mercurial/revset.py b/mercurial/revset.py
--- a/mercurial/revset.py
+++ b/mercurial/revset.py
@@ -1162,6 +1162,8 @@  def limit(repo, subset, x):
raise error.ParseError(_("limit requires one to three arguments"))
# i18n: "limit" is a keyword
lim = getinteger(args.get('n'), _("limit expects a number"), default=1)
+    if lim < 0:
+        raise error.ParseError(_("negative number to select"))
# i18n: "limit" is a keyword
ofs = getinteger(args.get('offset'), _("limit expects a number"), default=0)
if ofs < 0:
@@ -1192,6 +1194,8 @@  def last(repo, subset, x):
if len(l) == 2:
# i18n: "last" is a keyword
lim = getinteger(l[1], _("last expects a number"))
+    if lim < 0:
+        raise error.ParseError(_("negative number to select"))
os = getset(repo, fullreposet(repo), l[0])
os.reverse()
result = []
diff --git a/tests/test-revset.t b/tests/test-revset.t
--- a/tests/test-revset.t
+++ b/tests/test-revset.t
@@ -1001,7 +1001,14 @@  Test first (=limit) and last
\$ log 'limit(all(), 1, -1)'
hg: parse error: negative offset
[255]
-
+  \$ log 'limit(all(), -1)'
+  hg: parse error: negative number to select
+  [255]
+  \$ log 'limit(all(), 0)'
+
+  \$ log 'last(all(), -1)'
+  hg: parse error: negative number to select
+  [255]
\$ log 'last(all(), 0)'
\$ log 'last(all(), 1)'
9

```