##// END OF EJS Templates
revset: document the regular expression support for tag(name)...
Matt Harbison -
r20824:c57c9cec stable
parent child Browse files
Show More
@@ -1,2050 +1,2054 b''
1 1 # revset.py - revision set queries for mercurial
2 2 #
3 3 # Copyright 2010 Matt Mackall <mpm@selenic.com>
4 4 #
5 5 # This software may be used and distributed according to the terms of the
6 6 # GNU General Public License version 2 or any later version.
7 7
8 8 import re
9 9 import parser, util, error, discovery, hbisect, phases
10 10 import node
11 11 import match as matchmod
12 12 from i18n import _
13 13 import encoding
14 14 import obsolete as obsmod
15 15 import pathutil
16 16 import repoview
17 17
18 18 def _revancestors(repo, revs, followfirst):
19 19 """Like revlog.ancestors(), but supports followfirst."""
20 20 cut = followfirst and 1 or None
21 21 cl = repo.changelog
22 22 visit = util.deque(revs)
23 23 seen = set([node.nullrev])
24 24 while visit:
25 25 for parent in cl.parentrevs(visit.popleft())[:cut]:
26 26 if parent not in seen:
27 27 visit.append(parent)
28 28 seen.add(parent)
29 29 yield parent
30 30
31 31 def _revdescendants(repo, revs, followfirst):
32 32 """Like revlog.descendants() but supports followfirst."""
33 33 cut = followfirst and 1 or None
34 34 cl = repo.changelog
35 35 first = min(revs)
36 36 nullrev = node.nullrev
37 37 if first == nullrev:
38 38 # Are there nodes with a null first parent and a non-null
39 39 # second one? Maybe. Do we care? Probably not.
40 40 for i in cl:
41 41 yield i
42 42 return
43 43
44 44 seen = set(revs)
45 45 for i in cl.revs(first + 1):
46 46 for x in cl.parentrevs(i)[:cut]:
47 47 if x != nullrev and x in seen:
48 48 seen.add(i)
49 49 yield i
50 50 break
51 51
52 52 def _revsbetween(repo, roots, heads):
53 53 """Return all paths between roots and heads, inclusive of both endpoint
54 54 sets."""
55 55 if not roots:
56 56 return []
57 57 parentrevs = repo.changelog.parentrevs
58 58 visit = heads[:]
59 59 reachable = set()
60 60 seen = {}
61 61 minroot = min(roots)
62 62 roots = set(roots)
63 63 # open-code the post-order traversal due to the tiny size of
64 64 # sys.getrecursionlimit()
65 65 while visit:
66 66 rev = visit.pop()
67 67 if rev in roots:
68 68 reachable.add(rev)
69 69 parents = parentrevs(rev)
70 70 seen[rev] = parents
71 71 for parent in parents:
72 72 if parent >= minroot and parent not in seen:
73 73 visit.append(parent)
74 74 if not reachable:
75 75 return []
76 76 for rev in sorted(seen):
77 77 for parent in seen[rev]:
78 78 if parent in reachable:
79 79 reachable.add(rev)
80 80 return sorted(reachable)
81 81
82 82 elements = {
83 83 "(": (20, ("group", 1, ")"), ("func", 1, ")")),
84 84 "~": (18, None, ("ancestor", 18)),
85 85 "^": (18, None, ("parent", 18), ("parentpost", 18)),
86 86 "-": (5, ("negate", 19), ("minus", 5)),
87 87 "::": (17, ("dagrangepre", 17), ("dagrange", 17),
88 88 ("dagrangepost", 17)),
89 89 "..": (17, ("dagrangepre", 17), ("dagrange", 17),
90 90 ("dagrangepost", 17)),
91 91 ":": (15, ("rangepre", 15), ("range", 15), ("rangepost", 15)),
92 92 "not": (10, ("not", 10)),
93 93 "!": (10, ("not", 10)),
94 94 "and": (5, None, ("and", 5)),
95 95 "&": (5, None, ("and", 5)),
96 96 "or": (4, None, ("or", 4)),
97 97 "|": (4, None, ("or", 4)),
98 98 "+": (4, None, ("or", 4)),
99 99 ",": (2, None, ("list", 2)),
100 100 ")": (0, None, None),
101 101 "symbol": (0, ("symbol",), None),
102 102 "string": (0, ("string",), None),
103 103 "end": (0, None, None),
104 104 }
105 105
106 106 keywords = set(['and', 'or', 'not'])
107 107
108 108 def tokenize(program):
109 109 '''
110 110 Parse a revset statement into a stream of tokens
111 111
112 112 Check that @ is a valid unquoted token character (issue3686):
113 113 >>> list(tokenize("@::"))
114 114 [('symbol', '@', 0), ('::', None, 1), ('end', None, 3)]
115 115
116 116 '''
117 117
118 118 pos, l = 0, len(program)
119 119 while pos < l:
120 120 c = program[pos]
121 121 if c.isspace(): # skip inter-token whitespace
122 122 pass
123 123 elif c == ':' and program[pos:pos + 2] == '::': # look ahead carefully
124 124 yield ('::', None, pos)
125 125 pos += 1 # skip ahead
126 126 elif c == '.' and program[pos:pos + 2] == '..': # look ahead carefully
127 127 yield ('..', None, pos)
128 128 pos += 1 # skip ahead
129 129 elif c in "():,-|&+!~^": # handle simple operators
130 130 yield (c, None, pos)
131 131 elif (c in '"\'' or c == 'r' and
132 132 program[pos:pos + 2] in ("r'", 'r"')): # handle quoted strings
133 133 if c == 'r':
134 134 pos += 1
135 135 c = program[pos]
136 136 decode = lambda x: x
137 137 else:
138 138 decode = lambda x: x.decode('string-escape')
139 139 pos += 1
140 140 s = pos
141 141 while pos < l: # find closing quote
142 142 d = program[pos]
143 143 if d == '\\': # skip over escaped characters
144 144 pos += 2
145 145 continue
146 146 if d == c:
147 147 yield ('string', decode(program[s:pos]), s)
148 148 break
149 149 pos += 1
150 150 else:
151 151 raise error.ParseError(_("unterminated string"), s)
152 152 # gather up a symbol/keyword
153 153 elif c.isalnum() or c in '._@' or ord(c) > 127:
154 154 s = pos
155 155 pos += 1
156 156 while pos < l: # find end of symbol
157 157 d = program[pos]
158 158 if not (d.isalnum() or d in "._/@" or ord(d) > 127):
159 159 break
160 160 if d == '.' and program[pos - 1] == '.': # special case for ..
161 161 pos -= 1
162 162 break
163 163 pos += 1
164 164 sym = program[s:pos]
165 165 if sym in keywords: # operator keywords
166 166 yield (sym, None, s)
167 167 else:
168 168 yield ('symbol', sym, s)
169 169 pos -= 1
170 170 else:
171 171 raise error.ParseError(_("syntax error"), pos)
172 172 pos += 1
173 173 yield ('end', None, pos)
174 174
175 175 # helpers
176 176
177 177 def getstring(x, err):
178 178 if x and (x[0] == 'string' or x[0] == 'symbol'):
179 179 return x[1]
180 180 raise error.ParseError(err)
181 181
182 182 def getlist(x):
183 183 if not x:
184 184 return []
185 185 if x[0] == 'list':
186 186 return getlist(x[1]) + [x[2]]
187 187 return [x]
188 188
189 189 def getargs(x, min, max, err):
190 190 l = getlist(x)
191 191 if len(l) < min or (max >= 0 and len(l) > max):
192 192 raise error.ParseError(err)
193 193 return l
194 194
195 195 def getset(repo, subset, x):
196 196 if not x:
197 197 raise error.ParseError(_("missing argument"))
198 198 return methods[x[0]](repo, subset, *x[1:])
199 199
200 200 def _getrevsource(repo, r):
201 201 extra = repo[r].extra()
202 202 for label in ('source', 'transplant_source', 'rebase_source'):
203 203 if label in extra:
204 204 try:
205 205 return repo[extra[label]].rev()
206 206 except error.RepoLookupError:
207 207 pass
208 208 return None
209 209
210 210 # operator methods
211 211
212 212 def stringset(repo, subset, x):
213 213 x = repo[x].rev()
214 214 if x == -1 and len(subset) == len(repo):
215 215 return [-1]
216 216 if len(subset) == len(repo) or x in subset:
217 217 return [x]
218 218 return []
219 219
220 220 def symbolset(repo, subset, x):
221 221 if x in symbols:
222 222 raise error.ParseError(_("can't use %s here") % x)
223 223 return stringset(repo, subset, x)
224 224
225 225 def rangeset(repo, subset, x, y):
226 226 cl = repo.changelog
227 227 m = getset(repo, cl, x)
228 228 n = getset(repo, cl, y)
229 229
230 230 if not m or not n:
231 231 return []
232 232 m, n = m[0], n[-1]
233 233
234 234 if m < n:
235 235 r = range(m, n + 1)
236 236 else:
237 237 r = range(m, n - 1, -1)
238 238 s = set(subset)
239 239 return [x for x in r if x in s]
240 240
241 241 def dagrange(repo, subset, x, y):
242 242 r = list(repo)
243 243 xs = _revsbetween(repo, getset(repo, r, x), getset(repo, r, y))
244 244 s = set(subset)
245 245 return [r for r in xs if r in s]
246 246
247 247 def andset(repo, subset, x, y):
248 248 return getset(repo, getset(repo, subset, x), y)
249 249
250 250 def orset(repo, subset, x, y):
251 251 xl = getset(repo, subset, x)
252 252 s = set(xl)
253 253 yl = getset(repo, [r for r in subset if r not in s], y)
254 254 return xl + yl
255 255
256 256 def notset(repo, subset, x):
257 257 s = set(getset(repo, subset, x))
258 258 return [r for r in subset if r not in s]
259 259
260 260 def listset(repo, subset, a, b):
261 261 raise error.ParseError(_("can't use a list in this context"))
262 262
263 263 def func(repo, subset, a, b):
264 264 if a[0] == 'symbol' and a[1] in symbols:
265 265 return symbols[a[1]](repo, subset, b)
266 266 raise error.ParseError(_("not a function: %s") % a[1])
267 267
268 268 # functions
269 269
270 270 def adds(repo, subset, x):
271 271 """``adds(pattern)``
272 272 Changesets that add a file matching pattern.
273 273
274 274 The pattern without explicit kind like ``glob:`` is expected to be
275 275 relative to the current directory and match against a file or a
276 276 directory.
277 277 """
278 278 # i18n: "adds" is a keyword
279 279 pat = getstring(x, _("adds requires a pattern"))
280 280 return checkstatus(repo, subset, pat, 1)
281 281
282 282 def ancestor(repo, subset, x):
283 283 """``ancestor(*changeset)``
284 284 Greatest common ancestor of the changesets.
285 285
286 286 Accepts 0 or more changesets.
287 287 Will return empty list when passed no args.
288 288 Greatest common ancestor of a single changeset is that changeset.
289 289 """
290 290 # i18n: "ancestor" is a keyword
291 291 l = getlist(x)
292 292 rl = list(repo)
293 293 anc = None
294 294
295 295 # (getset(repo, rl, i) for i in l) generates a list of lists
296 296 rev = repo.changelog.rev
297 297 ancestor = repo.changelog.ancestor
298 298 node = repo.changelog.node
299 299 for revs in (getset(repo, rl, i) for i in l):
300 300 for r in revs:
301 301 if anc is None:
302 302 anc = r
303 303 else:
304 304 anc = rev(ancestor(node(anc), node(r)))
305 305
306 306 if anc is not None and anc in subset:
307 307 return [anc]
308 308 return []
309 309
310 310 def _ancestors(repo, subset, x, followfirst=False):
311 311 args = getset(repo, list(repo), x)
312 312 if not args:
313 313 return []
314 314 s = set(_revancestors(repo, args, followfirst)) | set(args)
315 315 return [r for r in subset if r in s]
316 316
317 317 def ancestors(repo, subset, x):
318 318 """``ancestors(set)``
319 319 Changesets that are ancestors of a changeset in set.
320 320 """
321 321 return _ancestors(repo, subset, x)
322 322
323 323 def _firstancestors(repo, subset, x):
324 324 # ``_firstancestors(set)``
325 325 # Like ``ancestors(set)`` but follows only the first parents.
326 326 return _ancestors(repo, subset, x, followfirst=True)
327 327
328 328 def ancestorspec(repo, subset, x, n):
329 329 """``set~n``
330 330 Changesets that are the Nth ancestor (first parents only) of a changeset
331 331 in set.
332 332 """
333 333 try:
334 334 n = int(n[1])
335 335 except (TypeError, ValueError):
336 336 raise error.ParseError(_("~ expects a number"))
337 337 ps = set()
338 338 cl = repo.changelog
339 339 for r in getset(repo, cl, x):
340 340 for i in range(n):
341 341 r = cl.parentrevs(r)[0]
342 342 ps.add(r)
343 343 return [r for r in subset if r in ps]
344 344
345 345 def author(repo, subset, x):
346 346 """``author(string)``
347 347 Alias for ``user(string)``.
348 348 """
349 349 # i18n: "author" is a keyword
350 350 n = encoding.lower(getstring(x, _("author requires a string")))
351 351 kind, pattern, matcher = _substringmatcher(n)
352 352 return [r for r in subset if matcher(encoding.lower(repo[r].user()))]
353 353
354 354 def bisect(repo, subset, x):
355 355 """``bisect(string)``
356 356 Changesets marked in the specified bisect status:
357 357
358 358 - ``good``, ``bad``, ``skip``: csets explicitly marked as good/bad/skip
359 359 - ``goods``, ``bads`` : csets topologically good/bad
360 360 - ``range`` : csets taking part in the bisection
361 361 - ``pruned`` : csets that are goods, bads or skipped
362 362 - ``untested`` : csets whose fate is yet unknown
363 363 - ``ignored`` : csets ignored due to DAG topology
364 364 - ``current`` : the cset currently being bisected
365 365 """
366 366 # i18n: "bisect" is a keyword
367 367 status = getstring(x, _("bisect requires a string")).lower()
368 368 state = set(hbisect.get(repo, status))
369 369 return [r for r in subset if r in state]
370 370
371 371 # Backward-compatibility
372 372 # - no help entry so that we do not advertise it any more
373 373 def bisected(repo, subset, x):
374 374 return bisect(repo, subset, x)
375 375
376 376 def bookmark(repo, subset, x):
377 377 """``bookmark([name])``
378 378 The named bookmark or all bookmarks.
379 379
380 380 If `name` starts with `re:`, the remainder of the name is treated as
381 381 a regular expression. To match a bookmark that actually starts with `re:`,
382 382 use the prefix `literal:`.
383 383 """
384 384 # i18n: "bookmark" is a keyword
385 385 args = getargs(x, 0, 1, _('bookmark takes one or no arguments'))
386 386 if args:
387 387 bm = getstring(args[0],
388 388 # i18n: "bookmark" is a keyword
389 389 _('the argument to bookmark must be a string'))
390 390 kind, pattern, matcher = _stringmatcher(bm)
391 391 if kind == 'literal':
392 392 bmrev = repo._bookmarks.get(bm, None)
393 393 if not bmrev:
394 394 raise util.Abort(_("bookmark '%s' does not exist") % bm)
395 395 bmrev = repo[bmrev].rev()
396 396 return [r for r in subset if r == bmrev]
397 397 else:
398 398 matchrevs = set()
399 399 for name, bmrev in repo._bookmarks.iteritems():
400 400 if matcher(name):
401 401 matchrevs.add(bmrev)
402 402 if not matchrevs:
403 403 raise util.Abort(_("no bookmarks exist that match '%s'")
404 404 % pattern)
405 405 bmrevs = set()
406 406 for bmrev in matchrevs:
407 407 bmrevs.add(repo[bmrev].rev())
408 408 return [r for r in subset if r in bmrevs]
409 409
410 410 bms = set([repo[r].rev()
411 411 for r in repo._bookmarks.values()])
412 412 return [r for r in subset if r in bms]
413 413
414 414 def branch(repo, subset, x):
415 415 """``branch(string or set)``
416 416 All changesets belonging to the given branch or the branches of the given
417 417 changesets.
418 418
419 419 If `string` starts with `re:`, the remainder of the name is treated as
420 420 a regular expression. To match a branch that actually starts with `re:`,
421 421 use the prefix `literal:`.
422 422 """
423 423 try:
424 424 b = getstring(x, '')
425 425 except error.ParseError:
426 426 # not a string, but another revspec, e.g. tip()
427 427 pass
428 428 else:
429 429 kind, pattern, matcher = _stringmatcher(b)
430 430 if kind == 'literal':
431 431 # note: falls through to the revspec case if no branch with
432 432 # this name exists
433 433 if pattern in repo.branchmap():
434 434 return [r for r in subset if matcher(repo[r].branch())]
435 435 else:
436 436 return [r for r in subset if matcher(repo[r].branch())]
437 437
438 438 s = getset(repo, list(repo), x)
439 439 b = set()
440 440 for r in s:
441 441 b.add(repo[r].branch())
442 442 s = set(s)
443 443 return [r for r in subset if r in s or repo[r].branch() in b]
444 444
445 445 def bumped(repo, subset, x):
446 446 """``bumped()``
447 447 Mutable changesets marked as successors of public changesets.
448 448
449 449 Only non-public and non-obsolete changesets can be `bumped`.
450 450 """
451 451 # i18n: "bumped" is a keyword
452 452 getargs(x, 0, 0, _("bumped takes no arguments"))
453 453 bumped = obsmod.getrevs(repo, 'bumped')
454 454 return [r for r in subset if r in bumped]
455 455
456 456 def bundle(repo, subset, x):
457 457 """``bundle()``
458 458 Changesets in the bundle.
459 459
460 460 Bundle must be specified by the -R option."""
461 461
462 462 try:
463 463 bundlerevs = repo.changelog.bundlerevs
464 464 except AttributeError:
465 465 raise util.Abort(_("no bundle provided - specify with -R"))
466 466 return [r for r in subset if r in bundlerevs]
467 467
468 468 def checkstatus(repo, subset, pat, field):
469 469 m = None
470 470 s = []
471 471 hasset = matchmod.patkind(pat) == 'set'
472 472 fname = None
473 473 for r in subset:
474 474 c = repo[r]
475 475 if not m or hasset:
476 476 m = matchmod.match(repo.root, repo.getcwd(), [pat], ctx=c)
477 477 if not m.anypats() and len(m.files()) == 1:
478 478 fname = m.files()[0]
479 479 if fname is not None:
480 480 if fname not in c.files():
481 481 continue
482 482 else:
483 483 for f in c.files():
484 484 if m(f):
485 485 break
486 486 else:
487 487 continue
488 488 files = repo.status(c.p1().node(), c.node())[field]
489 489 if fname is not None:
490 490 if fname in files:
491 491 s.append(r)
492 492 else:
493 493 for f in files:
494 494 if m(f):
495 495 s.append(r)
496 496 break
497 497 return s
498 498
499 499 def _children(repo, narrow, parentset):
500 500 cs = set()
501 501 if not parentset:
502 502 return cs
503 503 pr = repo.changelog.parentrevs
504 504 minrev = min(parentset)
505 505 for r in narrow:
506 506 if r <= minrev:
507 507 continue
508 508 for p in pr(r):
509 509 if p in parentset:
510 510 cs.add(r)
511 511 return cs
512 512
513 513 def children(repo, subset, x):
514 514 """``children(set)``
515 515 Child changesets of changesets in set.
516 516 """
517 517 s = set(getset(repo, list(repo), x))
518 518 cs = _children(repo, subset, s)
519 519 return [r for r in subset if r in cs]
520 520
521 521 def closed(repo, subset, x):
522 522 """``closed()``
523 523 Changeset is closed.
524 524 """
525 525 # i18n: "closed" is a keyword
526 526 getargs(x, 0, 0, _("closed takes no arguments"))
527 527 return [r for r in subset if repo[r].closesbranch()]
528 528
529 529 def contains(repo, subset, x):
530 530 """``contains(pattern)``
531 531 Revision contains a file matching pattern. See :hg:`help patterns`
532 532 for information about file patterns.
533 533
534 534 The pattern without explicit kind like ``glob:`` is expected to be
535 535 relative to the current directory and match against a file exactly
536 536 for efficiency.
537 537 """
538 538 # i18n: "contains" is a keyword
539 539 pat = getstring(x, _("contains requires a pattern"))
540 540 s = []
541 541 if not matchmod.patkind(pat):
542 542 pat = pathutil.canonpath(repo.root, repo.getcwd(), pat)
543 543 for r in subset:
544 544 if pat in repo[r]:
545 545 s.append(r)
546 546 else:
547 547 m = None
548 548 for r in subset:
549 549 c = repo[r]
550 550 if not m or matchmod.patkind(pat) == 'set':
551 551 m = matchmod.match(repo.root, repo.getcwd(), [pat], ctx=c)
552 552 for f in c.manifest():
553 553 if m(f):
554 554 s.append(r)
555 555 break
556 556 return s
557 557
558 558 def converted(repo, subset, x):
559 559 """``converted([id])``
560 560 Changesets converted from the given identifier in the old repository if
561 561 present, or all converted changesets if no identifier is specified.
562 562 """
563 563
564 564 # There is exactly no chance of resolving the revision, so do a simple
565 565 # string compare and hope for the best
566 566
567 567 rev = None
568 568 # i18n: "converted" is a keyword
569 569 l = getargs(x, 0, 1, _('converted takes one or no arguments'))
570 570 if l:
571 571 # i18n: "converted" is a keyword
572 572 rev = getstring(l[0], _('converted requires a revision'))
573 573
574 574 def _matchvalue(r):
575 575 source = repo[r].extra().get('convert_revision', None)
576 576 return source is not None and (rev is None or source.startswith(rev))
577 577
578 578 return [r for r in subset if _matchvalue(r)]
579 579
580 580 def date(repo, subset, x):
581 581 """``date(interval)``
582 582 Changesets within the interval, see :hg:`help dates`.
583 583 """
584 584 # i18n: "date" is a keyword
585 585 ds = getstring(x, _("date requires a string"))
586 586 dm = util.matchdate(ds)
587 587 return [r for r in subset if dm(repo[r].date()[0])]
588 588
589 589 def desc(repo, subset, x):
590 590 """``desc(string)``
591 591 Search commit message for string. The match is case-insensitive.
592 592 """
593 593 # i18n: "desc" is a keyword
594 594 ds = encoding.lower(getstring(x, _("desc requires a string")))
595 595 l = []
596 596 for r in subset:
597 597 c = repo[r]
598 598 if ds in encoding.lower(c.description()):
599 599 l.append(r)
600 600 return l
601 601
602 602 def _descendants(repo, subset, x, followfirst=False):
603 603 args = getset(repo, list(repo), x)
604 604 if not args:
605 605 return []
606 606 s = set(_revdescendants(repo, args, followfirst)) | set(args)
607 607 return [r for r in subset if r in s]
608 608
609 609 def descendants(repo, subset, x):
610 610 """``descendants(set)``
611 611 Changesets which are descendants of changesets in set.
612 612 """
613 613 return _descendants(repo, subset, x)
614 614
615 615 def _firstdescendants(repo, subset, x):
616 616 # ``_firstdescendants(set)``
617 617 # Like ``descendants(set)`` but follows only the first parents.
618 618 return _descendants(repo, subset, x, followfirst=True)
619 619
620 620 def destination(repo, subset, x):
621 621 """``destination([set])``
622 622 Changesets that were created by a graft, transplant or rebase operation,
623 623 with the given revisions specified as the source. Omitting the optional set
624 624 is the same as passing all().
625 625 """
626 626 if x is not None:
627 627 args = set(getset(repo, list(repo), x))
628 628 else:
629 629 args = set(getall(repo, list(repo), x))
630 630
631 631 dests = set()
632 632
633 633 # subset contains all of the possible destinations that can be returned, so
634 634 # iterate over them and see if their source(s) were provided in the args.
635 635 # Even if the immediate src of r is not in the args, src's source (or
636 636 # further back) may be. Scanning back further than the immediate src allows
637 637 # transitive transplants and rebases to yield the same results as transitive
638 638 # grafts.
639 639 for r in subset:
640 640 src = _getrevsource(repo, r)
641 641 lineage = None
642 642
643 643 while src is not None:
644 644 if lineage is None:
645 645 lineage = list()
646 646
647 647 lineage.append(r)
648 648
649 649 # The visited lineage is a match if the current source is in the arg
650 650 # set. Since every candidate dest is visited by way of iterating
651 651 # subset, any dests further back in the lineage will be tested by a
652 652 # different iteration over subset. Likewise, if the src was already
653 653 # selected, the current lineage can be selected without going back
654 654 # further.
655 655 if src in args or src in dests:
656 656 dests.update(lineage)
657 657 break
658 658
659 659 r = src
660 660 src = _getrevsource(repo, r)
661 661
662 662 return [r for r in subset if r in dests]
663 663
664 664 def divergent(repo, subset, x):
665 665 """``divergent()``
666 666 Final successors of changesets with an alternative set of final successors.
667 667 """
668 668 # i18n: "divergent" is a keyword
669 669 getargs(x, 0, 0, _("divergent takes no arguments"))
670 670 divergent = obsmod.getrevs(repo, 'divergent')
671 671 return [r for r in subset if r in divergent]
672 672
673 673 def draft(repo, subset, x):
674 674 """``draft()``
675 675 Changeset in draft phase."""
676 676 # i18n: "draft" is a keyword
677 677 getargs(x, 0, 0, _("draft takes no arguments"))
678 678 pc = repo._phasecache
679 679 return [r for r in subset if pc.phase(repo, r) == phases.draft]
680 680
681 681 def extinct(repo, subset, x):
682 682 """``extinct()``
683 683 Obsolete changesets with obsolete descendants only.
684 684 """
685 685 # i18n: "extinct" is a keyword
686 686 getargs(x, 0, 0, _("extinct takes no arguments"))
687 687 extincts = obsmod.getrevs(repo, 'extinct')
688 688 return [r for r in subset if r in extincts]
689 689
690 690 def extra(repo, subset, x):
691 691 """``extra(label, [value])``
692 692 Changesets with the given label in the extra metadata, with the given
693 693 optional value.
694 694
695 695 If `value` starts with `re:`, the remainder of the value is treated as
696 696 a regular expression. To match a value that actually starts with `re:`,
697 697 use the prefix `literal:`.
698 698 """
699 699
700 700 # i18n: "extra" is a keyword
701 701 l = getargs(x, 1, 2, _('extra takes at least 1 and at most 2 arguments'))
702 702 # i18n: "extra" is a keyword
703 703 label = getstring(l[0], _('first argument to extra must be a string'))
704 704 value = None
705 705
706 706 if len(l) > 1:
707 707 # i18n: "extra" is a keyword
708 708 value = getstring(l[1], _('second argument to extra must be a string'))
709 709 kind, value, matcher = _stringmatcher(value)
710 710
711 711 def _matchvalue(r):
712 712 extra = repo[r].extra()
713 713 return label in extra and (value is None or matcher(extra[label]))
714 714
715 715 return [r for r in subset if _matchvalue(r)]
716 716
717 717 def filelog(repo, subset, x):
718 718 """``filelog(pattern)``
719 719 Changesets connected to the specified filelog.
720 720
721 721 For performance reasons, ``filelog()`` does not show every changeset
722 722 that affects the requested file(s). See :hg:`help log` for details. For
723 723 a slower, more accurate result, use ``file()``.
724 724
725 725 The pattern without explicit kind like ``glob:`` is expected to be
726 726 relative to the current directory and match against a file exactly
727 727 for efficiency.
728 728 """
729 729
730 730 # i18n: "filelog" is a keyword
731 731 pat = getstring(x, _("filelog requires a pattern"))
732 732 s = set()
733 733
734 734 if not matchmod.patkind(pat):
735 735 f = pathutil.canonpath(repo.root, repo.getcwd(), pat)
736 736 fl = repo.file(f)
737 737 for fr in fl:
738 738 s.add(fl.linkrev(fr))
739 739 else:
740 740 m = matchmod.match(repo.root, repo.getcwd(), [pat], ctx=repo[None])
741 741 for f in repo[None]:
742 742 if m(f):
743 743 fl = repo.file(f)
744 744 for fr in fl:
745 745 s.add(fl.linkrev(fr))
746 746
747 747 return [r for r in subset if r in s]
748 748
749 749 def first(repo, subset, x):
750 750 """``first(set, [n])``
751 751 An alias for limit().
752 752 """
753 753 return limit(repo, subset, x)
754 754
755 755 def _follow(repo, subset, x, name, followfirst=False):
756 756 l = getargs(x, 0, 1, _("%s takes no arguments or a filename") % name)
757 757 c = repo['.']
758 758 if l:
759 759 x = getstring(l[0], _("%s expected a filename") % name)
760 760 if x in c:
761 761 cx = c[x]
762 762 s = set(ctx.rev() for ctx in cx.ancestors(followfirst=followfirst))
763 763 # include the revision responsible for the most recent version
764 764 s.add(cx.linkrev())
765 765 else:
766 766 return []
767 767 else:
768 768 s = set(_revancestors(repo, [c.rev()], followfirst)) | set([c.rev()])
769 769
770 770 return [r for r in subset if r in s]
771 771
772 772 def follow(repo, subset, x):
773 773 """``follow([file])``
774 774 An alias for ``::.`` (ancestors of the working copy's first parent).
775 775 If a filename is specified, the history of the given file is followed,
776 776 including copies.
777 777 """
778 778 return _follow(repo, subset, x, 'follow')
779 779
780 780 def _followfirst(repo, subset, x):
781 781 # ``followfirst([file])``
782 782 # Like ``follow([file])`` but follows only the first parent of
783 783 # every revision or file revision.
784 784 return _follow(repo, subset, x, '_followfirst', followfirst=True)
785 785
786 786 def getall(repo, subset, x):
787 787 """``all()``
788 788 All changesets, the same as ``0:tip``.
789 789 """
790 790 # i18n: "all" is a keyword
791 791 getargs(x, 0, 0, _("all takes no arguments"))
792 792 return subset
793 793
794 794 def grep(repo, subset, x):
795 795 """``grep(regex)``
796 796 Like ``keyword(string)`` but accepts a regex. Use ``grep(r'...')``
797 797 to ensure special escape characters are handled correctly. Unlike
798 798 ``keyword(string)``, the match is case-sensitive.
799 799 """
800 800 try:
801 801 # i18n: "grep" is a keyword
802 802 gr = re.compile(getstring(x, _("grep requires a string")))
803 803 except re.error, e:
804 804 raise error.ParseError(_('invalid match pattern: %s') % e)
805 805 l = []
806 806 for r in subset:
807 807 c = repo[r]
808 808 for e in c.files() + [c.user(), c.description()]:
809 809 if gr.search(e):
810 810 l.append(r)
811 811 break
812 812 return l
813 813
814 814 def _matchfiles(repo, subset, x):
815 815 # _matchfiles takes a revset list of prefixed arguments:
816 816 #
817 817 # [p:foo, i:bar, x:baz]
818 818 #
819 819 # builds a match object from them and filters subset. Allowed
820 820 # prefixes are 'p:' for regular patterns, 'i:' for include
821 821 # patterns and 'x:' for exclude patterns. Use 'r:' prefix to pass
822 822 # a revision identifier, or the empty string to reference the
823 823 # working directory, from which the match object is
824 824 # initialized. Use 'd:' to set the default matching mode, default
825 825 # to 'glob'. At most one 'r:' and 'd:' argument can be passed.
826 826
827 827 # i18n: "_matchfiles" is a keyword
828 828 l = getargs(x, 1, -1, _("_matchfiles requires at least one argument"))
829 829 pats, inc, exc = [], [], []
830 830 hasset = False
831 831 rev, default = None, None
832 832 for arg in l:
833 833 # i18n: "_matchfiles" is a keyword
834 834 s = getstring(arg, _("_matchfiles requires string arguments"))
835 835 prefix, value = s[:2], s[2:]
836 836 if prefix == 'p:':
837 837 pats.append(value)
838 838 elif prefix == 'i:':
839 839 inc.append(value)
840 840 elif prefix == 'x:':
841 841 exc.append(value)
842 842 elif prefix == 'r:':
843 843 if rev is not None:
844 844 # i18n: "_matchfiles" is a keyword
845 845 raise error.ParseError(_('_matchfiles expected at most one '
846 846 'revision'))
847 847 rev = value
848 848 elif prefix == 'd:':
849 849 if default is not None:
850 850 # i18n: "_matchfiles" is a keyword
851 851 raise error.ParseError(_('_matchfiles expected at most one '
852 852 'default mode'))
853 853 default = value
854 854 else:
855 855 # i18n: "_matchfiles" is a keyword
856 856 raise error.ParseError(_('invalid _matchfiles prefix: %s') % prefix)
857 857 if not hasset and matchmod.patkind(value) == 'set':
858 858 hasset = True
859 859 if not default:
860 860 default = 'glob'
861 861 m = None
862 862 s = []
863 863 for r in subset:
864 864 c = repo[r]
865 865 if not m or (hasset and rev is None):
866 866 ctx = c
867 867 if rev is not None:
868 868 ctx = repo[rev or None]
869 869 m = matchmod.match(repo.root, repo.getcwd(), pats, include=inc,
870 870 exclude=exc, ctx=ctx, default=default)
871 871 for f in c.files():
872 872 if m(f):
873 873 s.append(r)
874 874 break
875 875 return s
876 876
877 877 def hasfile(repo, subset, x):
878 878 """``file(pattern)``
879 879 Changesets affecting files matched by pattern.
880 880
881 881 For a faster but less accurate result, consider using ``filelog()``
882 882 instead.
883 883
884 884 This predicate uses ``glob:`` as the default kind of pattern.
885 885 """
886 886 # i18n: "file" is a keyword
887 887 pat = getstring(x, _("file requires a pattern"))
888 888 return _matchfiles(repo, subset, ('string', 'p:' + pat))
889 889
890 890 def head(repo, subset, x):
891 891 """``head()``
892 892 Changeset is a named branch head.
893 893 """
894 894 # i18n: "head" is a keyword
895 895 getargs(x, 0, 0, _("head takes no arguments"))
896 896 hs = set()
897 897 for b, ls in repo.branchmap().iteritems():
898 898 hs.update(repo[h].rev() for h in ls)
899 899 return [r for r in subset if r in hs]
900 900
901 901 def heads(repo, subset, x):
902 902 """``heads(set)``
903 903 Members of set with no children in set.
904 904 """
905 905 s = getset(repo, subset, x)
906 906 ps = set(parents(repo, subset, x))
907 907 return [r for r in s if r not in ps]
908 908
909 909 def hidden(repo, subset, x):
910 910 """``hidden()``
911 911 Hidden changesets.
912 912 """
913 913 # i18n: "hidden" is a keyword
914 914 getargs(x, 0, 0, _("hidden takes no arguments"))
915 915 hiddenrevs = repoview.filterrevs(repo, 'visible')
916 916 return [r for r in subset if r in hiddenrevs]
917 917
918 918 def keyword(repo, subset, x):
919 919 """``keyword(string)``
920 920 Search commit message, user name, and names of changed files for
921 921 string. The match is case-insensitive.
922 922 """
923 923 # i18n: "keyword" is a keyword
924 924 kw = encoding.lower(getstring(x, _("keyword requires a string")))
925 925 l = []
926 926 for r in subset:
927 927 c = repo[r]
928 928 if util.any(kw in encoding.lower(t)
929 929 for t in c.files() + [c.user(), c.description()]):
930 930 l.append(r)
931 931 return l
932 932
933 933 def limit(repo, subset, x):
934 934 """``limit(set, [n])``
935 935 First n members of set, defaulting to 1.
936 936 """
937 937 # i18n: "limit" is a keyword
938 938 l = getargs(x, 1, 2, _("limit requires one or two arguments"))
939 939 try:
940 940 lim = 1
941 941 if len(l) == 2:
942 942 # i18n: "limit" is a keyword
943 943 lim = int(getstring(l[1], _("limit requires a number")))
944 944 except (TypeError, ValueError):
945 945 # i18n: "limit" is a keyword
946 946 raise error.ParseError(_("limit expects a number"))
947 947 ss = set(subset)
948 948 os = getset(repo, list(repo), l[0])[:lim]
949 949 return [r for r in os if r in ss]
950 950
951 951 def last(repo, subset, x):
952 952 """``last(set, [n])``
953 953 Last n members of set, defaulting to 1.
954 954 """
955 955 # i18n: "last" is a keyword
956 956 l = getargs(x, 1, 2, _("last requires one or two arguments"))
957 957 try:
958 958 lim = 1
959 959 if len(l) == 2:
960 960 # i18n: "last" is a keyword
961 961 lim = int(getstring(l[1], _("last requires a number")))
962 962 except (TypeError, ValueError):
963 963 # i18n: "last" is a keyword
964 964 raise error.ParseError(_("last expects a number"))
965 965 ss = set(subset)
966 966 os = getset(repo, list(repo), l[0])[-lim:]
967 967 return [r for r in os if r in ss]
968 968
969 969 def maxrev(repo, subset, x):
970 970 """``max(set)``
971 971 Changeset with highest revision number in set.
972 972 """
973 973 os = getset(repo, list(repo), x)
974 974 if os:
975 975 m = max(os)
976 976 if m in subset:
977 977 return [m]
978 978 return []
979 979
980 980 def merge(repo, subset, x):
981 981 """``merge()``
982 982 Changeset is a merge changeset.
983 983 """
984 984 # i18n: "merge" is a keyword
985 985 getargs(x, 0, 0, _("merge takes no arguments"))
986 986 cl = repo.changelog
987 987 return [r for r in subset if cl.parentrevs(r)[1] != -1]
988 988
989 989 def branchpoint(repo, subset, x):
990 990 """``branchpoint()``
991 991 Changesets with more than one child.
992 992 """
993 993 # i18n: "branchpoint" is a keyword
994 994 getargs(x, 0, 0, _("branchpoint takes no arguments"))
995 995 cl = repo.changelog
996 996 if not subset:
997 997 return []
998 998 baserev = min(subset)
999 999 parentscount = [0]*(len(repo) - baserev)
1000 1000 for r in cl.revs(start=baserev + 1):
1001 1001 for p in cl.parentrevs(r):
1002 1002 if p >= baserev:
1003 1003 parentscount[p - baserev] += 1
1004 1004 return [r for r in subset if (parentscount[r - baserev] > 1)]
1005 1005
1006 1006 def minrev(repo, subset, x):
1007 1007 """``min(set)``
1008 1008 Changeset with lowest revision number in set.
1009 1009 """
1010 1010 os = getset(repo, list(repo), x)
1011 1011 if os:
1012 1012 m = min(os)
1013 1013 if m in subset:
1014 1014 return [m]
1015 1015 return []
1016 1016
1017 1017 def modifies(repo, subset, x):
1018 1018 """``modifies(pattern)``
1019 1019 Changesets modifying files matched by pattern.
1020 1020
1021 1021 The pattern without explicit kind like ``glob:`` is expected to be
1022 1022 relative to the current directory and match against a file or a
1023 1023 directory.
1024 1024 """
1025 1025 # i18n: "modifies" is a keyword
1026 1026 pat = getstring(x, _("modifies requires a pattern"))
1027 1027 return checkstatus(repo, subset, pat, 0)
1028 1028
1029 1029 def node_(repo, subset, x):
1030 1030 """``id(string)``
1031 1031 Revision non-ambiguously specified by the given hex string prefix.
1032 1032 """
1033 1033 # i18n: "id" is a keyword
1034 1034 l = getargs(x, 1, 1, _("id requires one argument"))
1035 1035 # i18n: "id" is a keyword
1036 1036 n = getstring(l[0], _("id requires a string"))
1037 1037 if len(n) == 40:
1038 1038 rn = repo[n].rev()
1039 1039 else:
1040 1040 rn = None
1041 1041 pm = repo.changelog._partialmatch(n)
1042 1042 if pm is not None:
1043 1043 rn = repo.changelog.rev(pm)
1044 1044
1045 1045 return [r for r in subset if r == rn]
1046 1046
1047 1047 def obsolete(repo, subset, x):
1048 1048 """``obsolete()``
1049 1049 Mutable changeset with a newer version."""
1050 1050 # i18n: "obsolete" is a keyword
1051 1051 getargs(x, 0, 0, _("obsolete takes no arguments"))
1052 1052 obsoletes = obsmod.getrevs(repo, 'obsolete')
1053 1053 return [r for r in subset if r in obsoletes]
1054 1054
1055 1055 def origin(repo, subset, x):
1056 1056 """``origin([set])``
1057 1057 Changesets that were specified as a source for the grafts, transplants or
1058 1058 rebases that created the given revisions. Omitting the optional set is the
1059 1059 same as passing all(). If a changeset created by these operations is itself
1060 1060 specified as a source for one of these operations, only the source changeset
1061 1061 for the first operation is selected.
1062 1062 """
1063 1063 if x is not None:
1064 1064 args = set(getset(repo, list(repo), x))
1065 1065 else:
1066 1066 args = set(getall(repo, list(repo), x))
1067 1067
1068 1068 def _firstsrc(rev):
1069 1069 src = _getrevsource(repo, rev)
1070 1070 if src is None:
1071 1071 return None
1072 1072
1073 1073 while True:
1074 1074 prev = _getrevsource(repo, src)
1075 1075
1076 1076 if prev is None:
1077 1077 return src
1078 1078 src = prev
1079 1079
1080 1080 o = set([_firstsrc(r) for r in args])
1081 1081 return [r for r in subset if r in o]
1082 1082
1083 1083 def outgoing(repo, subset, x):
1084 1084 """``outgoing([path])``
1085 1085 Changesets not found in the specified destination repository, or the
1086 1086 default push location.
1087 1087 """
1088 1088 import hg # avoid start-up nasties
1089 1089 # i18n: "outgoing" is a keyword
1090 1090 l = getargs(x, 0, 1, _("outgoing takes one or no arguments"))
1091 1091 # i18n: "outgoing" is a keyword
1092 1092 dest = l and getstring(l[0], _("outgoing requires a repository path")) or ''
1093 1093 dest = repo.ui.expandpath(dest or 'default-push', dest or 'default')
1094 1094 dest, branches = hg.parseurl(dest)
1095 1095 revs, checkout = hg.addbranchrevs(repo, repo, branches, [])
1096 1096 if revs:
1097 1097 revs = [repo.lookup(rev) for rev in revs]
1098 1098 other = hg.peer(repo, {}, dest)
1099 1099 repo.ui.pushbuffer()
1100 1100 outgoing = discovery.findcommonoutgoing(repo, other, onlyheads=revs)
1101 1101 repo.ui.popbuffer()
1102 1102 cl = repo.changelog
1103 1103 o = set([cl.rev(r) for r in outgoing.missing])
1104 1104 return [r for r in subset if r in o]
1105 1105
1106 1106 def p1(repo, subset, x):
1107 1107 """``p1([set])``
1108 1108 First parent of changesets in set, or the working directory.
1109 1109 """
1110 1110 if x is None:
1111 1111 p = repo[x].p1().rev()
1112 1112 return [r for r in subset if r == p]
1113 1113
1114 1114 ps = set()
1115 1115 cl = repo.changelog
1116 1116 for r in getset(repo, list(repo), x):
1117 1117 ps.add(cl.parentrevs(r)[0])
1118 1118 return [r for r in subset if r in ps]
1119 1119
1120 1120 def p2(repo, subset, x):
1121 1121 """``p2([set])``
1122 1122 Second parent of changesets in set, or the working directory.
1123 1123 """
1124 1124 if x is None:
1125 1125 ps = repo[x].parents()
1126 1126 try:
1127 1127 p = ps[1].rev()
1128 1128 return [r for r in subset if r == p]
1129 1129 except IndexError:
1130 1130 return []
1131 1131
1132 1132 ps = set()
1133 1133 cl = repo.changelog
1134 1134 for r in getset(repo, list(repo), x):
1135 1135 ps.add(cl.parentrevs(r)[1])
1136 1136 return [r for r in subset if r in ps]
1137 1137
1138 1138 def parents(repo, subset, x):
1139 1139 """``parents([set])``
1140 1140 The set of all parents for all changesets in set, or the working directory.
1141 1141 """
1142 1142 if x is None:
1143 1143 ps = tuple(p.rev() for p in repo[x].parents())
1144 1144 return [r for r in subset if r in ps]
1145 1145
1146 1146 ps = set()
1147 1147 cl = repo.changelog
1148 1148 for r in getset(repo, list(repo), x):
1149 1149 ps.update(cl.parentrevs(r))
1150 1150 return [r for r in subset if r in ps]
1151 1151
1152 1152 def parentspec(repo, subset, x, n):
1153 1153 """``set^0``
1154 1154 The set.
1155 1155 ``set^1`` (or ``set^``), ``set^2``
1156 1156 First or second parent, respectively, of all changesets in set.
1157 1157 """
1158 1158 try:
1159 1159 n = int(n[1])
1160 1160 if n not in (0, 1, 2):
1161 1161 raise ValueError
1162 1162 except (TypeError, ValueError):
1163 1163 raise error.ParseError(_("^ expects a number 0, 1, or 2"))
1164 1164 ps = set()
1165 1165 cl = repo.changelog
1166 1166 for r in getset(repo, cl, x):
1167 1167 if n == 0:
1168 1168 ps.add(r)
1169 1169 elif n == 1:
1170 1170 ps.add(cl.parentrevs(r)[0])
1171 1171 elif n == 2:
1172 1172 parents = cl.parentrevs(r)
1173 1173 if len(parents) > 1:
1174 1174 ps.add(parents[1])
1175 1175 return [r for r in subset if r in ps]
1176 1176
1177 1177 def present(repo, subset, x):
1178 1178 """``present(set)``
1179 1179 An empty set, if any revision in set isn't found; otherwise,
1180 1180 all revisions in set.
1181 1181
1182 1182 If any of specified revisions is not present in the local repository,
1183 1183 the query is normally aborted. But this predicate allows the query
1184 1184 to continue even in such cases.
1185 1185 """
1186 1186 try:
1187 1187 return getset(repo, subset, x)
1188 1188 except error.RepoLookupError:
1189 1189 return []
1190 1190
1191 1191 def public(repo, subset, x):
1192 1192 """``public()``
1193 1193 Changeset in public phase."""
1194 1194 # i18n: "public" is a keyword
1195 1195 getargs(x, 0, 0, _("public takes no arguments"))
1196 1196 pc = repo._phasecache
1197 1197 return [r for r in subset if pc.phase(repo, r) == phases.public]
1198 1198
1199 1199 def remote(repo, subset, x):
1200 1200 """``remote([id [,path]])``
1201 1201 Local revision that corresponds to the given identifier in a
1202 1202 remote repository, if present. Here, the '.' identifier is a
1203 1203 synonym for the current local branch.
1204 1204 """
1205 1205
1206 1206 import hg # avoid start-up nasties
1207 1207 # i18n: "remote" is a keyword
1208 1208 l = getargs(x, 0, 2, _("remote takes one, two or no arguments"))
1209 1209
1210 1210 q = '.'
1211 1211 if len(l) > 0:
1212 1212 # i18n: "remote" is a keyword
1213 1213 q = getstring(l[0], _("remote requires a string id"))
1214 1214 if q == '.':
1215 1215 q = repo['.'].branch()
1216 1216
1217 1217 dest = ''
1218 1218 if len(l) > 1:
1219 1219 # i18n: "remote" is a keyword
1220 1220 dest = getstring(l[1], _("remote requires a repository path"))
1221 1221 dest = repo.ui.expandpath(dest or 'default')
1222 1222 dest, branches = hg.parseurl(dest)
1223 1223 revs, checkout = hg.addbranchrevs(repo, repo, branches, [])
1224 1224 if revs:
1225 1225 revs = [repo.lookup(rev) for rev in revs]
1226 1226 other = hg.peer(repo, {}, dest)
1227 1227 n = other.lookup(q)
1228 1228 if n in repo:
1229 1229 r = repo[n].rev()
1230 1230 if r in subset:
1231 1231 return [r]
1232 1232 return []
1233 1233
1234 1234 def removes(repo, subset, x):
1235 1235 """``removes(pattern)``
1236 1236 Changesets which remove files matching pattern.
1237 1237
1238 1238 The pattern without explicit kind like ``glob:`` is expected to be
1239 1239 relative to the current directory and match against a file or a
1240 1240 directory.
1241 1241 """
1242 1242 # i18n: "removes" is a keyword
1243 1243 pat = getstring(x, _("removes requires a pattern"))
1244 1244 return checkstatus(repo, subset, pat, 2)
1245 1245
1246 1246 def rev(repo, subset, x):
1247 1247 """``rev(number)``
1248 1248 Revision with the given numeric identifier.
1249 1249 """
1250 1250 # i18n: "rev" is a keyword
1251 1251 l = getargs(x, 1, 1, _("rev requires one argument"))
1252 1252 try:
1253 1253 # i18n: "rev" is a keyword
1254 1254 l = int(getstring(l[0], _("rev requires a number")))
1255 1255 except (TypeError, ValueError):
1256 1256 # i18n: "rev" is a keyword
1257 1257 raise error.ParseError(_("rev expects a number"))
1258 1258 return [r for r in subset if r == l]
1259 1259
1260 1260 def matching(repo, subset, x):
1261 1261 """``matching(revision [, field])``
1262 1262 Changesets in which a given set of fields match the set of fields in the
1263 1263 selected revision or set.
1264 1264
1265 1265 To match more than one field pass the list of fields to match separated
1266 1266 by spaces (e.g. ``author description``).
1267 1267
1268 1268 Valid fields are most regular revision fields and some special fields.
1269 1269
1270 1270 Regular revision fields are ``description``, ``author``, ``branch``,
1271 1271 ``date``, ``files``, ``phase``, ``parents``, ``substate``, ``user``
1272 1272 and ``diff``.
1273 1273 Note that ``author`` and ``user`` are synonyms. ``diff`` refers to the
1274 1274 contents of the revision. Two revisions matching their ``diff`` will
1275 1275 also match their ``files``.
1276 1276
1277 1277 Special fields are ``summary`` and ``metadata``:
1278 1278 ``summary`` matches the first line of the description.
1279 1279 ``metadata`` is equivalent to matching ``description user date``
1280 1280 (i.e. it matches the main metadata fields).
1281 1281
1282 1282 ``metadata`` is the default field which is used when no fields are
1283 1283 specified. You can match more than one field at a time.
1284 1284 """
1285 1285 # i18n: "matching" is a keyword
1286 1286 l = getargs(x, 1, 2, _("matching takes 1 or 2 arguments"))
1287 1287
1288 1288 revs = getset(repo, repo.changelog, l[0])
1289 1289
1290 1290 fieldlist = ['metadata']
1291 1291 if len(l) > 1:
1292 1292 fieldlist = getstring(l[1],
1293 1293 # i18n: "matching" is a keyword
1294 1294 _("matching requires a string "
1295 1295 "as its second argument")).split()
1296 1296
1297 1297 # Make sure that there are no repeated fields,
1298 1298 # expand the 'special' 'metadata' field type
1299 1299 # and check the 'files' whenever we check the 'diff'
1300 1300 fields = []
1301 1301 for field in fieldlist:
1302 1302 if field == 'metadata':
1303 1303 fields += ['user', 'description', 'date']
1304 1304 elif field == 'diff':
1305 1305 # a revision matching the diff must also match the files
1306 1306 # since matching the diff is very costly, make sure to
1307 1307 # also match the files first
1308 1308 fields += ['files', 'diff']
1309 1309 else:
1310 1310 if field == 'author':
1311 1311 field = 'user'
1312 1312 fields.append(field)
1313 1313 fields = set(fields)
1314 1314 if 'summary' in fields and 'description' in fields:
1315 1315 # If a revision matches its description it also matches its summary
1316 1316 fields.discard('summary')
1317 1317
1318 1318 # We may want to match more than one field
1319 1319 # Not all fields take the same amount of time to be matched
1320 1320 # Sort the selected fields in order of increasing matching cost
1321 1321 fieldorder = ['phase', 'parents', 'user', 'date', 'branch', 'summary',
1322 1322 'files', 'description', 'substate', 'diff']
1323 1323 def fieldkeyfunc(f):
1324 1324 try:
1325 1325 return fieldorder.index(f)
1326 1326 except ValueError:
1327 1327 # assume an unknown field is very costly
1328 1328 return len(fieldorder)
1329 1329 fields = list(fields)
1330 1330 fields.sort(key=fieldkeyfunc)
1331 1331
1332 1332 # Each field will be matched with its own "getfield" function
1333 1333 # which will be added to the getfieldfuncs array of functions
1334 1334 getfieldfuncs = []
1335 1335 _funcs = {
1336 1336 'user': lambda r: repo[r].user(),
1337 1337 'branch': lambda r: repo[r].branch(),
1338 1338 'date': lambda r: repo[r].date(),
1339 1339 'description': lambda r: repo[r].description(),
1340 1340 'files': lambda r: repo[r].files(),
1341 1341 'parents': lambda r: repo[r].parents(),
1342 1342 'phase': lambda r: repo[r].phase(),
1343 1343 'substate': lambda r: repo[r].substate,
1344 1344 'summary': lambda r: repo[r].description().splitlines()[0],
1345 1345 'diff': lambda r: list(repo[r].diff(git=True),)
1346 1346 }
1347 1347 for info in fields:
1348 1348 getfield = _funcs.get(info, None)
1349 1349 if getfield is None:
1350 1350 raise error.ParseError(
1351 1351 # i18n: "matching" is a keyword
1352 1352 _("unexpected field name passed to matching: %s") % info)
1353 1353 getfieldfuncs.append(getfield)
1354 1354 # convert the getfield array of functions into a "getinfo" function
1355 1355 # which returns an array of field values (or a single value if there
1356 1356 # is only one field to match)
1357 1357 getinfo = lambda r: [f(r) for f in getfieldfuncs]
1358 1358
1359 1359 matches = set()
1360 1360 for rev in revs:
1361 1361 target = getinfo(rev)
1362 1362 for r in subset:
1363 1363 match = True
1364 1364 for n, f in enumerate(getfieldfuncs):
1365 1365 if target[n] != f(r):
1366 1366 match = False
1367 1367 break
1368 1368 if match:
1369 1369 matches.add(r)
1370 1370 return [r for r in subset if r in matches]
1371 1371
1372 1372 def reverse(repo, subset, x):
1373 1373 """``reverse(set)``
1374 1374 Reverse order of set.
1375 1375 """
1376 1376 l = getset(repo, subset, x)
1377 1377 if not isinstance(l, list):
1378 1378 l = list(l)
1379 1379 l.reverse()
1380 1380 return l
1381 1381
1382 1382 def roots(repo, subset, x):
1383 1383 """``roots(set)``
1384 1384 Changesets in set with no parent changeset in set.
1385 1385 """
1386 1386 s = set(getset(repo, repo.changelog, x))
1387 1387 subset = [r for r in subset if r in s]
1388 1388 cs = _children(repo, subset, s)
1389 1389 return [r for r in subset if r not in cs]
1390 1390
1391 1391 def secret(repo, subset, x):
1392 1392 """``secret()``
1393 1393 Changeset in secret phase."""
1394 1394 # i18n: "secret" is a keyword
1395 1395 getargs(x, 0, 0, _("secret takes no arguments"))
1396 1396 pc = repo._phasecache
1397 1397 return [r for r in subset if pc.phase(repo, r) == phases.secret]
1398 1398
1399 1399 def sort(repo, subset, x):
1400 1400 """``sort(set[, [-]key...])``
1401 1401 Sort set by keys. The default sort order is ascending, specify a key
1402 1402 as ``-key`` to sort in descending order.
1403 1403
1404 1404 The keys can be:
1405 1405
1406 1406 - ``rev`` for the revision number,
1407 1407 - ``branch`` for the branch name,
1408 1408 - ``desc`` for the commit message (description),
1409 1409 - ``user`` for user name (``author`` can be used as an alias),
1410 1410 - ``date`` for the commit date
1411 1411 """
1412 1412 # i18n: "sort" is a keyword
1413 1413 l = getargs(x, 1, 2, _("sort requires one or two arguments"))
1414 1414 keys = "rev"
1415 1415 if len(l) == 2:
1416 1416 # i18n: "sort" is a keyword
1417 1417 keys = getstring(l[1], _("sort spec must be a string"))
1418 1418
1419 1419 s = l[0]
1420 1420 keys = keys.split()
1421 1421 l = []
1422 1422 def invert(s):
1423 1423 return "".join(chr(255 - ord(c)) for c in s)
1424 1424 for r in getset(repo, subset, s):
1425 1425 c = repo[r]
1426 1426 e = []
1427 1427 for k in keys:
1428 1428 if k == 'rev':
1429 1429 e.append(r)
1430 1430 elif k == '-rev':
1431 1431 e.append(-r)
1432 1432 elif k == 'branch':
1433 1433 e.append(c.branch())
1434 1434 elif k == '-branch':
1435 1435 e.append(invert(c.branch()))
1436 1436 elif k == 'desc':
1437 1437 e.append(c.description())
1438 1438 elif k == '-desc':
1439 1439 e.append(invert(c.description()))
1440 1440 elif k in 'user author':
1441 1441 e.append(c.user())
1442 1442 elif k in '-user -author':
1443 1443 e.append(invert(c.user()))
1444 1444 elif k == 'date':
1445 1445 e.append(c.date()[0])
1446 1446 elif k == '-date':
1447 1447 e.append(-c.date()[0])
1448 1448 else:
1449 1449 raise error.ParseError(_("unknown sort key %r") % k)
1450 1450 e.append(r)
1451 1451 l.append(e)
1452 1452 l.sort()
1453 1453 return [e[-1] for e in l]
1454 1454
1455 1455 def _stringmatcher(pattern):
1456 1456 """
1457 1457 accepts a string, possibly starting with 're:' or 'literal:' prefix.
1458 1458 returns the matcher name, pattern, and matcher function.
1459 1459 missing or unknown prefixes are treated as literal matches.
1460 1460
1461 1461 helper for tests:
1462 1462 >>> def test(pattern, *tests):
1463 1463 ... kind, pattern, matcher = _stringmatcher(pattern)
1464 1464 ... return (kind, pattern, [bool(matcher(t)) for t in tests])
1465 1465
1466 1466 exact matching (no prefix):
1467 1467 >>> test('abcdefg', 'abc', 'def', 'abcdefg')
1468 1468 ('literal', 'abcdefg', [False, False, True])
1469 1469
1470 1470 regex matching ('re:' prefix)
1471 1471 >>> test('re:a.+b', 'nomatch', 'fooadef', 'fooadefbar')
1472 1472 ('re', 'a.+b', [False, False, True])
1473 1473
1474 1474 force exact matches ('literal:' prefix)
1475 1475 >>> test('literal:re:foobar', 'foobar', 're:foobar')
1476 1476 ('literal', 're:foobar', [False, True])
1477 1477
1478 1478 unknown prefixes are ignored and treated as literals
1479 1479 >>> test('foo:bar', 'foo', 'bar', 'foo:bar')
1480 1480 ('literal', 'foo:bar', [False, False, True])
1481 1481 """
1482 1482 if pattern.startswith('re:'):
1483 1483 pattern = pattern[3:]
1484 1484 try:
1485 1485 regex = re.compile(pattern)
1486 1486 except re.error, e:
1487 1487 raise error.ParseError(_('invalid regular expression: %s')
1488 1488 % e)
1489 1489 return 're', pattern, regex.search
1490 1490 elif pattern.startswith('literal:'):
1491 1491 pattern = pattern[8:]
1492 1492 return 'literal', pattern, pattern.__eq__
1493 1493
1494 1494 def _substringmatcher(pattern):
1495 1495 kind, pattern, matcher = _stringmatcher(pattern)
1496 1496 if kind == 'literal':
1497 1497 matcher = lambda s: pattern in s
1498 1498 return kind, pattern, matcher
1499 1499
1500 1500 def tag(repo, subset, x):
1501 1501 """``tag([name])``
1502 1502 The specified tag by name, or all tagged revisions if no name is given.
1503
1504 If `name` starts with `re:`, the remainder of the name is treated as
1505 a regular expression. To match a tag that actually starts with `re:`,
1506 use the prefix `literal:`.
1503 1507 """
1504 1508 # i18n: "tag" is a keyword
1505 1509 args = getargs(x, 0, 1, _("tag takes one or no arguments"))
1506 1510 cl = repo.changelog
1507 1511 if args:
1508 1512 pattern = getstring(args[0],
1509 1513 # i18n: "tag" is a keyword
1510 1514 _('the argument to tag must be a string'))
1511 1515 kind, pattern, matcher = _stringmatcher(pattern)
1512 1516 if kind == 'literal':
1513 1517 # avoid resolving all tags
1514 1518 tn = repo._tagscache.tags.get(pattern, None)
1515 1519 if tn is None:
1516 1520 raise util.Abort(_("tag '%s' does not exist") % pattern)
1517 1521 s = set([repo[tn].rev()])
1518 1522 else:
1519 1523 s = set([cl.rev(n) for t, n in repo.tagslist() if matcher(t)])
1520 1524 else:
1521 1525 s = set([cl.rev(n) for t, n in repo.tagslist() if t != 'tip'])
1522 1526 return [r for r in subset if r in s]
1523 1527
1524 1528 def tagged(repo, subset, x):
1525 1529 return tag(repo, subset, x)
1526 1530
1527 1531 def unstable(repo, subset, x):
1528 1532 """``unstable()``
1529 1533 Non-obsolete changesets with obsolete ancestors.
1530 1534 """
1531 1535 # i18n: "unstable" is a keyword
1532 1536 getargs(x, 0, 0, _("unstable takes no arguments"))
1533 1537 unstables = obsmod.getrevs(repo, 'unstable')
1534 1538 return [r for r in subset if r in unstables]
1535 1539
1536 1540
1537 1541 def user(repo, subset, x):
1538 1542 """``user(string)``
1539 1543 User name contains string. The match is case-insensitive.
1540 1544
1541 1545 If `string` starts with `re:`, the remainder of the string is treated as
1542 1546 a regular expression. To match a user that actually contains `re:`, use
1543 1547 the prefix `literal:`.
1544 1548 """
1545 1549 return author(repo, subset, x)
1546 1550
1547 1551 # for internal use
1548 1552 def _list(repo, subset, x):
1549 1553 s = getstring(x, "internal error")
1550 1554 if not s:
1551 1555 return []
1552 1556 if not isinstance(subset, set):
1553 1557 subset = set(subset)
1554 1558 ls = [repo[r].rev() for r in s.split('\0')]
1555 1559 return [r for r in ls if r in subset]
1556 1560
1557 1561 symbols = {
1558 1562 "adds": adds,
1559 1563 "all": getall,
1560 1564 "ancestor": ancestor,
1561 1565 "ancestors": ancestors,
1562 1566 "_firstancestors": _firstancestors,
1563 1567 "author": author,
1564 1568 "bisect": bisect,
1565 1569 "bisected": bisected,
1566 1570 "bookmark": bookmark,
1567 1571 "branch": branch,
1568 1572 "branchpoint": branchpoint,
1569 1573 "bumped": bumped,
1570 1574 "bundle": bundle,
1571 1575 "children": children,
1572 1576 "closed": closed,
1573 1577 "contains": contains,
1574 1578 "converted": converted,
1575 1579 "date": date,
1576 1580 "desc": desc,
1577 1581 "descendants": descendants,
1578 1582 "_firstdescendants": _firstdescendants,
1579 1583 "destination": destination,
1580 1584 "divergent": divergent,
1581 1585 "draft": draft,
1582 1586 "extinct": extinct,
1583 1587 "extra": extra,
1584 1588 "file": hasfile,
1585 1589 "filelog": filelog,
1586 1590 "first": first,
1587 1591 "follow": follow,
1588 1592 "_followfirst": _followfirst,
1589 1593 "grep": grep,
1590 1594 "head": head,
1591 1595 "heads": heads,
1592 1596 "hidden": hidden,
1593 1597 "id": node_,
1594 1598 "keyword": keyword,
1595 1599 "last": last,
1596 1600 "limit": limit,
1597 1601 "_matchfiles": _matchfiles,
1598 1602 "max": maxrev,
1599 1603 "merge": merge,
1600 1604 "min": minrev,
1601 1605 "modifies": modifies,
1602 1606 "obsolete": obsolete,
1603 1607 "origin": origin,
1604 1608 "outgoing": outgoing,
1605 1609 "p1": p1,
1606 1610 "p2": p2,
1607 1611 "parents": parents,
1608 1612 "present": present,
1609 1613 "public": public,
1610 1614 "remote": remote,
1611 1615 "removes": removes,
1612 1616 "rev": rev,
1613 1617 "reverse": reverse,
1614 1618 "roots": roots,
1615 1619 "sort": sort,
1616 1620 "secret": secret,
1617 1621 "matching": matching,
1618 1622 "tag": tag,
1619 1623 "tagged": tagged,
1620 1624 "user": user,
1621 1625 "unstable": unstable,
1622 1626 "_list": _list,
1623 1627 }
1624 1628
1625 1629 # symbols which can't be used for a DoS attack for any given input
1626 1630 # (e.g. those which accept regexes as plain strings shouldn't be included)
1627 1631 # functions that just return a lot of changesets (like all) don't count here
1628 1632 safesymbols = set([
1629 1633 "adds",
1630 1634 "all",
1631 1635 "ancestor",
1632 1636 "ancestors",
1633 1637 "_firstancestors",
1634 1638 "author",
1635 1639 "bisect",
1636 1640 "bisected",
1637 1641 "bookmark",
1638 1642 "branch",
1639 1643 "branchpoint",
1640 1644 "bumped",
1641 1645 "bundle",
1642 1646 "children",
1643 1647 "closed",
1644 1648 "converted",
1645 1649 "date",
1646 1650 "desc",
1647 1651 "descendants",
1648 1652 "_firstdescendants",
1649 1653 "destination",
1650 1654 "divergent",
1651 1655 "draft",
1652 1656 "extinct",
1653 1657 "extra",
1654 1658 "file",
1655 1659 "filelog",
1656 1660 "first",
1657 1661 "follow",
1658 1662 "_followfirst",
1659 1663 "head",
1660 1664 "heads",
1661 1665 "hidden",
1662 1666 "id",
1663 1667 "keyword",
1664 1668 "last",
1665 1669 "limit",
1666 1670 "_matchfiles",
1667 1671 "max",
1668 1672 "merge",
1669 1673 "min",
1670 1674 "modifies",
1671 1675 "obsolete",
1672 1676 "origin",
1673 1677 "outgoing",
1674 1678 "p1",
1675 1679 "p2",
1676 1680 "parents",
1677 1681 "present",
1678 1682 "public",
1679 1683 "remote",
1680 1684 "removes",
1681 1685 "rev",
1682 1686 "reverse",
1683 1687 "roots",
1684 1688 "sort",
1685 1689 "secret",
1686 1690 "matching",
1687 1691 "tag",
1688 1692 "tagged",
1689 1693 "user",
1690 1694 "unstable",
1691 1695 "_list",
1692 1696 ])
1693 1697
1694 1698 methods = {
1695 1699 "range": rangeset,
1696 1700 "dagrange": dagrange,
1697 1701 "string": stringset,
1698 1702 "symbol": symbolset,
1699 1703 "and": andset,
1700 1704 "or": orset,
1701 1705 "not": notset,
1702 1706 "list": listset,
1703 1707 "func": func,
1704 1708 "ancestor": ancestorspec,
1705 1709 "parent": parentspec,
1706 1710 "parentpost": p1,
1707 1711 }
1708 1712
1709 1713 def optimize(x, small):
1710 1714 if x is None:
1711 1715 return 0, x
1712 1716
1713 1717 smallbonus = 1
1714 1718 if small:
1715 1719 smallbonus = .5
1716 1720
1717 1721 op = x[0]
1718 1722 if op == 'minus':
1719 1723 return optimize(('and', x[1], ('not', x[2])), small)
1720 1724 elif op == 'dagrangepre':
1721 1725 return optimize(('func', ('symbol', 'ancestors'), x[1]), small)
1722 1726 elif op == 'dagrangepost':
1723 1727 return optimize(('func', ('symbol', 'descendants'), x[1]), small)
1724 1728 elif op == 'rangepre':
1725 1729 return optimize(('range', ('string', '0'), x[1]), small)
1726 1730 elif op == 'rangepost':
1727 1731 return optimize(('range', x[1], ('string', 'tip')), small)
1728 1732 elif op == 'negate':
1729 1733 return optimize(('string',
1730 1734 '-' + getstring(x[1], _("can't negate that"))), small)
1731 1735 elif op in 'string symbol negate':
1732 1736 return smallbonus, x # single revisions are small
1733 1737 elif op == 'and':
1734 1738 wa, ta = optimize(x[1], True)
1735 1739 wb, tb = optimize(x[2], True)
1736 1740 w = min(wa, wb)
1737 1741 if wa > wb:
1738 1742 return w, (op, tb, ta)
1739 1743 return w, (op, ta, tb)
1740 1744 elif op == 'or':
1741 1745 wa, ta = optimize(x[1], False)
1742 1746 wb, tb = optimize(x[2], False)
1743 1747 if wb < wa:
1744 1748 wb, wa = wa, wb
1745 1749 return max(wa, wb), (op, ta, tb)
1746 1750 elif op == 'not':
1747 1751 o = optimize(x[1], not small)
1748 1752 return o[0], (op, o[1])
1749 1753 elif op == 'parentpost':
1750 1754 o = optimize(x[1], small)
1751 1755 return o[0], (op, o[1])
1752 1756 elif op == 'group':
1753 1757 return optimize(x[1], small)
1754 1758 elif op in 'dagrange range list parent ancestorspec':
1755 1759 if op == 'parent':
1756 1760 # x^:y means (x^) : y, not x ^ (:y)
1757 1761 post = ('parentpost', x[1])
1758 1762 if x[2][0] == 'dagrangepre':
1759 1763 return optimize(('dagrange', post, x[2][1]), small)
1760 1764 elif x[2][0] == 'rangepre':
1761 1765 return optimize(('range', post, x[2][1]), small)
1762 1766
1763 1767 wa, ta = optimize(x[1], small)
1764 1768 wb, tb = optimize(x[2], small)
1765 1769 return wa + wb, (op, ta, tb)
1766 1770 elif op == 'func':
1767 1771 f = getstring(x[1], _("not a symbol"))
1768 1772 wa, ta = optimize(x[2], small)
1769 1773 if f in ("author branch closed date desc file grep keyword "
1770 1774 "outgoing user"):
1771 1775 w = 10 # slow
1772 1776 elif f in "modifies adds removes":
1773 1777 w = 30 # slower
1774 1778 elif f == "contains":
1775 1779 w = 100 # very slow
1776 1780 elif f == "ancestor":
1777 1781 w = 1 * smallbonus
1778 1782 elif f in "reverse limit first":
1779 1783 w = 0
1780 1784 elif f in "sort":
1781 1785 w = 10 # assume most sorts look at changelog
1782 1786 else:
1783 1787 w = 1
1784 1788 return w + wa, (op, x[1], ta)
1785 1789 return 1, x
1786 1790
1787 1791 _aliasarg = ('func', ('symbol', '_aliasarg'))
1788 1792 def _getaliasarg(tree):
1789 1793 """If tree matches ('func', ('symbol', '_aliasarg'), ('string', X))
1790 1794 return X, None otherwise.
1791 1795 """
1792 1796 if (len(tree) == 3 and tree[:2] == _aliasarg
1793 1797 and tree[2][0] == 'string'):
1794 1798 return tree[2][1]
1795 1799 return None
1796 1800
1797 1801 def _checkaliasarg(tree, known=None):
1798 1802 """Check tree contains no _aliasarg construct or only ones which
1799 1803 value is in known. Used to avoid alias placeholders injection.
1800 1804 """
1801 1805 if isinstance(tree, tuple):
1802 1806 arg = _getaliasarg(tree)
1803 1807 if arg is not None and (not known or arg not in known):
1804 1808 raise error.ParseError(_("not a function: %s") % '_aliasarg')
1805 1809 for t in tree:
1806 1810 _checkaliasarg(t, known)
1807 1811
1808 1812 class revsetalias(object):
1809 1813 funcre = re.compile('^([^(]+)\(([^)]+)\)$')
1810 1814 args = None
1811 1815
1812 1816 def __init__(self, name, value):
1813 1817 '''Aliases like:
1814 1818
1815 1819 h = heads(default)
1816 1820 b($1) = ancestors($1) - ancestors(default)
1817 1821 '''
1818 1822 m = self.funcre.search(name)
1819 1823 if m:
1820 1824 self.name = m.group(1)
1821 1825 self.tree = ('func', ('symbol', m.group(1)))
1822 1826 self.args = [x.strip() for x in m.group(2).split(',')]
1823 1827 for arg in self.args:
1824 1828 # _aliasarg() is an unknown symbol only used separate
1825 1829 # alias argument placeholders from regular strings.
1826 1830 value = value.replace(arg, '_aliasarg(%r)' % (arg,))
1827 1831 else:
1828 1832 self.name = name
1829 1833 self.tree = ('symbol', name)
1830 1834
1831 1835 self.replacement, pos = parse(value)
1832 1836 if pos != len(value):
1833 1837 raise error.ParseError(_('invalid token'), pos)
1834 1838 # Check for placeholder injection
1835 1839 _checkaliasarg(self.replacement, self.args)
1836 1840
1837 1841 def _getalias(aliases, tree):
1838 1842 """If tree looks like an unexpanded alias, return it. Return None
1839 1843 otherwise.
1840 1844 """
1841 1845 if isinstance(tree, tuple) and tree:
1842 1846 if tree[0] == 'symbol' and len(tree) == 2:
1843 1847 name = tree[1]
1844 1848 alias = aliases.get(name)
1845 1849 if alias and alias.args is None and alias.tree == tree:
1846 1850 return alias
1847 1851 if tree[0] == 'func' and len(tree) > 1:
1848 1852 if tree[1][0] == 'symbol' and len(tree[1]) == 2:
1849 1853 name = tree[1][1]
1850 1854 alias = aliases.get(name)
1851 1855 if alias and alias.args is not None and alias.tree == tree[:2]:
1852 1856 return alias
1853 1857 return None
1854 1858
1855 1859 def _expandargs(tree, args):
1856 1860 """Replace _aliasarg instances with the substitution value of the
1857 1861 same name in args, recursively.
1858 1862 """
1859 1863 if not tree or not isinstance(tree, tuple):
1860 1864 return tree
1861 1865 arg = _getaliasarg(tree)
1862 1866 if arg is not None:
1863 1867 return args[arg]
1864 1868 return tuple(_expandargs(t, args) for t in tree)
1865 1869
1866 1870 def _expandaliases(aliases, tree, expanding, cache):
1867 1871 """Expand aliases in tree, recursively.
1868 1872
1869 1873 'aliases' is a dictionary mapping user defined aliases to
1870 1874 revsetalias objects.
1871 1875 """
1872 1876 if not isinstance(tree, tuple):
1873 1877 # Do not expand raw strings
1874 1878 return tree
1875 1879 alias = _getalias(aliases, tree)
1876 1880 if alias is not None:
1877 1881 if alias in expanding:
1878 1882 raise error.ParseError(_('infinite expansion of revset alias "%s" '
1879 1883 'detected') % alias.name)
1880 1884 expanding.append(alias)
1881 1885 if alias.name not in cache:
1882 1886 cache[alias.name] = _expandaliases(aliases, alias.replacement,
1883 1887 expanding, cache)
1884 1888 result = cache[alias.name]
1885 1889 expanding.pop()
1886 1890 if alias.args is not None:
1887 1891 l = getlist(tree[2])
1888 1892 if len(l) != len(alias.args):
1889 1893 raise error.ParseError(
1890 1894 _('invalid number of arguments: %s') % len(l))
1891 1895 l = [_expandaliases(aliases, a, [], cache) for a in l]
1892 1896 result = _expandargs(result, dict(zip(alias.args, l)))
1893 1897 else:
1894 1898 result = tuple(_expandaliases(aliases, t, expanding, cache)
1895 1899 for t in tree)
1896 1900 return result
1897 1901
1898 1902 def findaliases(ui, tree):
1899 1903 _checkaliasarg(tree)
1900 1904 aliases = {}
1901 1905 for k, v in ui.configitems('revsetalias'):
1902 1906 alias = revsetalias(k, v)
1903 1907 aliases[alias.name] = alias
1904 1908 return _expandaliases(aliases, tree, [], {})
1905 1909
1906 1910 def parse(spec):
1907 1911 p = parser.parser(tokenize, elements)
1908 1912 return p.parse(spec)
1909 1913
1910 1914 def match(ui, spec):
1911 1915 if not spec:
1912 1916 raise error.ParseError(_("empty query"))
1913 1917 tree, pos = parse(spec)
1914 1918 if (pos != len(spec)):
1915 1919 raise error.ParseError(_("invalid token"), pos)
1916 1920 if ui:
1917 1921 tree = findaliases(ui, tree)
1918 1922 weight, tree = optimize(tree, True)
1919 1923 def mfunc(repo, subset):
1920 1924 return getset(repo, subset, tree)
1921 1925 return mfunc
1922 1926
1923 1927 def formatspec(expr, *args):
1924 1928 '''
1925 1929 This is a convenience function for using revsets internally, and
1926 1930 escapes arguments appropriately. Aliases are intentionally ignored
1927 1931 so that intended expression behavior isn't accidentally subverted.
1928 1932
1929 1933 Supported arguments:
1930 1934
1931 1935 %r = revset expression, parenthesized
1932 1936 %d = int(arg), no quoting
1933 1937 %s = string(arg), escaped and single-quoted
1934 1938 %b = arg.branch(), escaped and single-quoted
1935 1939 %n = hex(arg), single-quoted
1936 1940 %% = a literal '%'
1937 1941
1938 1942 Prefixing the type with 'l' specifies a parenthesized list of that type.
1939 1943
1940 1944 >>> formatspec('%r:: and %lr', '10 or 11', ("this()", "that()"))
1941 1945 '(10 or 11):: and ((this()) or (that()))'
1942 1946 >>> formatspec('%d:: and not %d::', 10, 20)
1943 1947 '10:: and not 20::'
1944 1948 >>> formatspec('%ld or %ld', [], [1])
1945 1949 "_list('') or 1"
1946 1950 >>> formatspec('keyword(%s)', 'foo\\xe9')
1947 1951 "keyword('foo\\\\xe9')"
1948 1952 >>> b = lambda: 'default'
1949 1953 >>> b.branch = b
1950 1954 >>> formatspec('branch(%b)', b)
1951 1955 "branch('default')"
1952 1956 >>> formatspec('root(%ls)', ['a', 'b', 'c', 'd'])
1953 1957 "root(_list('a\\x00b\\x00c\\x00d'))"
1954 1958 '''
1955 1959
1956 1960 def quote(s):
1957 1961 return repr(str(s))
1958 1962
1959 1963 def argtype(c, arg):
1960 1964 if c == 'd':
1961 1965 return str(int(arg))
1962 1966 elif c == 's':
1963 1967 return quote(arg)
1964 1968 elif c == 'r':
1965 1969 parse(arg) # make sure syntax errors are confined
1966 1970 return '(%s)' % arg
1967 1971 elif c == 'n':
1968 1972 return quote(node.hex(arg))
1969 1973 elif c == 'b':
1970 1974 return quote(arg.branch())
1971 1975
1972 1976 def listexp(s, t):
1973 1977 l = len(s)
1974 1978 if l == 0:
1975 1979 return "_list('')"
1976 1980 elif l == 1:
1977 1981 return argtype(t, s[0])
1978 1982 elif t == 'd':
1979 1983 return "_list('%s')" % "\0".join(str(int(a)) for a in s)
1980 1984 elif t == 's':
1981 1985 return "_list('%s')" % "\0".join(s)
1982 1986 elif t == 'n':
1983 1987 return "_list('%s')" % "\0".join(node.hex(a) for a in s)
1984 1988 elif t == 'b':
1985 1989 return "_list('%s')" % "\0".join(a.branch() for a in s)
1986 1990
1987 1991 m = l // 2
1988 1992 return '(%s or %s)' % (listexp(s[:m], t), listexp(s[m:], t))
1989 1993
1990 1994 ret = ''
1991 1995 pos = 0
1992 1996 arg = 0
1993 1997 while pos < len(expr):
1994 1998 c = expr[pos]
1995 1999 if c == '%':
1996 2000 pos += 1
1997 2001 d = expr[pos]
1998 2002 if d == '%':
1999 2003 ret += d
2000 2004 elif d in 'dsnbr':
2001 2005 ret += argtype(d, args[arg])
2002 2006 arg += 1
2003 2007 elif d == 'l':
2004 2008 # a list of some type
2005 2009 pos += 1
2006 2010 d = expr[pos]
2007 2011 ret += listexp(list(args[arg]), d)
2008 2012 arg += 1
2009 2013 else:
2010 2014 raise util.Abort('unexpected revspec format character %s' % d)
2011 2015 else:
2012 2016 ret += c
2013 2017 pos += 1
2014 2018
2015 2019 return ret
2016 2020
2017 2021 def prettyformat(tree):
2018 2022 def _prettyformat(tree, level, lines):
2019 2023 if not isinstance(tree, tuple) or tree[0] in ('string', 'symbol'):
2020 2024 lines.append((level, str(tree)))
2021 2025 else:
2022 2026 lines.append((level, '(%s' % tree[0]))
2023 2027 for s in tree[1:]:
2024 2028 _prettyformat(s, level + 1, lines)
2025 2029 lines[-1:] = [(lines[-1][0], lines[-1][1] + ')')]
2026 2030
2027 2031 lines = []
2028 2032 _prettyformat(tree, 0, lines)
2029 2033 output = '\n'.join((' '*l + s) for l, s in lines)
2030 2034 return output
2031 2035
2032 2036 def depth(tree):
2033 2037 if isinstance(tree, tuple):
2034 2038 return max(map(depth, tree)) + 1
2035 2039 else:
2036 2040 return 0
2037 2041
2038 2042 def funcsused(tree):
2039 2043 if not isinstance(tree, tuple) or tree[0] in ('string', 'symbol'):
2040 2044 return set()
2041 2045 else:
2042 2046 funcs = set()
2043 2047 for s in tree[1:]:
2044 2048 funcs |= funcsused(s)
2045 2049 if tree[0] == 'func':
2046 2050 funcs.add(tree[1][1])
2047 2051 return funcs
2048 2052
2049 2053 # tell hggettext to extract docstrings from these functions:
2050 2054 i18nfunctions = symbols.values()
General Comments 0
You need to be logged in to leave comments. Login now