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