##// END OF EJS Templates
revset: optimize the parse tree directly...
Matt Mackall -
r11279:62ccf4cd default
parent child Browse files
Show More
@@ -1,546 +1,548
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 rangepreset(repo, subset, x):
136 return range(0, getset(repo, subset, x)[-1] + 1)
137
138 def rangepostset(repo, subset, x):
139 return range(getset(repo, subset, x)[0], len(repo))
140
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):
135 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)
136 return getset(repo, getset(repo, subset, x), y)
150
137
151 def orset(repo, subset, x, y):
138 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))
139 s = set(getset(repo, subset, x))
155 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))
156 return [r for r in subset if r in s]
141 return [r for r in subset if r in s]
157
142
158 def notset(repo, subset, x):
143 def notset(repo, subset, x):
159 s = set(getset(repo, subset, x))
144 s = set(getset(repo, subset, x))
160 return [r for r in subset if r not in s]
145 return [r for r in subset if r not in s]
161
146
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):
147 def listset(repo, subset, a, b):
168 raise "can't use a list in this context"
148 raise "can't use a list in this context"
169
149
170 def func(repo, subset, a, b):
150 def func(repo, subset, a, b):
171 if a[0] == 'symbol' and a[1] in symbols:
151 if a[0] == 'symbol' and a[1] in symbols:
172 return symbols[a[1]](repo, subset, b)
152 return symbols[a[1]](repo, subset, b)
173 raise "that's not a function: %s" % a[1]
153 raise "that's not a function: %s" % a[1]
174
154
175 # functions
155 # functions
176
156
177 def p1(repo, subset, x):
157 def p1(repo, subset, x):
178 ps = set()
158 ps = set()
179 cl = repo.changelog
159 cl = repo.changelog
180 for r in getset(repo, subset, x):
160 for r in getset(repo, subset, x):
181 ps.add(cl.parentrevs(r)[0])
161 ps.add(cl.parentrevs(r)[0])
182 return [r for r in subset if r in ps]
162 return [r for r in subset if r in ps]
183
163
184 def p2(repo, subset, x):
164 def p2(repo, subset, x):
185 ps = set()
165 ps = set()
186 cl = repo.changelog
166 cl = repo.changelog
187 for r in getset(repo, subset, x):
167 for r in getset(repo, subset, x):
188 ps.add(cl.parentrevs(r)[1])
168 ps.add(cl.parentrevs(r)[1])
189 return [r for r in subset if r in ps]
169 return [r for r in subset if r in ps]
190
170
191 def parents(repo, subset, x):
171 def parents(repo, subset, x):
192 ps = set()
172 ps = set()
193 cl = repo.changelog
173 cl = repo.changelog
194 for r in getset(repo, subset, x):
174 for r in getset(repo, subset, x):
195 ps.update(cl.parentrevs(r))
175 ps.update(cl.parentrevs(r))
196 return [r for r in subset if r in ps]
176 return [r for r in subset if r in ps]
197
177
198 def maxrev(repo, subset, x):
178 def maxrev(repo, subset, x):
199 s = getset(repo, subset, x)
179 s = getset(repo, subset, x)
200 if s:
180 if s:
201 m = max(s)
181 m = max(s)
202 if m in subset:
182 if m in subset:
203 return [m]
183 return [m]
204 return []
184 return []
205
185
206 def limit(repo, subset, x):
186 def limit(repo, subset, x):
207 l = getpair(x, "limit wants two args")
187 l = getpair(x, "limit wants two args")
208 try:
188 try:
209 lim = int(getstring(l[1], "limit wants a number"))
189 lim = int(getstring(l[1], "limit wants a number"))
210 except ValueError:
190 except ValueError:
211 raise "wants a number"
191 raise "wants a number"
212 return getset(repo, subset, l[0])[:lim]
192 return getset(repo, subset, l[0])[:lim]
213
193
214 def children(repo, subset, x):
194 def children(repo, subset, x):
215 cs = set()
195 cs = set()
216 cl = repo.changelog
196 cl = repo.changelog
217 s = set(getset(repo, subset, x))
197 s = set(getset(repo, subset, x))
218 for r in xrange(0, len(repo)):
198 for r in xrange(0, len(repo)):
219 for p in cl.parentrevs(r):
199 for p in cl.parentrevs(r):
220 if p in s:
200 if p in s:
221 cs.add(r)
201 cs.add(r)
222 return [r for r in subset if r in cs]
202 return [r for r in subset if r in cs]
223
203
224 def branch(repo, subset, x):
204 def branch(repo, subset, x):
225 s = getset(repo, range(len(repo)), x)
205 s = getset(repo, range(len(repo)), x)
226 b = set()
206 b = set()
227 for r in s:
207 for r in s:
228 b.add(repo[r].branch())
208 b.add(repo[r].branch())
229 s = set(s)
209 s = set(s)
230 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]
231
211
232 def ancestor(repo, subset, x):
212 def ancestor(repo, subset, x):
233 l = getpair(x, "ancestor wants two args")
213 l = getpair(x, "ancestor wants two args")
234 a = getset(repo, subset, l[0])
214 a = getset(repo, subset, l[0])
235 b = getset(repo, subset, l[1])
215 b = getset(repo, subset, l[1])
236 if len(a) > 1 or len(b) > 1:
216 if len(a) > 1 or len(b) > 1:
237 raise "arguments to ancestor must be single revisions"
217 raise "arguments to ancestor must be single revisions"
238 return [repo[a[0]].ancestor(repo[b[0]]).rev()]
218 return [repo[a[0]].ancestor(repo[b[0]]).rev()]
239
219
240 def ancestors(repo, subset, x):
220 def ancestors(repo, subset, x):
241 args = getset(repo, range(len(repo)), x)
221 args = getset(repo, range(len(repo)), x)
242 s = set(repo.changelog.ancestors(*args)) | set(args)
222 s = set(repo.changelog.ancestors(*args)) | set(args)
243 return [r for r in subset if r in s]
223 return [r for r in subset if r in s]
244
224
245 def descendants(repo, subset, x):
225 def descendants(repo, subset, x):
246 args = getset(repo, range(len(repo)), x)
226 args = getset(repo, range(len(repo)), x)
247 s = set(repo.changelog.descendants(*args)) | set(args)
227 s = set(repo.changelog.descendants(*args)) | set(args)
248 return [r for r in subset if r in s]
228 return [r for r in subset if r in s]
249
229
250 def follow(repo, subset, x):
230 def follow(repo, subset, x):
251 if x:
231 if x:
252 raise "follow takes no args"
232 raise "follow takes no args"
253 p = repo['.'].rev()
233 p = repo['.'].rev()
254 s = set(repo.changelog.ancestors(p)) | set([p])
234 s = set(repo.changelog.ancestors(p)) | set([p])
255 return [r for r in subset if r in s]
235 return [r for r in subset if r in s]
256
236
257 def date(repo, subset, x):
237 def date(repo, subset, x):
258 ds = getstring(x, 'date wants a string')
238 ds = getstring(x, 'date wants a string')
259 dm = util.matchdate(ds)
239 dm = util.matchdate(ds)
260 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])]
261
241
262 def keyword(repo, subset, x):
242 def keyword(repo, subset, x):
263 kw = getstring(x, "keyword wants a string").lower()
243 kw = getstring(x, "keyword wants a string").lower()
264 l = []
244 l = []
265 for r in subset:
245 for r in subset:
266 c = repo[r]
246 c = repo[r]
267 t = " ".join(c.files() + [c.user(), c.description()])
247 t = " ".join(c.files() + [c.user(), c.description()])
268 if kw in t.lower():
248 if kw in t.lower():
269 l.append(r)
249 l.append(r)
270 return l
250 return l
271
251
272 def grep(repo, subset, x):
252 def grep(repo, subset, x):
273 gr = re.compile(getstring(x, "grep wants a string"))
253 gr = re.compile(getstring(x, "grep wants a string"))
274 l = []
254 l = []
275 for r in subset:
255 for r in subset:
276 c = repo[r]
256 c = repo[r]
277 for e in c.files() + [c.user(), c.description()]:
257 for e in c.files() + [c.user(), c.description()]:
278 if gr.search(e):
258 if gr.search(e):
279 l.append(r)
259 l.append(r)
280 continue
260 continue
281 return l
261 return l
282
262
283 def author(repo, subset, x):
263 def author(repo, subset, x):
284 n = getstring(x, "author wants a string").lower()
264 n = getstring(x, "author wants a string").lower()
285 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()]
286
266
287 def hasfile(repo, subset, x):
267 def hasfile(repo, subset, x):
288 pat = getstring(x, "file wants a pattern")
268 pat = getstring(x, "file wants a pattern")
289 m = _match.match(repo.root, repo.getcwd(), [pat])
269 m = _match.match(repo.root, repo.getcwd(), [pat])
290 s = []
270 s = []
291 for r in subset:
271 for r in subset:
292 for f in repo[r].files():
272 for f in repo[r].files():
293 if m(f):
273 if m(f):
294 s.append(r)
274 s.append(r)
295 continue
275 continue
296 return s
276 return s
297
277
298 def contains(repo, subset, x):
278 def contains(repo, subset, x):
299 pat = getstring(x, "file wants a pattern")
279 pat = getstring(x, "file wants a pattern")
300 m = _match.match(repo.root, repo.getcwd(), [pat])
280 m = _match.match(repo.root, repo.getcwd(), [pat])
301 s = []
281 s = []
302 if m.files() == [pat]:
282 if m.files() == [pat]:
303 for r in subset:
283 for r in subset:
304 if pat in repo[r]:
284 if pat in repo[r]:
305 s.append(r)
285 s.append(r)
306 continue
286 continue
307 else:
287 else:
308 for r in subset:
288 for r in subset:
309 c = repo[r]
289 c = repo[r]
310 for f in repo[r].manifest():
290 for f in repo[r].manifest():
311 if m(f):
291 if m(f):
312 s.append(r)
292 s.append(r)
313 continue
293 continue
314 return s
294 return s
315
295
316 def checkstatus(repo, subset, pat, field):
296 def checkstatus(repo, subset, pat, field):
317 m = _match.match(repo.root, repo.getcwd(), [pat])
297 m = _match.match(repo.root, repo.getcwd(), [pat])
318 s = []
298 s = []
319 fast = (m.files() == [pat])
299 fast = (m.files() == [pat])
320 for r in subset:
300 for r in subset:
321 c = repo[r]
301 c = repo[r]
322 if fast:
302 if fast:
323 if pat not in c.files():
303 if pat not in c.files():
324 continue
304 continue
325 else:
305 else:
326 for f in c.files():
306 for f in c.files():
327 if m(f):
307 if m(f):
328 break
308 break
329 else:
309 else:
330 continue
310 continue
331 files = repo.status(c.p1().node(), c.node())[field]
311 files = repo.status(c.p1().node(), c.node())[field]
332 if fast:
312 if fast:
333 if pat in files:
313 if pat in files:
334 s.append(r)
314 s.append(r)
335 continue
315 continue
336 else:
316 else:
337 for f in files:
317 for f in files:
338 if m(f):
318 if m(f):
339 s.append(r)
319 s.append(r)
340 continue
320 continue
341 return s
321 return s
342
322
343 def modifies(repo, subset, x):
323 def modifies(repo, subset, x):
344 pat = getstring(x, "modifies wants a pattern")
324 pat = getstring(x, "modifies wants a pattern")
345 return checkstatus(repo, subset, pat, 0)
325 return checkstatus(repo, subset, pat, 0)
346
326
347 def adds(repo, subset, x):
327 def adds(repo, subset, x):
348 pat = getstring(x, "adds wants a pattern")
328 pat = getstring(x, "adds wants a pattern")
349 return checkstatus(repo, subset, pat, 1)
329 return checkstatus(repo, subset, pat, 1)
350
330
351 def removes(repo, subset, x):
331 def removes(repo, subset, x):
352 pat = getstring(x, "removes wants a pattern")
332 pat = getstring(x, "removes wants a pattern")
353 return checkstatus(repo, subset, pat, 2)
333 return checkstatus(repo, subset, pat, 2)
354
334
355 def merge(repo, subset, x):
335 def merge(repo, subset, x):
356 if x:
336 if x:
357 raise "merge takes no args"
337 raise "merge takes no args"
358 cl = repo.changelog
338 cl = repo.changelog
359 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]
360
340
361 def closed(repo, subset, x):
341 def closed(repo, subset, x):
362 return [r for r in subset if repo[r].extra('close')]
342 return [r for r in subset if repo[r].extra('close')]
363
343
364 def head(repo, subset, x):
344 def head(repo, subset, x):
365 hs = set()
345 hs = set()
366 for b, ls in repo.branchmap().iteritems():
346 for b, ls in repo.branchmap().iteritems():
367 hs.update(repo[h].rev() for h in ls)
347 hs.update(repo[h].rev() for h in ls)
368 return [r for r in subset if r in hs]
348 return [r for r in subset if r in hs]
369
349
370 def reverse(repo, subset, x):
350 def reverse(repo, subset, x):
371 l = getset(repo, subset, x)
351 l = getset(repo, subset, x)
372 l.reverse()
352 l.reverse()
373 return l
353 return l
374
354
375 def sort(repo, subset, x):
355 def sort(repo, subset, x):
376 l = getlist(x)
356 l = getlist(x)
377 keys = "rev"
357 keys = "rev"
378 if len(l) == 2:
358 if len(l) == 2:
379 keys = getstring(l[1], "sort spec must be a string")
359 keys = getstring(l[1], "sort spec must be a string")
380
360
381 s = l[0]
361 s = l[0]
382 keys = keys.split()
362 keys = keys.split()
383 l = []
363 l = []
384 def invert(s):
364 def invert(s):
385 return "".join(chr(255 - ord(c)) for c in s)
365 return "".join(chr(255 - ord(c)) for c in s)
386 for r in getset(repo, subset, s):
366 for r in getset(repo, subset, s):
387 c = repo[r]
367 c = repo[r]
388 e = []
368 e = []
389 for k in keys:
369 for k in keys:
390 if k == 'rev':
370 if k == 'rev':
391 e.append(r)
371 e.append(r)
392 elif k == '-rev':
372 elif k == '-rev':
393 e.append(-r)
373 e.append(-r)
394 elif k == 'branch':
374 elif k == 'branch':
395 e.append(c.branch())
375 e.append(c.branch())
396 elif k == '-branch':
376 elif k == '-branch':
397 e.append(invert(c.branch()))
377 e.append(invert(c.branch()))
398 elif k == 'desc':
378 elif k == 'desc':
399 e.append(c.description())
379 e.append(c.description())
400 elif k == '-desc':
380 elif k == '-desc':
401 e.append(invert(c.description()))
381 e.append(invert(c.description()))
402 elif k in 'user author':
382 elif k in 'user author':
403 e.append(c.user())
383 e.append(c.user())
404 elif k in '-user -author':
384 elif k in '-user -author':
405 e.append(invert(c.user()))
385 e.append(invert(c.user()))
406 elif k == 'date':
386 elif k == 'date':
407 e.append(c.date()[0])
387 e.append(c.date()[0])
408 elif k == '-date':
388 elif k == '-date':
409 e.append(-c.date()[0])
389 e.append(-c.date()[0])
410 else:
390 else:
411 raise "unknown sort key %r" % k
391 raise "unknown sort key %r" % k
412 e.append(r)
392 e.append(r)
413 l.append(e)
393 l.append(e)
414 l.sort()
394 l.sort()
415 return [e[-1] for e in l]
395 return [e[-1] for e in l]
416
396
417 def getall(repo, subset, x):
397 def getall(repo, subset, x):
418 return subset
398 return subset
419
399
420 def heads(repo, subset, x):
400 def heads(repo, subset, x):
421 s = getset(repo, subset, x)
401 s = getset(repo, subset, x)
422 ps = set(parents(repo, subset, x))
402 ps = set(parents(repo, subset, x))
423 return [r for r in s if r not in ps]
403 return [r for r in s if r not in ps]
424
404
425 def roots(repo, subset, x):
405 def roots(repo, subset, x):
426 s = getset(repo, subset, x)
406 s = getset(repo, subset, x)
427 cs = set(children(repo, subset, x))
407 cs = set(children(repo, subset, x))
428 return [r for r in s if r not in cs]
408 return [r for r in s if r not in cs]
429
409
430 def outgoing(repo, subset, x):
410 def outgoing(repo, subset, x):
431 l = getlist(x)
411 l = getlist(x)
432 if len(l) == 1:
412 if len(l) == 1:
433 dest = getstring(l[0], "outgoing wants a repo path")
413 dest = getstring(l[0], "outgoing wants a repo path")
434 else:
414 else:
435 dest = ''
415 dest = ''
436 dest = repo.ui.expandpath(dest or 'default-push', dest or 'default')
416 dest = repo.ui.expandpath(dest or 'default-push', dest or 'default')
437 dest, branches = hg.parseurl(dest)
417 dest, branches = hg.parseurl(dest)
438 other = hg.repository(hg.remoteui(repo, {}), dest)
418 other = hg.repository(hg.remoteui(repo, {}), dest)
439 repo.ui.pushbuffer()
419 repo.ui.pushbuffer()
440 o = repo.findoutgoing(other)
420 o = repo.findoutgoing(other)
441 repo.ui.popbuffer()
421 repo.ui.popbuffer()
442 cl = repo.changelog
422 cl = repo.changelog
443 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]])
444 print 'out', dest, o
424 print 'out', dest, o
445 return [r for r in subset if r in o]
425 return [r for r in subset if r in o]
446
426
447 symbols = {
427 symbols = {
448 "ancestor": ancestor,
428 "ancestor": ancestor,
449 "ancestors": ancestors,
429 "ancestors": ancestors,
450 "descendants": descendants,
430 "descendants": descendants,
451 "follow": follow,
431 "follow": follow,
452 "merge": merge,
432 "merge": merge,
453 "reverse": reverse,
433 "reverse": reverse,
454 "sort": sort,
434 "sort": sort,
455 "branch": branch,
435 "branch": branch,
456 "keyword": keyword,
436 "keyword": keyword,
457 "author": author,
437 "author": author,
458 "user": author,
438 "user": author,
459 "date": date,
439 "date": date,
460 "grep": grep,
440 "grep": grep,
461 "p1": p1,
441 "p1": p1,
462 "p2": p2,
442 "p2": p2,
463 "parents": parents,
443 "parents": parents,
464 "children": children,
444 "children": children,
465 "max": maxrev,
445 "max": maxrev,
466 "limit": limit,
446 "limit": limit,
467 "file": hasfile,
447 "file": hasfile,
468 "contains": contains,
448 "contains": contains,
469 "heads": heads,
449 "heads": heads,
470 "roots": roots,
450 "roots": roots,
471 "all": getall,
451 "all": getall,
472 "closed": closed,
452 "closed": closed,
473 "head": head,
453 "head": head,
474 "modifies": modifies,
454 "modifies": modifies,
475 "adds": adds,
455 "adds": adds,
476 "removes": removes,
456 "removes": removes,
477 "outgoing": outgoing,
457 "outgoing": outgoing,
478 }
458 }
479
459
480 methods = {
460 methods = {
481 "negate": negate,
461 "negate": negate,
482 "minus": minusset,
483 "range": rangeset,
462 "range": rangeset,
484 "rangepre": rangepreset,
485 "rangepost": rangepostset,
486 "dagrange": dagrangeset,
487 "dagrangepre": ancestors,
488 "dagrangepost": descendants,
489 "string": stringset,
463 "string": stringset,
490 "symbol": symbolset,
464 "symbol": symbolset,
491 "and": andset,
465 "and": andset,
492 "or": orset,
466 "or": orset,
493 "not": notset,
467 "not": notset,
494 "list": listset,
468 "list": listset,
495 "func": func,
469 "func": func,
496 "group": lambda r, s, x: getset(r, s, x),
497 }
470 }
498
471
499 def weight(x, small):
472 def optimize(x, small):
473 if x == None:
474 return 0, x
475
500 smallbonus = 1
476 smallbonus = 1
501 if small:
477 if small:
502 smallbonus = .5
478 smallbonus = .5
503
479
504 op = x[0]
480 op = x[0]
505 if op in 'string symbol negate':
481 if op == '-':
506 return smallbonus # single revisions are small
482 return optimize(('and', x[1], ('not', x[2])), small)
483 elif op == 'dagrange':
484 return optimize(('and', ('func', ('symbol', 'descendants'), x[1]),
485 ('func', ('symbol', 'ancestors'), x[2])), small)
486 elif op == 'dagrangepre':
487 return optimize(('func', ('symbol', 'ancestors'), x[1]), small)
488 elif op == 'dagrangepost':
489 return optimize(('func', ('symbol', 'descendants'), x[1]), small)
490 elif op == 'rangepre':
491 return optimize(('range', ('string', '0'), x[1]), small)
492 elif op == 'rangepost':
493 return optimize(('range', x[1], ('string', 'tip')), small)
494 elif op in 'string symbol negate':
495 return smallbonus, x # single revisions are small
507 elif op == 'and' or op == 'dagrange':
496 elif op == 'and' or op == 'dagrange':
508 return min(weight(x[1], True), weight(x[2], True))
497 wa, ta = optimize(x[1], True)
509 elif op in 'or -':
498 wb, tb = optimize(x[2], True)
510 return max(weight(x[1], False), weight(x[2], False))
499 w = min(wa, wb)
500 if wa > wb:
501 return w, (op, tb, ta)
502 return w, (op, ta, tb)
503 elif op == 'or':
504 wa, ta = optimize(x[1], False)
505 wb, tb = optimize(x[2], False)
506 if wb < wa:
507 wb, wa = wa, wb
508 return max(wa, wb), (op, ta, tb)
511 elif op == 'not':
509 elif op == 'not':
512 return weight(x[1], not small)
510 o = optimize(x[1], not small)
511 return o[0], (op, o[1])
513 elif op == 'group':
512 elif op == 'group':
514 return weight(x[1], small)
513 return optimize(x[1], small)
515 elif op == 'range':
514 elif op in 'rangepre rangepost dagrangepre dagrangepost':
516 return weight(x[1], small) + weight(x[2], small)
515 wa, ta = optimize(x[1], small)
516 return wa + 1, (op, ta)
517 elif op in 'range list':
518 wa, ta = optimize(x[1], small)
519 wb, tb = optimize(x[2], small)
520 return wa + wb, (op, ta, tb)
517 elif op == 'func':
521 elif op == 'func':
518 f = getstring(x[1], "not a symbol")
522 f = getstring(x[1], "not a symbol")
523 wa, ta = optimize(x[2], small)
519 if f in "grep date user author keyword branch file":
524 if f in "grep date user author keyword branch file":
520 return 10 # slow
525 w = 10 # slow
521 elif f in "modifies adds removes":
526 elif f in "modifies adds removes outgoing":
522 return 30 # slower
527 w = 30 # slower
523 elif f == "contains":
528 elif f == "contains":
524 return 100 # very slow
529 w = 100 # very slow
525 elif f == "ancestor":
530 elif f == "ancestor":
526 return (weight(x[1][1], small) +
531 w = 1 * smallbonus
527 weight(x[1][2], small)) * smallbonus
528 elif f == "reverse limit":
532 elif f == "reverse limit":
529 return weight(x[1], small)
533 w = 0
530 elif f in "sort":
534 elif f in "sort":
531 base = x[1]
535 w = 10 # assume most sorts look at changelog
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:
536 else:
538 return 1
537 w = 1
538 return w + wa, (op, x[1], ta)
539 return 1, x
539
540
540 parse = parser.parser(tokenize, elements).parse
541 parse = parser.parser(tokenize, elements).parse
541
542
542 def match(spec):
543 def match(spec):
543 tree = parse(spec)
544 tree = parse(spec)
545 weight, tree = optimize(tree, True)
544 def mfunc(repo, subset):
546 def mfunc(repo, subset):
545 return getset(repo, subset, tree)
547 return getset(repo, subset, tree)
546 return mfunc
548 return mfunc
General Comments 0
You need to be logged in to leave comments. Login now