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