Mercurial > public > mercurial-scm > hg-stable
annotate mercurial/revset.py @ 11278:7df88cdf47fd
revset: add support for prefix and suffix versions of : and ::
author | Matt Mackall <mpm@selenic.com> |
---|---|
date | Wed, 02 Jun 2010 14:07:46 -0500 |
parents | c9ce8ecd6ca1 |
children | 62ccf4cd6e7f |
rev | line source |
---|---|
11275 | 1 # revset.py - revision set queries for mercurial |
2 # | |
3 # Copyright 2010 Matt Mackall <mpm@selenic.com> | |
4 # | |
5 # This software may be used and distributed according to the terms of the | |
6 # GNU General Public License version 2 or any later version. | |
7 | |
8 import re | |
9 import parser, util, hg | |
10 import match as _match | |
11 | |
12 elements = { | |
13 "(": (20, ("group", 1, ")"), ("func", 1, ")")), | |
14 "-": (19, ("negate", 19), ("minus", 19)), | |
11278
7df88cdf47fd
revset: add support for prefix and suffix versions of : and ::
Matt Mackall <mpm@selenic.com>
parents:
11275
diff
changeset
|
15 "::": (17, ("dagrangepre", 17), ("dagrange", 17), |
7df88cdf47fd
revset: add support for prefix and suffix versions of : and ::
Matt Mackall <mpm@selenic.com>
parents:
11275
diff
changeset
|
16 ("dagrangepost", 17)), |
7df88cdf47fd
revset: add support for prefix and suffix versions of : and ::
Matt Mackall <mpm@selenic.com>
parents:
11275
diff
changeset
|
17 "..": (17, ("dagrangepre", 17), ("dagrange", 17), |
7df88cdf47fd
revset: add support for prefix and suffix versions of : and ::
Matt Mackall <mpm@selenic.com>
parents:
11275
diff
changeset
|
18 ("dagrangepost", 17)), |
7df88cdf47fd
revset: add support for prefix and suffix versions of : and ::
Matt Mackall <mpm@selenic.com>
parents:
11275
diff
changeset
|
19 ":": (15, ("rangepre", 15), ("range", 15), ("rangepost", 15)), |
11275 | 20 "not": (10, ("not", 10)), |
21 "!": (10, ("not", 10)), | |
22 "and": (5, None, ("and", 5)), | |
23 "&": (5, None, ("and", 5)), | |
24 "or": (4, None, ("or", 4)), | |
25 "|": (4, None, ("or", 4)), | |
26 "+": (4, None, ("or", 4)), | |
27 ",": (2, None, ("list", 2)), | |
28 ")": (0, None, None), | |
29 "symbol": (0, ("symbol",), None), | |
30 "string": (0, ("string",), None), | |
31 "end": (0, None, None), | |
32 } | |
33 | |
34 keywords = set(['and', 'or', 'not']) | |
35 | |
36 def tokenize(program): | |
37 pos, l = 0, len(program) | |
38 while pos < l: | |
39 c = program[pos] | |
40 if c.isspace(): # skip inter-token whitespace | |
41 pass | |
11278
7df88cdf47fd
revset: add support for prefix and suffix versions of : and ::
Matt Mackall <mpm@selenic.com>
parents:
11275
diff
changeset
|
42 elif c == ':' and program[pos:pos + 2] == '::': # look ahead carefully |
7df88cdf47fd
revset: add support for prefix and suffix versions of : and ::
Matt Mackall <mpm@selenic.com>
parents:
11275
diff
changeset
|
43 yield ('::', None) |
7df88cdf47fd
revset: add support for prefix and suffix versions of : and ::
Matt Mackall <mpm@selenic.com>
parents:
11275
diff
changeset
|
44 pos += 1 # skip ahead |
11275 | 45 elif c == '.' and program[pos:pos + 2] == '..': # look ahead carefully |
46 yield ('..', None) | |
47 pos += 1 # skip ahead | |
11278
7df88cdf47fd
revset: add support for prefix and suffix versions of : and ::
Matt Mackall <mpm@selenic.com>
parents:
11275
diff
changeset
|
48 elif c in "():,-|&+!": # handle simple operators |
7df88cdf47fd
revset: add support for prefix and suffix versions of : and ::
Matt Mackall <mpm@selenic.com>
parents:
11275
diff
changeset
|
49 yield (c, None) |
11275 | 50 elif c in '"\'': # handle quoted strings |
51 pos += 1 | |
52 s = pos | |
53 while pos < l: # find closing quote | |
54 d = program[pos] | |
55 if d == '\\': # skip over escaped characters | |
56 pos += 2 | |
57 continue | |
58 if d == c: | |
59 yield ('string', program[s:pos].decode('string-escape')) | |
60 break | |
61 pos += 1 | |
62 else: | |
63 raise "unterminated string" | |
64 elif c.isalnum() or c in '.': # gather up a symbol/keyword | |
65 s = pos | |
66 pos += 1 | |
67 while pos < l: # find end of symbol | |
68 d = program[pos] | |
69 if not (d.isalnum() or d in "._"): | |
70 break | |
71 if d == '.' and program[pos - 1] == '.': # special case for .. | |
72 pos -= 1 | |
73 break | |
74 pos += 1 | |
75 sym = program[s:pos] | |
76 if sym in keywords: # operator keywords | |
77 yield (sym, None) | |
78 else: | |
79 yield ('symbol', sym) | |
80 pos -= 1 | |
81 else: | |
82 raise "syntax error at %d" % pos | |
83 pos += 1 | |
84 yield ('end', None) | |
85 | |
86 # helpers | |
87 | |
88 def getstring(x, err): | |
89 if x[0] == 'string' or x[0] == 'symbol': | |
90 return x[1] | |
91 raise err | |
92 | |
93 def getlist(x): | |
94 if not x: | |
95 return [] | |
96 if x[0] == 'list': | |
97 return getlist(x[1]) + [x[2]] | |
98 return [x] | |
99 | |
100 def getpair(x, err): | |
101 l = getlist(x) | |
102 if len(l) != 2: | |
103 raise err | |
104 return l | |
105 | |
106 def getset(repo, subset, x): | |
107 if not x: | |
108 raise "missing argument" | |
109 return methods[x[0]](repo, subset, *x[1:]) | |
110 | |
111 # operator methods | |
112 | |
113 def negate(repo, subset, x): | |
114 return getset(repo, subset, | |
115 ('string', '-' + getstring(x, "can't negate that"))) | |
116 | |
117 def stringset(repo, subset, x): | |
118 x = repo[x].rev() | |
119 if x in subset: | |
120 return [x] | |
121 return [] | |
122 | |
123 def symbolset(repo, subset, x): | |
124 if x in symbols: | |
125 raise "can't use %s here" % x | |
126 return stringset(repo, subset, x) | |
127 | |
128 def rangeset(repo, subset, x, y): | |
129 m = getset(repo, subset, x)[0] | |
130 n = getset(repo, subset, y)[-1] | |
131 if m < n: | |
132 return range(m, n + 1) | |
133 return range(m, n - 1, -1) | |
134 | |
11278
7df88cdf47fd
revset: add support for prefix and suffix versions of : and ::
Matt Mackall <mpm@selenic.com>
parents:
11275
diff
changeset
|
135 def rangepreset(repo, subset, x): |
7df88cdf47fd
revset: add support for prefix and suffix versions of : and ::
Matt Mackall <mpm@selenic.com>
parents:
11275
diff
changeset
|
136 return range(0, getset(repo, subset, x)[-1] + 1) |
7df88cdf47fd
revset: add support for prefix and suffix versions of : and ::
Matt Mackall <mpm@selenic.com>
parents:
11275
diff
changeset
|
137 |
7df88cdf47fd
revset: add support for prefix and suffix versions of : and ::
Matt Mackall <mpm@selenic.com>
parents:
11275
diff
changeset
|
138 def rangepostset(repo, subset, x): |
7df88cdf47fd
revset: add support for prefix and suffix versions of : and ::
Matt Mackall <mpm@selenic.com>
parents:
11275
diff
changeset
|
139 return range(getset(repo, subset, x)[0], len(repo)) |
7df88cdf47fd
revset: add support for prefix and suffix versions of : and ::
Matt Mackall <mpm@selenic.com>
parents:
11275
diff
changeset
|
140 |
11275 | 141 def dagrangeset(repo, subset, x, y): |
142 return andset(repo, subset, | |
143 ('func', ('symbol', 'descendants'), x), | |
144 ('func', ('symbol', 'ancestors'), y)) | |
145 | |
146 def andset(repo, subset, x, y): | |
147 if weight(x, True) > weight(y, True): | |
148 x, y = y, x | |
149 return getset(repo, getset(repo, subset, x), y) | |
150 | |
151 def orset(repo, subset, x, y): | |
152 if weight(y, False) < weight(x, False): | |
153 x, y = y, x | |
154 s = set(getset(repo, subset, x)) | |
155 s |= set(getset(repo, [r for r in subset if r not in s], y)) | |
156 return [r for r in subset if r in s] | |
157 | |
158 def notset(repo, subset, x): | |
159 s = set(getset(repo, subset, x)) | |
160 return [r for r in subset if r not in s] | |
161 | |
162 def minusset(repo, subset, x, y): | |
163 if weight(x, True) > weight(y, True): | |
164 return getset(repo, notset(repo, subset, y), x) | |
165 return notset(repo, getset(repo, subset, x), y) | |
166 | |
167 def listset(repo, subset, a, b): | |
168 raise "can't use a list in this context" | |
169 | |
170 def func(repo, subset, a, b): | |
171 if a[0] == 'symbol' and a[1] in symbols: | |
172 return symbols[a[1]](repo, subset, b) | |
173 raise "that's not a function: %s" % a[1] | |
174 | |
175 # functions | |
176 | |
177 def p1(repo, subset, x): | |
178 ps = set() | |
179 cl = repo.changelog | |
180 for r in getset(repo, subset, x): | |
181 ps.add(cl.parentrevs(r)[0]) | |
182 return [r for r in subset if r in ps] | |
183 | |
184 def p2(repo, subset, x): | |
185 ps = set() | |
186 cl = repo.changelog | |
187 for r in getset(repo, subset, x): | |
188 ps.add(cl.parentrevs(r)[1]) | |
189 return [r for r in subset if r in ps] | |
190 | |
191 def parents(repo, subset, x): | |
192 ps = set() | |
193 cl = repo.changelog | |
194 for r in getset(repo, subset, x): | |
195 ps.update(cl.parentrevs(r)) | |
196 return [r for r in subset if r in ps] | |
197 | |
198 def maxrev(repo, subset, x): | |
199 s = getset(repo, subset, x) | |
200 if s: | |
201 m = max(s) | |
202 if m in subset: | |
203 return [m] | |
204 return [] | |
205 | |
206 def limit(repo, subset, x): | |
207 l = getpair(x, "limit wants two args") | |
208 try: | |
209 lim = int(getstring(l[1], "limit wants a number")) | |
210 except ValueError: | |
211 raise "wants a number" | |
212 return getset(repo, subset, l[0])[:lim] | |
213 | |
214 def children(repo, subset, x): | |
215 cs = set() | |
216 cl = repo.changelog | |
217 s = set(getset(repo, subset, x)) | |
218 for r in xrange(0, len(repo)): | |
219 for p in cl.parentrevs(r): | |
220 if p in s: | |
221 cs.add(r) | |
222 return [r for r in subset if r in cs] | |
223 | |
224 def branch(repo, subset, x): | |
225 s = getset(repo, range(len(repo)), x) | |
226 b = set() | |
227 for r in s: | |
228 b.add(repo[r].branch()) | |
229 s = set(s) | |
230 return [r for r in subset if r in s or repo[r].branch() in b] | |
231 | |
232 def ancestor(repo, subset, x): | |
233 l = getpair(x, "ancestor wants two args") | |
234 a = getset(repo, subset, l[0]) | |
235 b = getset(repo, subset, l[1]) | |
236 if len(a) > 1 or len(b) > 1: | |
237 raise "arguments to ancestor must be single revisions" | |
238 return [repo[a[0]].ancestor(repo[b[0]]).rev()] | |
239 | |
240 def ancestors(repo, subset, x): | |
241 args = getset(repo, range(len(repo)), x) | |
242 s = set(repo.changelog.ancestors(*args)) | set(args) | |
243 return [r for r in subset if r in s] | |
244 | |
245 def descendants(repo, subset, x): | |
246 args = getset(repo, range(len(repo)), x) | |
247 s = set(repo.changelog.descendants(*args)) | set(args) | |
248 return [r for r in subset if r in s] | |
249 | |
250 def follow(repo, subset, x): | |
251 if x: | |
252 raise "follow takes no args" | |
253 p = repo['.'].rev() | |
254 s = set(repo.changelog.ancestors(p)) | set([p]) | |
255 return [r for r in subset if r in s] | |
256 | |
257 def date(repo, subset, x): | |
258 ds = getstring(x, 'date wants a string') | |
259 dm = util.matchdate(ds) | |
260 return [r for r in subset if dm(repo[r].date()[0])] | |
261 | |
262 def keyword(repo, subset, x): | |
263 kw = getstring(x, "keyword wants a string").lower() | |
264 l = [] | |
265 for r in subset: | |
266 c = repo[r] | |
267 t = " ".join(c.files() + [c.user(), c.description()]) | |
268 if kw in t.lower(): | |
269 l.append(r) | |
270 return l | |
271 | |
272 def grep(repo, subset, x): | |
273 gr = re.compile(getstring(x, "grep wants a string")) | |
274 l = [] | |
275 for r in subset: | |
276 c = repo[r] | |
277 for e in c.files() + [c.user(), c.description()]: | |
278 if gr.search(e): | |
279 l.append(r) | |
280 continue | |
281 return l | |
282 | |
283 def author(repo, subset, x): | |
284 n = getstring(x, "author wants a string").lower() | |
285 return [r for r in subset if n in repo[r].user().lower()] | |
286 | |
287 def hasfile(repo, subset, x): | |
288 pat = getstring(x, "file wants a pattern") | |
289 m = _match.match(repo.root, repo.getcwd(), [pat]) | |
290 s = [] | |
291 for r in subset: | |
292 for f in repo[r].files(): | |
293 if m(f): | |
294 s.append(r) | |
295 continue | |
296 return s | |
297 | |
298 def contains(repo, subset, x): | |
299 pat = getstring(x, "file wants a pattern") | |
300 m = _match.match(repo.root, repo.getcwd(), [pat]) | |
301 s = [] | |
302 if m.files() == [pat]: | |
303 for r in subset: | |
304 if pat in repo[r]: | |
305 s.append(r) | |
306 continue | |
307 else: | |
308 for r in subset: | |
309 c = repo[r] | |
310 for f in repo[r].manifest(): | |
311 if m(f): | |
312 s.append(r) | |
313 continue | |
314 return s | |
315 | |
316 def checkstatus(repo, subset, pat, field): | |
317 m = _match.match(repo.root, repo.getcwd(), [pat]) | |
318 s = [] | |
319 fast = (m.files() == [pat]) | |
320 for r in subset: | |
321 c = repo[r] | |
322 if fast: | |
323 if pat not in c.files(): | |
324 continue | |
325 else: | |
326 for f in c.files(): | |
327 if m(f): | |
328 break | |
329 else: | |
330 continue | |
331 files = repo.status(c.p1().node(), c.node())[field] | |
332 if fast: | |
333 if pat in files: | |
334 s.append(r) | |
335 continue | |
336 else: | |
337 for f in files: | |
338 if m(f): | |
339 s.append(r) | |
340 continue | |
341 return s | |
342 | |
343 def modifies(repo, subset, x): | |
344 pat = getstring(x, "modifies wants a pattern") | |
345 return checkstatus(repo, subset, pat, 0) | |
346 | |
347 def adds(repo, subset, x): | |
348 pat = getstring(x, "adds wants a pattern") | |
349 return checkstatus(repo, subset, pat, 1) | |
350 | |
351 def removes(repo, subset, x): | |
352 pat = getstring(x, "removes wants a pattern") | |
353 return checkstatus(repo, subset, pat, 2) | |
354 | |
355 def merge(repo, subset, x): | |
356 if x: | |
357 raise "merge takes no args" | |
358 cl = repo.changelog | |
359 return [r for r in subset if cl.parentrevs(r)[1] != -1] | |
360 | |
361 def closed(repo, subset, x): | |
362 return [r for r in subset if repo[r].extra('close')] | |
363 | |
364 def head(repo, subset, x): | |
365 hs = set() | |
366 for b, ls in repo.branchmap().iteritems(): | |
367 hs.update(repo[h].rev() for h in ls) | |
368 return [r for r in subset if r in hs] | |
369 | |
370 def reverse(repo, subset, x): | |
371 l = getset(repo, subset, x) | |
372 l.reverse() | |
373 return l | |
374 | |
375 def sort(repo, subset, x): | |
376 l = getlist(x) | |
377 keys = "rev" | |
378 if len(l) == 2: | |
379 keys = getstring(l[1], "sort spec must be a string") | |
380 | |
381 s = l[0] | |
382 keys = keys.split() | |
383 l = [] | |
384 def invert(s): | |
385 return "".join(chr(255 - ord(c)) for c in s) | |
386 for r in getset(repo, subset, s): | |
387 c = repo[r] | |
388 e = [] | |
389 for k in keys: | |
390 if k == 'rev': | |
391 e.append(r) | |
392 elif k == '-rev': | |
393 e.append(-r) | |
394 elif k == 'branch': | |
395 e.append(c.branch()) | |
396 elif k == '-branch': | |
397 e.append(invert(c.branch())) | |
398 elif k == 'desc': | |
399 e.append(c.description()) | |
400 elif k == '-desc': | |
401 e.append(invert(c.description())) | |
402 elif k in 'user author': | |
403 e.append(c.user()) | |
404 elif k in '-user -author': | |
405 e.append(invert(c.user())) | |
406 elif k == 'date': | |
407 e.append(c.date()[0]) | |
408 elif k == '-date': | |
409 e.append(-c.date()[0]) | |
410 else: | |
411 raise "unknown sort key %r" % k | |
412 e.append(r) | |
413 l.append(e) | |
414 l.sort() | |
415 return [e[-1] for e in l] | |
416 | |
417 def getall(repo, subset, x): | |
418 return subset | |
419 | |
420 def heads(repo, subset, x): | |
421 s = getset(repo, subset, x) | |
422 ps = set(parents(repo, subset, x)) | |
423 return [r for r in s if r not in ps] | |
424 | |
425 def roots(repo, subset, x): | |
426 s = getset(repo, subset, x) | |
427 cs = set(children(repo, subset, x)) | |
428 return [r for r in s if r not in cs] | |
429 | |
430 def outgoing(repo, subset, x): | |
431 l = getlist(x) | |
432 if len(l) == 1: | |
433 dest = getstring(l[0], "outgoing wants a repo path") | |
434 else: | |
435 dest = '' | |
436 dest = repo.ui.expandpath(dest or 'default-push', dest or 'default') | |
437 dest, branches = hg.parseurl(dest) | |
438 other = hg.repository(hg.remoteui(repo, {}), dest) | |
439 repo.ui.pushbuffer() | |
440 o = repo.findoutgoing(other) | |
441 repo.ui.popbuffer() | |
442 cl = repo.changelog | |
443 o = set([cl.rev(r) for r in repo.changelog.nodesbetween(o, None)[0]]) | |
444 print 'out', dest, o | |
445 return [r for r in subset if r in o] | |
446 | |
447 symbols = { | |
448 "ancestor": ancestor, | |
449 "ancestors": ancestors, | |
450 "descendants": descendants, | |
451 "follow": follow, | |
452 "merge": merge, | |
453 "reverse": reverse, | |
454 "sort": sort, | |
455 "branch": branch, | |
456 "keyword": keyword, | |
457 "author": author, | |
458 "user": author, | |
459 "date": date, | |
460 "grep": grep, | |
461 "p1": p1, | |
462 "p2": p2, | |
463 "parents": parents, | |
464 "children": children, | |
465 "max": maxrev, | |
466 "limit": limit, | |
467 "file": hasfile, | |
468 "contains": contains, | |
469 "heads": heads, | |
470 "roots": roots, | |
471 "all": getall, | |
472 "closed": closed, | |
473 "head": head, | |
474 "modifies": modifies, | |
475 "adds": adds, | |
476 "removes": removes, | |
477 "outgoing": outgoing, | |
478 } | |
479 | |
480 methods = { | |
481 "negate": negate, | |
482 "minus": minusset, | |
483 "range": rangeset, | |
11278
7df88cdf47fd
revset: add support for prefix and suffix versions of : and ::
Matt Mackall <mpm@selenic.com>
parents:
11275
diff
changeset
|
484 "rangepre": rangepreset, |
7df88cdf47fd
revset: add support for prefix and suffix versions of : and ::
Matt Mackall <mpm@selenic.com>
parents:
11275
diff
changeset
|
485 "rangepost": rangepostset, |
11275 | 486 "dagrange": dagrangeset, |
11278
7df88cdf47fd
revset: add support for prefix and suffix versions of : and ::
Matt Mackall <mpm@selenic.com>
parents:
11275
diff
changeset
|
487 "dagrangepre": ancestors, |
7df88cdf47fd
revset: add support for prefix and suffix versions of : and ::
Matt Mackall <mpm@selenic.com>
parents:
11275
diff
changeset
|
488 "dagrangepost": descendants, |
11275 | 489 "string": stringset, |
490 "symbol": symbolset, | |
491 "and": andset, | |
492 "or": orset, | |
493 "not": notset, | |
494 "list": listset, | |
495 "func": func, | |
496 "group": lambda r, s, x: getset(r, s, x), | |
497 } | |
498 | |
499 def weight(x, small): | |
500 smallbonus = 1 | |
501 if small: | |
502 smallbonus = .5 | |
503 | |
504 op = x[0] | |
505 if op in 'string symbol negate': | |
506 return smallbonus # single revisions are small | |
507 elif op == 'and' or op == 'dagrange': | |
508 return min(weight(x[1], True), weight(x[2], True)) | |
509 elif op in 'or -': | |
510 return max(weight(x[1], False), weight(x[2], False)) | |
511 elif op == 'not': | |
512 return weight(x[1], not small) | |
513 elif op == 'group': | |
514 return weight(x[1], small) | |
515 elif op == 'range': | |
516 return weight(x[1], small) + weight(x[2], small) | |
517 elif op == 'func': | |
518 f = getstring(x[1], "not a symbol") | |
519 if f in "grep date user author keyword branch file": | |
520 return 10 # slow | |
521 elif f in "modifies adds removes": | |
522 return 30 # slower | |
523 elif f == "contains": | |
524 return 100 # very slow | |
525 elif f == "ancestor": | |
526 return (weight(x[1][1], small) + | |
527 weight(x[1][2], small)) * smallbonus | |
528 elif f == "reverse limit": | |
529 return weight(x[1], small) | |
530 elif f in "sort": | |
531 base = x[1] | |
532 spec = "rev" | |
533 if x[1][0] == 'list': | |
534 base = x[1][1] | |
535 spec = x[1][2] | |
536 return max(weight(base, small), 10) | |
537 else: | |
538 return 1 | |
539 | |
540 parse = parser.parser(tokenize, elements).parse | |
541 | |
542 def match(spec): | |
543 tree = parse(spec) | |
544 def mfunc(repo, subset): | |
545 return getset(repo, subset, tree) | |
546 return mfunc |