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