Mercurial > public > mercurial-scm > hg
comparison mercurial/revset.py @ 30801:67ee7874e53b
revset: factor out getinteger() helper
We have 4 revset functions that take integer arguments, and they handle
their arguments in slightly different ways. This patch unifies them:
- getstring() in place of getsymbol(), which is more consistent with the
handling of integer revisions (both 1 and '1' are valid)
- say "expects" instead of "requires" for type errors
We don't need to catch TypeError since getstring() must return a string.
author | Yuya Nishihara <yuya@tcha.org> |
---|---|
date | Mon, 09 Jan 2017 17:39:44 +0900 |
parents | cd23879cbac7 |
children | 5eb3e4568c94 |
comparison
equal
deleted
inserted
replaced
30800:cd23879cbac7 | 30801:67ee7874e53b |
---|---|
310 def getstring(x, err): | 310 def getstring(x, err): |
311 if x and (x[0] == 'string' or x[0] == 'symbol'): | 311 if x and (x[0] == 'string' or x[0] == 'symbol'): |
312 return x[1] | 312 return x[1] |
313 raise error.ParseError(err) | 313 raise error.ParseError(err) |
314 | 314 |
315 def getinteger(x, err): | |
316 try: | |
317 return int(getstring(x, err)) | |
318 except ValueError: | |
319 raise error.ParseError(err) | |
320 | |
315 def getlist(x): | 321 def getlist(x): |
316 if not x: | 322 if not x: |
317 return [] | 323 return [] |
318 if x[0] == 'list': | 324 if x[0] == 'list': |
319 return list(x[1:]) | 325 return list(x[1:]) |
537 def ancestorspec(repo, subset, x, n, order): | 543 def ancestorspec(repo, subset, x, n, order): |
538 """``set~n`` | 544 """``set~n`` |
539 Changesets that are the Nth ancestor (first parents only) of a changeset | 545 Changesets that are the Nth ancestor (first parents only) of a changeset |
540 in set. | 546 in set. |
541 """ | 547 """ |
542 try: | 548 n = getinteger(n, _("~ expects a number")) |
543 n = int(n[1]) | |
544 except (TypeError, ValueError): | |
545 raise error.ParseError(_("~ expects a number")) | |
546 ps = set() | 549 ps = set() |
547 cl = repo.changelog | 550 cl = repo.changelog |
548 for r in getset(repo, fullreposet(repo), x): | 551 for r in getset(repo, fullreposet(repo), x): |
549 for i in range(n): | 552 for i in range(n): |
550 r = cl.parentrevs(r)[0] | 553 r = cl.parentrevs(r)[0] |
1096 files = [f for f in repo[rev] if m(f)] | 1099 files = [f for f in repo[rev] if m(f)] |
1097 if len(files) != 1: | 1100 if len(files) != 1: |
1098 raise error.ParseError(_("followlines expects exactly one file")) | 1101 raise error.ParseError(_("followlines expects exactly one file")) |
1099 fname = files[0] | 1102 fname = files[0] |
1100 | 1103 |
1101 try: | 1104 fromline, toline = [getinteger(a, _("line range bounds must be integers")) |
1102 fromline, toline = [int(getsymbol(a)) for a in args['lines']] | 1105 for a in args['lines']] |
1103 except ValueError: | |
1104 raise error.ParseError(_("line range bounds must be integers")) | |
1105 if toline - fromline < 0: | 1106 if toline - fromline < 0: |
1106 raise error.ParseError(_("line range must be positive")) | 1107 raise error.ParseError(_("line range must be positive")) |
1107 if fromline < 1: | 1108 if fromline < 1: |
1108 raise error.ParseError(_("fromline must be strictly positive")) | 1109 raise error.ParseError(_("fromline must be strictly positive")) |
1109 fromline -= 1 | 1110 fromline -= 1 |
1271 """ | 1272 """ |
1272 args = getargsdict(x, 'limit', 'set n offset') | 1273 args = getargsdict(x, 'limit', 'set n offset') |
1273 if 'set' not in args: | 1274 if 'set' not in args: |
1274 # i18n: "limit" is a keyword | 1275 # i18n: "limit" is a keyword |
1275 raise error.ParseError(_("limit requires one to three arguments")) | 1276 raise error.ParseError(_("limit requires one to three arguments")) |
1276 try: | 1277 lim, ofs = 1, 0 |
1277 lim, ofs = 1, 0 | 1278 if 'n' in args: |
1278 if 'n' in args: | |
1279 # i18n: "limit" is a keyword | |
1280 lim = int(getstring(args['n'], _("limit requires a number"))) | |
1281 if 'offset' in args: | |
1282 # i18n: "limit" is a keyword | |
1283 ofs = int(getstring(args['offset'], _("limit requires a number"))) | |
1284 if ofs < 0: | |
1285 raise error.ParseError(_("negative offset")) | |
1286 except (TypeError, ValueError): | |
1287 # i18n: "limit" is a keyword | 1279 # i18n: "limit" is a keyword |
1288 raise error.ParseError(_("limit expects a number")) | 1280 lim = getinteger(args['n'], _("limit expects a number")) |
1281 if 'offset' in args: | |
1282 # i18n: "limit" is a keyword | |
1283 ofs = getinteger(args['offset'], _("limit expects a number")) | |
1284 if ofs < 0: | |
1285 raise error.ParseError(_("negative offset")) | |
1289 os = getset(repo, fullreposet(repo), args['set']) | 1286 os = getset(repo, fullreposet(repo), args['set']) |
1290 result = [] | 1287 result = [] |
1291 it = iter(os) | 1288 it = iter(os) |
1292 for x in xrange(ofs): | 1289 for x in xrange(ofs): |
1293 y = next(it, None) | 1290 y = next(it, None) |
1306 def last(repo, subset, x): | 1303 def last(repo, subset, x): |
1307 """Last n members of set, defaulting to 1. | 1304 """Last n members of set, defaulting to 1. |
1308 """ | 1305 """ |
1309 # i18n: "last" is a keyword | 1306 # i18n: "last" is a keyword |
1310 l = getargs(x, 1, 2, _("last requires one or two arguments")) | 1307 l = getargs(x, 1, 2, _("last requires one or two arguments")) |
1311 try: | 1308 lim = 1 |
1312 lim = 1 | 1309 if len(l) == 2: |
1313 if len(l) == 2: | |
1314 # i18n: "last" is a keyword | |
1315 lim = int(getstring(l[1], _("last requires a number"))) | |
1316 except (TypeError, ValueError): | |
1317 # i18n: "last" is a keyword | 1310 # i18n: "last" is a keyword |
1318 raise error.ParseError(_("last expects a number")) | 1311 lim = getinteger(l[1], _("last expects a number")) |
1319 os = getset(repo, fullreposet(repo), l[0]) | 1312 os = getset(repo, fullreposet(repo), l[0]) |
1320 os.reverse() | 1313 os.reverse() |
1321 result = [] | 1314 result = [] |
1322 it = iter(os) | 1315 it = iter(os) |
1323 for x in xrange(lim): | 1316 for x in xrange(lim): |