##// END OF EJS Templates
revset: try to handle hyphenated symbols if lookup callback is available...
Matt Mackall -
r20780:403f1f73 default
parent child Browse files
Show More
@@ -1,2826 +1,2842 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, error, discovery, hbisect, phases
9 import parser, util, error, discovery, hbisect, phases
10 import node
10 import node
11 import heapq
11 import heapq
12 import match as matchmod
12 import match as matchmod
13 import ancestor as ancestormod
13 import ancestor as ancestormod
14 from i18n import _
14 from i18n import _
15 import encoding
15 import encoding
16 import obsolete as obsmod
16 import obsolete as obsmod
17 import pathutil
17 import pathutil
18 import repoview
18 import repoview
19
19
20 def _revancestors(repo, revs, followfirst):
20 def _revancestors(repo, revs, followfirst):
21 """Like revlog.ancestors(), but supports followfirst."""
21 """Like revlog.ancestors(), but supports followfirst."""
22 cut = followfirst and 1 or None
22 cut = followfirst and 1 or None
23 cl = repo.changelog
23 cl = repo.changelog
24
24
25 def iterate():
25 def iterate():
26 revqueue, revsnode = None, None
26 revqueue, revsnode = None, None
27 h = []
27 h = []
28
28
29 revs.descending()
29 revs.descending()
30 revqueue = util.deque(revs)
30 revqueue = util.deque(revs)
31 if revqueue:
31 if revqueue:
32 revsnode = revqueue.popleft()
32 revsnode = revqueue.popleft()
33 heapq.heappush(h, -revsnode)
33 heapq.heappush(h, -revsnode)
34
34
35 seen = set([node.nullrev])
35 seen = set([node.nullrev])
36 while h:
36 while h:
37 current = -heapq.heappop(h)
37 current = -heapq.heappop(h)
38 if current not in seen:
38 if current not in seen:
39 if revsnode and current == revsnode:
39 if revsnode and current == revsnode:
40 if revqueue:
40 if revqueue:
41 revsnode = revqueue.popleft()
41 revsnode = revqueue.popleft()
42 heapq.heappush(h, -revsnode)
42 heapq.heappush(h, -revsnode)
43 seen.add(current)
43 seen.add(current)
44 yield current
44 yield current
45 for parent in cl.parentrevs(current)[:cut]:
45 for parent in cl.parentrevs(current)[:cut]:
46 if parent != node.nullrev:
46 if parent != node.nullrev:
47 heapq.heappush(h, -parent)
47 heapq.heappush(h, -parent)
48
48
49 return _descgeneratorset(iterate())
49 return _descgeneratorset(iterate())
50
50
51 def _revdescendants(repo, revs, followfirst):
51 def _revdescendants(repo, revs, followfirst):
52 """Like revlog.descendants() but supports followfirst."""
52 """Like revlog.descendants() but supports followfirst."""
53 cut = followfirst and 1 or None
53 cut = followfirst and 1 or None
54
54
55 def iterate():
55 def iterate():
56 cl = repo.changelog
56 cl = repo.changelog
57 first = min(revs)
57 first = min(revs)
58 nullrev = node.nullrev
58 nullrev = node.nullrev
59 if first == nullrev:
59 if first == nullrev:
60 # Are there nodes with a null first parent and a non-null
60 # Are there nodes with a null first parent and a non-null
61 # second one? Maybe. Do we care? Probably not.
61 # second one? Maybe. Do we care? Probably not.
62 for i in cl:
62 for i in cl:
63 yield i
63 yield i
64 else:
64 else:
65 seen = set(revs)
65 seen = set(revs)
66 for i in cl.revs(first + 1):
66 for i in cl.revs(first + 1):
67 for x in cl.parentrevs(i)[:cut]:
67 for x in cl.parentrevs(i)[:cut]:
68 if x != nullrev and x in seen:
68 if x != nullrev and x in seen:
69 seen.add(i)
69 seen.add(i)
70 yield i
70 yield i
71 break
71 break
72
72
73 return _ascgeneratorset(iterate())
73 return _ascgeneratorset(iterate())
74
74
75 def _revsbetween(repo, roots, heads):
75 def _revsbetween(repo, roots, heads):
76 """Return all paths between roots and heads, inclusive of both endpoint
76 """Return all paths between roots and heads, inclusive of both endpoint
77 sets."""
77 sets."""
78 if not roots:
78 if not roots:
79 return baseset([])
79 return baseset([])
80 parentrevs = repo.changelog.parentrevs
80 parentrevs = repo.changelog.parentrevs
81 visit = baseset(heads)
81 visit = baseset(heads)
82 reachable = set()
82 reachable = set()
83 seen = {}
83 seen = {}
84 minroot = min(roots)
84 minroot = min(roots)
85 roots = set(roots)
85 roots = set(roots)
86 # open-code the post-order traversal due to the tiny size of
86 # open-code the post-order traversal due to the tiny size of
87 # sys.getrecursionlimit()
87 # sys.getrecursionlimit()
88 while visit:
88 while visit:
89 rev = visit.pop()
89 rev = visit.pop()
90 if rev in roots:
90 if rev in roots:
91 reachable.add(rev)
91 reachable.add(rev)
92 parents = parentrevs(rev)
92 parents = parentrevs(rev)
93 seen[rev] = parents
93 seen[rev] = parents
94 for parent in parents:
94 for parent in parents:
95 if parent >= minroot and parent not in seen:
95 if parent >= minroot and parent not in seen:
96 visit.append(parent)
96 visit.append(parent)
97 if not reachable:
97 if not reachable:
98 return baseset([])
98 return baseset([])
99 for rev in sorted(seen):
99 for rev in sorted(seen):
100 for parent in seen[rev]:
100 for parent in seen[rev]:
101 if parent in reachable:
101 if parent in reachable:
102 reachable.add(rev)
102 reachable.add(rev)
103 return baseset(sorted(reachable))
103 return baseset(sorted(reachable))
104
104
105 elements = {
105 elements = {
106 "(": (20, ("group", 1, ")"), ("func", 1, ")")),
106 "(": (20, ("group", 1, ")"), ("func", 1, ")")),
107 "~": (18, None, ("ancestor", 18)),
107 "~": (18, None, ("ancestor", 18)),
108 "^": (18, None, ("parent", 18), ("parentpost", 18)),
108 "^": (18, None, ("parent", 18), ("parentpost", 18)),
109 "-": (5, ("negate", 19), ("minus", 5)),
109 "-": (5, ("negate", 19), ("minus", 5)),
110 "::": (17, ("dagrangepre", 17), ("dagrange", 17),
110 "::": (17, ("dagrangepre", 17), ("dagrange", 17),
111 ("dagrangepost", 17)),
111 ("dagrangepost", 17)),
112 "..": (17, ("dagrangepre", 17), ("dagrange", 17),
112 "..": (17, ("dagrangepre", 17), ("dagrange", 17),
113 ("dagrangepost", 17)),
113 ("dagrangepost", 17)),
114 ":": (15, ("rangepre", 15), ("range", 15), ("rangepost", 15)),
114 ":": (15, ("rangepre", 15), ("range", 15), ("rangepost", 15)),
115 "not": (10, ("not", 10)),
115 "not": (10, ("not", 10)),
116 "!": (10, ("not", 10)),
116 "!": (10, ("not", 10)),
117 "and": (5, None, ("and", 5)),
117 "and": (5, None, ("and", 5)),
118 "&": (5, None, ("and", 5)),
118 "&": (5, None, ("and", 5)),
119 "or": (4, None, ("or", 4)),
119 "or": (4, None, ("or", 4)),
120 "|": (4, None, ("or", 4)),
120 "|": (4, None, ("or", 4)),
121 "+": (4, None, ("or", 4)),
121 "+": (4, None, ("or", 4)),
122 ",": (2, None, ("list", 2)),
122 ",": (2, None, ("list", 2)),
123 ")": (0, None, None),
123 ")": (0, None, None),
124 "symbol": (0, ("symbol",), None),
124 "symbol": (0, ("symbol",), None),
125 "string": (0, ("string",), None),
125 "string": (0, ("string",), None),
126 "end": (0, None, None),
126 "end": (0, None, None),
127 }
127 }
128
128
129 keywords = set(['and', 'or', 'not'])
129 keywords = set(['and', 'or', 'not'])
130
130
131 def tokenize(program, lookup=None):
131 def tokenize(program, lookup=None):
132 '''
132 '''
133 Parse a revset statement into a stream of tokens
133 Parse a revset statement into a stream of tokens
134
134
135 Check that @ is a valid unquoted token character (issue3686):
135 Check that @ is a valid unquoted token character (issue3686):
136 >>> list(tokenize("@::"))
136 >>> list(tokenize("@::"))
137 [('symbol', '@', 0), ('::', None, 1), ('end', None, 3)]
137 [('symbol', '@', 0), ('::', None, 1), ('end', None, 3)]
138
138
139 '''
139 '''
140
140
141 pos, l = 0, len(program)
141 pos, l = 0, len(program)
142 while pos < l:
142 while pos < l:
143 c = program[pos]
143 c = program[pos]
144 if c.isspace(): # skip inter-token whitespace
144 if c.isspace(): # skip inter-token whitespace
145 pass
145 pass
146 elif c == ':' and program[pos:pos + 2] == '::': # look ahead carefully
146 elif c == ':' and program[pos:pos + 2] == '::': # look ahead carefully
147 yield ('::', None, pos)
147 yield ('::', None, pos)
148 pos += 1 # skip ahead
148 pos += 1 # skip ahead
149 elif c == '.' and program[pos:pos + 2] == '..': # look ahead carefully
149 elif c == '.' and program[pos:pos + 2] == '..': # look ahead carefully
150 yield ('..', None, pos)
150 yield ('..', None, pos)
151 pos += 1 # skip ahead
151 pos += 1 # skip ahead
152 elif c in "():,-|&+!~^": # handle simple operators
152 elif c in "():,-|&+!~^": # handle simple operators
153 yield (c, None, pos)
153 yield (c, None, pos)
154 elif (c in '"\'' or c == 'r' and
154 elif (c in '"\'' or c == 'r' and
155 program[pos:pos + 2] in ("r'", 'r"')): # handle quoted strings
155 program[pos:pos + 2] in ("r'", 'r"')): # handle quoted strings
156 if c == 'r':
156 if c == 'r':
157 pos += 1
157 pos += 1
158 c = program[pos]
158 c = program[pos]
159 decode = lambda x: x
159 decode = lambda x: x
160 else:
160 else:
161 decode = lambda x: x.decode('string-escape')
161 decode = lambda x: x.decode('string-escape')
162 pos += 1
162 pos += 1
163 s = pos
163 s = pos
164 while pos < l: # find closing quote
164 while pos < l: # find closing quote
165 d = program[pos]
165 d = program[pos]
166 if d == '\\': # skip over escaped characters
166 if d == '\\': # skip over escaped characters
167 pos += 2
167 pos += 2
168 continue
168 continue
169 if d == c:
169 if d == c:
170 yield ('string', decode(program[s:pos]), s)
170 yield ('string', decode(program[s:pos]), s)
171 break
171 break
172 pos += 1
172 pos += 1
173 else:
173 else:
174 raise error.ParseError(_("unterminated string"), s)
174 raise error.ParseError(_("unterminated string"), s)
175 # gather up a symbol/keyword
175 # gather up a symbol/keyword
176 elif c.isalnum() or c in '._@' or ord(c) > 127:
176 elif c.isalnum() or c in '._@' or ord(c) > 127:
177 s = pos
177 s = pos
178 pos += 1
178 pos += 1
179 while pos < l: # find end of symbol
179 while pos < l: # find end of symbol
180 d = program[pos]
180 d = program[pos]
181 if not (d.isalnum() or d in "._/@" or ord(d) > 127):
181 if not (d.isalnum() or d in "-._/@" or ord(d) > 127):
182 break
182 break
183 if d == '.' and program[pos - 1] == '.': # special case for ..
183 if d == '.' and program[pos - 1] == '.': # special case for ..
184 pos -= 1
184 pos -= 1
185 break
185 break
186 pos += 1
186 pos += 1
187 sym = program[s:pos]
187 sym = program[s:pos]
188 if sym in keywords: # operator keywords
188 if sym in keywords: # operator keywords
189 yield (sym, None, s)
189 yield (sym, None, s)
190 elif '-' in sym:
191 # some jerk gave us foo-bar-baz, try to check if it's a symbol
192 if lookup and lookup(sym):
193 # looks like a real symbol
194 yield ('symbol', sym, s)
195 else:
196 # looks like an expression
197 parts = sym.split('-')
198 for p in parts[:-1]:
199 if p: # possible consecutive -
200 yield ('symbol', p, s)
201 s += len(p)
202 yield ('-', None, pos)
203 s += 1
204 if parts[-1]: # possible trailing -
205 yield ('symbol', parts[-1], s)
190 else:
206 else:
191 yield ('symbol', sym, s)
207 yield ('symbol', sym, s)
192 pos -= 1
208 pos -= 1
193 else:
209 else:
194 raise error.ParseError(_("syntax error"), pos)
210 raise error.ParseError(_("syntax error"), pos)
195 pos += 1
211 pos += 1
196 yield ('end', None, pos)
212 yield ('end', None, pos)
197
213
198 # helpers
214 # helpers
199
215
200 def getstring(x, err):
216 def getstring(x, err):
201 if x and (x[0] == 'string' or x[0] == 'symbol'):
217 if x and (x[0] == 'string' or x[0] == 'symbol'):
202 return x[1]
218 return x[1]
203 raise error.ParseError(err)
219 raise error.ParseError(err)
204
220
205 def getlist(x):
221 def getlist(x):
206 if not x:
222 if not x:
207 return []
223 return []
208 if x[0] == 'list':
224 if x[0] == 'list':
209 return getlist(x[1]) + [x[2]]
225 return getlist(x[1]) + [x[2]]
210 return [x]
226 return [x]
211
227
212 def getargs(x, min, max, err):
228 def getargs(x, min, max, err):
213 l = getlist(x)
229 l = getlist(x)
214 if len(l) < min or (max >= 0 and len(l) > max):
230 if len(l) < min or (max >= 0 and len(l) > max):
215 raise error.ParseError(err)
231 raise error.ParseError(err)
216 return l
232 return l
217
233
218 def getset(repo, subset, x):
234 def getset(repo, subset, x):
219 if not x:
235 if not x:
220 raise error.ParseError(_("missing argument"))
236 raise error.ParseError(_("missing argument"))
221 s = methods[x[0]](repo, subset, *x[1:])
237 s = methods[x[0]](repo, subset, *x[1:])
222 if util.safehasattr(s, 'set'):
238 if util.safehasattr(s, 'set'):
223 return s
239 return s
224 return baseset(s)
240 return baseset(s)
225
241
226 def _getrevsource(repo, r):
242 def _getrevsource(repo, r):
227 extra = repo[r].extra()
243 extra = repo[r].extra()
228 for label in ('source', 'transplant_source', 'rebase_source'):
244 for label in ('source', 'transplant_source', 'rebase_source'):
229 if label in extra:
245 if label in extra:
230 try:
246 try:
231 return repo[extra[label]].rev()
247 return repo[extra[label]].rev()
232 except error.RepoLookupError:
248 except error.RepoLookupError:
233 pass
249 pass
234 return None
250 return None
235
251
236 # operator methods
252 # operator methods
237
253
238 def stringset(repo, subset, x):
254 def stringset(repo, subset, x):
239 x = repo[x].rev()
255 x = repo[x].rev()
240 if x == -1 and len(subset) == len(repo):
256 if x == -1 and len(subset) == len(repo):
241 return baseset([-1])
257 return baseset([-1])
242 if len(subset) == len(repo) or x in subset:
258 if len(subset) == len(repo) or x in subset:
243 return baseset([x])
259 return baseset([x])
244 return baseset([])
260 return baseset([])
245
261
246 def symbolset(repo, subset, x):
262 def symbolset(repo, subset, x):
247 if x in symbols:
263 if x in symbols:
248 raise error.ParseError(_("can't use %s here") % x)
264 raise error.ParseError(_("can't use %s here") % x)
249 return stringset(repo, subset, x)
265 return stringset(repo, subset, x)
250
266
251 def rangeset(repo, subset, x, y):
267 def rangeset(repo, subset, x, y):
252 cl = baseset(repo.changelog)
268 cl = baseset(repo.changelog)
253 m = getset(repo, cl, x)
269 m = getset(repo, cl, x)
254 n = getset(repo, cl, y)
270 n = getset(repo, cl, y)
255
271
256 if not m or not n:
272 if not m or not n:
257 return baseset([])
273 return baseset([])
258 m, n = m[0], n[-1]
274 m, n = m[0], n[-1]
259
275
260 if m < n:
276 if m < n:
261 r = spanset(repo, m, n + 1)
277 r = spanset(repo, m, n + 1)
262 else:
278 else:
263 r = spanset(repo, m, n - 1)
279 r = spanset(repo, m, n - 1)
264 return r & subset
280 return r & subset
265
281
266 def dagrange(repo, subset, x, y):
282 def dagrange(repo, subset, x, y):
267 r = spanset(repo)
283 r = spanset(repo)
268 xs = _revsbetween(repo, getset(repo, r, x), getset(repo, r, y))
284 xs = _revsbetween(repo, getset(repo, r, x), getset(repo, r, y))
269 s = subset.set()
285 s = subset.set()
270 return xs.filter(lambda r: r in s)
286 return xs.filter(lambda r: r in s)
271
287
272 def andset(repo, subset, x, y):
288 def andset(repo, subset, x, y):
273 return getset(repo, getset(repo, subset, x), y)
289 return getset(repo, getset(repo, subset, x), y)
274
290
275 def orset(repo, subset, x, y):
291 def orset(repo, subset, x, y):
276 xl = getset(repo, subset, x)
292 xl = getset(repo, subset, x)
277 yl = getset(repo, subset - xl, y)
293 yl = getset(repo, subset - xl, y)
278 return xl + yl
294 return xl + yl
279
295
280 def notset(repo, subset, x):
296 def notset(repo, subset, x):
281 return subset - getset(repo, subset, x)
297 return subset - getset(repo, subset, x)
282
298
283 def listset(repo, subset, a, b):
299 def listset(repo, subset, a, b):
284 raise error.ParseError(_("can't use a list in this context"))
300 raise error.ParseError(_("can't use a list in this context"))
285
301
286 def func(repo, subset, a, b):
302 def func(repo, subset, a, b):
287 if a[0] == 'symbol' and a[1] in symbols:
303 if a[0] == 'symbol' and a[1] in symbols:
288 return symbols[a[1]](repo, subset, b)
304 return symbols[a[1]](repo, subset, b)
289 raise error.ParseError(_("not a function: %s") % a[1])
305 raise error.ParseError(_("not a function: %s") % a[1])
290
306
291 # functions
307 # functions
292
308
293 def adds(repo, subset, x):
309 def adds(repo, subset, x):
294 """``adds(pattern)``
310 """``adds(pattern)``
295 Changesets that add a file matching pattern.
311 Changesets that add a file matching pattern.
296
312
297 The pattern without explicit kind like ``glob:`` is expected to be
313 The pattern without explicit kind like ``glob:`` is expected to be
298 relative to the current directory and match against a file or a
314 relative to the current directory and match against a file or a
299 directory.
315 directory.
300 """
316 """
301 # i18n: "adds" is a keyword
317 # i18n: "adds" is a keyword
302 pat = getstring(x, _("adds requires a pattern"))
318 pat = getstring(x, _("adds requires a pattern"))
303 return checkstatus(repo, subset, pat, 1)
319 return checkstatus(repo, subset, pat, 1)
304
320
305 def ancestor(repo, subset, x):
321 def ancestor(repo, subset, x):
306 """``ancestor(*changeset)``
322 """``ancestor(*changeset)``
307 Greatest common ancestor of the changesets.
323 Greatest common ancestor of the changesets.
308
324
309 Accepts 0 or more changesets.
325 Accepts 0 or more changesets.
310 Will return empty list when passed no args.
326 Will return empty list when passed no args.
311 Greatest common ancestor of a single changeset is that changeset.
327 Greatest common ancestor of a single changeset is that changeset.
312 """
328 """
313 # i18n: "ancestor" is a keyword
329 # i18n: "ancestor" is a keyword
314 l = getlist(x)
330 l = getlist(x)
315 rl = spanset(repo)
331 rl = spanset(repo)
316 anc = None
332 anc = None
317
333
318 # (getset(repo, rl, i) for i in l) generates a list of lists
334 # (getset(repo, rl, i) for i in l) generates a list of lists
319 rev = repo.changelog.rev
335 rev = repo.changelog.rev
320 ancestor = repo.changelog.ancestor
336 ancestor = repo.changelog.ancestor
321 node = repo.changelog.node
337 node = repo.changelog.node
322 for revs in (getset(repo, rl, i) for i in l):
338 for revs in (getset(repo, rl, i) for i in l):
323 for r in revs:
339 for r in revs:
324 if anc is None:
340 if anc is None:
325 anc = r
341 anc = r
326 else:
342 else:
327 anc = rev(ancestor(node(anc), node(r)))
343 anc = rev(ancestor(node(anc), node(r)))
328
344
329 if anc is not None and anc in subset:
345 if anc is not None and anc in subset:
330 return baseset([anc])
346 return baseset([anc])
331 return baseset([])
347 return baseset([])
332
348
333 def _ancestors(repo, subset, x, followfirst=False):
349 def _ancestors(repo, subset, x, followfirst=False):
334 args = getset(repo, spanset(repo), x)
350 args = getset(repo, spanset(repo), x)
335 if not args:
351 if not args:
336 return baseset([])
352 return baseset([])
337 s = _revancestors(repo, args, followfirst)
353 s = _revancestors(repo, args, followfirst)
338 return subset.filter(lambda r: r in s)
354 return subset.filter(lambda r: r in s)
339
355
340 def ancestors(repo, subset, x):
356 def ancestors(repo, subset, x):
341 """``ancestors(set)``
357 """``ancestors(set)``
342 Changesets that are ancestors of a changeset in set.
358 Changesets that are ancestors of a changeset in set.
343 """
359 """
344 return _ancestors(repo, subset, x)
360 return _ancestors(repo, subset, x)
345
361
346 def _firstancestors(repo, subset, x):
362 def _firstancestors(repo, subset, x):
347 # ``_firstancestors(set)``
363 # ``_firstancestors(set)``
348 # Like ``ancestors(set)`` but follows only the first parents.
364 # Like ``ancestors(set)`` but follows only the first parents.
349 return _ancestors(repo, subset, x, followfirst=True)
365 return _ancestors(repo, subset, x, followfirst=True)
350
366
351 def ancestorspec(repo, subset, x, n):
367 def ancestorspec(repo, subset, x, n):
352 """``set~n``
368 """``set~n``
353 Changesets that are the Nth ancestor (first parents only) of a changeset
369 Changesets that are the Nth ancestor (first parents only) of a changeset
354 in set.
370 in set.
355 """
371 """
356 try:
372 try:
357 n = int(n[1])
373 n = int(n[1])
358 except (TypeError, ValueError):
374 except (TypeError, ValueError):
359 raise error.ParseError(_("~ expects a number"))
375 raise error.ParseError(_("~ expects a number"))
360 ps = set()
376 ps = set()
361 cl = repo.changelog
377 cl = repo.changelog
362 for r in getset(repo, baseset(cl), x):
378 for r in getset(repo, baseset(cl), x):
363 for i in range(n):
379 for i in range(n):
364 r = cl.parentrevs(r)[0]
380 r = cl.parentrevs(r)[0]
365 ps.add(r)
381 ps.add(r)
366 return subset.filter(lambda r: r in ps)
382 return subset.filter(lambda r: r in ps)
367
383
368 def author(repo, subset, x):
384 def author(repo, subset, x):
369 """``author(string)``
385 """``author(string)``
370 Alias for ``user(string)``.
386 Alias for ``user(string)``.
371 """
387 """
372 # i18n: "author" is a keyword
388 # i18n: "author" is a keyword
373 n = encoding.lower(getstring(x, _("author requires a string")))
389 n = encoding.lower(getstring(x, _("author requires a string")))
374 kind, pattern, matcher = _substringmatcher(n)
390 kind, pattern, matcher = _substringmatcher(n)
375 return subset.filter(lambda x: matcher(encoding.lower(repo[x].user())))
391 return subset.filter(lambda x: matcher(encoding.lower(repo[x].user())))
376
392
377 def only(repo, subset, x):
393 def only(repo, subset, x):
378 """``only(set, [set])``
394 """``only(set, [set])``
379 Changesets that are ancestors of the first set that are not ancestors
395 Changesets that are ancestors of the first set that are not ancestors
380 of any other head in the repo. If a second set is specified, the result
396 of any other head in the repo. If a second set is specified, the result
381 is ancestors of the first set that are not ancestors of the second set
397 is ancestors of the first set that are not ancestors of the second set
382 (i.e. ::<set1> - ::<set2>).
398 (i.e. ::<set1> - ::<set2>).
383 """
399 """
384 cl = repo.changelog
400 cl = repo.changelog
385 args = getargs(x, 1, 2, _('only takes one or two arguments'))
401 args = getargs(x, 1, 2, _('only takes one or two arguments'))
386 include = getset(repo, spanset(repo), args[0]).set()
402 include = getset(repo, spanset(repo), args[0]).set()
387 if len(args) == 1:
403 if len(args) == 1:
388 descendants = set(_revdescendants(repo, include, False))
404 descendants = set(_revdescendants(repo, include, False))
389 exclude = [rev for rev in cl.headrevs()
405 exclude = [rev for rev in cl.headrevs()
390 if not rev in descendants and not rev in include]
406 if not rev in descendants and not rev in include]
391 else:
407 else:
392 exclude = getset(repo, spanset(repo), args[1])
408 exclude = getset(repo, spanset(repo), args[1])
393
409
394 results = set(ancestormod.missingancestors(include, exclude, cl.parentrevs))
410 results = set(ancestormod.missingancestors(include, exclude, cl.parentrevs))
395 return lazyset(subset, lambda x: x in results)
411 return lazyset(subset, lambda x: x in results)
396
412
397 def bisect(repo, subset, x):
413 def bisect(repo, subset, x):
398 """``bisect(string)``
414 """``bisect(string)``
399 Changesets marked in the specified bisect status:
415 Changesets marked in the specified bisect status:
400
416
401 - ``good``, ``bad``, ``skip``: csets explicitly marked as good/bad/skip
417 - ``good``, ``bad``, ``skip``: csets explicitly marked as good/bad/skip
402 - ``goods``, ``bads`` : csets topologically good/bad
418 - ``goods``, ``bads`` : csets topologically good/bad
403 - ``range`` : csets taking part in the bisection
419 - ``range`` : csets taking part in the bisection
404 - ``pruned`` : csets that are goods, bads or skipped
420 - ``pruned`` : csets that are goods, bads or skipped
405 - ``untested`` : csets whose fate is yet unknown
421 - ``untested`` : csets whose fate is yet unknown
406 - ``ignored`` : csets ignored due to DAG topology
422 - ``ignored`` : csets ignored due to DAG topology
407 - ``current`` : the cset currently being bisected
423 - ``current`` : the cset currently being bisected
408 """
424 """
409 # i18n: "bisect" is a keyword
425 # i18n: "bisect" is a keyword
410 status = getstring(x, _("bisect requires a string")).lower()
426 status = getstring(x, _("bisect requires a string")).lower()
411 state = set(hbisect.get(repo, status))
427 state = set(hbisect.get(repo, status))
412 return subset.filter(lambda r: r in state)
428 return subset.filter(lambda r: r in state)
413
429
414 # Backward-compatibility
430 # Backward-compatibility
415 # - no help entry so that we do not advertise it any more
431 # - no help entry so that we do not advertise it any more
416 def bisected(repo, subset, x):
432 def bisected(repo, subset, x):
417 return bisect(repo, subset, x)
433 return bisect(repo, subset, x)
418
434
419 def bookmark(repo, subset, x):
435 def bookmark(repo, subset, x):
420 """``bookmark([name])``
436 """``bookmark([name])``
421 The named bookmark or all bookmarks.
437 The named bookmark or all bookmarks.
422
438
423 If `name` starts with `re:`, the remainder of the name is treated as
439 If `name` starts with `re:`, the remainder of the name is treated as
424 a regular expression. To match a bookmark that actually starts with `re:`,
440 a regular expression. To match a bookmark that actually starts with `re:`,
425 use the prefix `literal:`.
441 use the prefix `literal:`.
426 """
442 """
427 # i18n: "bookmark" is a keyword
443 # i18n: "bookmark" is a keyword
428 args = getargs(x, 0, 1, _('bookmark takes one or no arguments'))
444 args = getargs(x, 0, 1, _('bookmark takes one or no arguments'))
429 if args:
445 if args:
430 bm = getstring(args[0],
446 bm = getstring(args[0],
431 # i18n: "bookmark" is a keyword
447 # i18n: "bookmark" is a keyword
432 _('the argument to bookmark must be a string'))
448 _('the argument to bookmark must be a string'))
433 kind, pattern, matcher = _stringmatcher(bm)
449 kind, pattern, matcher = _stringmatcher(bm)
434 if kind == 'literal':
450 if kind == 'literal':
435 bmrev = repo._bookmarks.get(bm, None)
451 bmrev = repo._bookmarks.get(bm, None)
436 if not bmrev:
452 if not bmrev:
437 raise util.Abort(_("bookmark '%s' does not exist") % bm)
453 raise util.Abort(_("bookmark '%s' does not exist") % bm)
438 bmrev = repo[bmrev].rev()
454 bmrev = repo[bmrev].rev()
439 return subset.filter(lambda r: r == bmrev)
455 return subset.filter(lambda r: r == bmrev)
440 else:
456 else:
441 matchrevs = set()
457 matchrevs = set()
442 for name, bmrev in repo._bookmarks.iteritems():
458 for name, bmrev in repo._bookmarks.iteritems():
443 if matcher(name):
459 if matcher(name):
444 matchrevs.add(bmrev)
460 matchrevs.add(bmrev)
445 if not matchrevs:
461 if not matchrevs:
446 raise util.Abort(_("no bookmarks exist that match '%s'")
462 raise util.Abort(_("no bookmarks exist that match '%s'")
447 % pattern)
463 % pattern)
448 bmrevs = set()
464 bmrevs = set()
449 for bmrev in matchrevs:
465 for bmrev in matchrevs:
450 bmrevs.add(repo[bmrev].rev())
466 bmrevs.add(repo[bmrev].rev())
451 return subset & bmrevs
467 return subset & bmrevs
452
468
453 bms = set([repo[r].rev()
469 bms = set([repo[r].rev()
454 for r in repo._bookmarks.values()])
470 for r in repo._bookmarks.values()])
455 return subset.filter(lambda r: r in bms)
471 return subset.filter(lambda r: r in bms)
456
472
457 def branch(repo, subset, x):
473 def branch(repo, subset, x):
458 """``branch(string or set)``
474 """``branch(string or set)``
459 All changesets belonging to the given branch or the branches of the given
475 All changesets belonging to the given branch or the branches of the given
460 changesets.
476 changesets.
461
477
462 If `string` starts with `re:`, the remainder of the name is treated as
478 If `string` starts with `re:`, the remainder of the name is treated as
463 a regular expression. To match a branch that actually starts with `re:`,
479 a regular expression. To match a branch that actually starts with `re:`,
464 use the prefix `literal:`.
480 use the prefix `literal:`.
465 """
481 """
466 try:
482 try:
467 b = getstring(x, '')
483 b = getstring(x, '')
468 except error.ParseError:
484 except error.ParseError:
469 # not a string, but another revspec, e.g. tip()
485 # not a string, but another revspec, e.g. tip()
470 pass
486 pass
471 else:
487 else:
472 kind, pattern, matcher = _stringmatcher(b)
488 kind, pattern, matcher = _stringmatcher(b)
473 if kind == 'literal':
489 if kind == 'literal':
474 # note: falls through to the revspec case if no branch with
490 # note: falls through to the revspec case if no branch with
475 # this name exists
491 # this name exists
476 if pattern in repo.branchmap():
492 if pattern in repo.branchmap():
477 return subset.filter(lambda r: matcher(repo[r].branch()))
493 return subset.filter(lambda r: matcher(repo[r].branch()))
478 else:
494 else:
479 return subset.filter(lambda r: matcher(repo[r].branch()))
495 return subset.filter(lambda r: matcher(repo[r].branch()))
480
496
481 s = getset(repo, spanset(repo), x)
497 s = getset(repo, spanset(repo), x)
482 b = set()
498 b = set()
483 for r in s:
499 for r in s:
484 b.add(repo[r].branch())
500 b.add(repo[r].branch())
485 s = s.set()
501 s = s.set()
486 return subset.filter(lambda r: r in s or repo[r].branch() in b)
502 return subset.filter(lambda r: r in s or repo[r].branch() in b)
487
503
488 def bumped(repo, subset, x):
504 def bumped(repo, subset, x):
489 """``bumped()``
505 """``bumped()``
490 Mutable changesets marked as successors of public changesets.
506 Mutable changesets marked as successors of public changesets.
491
507
492 Only non-public and non-obsolete changesets can be `bumped`.
508 Only non-public and non-obsolete changesets can be `bumped`.
493 """
509 """
494 # i18n: "bumped" is a keyword
510 # i18n: "bumped" is a keyword
495 getargs(x, 0, 0, _("bumped takes no arguments"))
511 getargs(x, 0, 0, _("bumped takes no arguments"))
496 bumped = obsmod.getrevs(repo, 'bumped')
512 bumped = obsmod.getrevs(repo, 'bumped')
497 return subset & bumped
513 return subset & bumped
498
514
499 def bundle(repo, subset, x):
515 def bundle(repo, subset, x):
500 """``bundle()``
516 """``bundle()``
501 Changesets in the bundle.
517 Changesets in the bundle.
502
518
503 Bundle must be specified by the -R option."""
519 Bundle must be specified by the -R option."""
504
520
505 try:
521 try:
506 bundlerevs = repo.changelog.bundlerevs
522 bundlerevs = repo.changelog.bundlerevs
507 except AttributeError:
523 except AttributeError:
508 raise util.Abort(_("no bundle provided - specify with -R"))
524 raise util.Abort(_("no bundle provided - specify with -R"))
509 return subset & bundlerevs
525 return subset & bundlerevs
510
526
511 def checkstatus(repo, subset, pat, field):
527 def checkstatus(repo, subset, pat, field):
512 hasset = matchmod.patkind(pat) == 'set'
528 hasset = matchmod.patkind(pat) == 'set'
513
529
514 def matches(x):
530 def matches(x):
515 m = None
531 m = None
516 fname = None
532 fname = None
517 c = repo[x]
533 c = repo[x]
518 if not m or hasset:
534 if not m or hasset:
519 m = matchmod.match(repo.root, repo.getcwd(), [pat], ctx=c)
535 m = matchmod.match(repo.root, repo.getcwd(), [pat], ctx=c)
520 if not m.anypats() and len(m.files()) == 1:
536 if not m.anypats() and len(m.files()) == 1:
521 fname = m.files()[0]
537 fname = m.files()[0]
522 if fname is not None:
538 if fname is not None:
523 if fname not in c.files():
539 if fname not in c.files():
524 return False
540 return False
525 else:
541 else:
526 for f in c.files():
542 for f in c.files():
527 if m(f):
543 if m(f):
528 break
544 break
529 else:
545 else:
530 return False
546 return False
531 files = repo.status(c.p1().node(), c.node())[field]
547 files = repo.status(c.p1().node(), c.node())[field]
532 if fname is not None:
548 if fname is not None:
533 if fname in files:
549 if fname in files:
534 return True
550 return True
535 else:
551 else:
536 for f in files:
552 for f in files:
537 if m(f):
553 if m(f):
538 return True
554 return True
539
555
540 return subset.filter(matches)
556 return subset.filter(matches)
541
557
542 def _children(repo, narrow, parentset):
558 def _children(repo, narrow, parentset):
543 cs = set()
559 cs = set()
544 if not parentset:
560 if not parentset:
545 return baseset(cs)
561 return baseset(cs)
546 pr = repo.changelog.parentrevs
562 pr = repo.changelog.parentrevs
547 minrev = min(parentset)
563 minrev = min(parentset)
548 for r in narrow:
564 for r in narrow:
549 if r <= minrev:
565 if r <= minrev:
550 continue
566 continue
551 for p in pr(r):
567 for p in pr(r):
552 if p in parentset:
568 if p in parentset:
553 cs.add(r)
569 cs.add(r)
554 return baseset(cs)
570 return baseset(cs)
555
571
556 def children(repo, subset, x):
572 def children(repo, subset, x):
557 """``children(set)``
573 """``children(set)``
558 Child changesets of changesets in set.
574 Child changesets of changesets in set.
559 """
575 """
560 s = getset(repo, baseset(repo), x).set()
576 s = getset(repo, baseset(repo), x).set()
561 cs = _children(repo, subset, s)
577 cs = _children(repo, subset, s)
562 return subset & cs
578 return subset & cs
563
579
564 def closed(repo, subset, x):
580 def closed(repo, subset, x):
565 """``closed()``
581 """``closed()``
566 Changeset is closed.
582 Changeset is closed.
567 """
583 """
568 # i18n: "closed" is a keyword
584 # i18n: "closed" is a keyword
569 getargs(x, 0, 0, _("closed takes no arguments"))
585 getargs(x, 0, 0, _("closed takes no arguments"))
570 return subset.filter(lambda r: repo[r].closesbranch())
586 return subset.filter(lambda r: repo[r].closesbranch())
571
587
572 def contains(repo, subset, x):
588 def contains(repo, subset, x):
573 """``contains(pattern)``
589 """``contains(pattern)``
574 Revision contains a file matching pattern. See :hg:`help patterns`
590 Revision contains a file matching pattern. See :hg:`help patterns`
575 for information about file patterns.
591 for information about file patterns.
576
592
577 The pattern without explicit kind like ``glob:`` is expected to be
593 The pattern without explicit kind like ``glob:`` is expected to be
578 relative to the current directory and match against a file exactly
594 relative to the current directory and match against a file exactly
579 for efficiency.
595 for efficiency.
580 """
596 """
581 # i18n: "contains" is a keyword
597 # i18n: "contains" is a keyword
582 pat = getstring(x, _("contains requires a pattern"))
598 pat = getstring(x, _("contains requires a pattern"))
583
599
584 def matches(x):
600 def matches(x):
585 if not matchmod.patkind(pat):
601 if not matchmod.patkind(pat):
586 pats = pathutil.canonpath(repo.root, repo.getcwd(), pat)
602 pats = pathutil.canonpath(repo.root, repo.getcwd(), pat)
587 if pats in repo[x]:
603 if pats in repo[x]:
588 return True
604 return True
589 else:
605 else:
590 c = repo[x]
606 c = repo[x]
591 m = matchmod.match(repo.root, repo.getcwd(), [pat], ctx=c)
607 m = matchmod.match(repo.root, repo.getcwd(), [pat], ctx=c)
592 for f in c.manifest():
608 for f in c.manifest():
593 if m(f):
609 if m(f):
594 return True
610 return True
595 return False
611 return False
596
612
597 return subset.filter(matches)
613 return subset.filter(matches)
598
614
599 def converted(repo, subset, x):
615 def converted(repo, subset, x):
600 """``converted([id])``
616 """``converted([id])``
601 Changesets converted from the given identifier in the old repository if
617 Changesets converted from the given identifier in the old repository if
602 present, or all converted changesets if no identifier is specified.
618 present, or all converted changesets if no identifier is specified.
603 """
619 """
604
620
605 # There is exactly no chance of resolving the revision, so do a simple
621 # There is exactly no chance of resolving the revision, so do a simple
606 # string compare and hope for the best
622 # string compare and hope for the best
607
623
608 rev = None
624 rev = None
609 # i18n: "converted" is a keyword
625 # i18n: "converted" is a keyword
610 l = getargs(x, 0, 1, _('converted takes one or no arguments'))
626 l = getargs(x, 0, 1, _('converted takes one or no arguments'))
611 if l:
627 if l:
612 # i18n: "converted" is a keyword
628 # i18n: "converted" is a keyword
613 rev = getstring(l[0], _('converted requires a revision'))
629 rev = getstring(l[0], _('converted requires a revision'))
614
630
615 def _matchvalue(r):
631 def _matchvalue(r):
616 source = repo[r].extra().get('convert_revision', None)
632 source = repo[r].extra().get('convert_revision', None)
617 return source is not None and (rev is None or source.startswith(rev))
633 return source is not None and (rev is None or source.startswith(rev))
618
634
619 return subset.filter(lambda r: _matchvalue(r))
635 return subset.filter(lambda r: _matchvalue(r))
620
636
621 def date(repo, subset, x):
637 def date(repo, subset, x):
622 """``date(interval)``
638 """``date(interval)``
623 Changesets within the interval, see :hg:`help dates`.
639 Changesets within the interval, see :hg:`help dates`.
624 """
640 """
625 # i18n: "date" is a keyword
641 # i18n: "date" is a keyword
626 ds = getstring(x, _("date requires a string"))
642 ds = getstring(x, _("date requires a string"))
627 dm = util.matchdate(ds)
643 dm = util.matchdate(ds)
628 return subset.filter(lambda x: dm(repo[x].date()[0]))
644 return subset.filter(lambda x: dm(repo[x].date()[0]))
629
645
630 def desc(repo, subset, x):
646 def desc(repo, subset, x):
631 """``desc(string)``
647 """``desc(string)``
632 Search commit message for string. The match is case-insensitive.
648 Search commit message for string. The match is case-insensitive.
633 """
649 """
634 # i18n: "desc" is a keyword
650 # i18n: "desc" is a keyword
635 ds = encoding.lower(getstring(x, _("desc requires a string")))
651 ds = encoding.lower(getstring(x, _("desc requires a string")))
636
652
637 def matches(x):
653 def matches(x):
638 c = repo[x]
654 c = repo[x]
639 return ds in encoding.lower(c.description())
655 return ds in encoding.lower(c.description())
640
656
641 return subset.filter(matches)
657 return subset.filter(matches)
642
658
643 def _descendants(repo, subset, x, followfirst=False):
659 def _descendants(repo, subset, x, followfirst=False):
644 args = getset(repo, spanset(repo), x)
660 args = getset(repo, spanset(repo), x)
645 if not args:
661 if not args:
646 return baseset([])
662 return baseset([])
647 s = _revdescendants(repo, args, followfirst)
663 s = _revdescendants(repo, args, followfirst)
648 a = set(args)
664 a = set(args)
649 return subset.filter(lambda r: r in s or r in a)
665 return subset.filter(lambda r: r in s or r in a)
650
666
651 def descendants(repo, subset, x):
667 def descendants(repo, subset, x):
652 """``descendants(set)``
668 """``descendants(set)``
653 Changesets which are descendants of changesets in set.
669 Changesets which are descendants of changesets in set.
654 """
670 """
655 return _descendants(repo, subset, x)
671 return _descendants(repo, subset, x)
656
672
657 def _firstdescendants(repo, subset, x):
673 def _firstdescendants(repo, subset, x):
658 # ``_firstdescendants(set)``
674 # ``_firstdescendants(set)``
659 # Like ``descendants(set)`` but follows only the first parents.
675 # Like ``descendants(set)`` but follows only the first parents.
660 return _descendants(repo, subset, x, followfirst=True)
676 return _descendants(repo, subset, x, followfirst=True)
661
677
662 def destination(repo, subset, x):
678 def destination(repo, subset, x):
663 """``destination([set])``
679 """``destination([set])``
664 Changesets that were created by a graft, transplant or rebase operation,
680 Changesets that were created by a graft, transplant or rebase operation,
665 with the given revisions specified as the source. Omitting the optional set
681 with the given revisions specified as the source. Omitting the optional set
666 is the same as passing all().
682 is the same as passing all().
667 """
683 """
668 if x is not None:
684 if x is not None:
669 args = getset(repo, spanset(repo), x).set()
685 args = getset(repo, spanset(repo), x).set()
670 else:
686 else:
671 args = getall(repo, spanset(repo), x).set()
687 args = getall(repo, spanset(repo), x).set()
672
688
673 dests = set()
689 dests = set()
674
690
675 # subset contains all of the possible destinations that can be returned, so
691 # subset contains all of the possible destinations that can be returned, so
676 # iterate over them and see if their source(s) were provided in the args.
692 # iterate over them and see if their source(s) were provided in the args.
677 # Even if the immediate src of r is not in the args, src's source (or
693 # Even if the immediate src of r is not in the args, src's source (or
678 # further back) may be. Scanning back further than the immediate src allows
694 # further back) may be. Scanning back further than the immediate src allows
679 # transitive transplants and rebases to yield the same results as transitive
695 # transitive transplants and rebases to yield the same results as transitive
680 # grafts.
696 # grafts.
681 for r in subset:
697 for r in subset:
682 src = _getrevsource(repo, r)
698 src = _getrevsource(repo, r)
683 lineage = None
699 lineage = None
684
700
685 while src is not None:
701 while src is not None:
686 if lineage is None:
702 if lineage is None:
687 lineage = list()
703 lineage = list()
688
704
689 lineage.append(r)
705 lineage.append(r)
690
706
691 # The visited lineage is a match if the current source is in the arg
707 # The visited lineage is a match if the current source is in the arg
692 # set. Since every candidate dest is visited by way of iterating
708 # set. Since every candidate dest is visited by way of iterating
693 # subset, any dests further back in the lineage will be tested by a
709 # subset, any dests further back in the lineage will be tested by a
694 # different iteration over subset. Likewise, if the src was already
710 # different iteration over subset. Likewise, if the src was already
695 # selected, the current lineage can be selected without going back
711 # selected, the current lineage can be selected without going back
696 # further.
712 # further.
697 if src in args or src in dests:
713 if src in args or src in dests:
698 dests.update(lineage)
714 dests.update(lineage)
699 break
715 break
700
716
701 r = src
717 r = src
702 src = _getrevsource(repo, r)
718 src = _getrevsource(repo, r)
703
719
704 return subset.filter(lambda r: r in dests)
720 return subset.filter(lambda r: r in dests)
705
721
706 def divergent(repo, subset, x):
722 def divergent(repo, subset, x):
707 """``divergent()``
723 """``divergent()``
708 Final successors of changesets with an alternative set of final successors.
724 Final successors of changesets with an alternative set of final successors.
709 """
725 """
710 # i18n: "divergent" is a keyword
726 # i18n: "divergent" is a keyword
711 getargs(x, 0, 0, _("divergent takes no arguments"))
727 getargs(x, 0, 0, _("divergent takes no arguments"))
712 divergent = obsmod.getrevs(repo, 'divergent')
728 divergent = obsmod.getrevs(repo, 'divergent')
713 return subset.filter(lambda r: r in divergent)
729 return subset.filter(lambda r: r in divergent)
714
730
715 def draft(repo, subset, x):
731 def draft(repo, subset, x):
716 """``draft()``
732 """``draft()``
717 Changeset in draft phase."""
733 Changeset in draft phase."""
718 # i18n: "draft" is a keyword
734 # i18n: "draft" is a keyword
719 getargs(x, 0, 0, _("draft takes no arguments"))
735 getargs(x, 0, 0, _("draft takes no arguments"))
720 pc = repo._phasecache
736 pc = repo._phasecache
721 return subset.filter(lambda r: pc.phase(repo, r) == phases.draft)
737 return subset.filter(lambda r: pc.phase(repo, r) == phases.draft)
722
738
723 def extinct(repo, subset, x):
739 def extinct(repo, subset, x):
724 """``extinct()``
740 """``extinct()``
725 Obsolete changesets with obsolete descendants only.
741 Obsolete changesets with obsolete descendants only.
726 """
742 """
727 # i18n: "extinct" is a keyword
743 # i18n: "extinct" is a keyword
728 getargs(x, 0, 0, _("extinct takes no arguments"))
744 getargs(x, 0, 0, _("extinct takes no arguments"))
729 extincts = obsmod.getrevs(repo, 'extinct')
745 extincts = obsmod.getrevs(repo, 'extinct')
730 return subset & extincts
746 return subset & extincts
731
747
732 def extra(repo, subset, x):
748 def extra(repo, subset, x):
733 """``extra(label, [value])``
749 """``extra(label, [value])``
734 Changesets with the given label in the extra metadata, with the given
750 Changesets with the given label in the extra metadata, with the given
735 optional value.
751 optional value.
736
752
737 If `value` starts with `re:`, the remainder of the value is treated as
753 If `value` starts with `re:`, the remainder of the value is treated as
738 a regular expression. To match a value that actually starts with `re:`,
754 a regular expression. To match a value that actually starts with `re:`,
739 use the prefix `literal:`.
755 use the prefix `literal:`.
740 """
756 """
741
757
742 # i18n: "extra" is a keyword
758 # i18n: "extra" is a keyword
743 l = getargs(x, 1, 2, _('extra takes at least 1 and at most 2 arguments'))
759 l = getargs(x, 1, 2, _('extra takes at least 1 and at most 2 arguments'))
744 # i18n: "extra" is a keyword
760 # i18n: "extra" is a keyword
745 label = getstring(l[0], _('first argument to extra must be a string'))
761 label = getstring(l[0], _('first argument to extra must be a string'))
746 value = None
762 value = None
747
763
748 if len(l) > 1:
764 if len(l) > 1:
749 # i18n: "extra" is a keyword
765 # i18n: "extra" is a keyword
750 value = getstring(l[1], _('second argument to extra must be a string'))
766 value = getstring(l[1], _('second argument to extra must be a string'))
751 kind, value, matcher = _stringmatcher(value)
767 kind, value, matcher = _stringmatcher(value)
752
768
753 def _matchvalue(r):
769 def _matchvalue(r):
754 extra = repo[r].extra()
770 extra = repo[r].extra()
755 return label in extra and (value is None or matcher(extra[label]))
771 return label in extra and (value is None or matcher(extra[label]))
756
772
757 return subset.filter(lambda r: _matchvalue(r))
773 return subset.filter(lambda r: _matchvalue(r))
758
774
759 def filelog(repo, subset, x):
775 def filelog(repo, subset, x):
760 """``filelog(pattern)``
776 """``filelog(pattern)``
761 Changesets connected to the specified filelog.
777 Changesets connected to the specified filelog.
762
778
763 For performance reasons, ``filelog()`` does not show every changeset
779 For performance reasons, ``filelog()`` does not show every changeset
764 that affects the requested file(s). See :hg:`help log` for details. For
780 that affects the requested file(s). See :hg:`help log` for details. For
765 a slower, more accurate result, use ``file()``.
781 a slower, more accurate result, use ``file()``.
766
782
767 The pattern without explicit kind like ``glob:`` is expected to be
783 The pattern without explicit kind like ``glob:`` is expected to be
768 relative to the current directory and match against a file exactly
784 relative to the current directory and match against a file exactly
769 for efficiency.
785 for efficiency.
770 """
786 """
771
787
772 # i18n: "filelog" is a keyword
788 # i18n: "filelog" is a keyword
773 pat = getstring(x, _("filelog requires a pattern"))
789 pat = getstring(x, _("filelog requires a pattern"))
774 s = set()
790 s = set()
775
791
776 if not matchmod.patkind(pat):
792 if not matchmod.patkind(pat):
777 f = pathutil.canonpath(repo.root, repo.getcwd(), pat)
793 f = pathutil.canonpath(repo.root, repo.getcwd(), pat)
778 fl = repo.file(f)
794 fl = repo.file(f)
779 for fr in fl:
795 for fr in fl:
780 s.add(fl.linkrev(fr))
796 s.add(fl.linkrev(fr))
781 else:
797 else:
782 m = matchmod.match(repo.root, repo.getcwd(), [pat], ctx=repo[None])
798 m = matchmod.match(repo.root, repo.getcwd(), [pat], ctx=repo[None])
783 for f in repo[None]:
799 for f in repo[None]:
784 if m(f):
800 if m(f):
785 fl = repo.file(f)
801 fl = repo.file(f)
786 for fr in fl:
802 for fr in fl:
787 s.add(fl.linkrev(fr))
803 s.add(fl.linkrev(fr))
788
804
789 return subset.filter(lambda r: r in s)
805 return subset.filter(lambda r: r in s)
790
806
791 def first(repo, subset, x):
807 def first(repo, subset, x):
792 """``first(set, [n])``
808 """``first(set, [n])``
793 An alias for limit().
809 An alias for limit().
794 """
810 """
795 return limit(repo, subset, x)
811 return limit(repo, subset, x)
796
812
797 def _follow(repo, subset, x, name, followfirst=False):
813 def _follow(repo, subset, x, name, followfirst=False):
798 l = getargs(x, 0, 1, _("%s takes no arguments or a filename") % name)
814 l = getargs(x, 0, 1, _("%s takes no arguments or a filename") % name)
799 c = repo['.']
815 c = repo['.']
800 if l:
816 if l:
801 x = getstring(l[0], _("%s expected a filename") % name)
817 x = getstring(l[0], _("%s expected a filename") % name)
802 if x in c:
818 if x in c:
803 cx = c[x]
819 cx = c[x]
804 s = set(ctx.rev() for ctx in cx.ancestors(followfirst=followfirst))
820 s = set(ctx.rev() for ctx in cx.ancestors(followfirst=followfirst))
805 # include the revision responsible for the most recent version
821 # include the revision responsible for the most recent version
806 s.add(cx.linkrev())
822 s.add(cx.linkrev())
807 else:
823 else:
808 return baseset([])
824 return baseset([])
809 else:
825 else:
810 s = _revancestors(repo, baseset([c.rev()]), followfirst)
826 s = _revancestors(repo, baseset([c.rev()]), followfirst)
811
827
812 return subset.filter(lambda r: r in s)
828 return subset.filter(lambda r: r in s)
813
829
814 def follow(repo, subset, x):
830 def follow(repo, subset, x):
815 """``follow([file])``
831 """``follow([file])``
816 An alias for ``::.`` (ancestors of the working copy's first parent).
832 An alias for ``::.`` (ancestors of the working copy's first parent).
817 If a filename is specified, the history of the given file is followed,
833 If a filename is specified, the history of the given file is followed,
818 including copies.
834 including copies.
819 """
835 """
820 return _follow(repo, subset, x, 'follow')
836 return _follow(repo, subset, x, 'follow')
821
837
822 def _followfirst(repo, subset, x):
838 def _followfirst(repo, subset, x):
823 # ``followfirst([file])``
839 # ``followfirst([file])``
824 # Like ``follow([file])`` but follows only the first parent of
840 # Like ``follow([file])`` but follows only the first parent of
825 # every revision or file revision.
841 # every revision or file revision.
826 return _follow(repo, subset, x, '_followfirst', followfirst=True)
842 return _follow(repo, subset, x, '_followfirst', followfirst=True)
827
843
828 def getall(repo, subset, x):
844 def getall(repo, subset, x):
829 """``all()``
845 """``all()``
830 All changesets, the same as ``0:tip``.
846 All changesets, the same as ``0:tip``.
831 """
847 """
832 # i18n: "all" is a keyword
848 # i18n: "all" is a keyword
833 getargs(x, 0, 0, _("all takes no arguments"))
849 getargs(x, 0, 0, _("all takes no arguments"))
834 return subset
850 return subset
835
851
836 def grep(repo, subset, x):
852 def grep(repo, subset, x):
837 """``grep(regex)``
853 """``grep(regex)``
838 Like ``keyword(string)`` but accepts a regex. Use ``grep(r'...')``
854 Like ``keyword(string)`` but accepts a regex. Use ``grep(r'...')``
839 to ensure special escape characters are handled correctly. Unlike
855 to ensure special escape characters are handled correctly. Unlike
840 ``keyword(string)``, the match is case-sensitive.
856 ``keyword(string)``, the match is case-sensitive.
841 """
857 """
842 try:
858 try:
843 # i18n: "grep" is a keyword
859 # i18n: "grep" is a keyword
844 gr = re.compile(getstring(x, _("grep requires a string")))
860 gr = re.compile(getstring(x, _("grep requires a string")))
845 except re.error, e:
861 except re.error, e:
846 raise error.ParseError(_('invalid match pattern: %s') % e)
862 raise error.ParseError(_('invalid match pattern: %s') % e)
847
863
848 def matches(x):
864 def matches(x):
849 c = repo[x]
865 c = repo[x]
850 for e in c.files() + [c.user(), c.description()]:
866 for e in c.files() + [c.user(), c.description()]:
851 if gr.search(e):
867 if gr.search(e):
852 return True
868 return True
853 return False
869 return False
854
870
855 return subset.filter(matches)
871 return subset.filter(matches)
856
872
857 def _matchfiles(repo, subset, x):
873 def _matchfiles(repo, subset, x):
858 # _matchfiles takes a revset list of prefixed arguments:
874 # _matchfiles takes a revset list of prefixed arguments:
859 #
875 #
860 # [p:foo, i:bar, x:baz]
876 # [p:foo, i:bar, x:baz]
861 #
877 #
862 # builds a match object from them and filters subset. Allowed
878 # builds a match object from them and filters subset. Allowed
863 # prefixes are 'p:' for regular patterns, 'i:' for include
879 # prefixes are 'p:' for regular patterns, 'i:' for include
864 # patterns and 'x:' for exclude patterns. Use 'r:' prefix to pass
880 # patterns and 'x:' for exclude patterns. Use 'r:' prefix to pass
865 # a revision identifier, or the empty string to reference the
881 # a revision identifier, or the empty string to reference the
866 # working directory, from which the match object is
882 # working directory, from which the match object is
867 # initialized. Use 'd:' to set the default matching mode, default
883 # initialized. Use 'd:' to set the default matching mode, default
868 # to 'glob'. At most one 'r:' and 'd:' argument can be passed.
884 # to 'glob'. At most one 'r:' and 'd:' argument can be passed.
869
885
870 # i18n: "_matchfiles" is a keyword
886 # i18n: "_matchfiles" is a keyword
871 l = getargs(x, 1, -1, _("_matchfiles requires at least one argument"))
887 l = getargs(x, 1, -1, _("_matchfiles requires at least one argument"))
872 pats, inc, exc = [], [], []
888 pats, inc, exc = [], [], []
873 hasset = False
889 hasset = False
874 rev, default = None, None
890 rev, default = None, None
875 for arg in l:
891 for arg in l:
876 # i18n: "_matchfiles" is a keyword
892 # i18n: "_matchfiles" is a keyword
877 s = getstring(arg, _("_matchfiles requires string arguments"))
893 s = getstring(arg, _("_matchfiles requires string arguments"))
878 prefix, value = s[:2], s[2:]
894 prefix, value = s[:2], s[2:]
879 if prefix == 'p:':
895 if prefix == 'p:':
880 pats.append(value)
896 pats.append(value)
881 elif prefix == 'i:':
897 elif prefix == 'i:':
882 inc.append(value)
898 inc.append(value)
883 elif prefix == 'x:':
899 elif prefix == 'x:':
884 exc.append(value)
900 exc.append(value)
885 elif prefix == 'r:':
901 elif prefix == 'r:':
886 if rev is not None:
902 if rev is not None:
887 # i18n: "_matchfiles" is a keyword
903 # i18n: "_matchfiles" is a keyword
888 raise error.ParseError(_('_matchfiles expected at most one '
904 raise error.ParseError(_('_matchfiles expected at most one '
889 'revision'))
905 'revision'))
890 rev = value
906 rev = value
891 elif prefix == 'd:':
907 elif prefix == 'd:':
892 if default is not None:
908 if default is not None:
893 # i18n: "_matchfiles" is a keyword
909 # i18n: "_matchfiles" is a keyword
894 raise error.ParseError(_('_matchfiles expected at most one '
910 raise error.ParseError(_('_matchfiles expected at most one '
895 'default mode'))
911 'default mode'))
896 default = value
912 default = value
897 else:
913 else:
898 # i18n: "_matchfiles" is a keyword
914 # i18n: "_matchfiles" is a keyword
899 raise error.ParseError(_('invalid _matchfiles prefix: %s') % prefix)
915 raise error.ParseError(_('invalid _matchfiles prefix: %s') % prefix)
900 if not hasset and matchmod.patkind(value) == 'set':
916 if not hasset and matchmod.patkind(value) == 'set':
901 hasset = True
917 hasset = True
902 if not default:
918 if not default:
903 default = 'glob'
919 default = 'glob'
904
920
905 def matches(x):
921 def matches(x):
906 m = None
922 m = None
907 c = repo[x]
923 c = repo[x]
908 if not m or (hasset and rev is None):
924 if not m or (hasset and rev is None):
909 ctx = c
925 ctx = c
910 if rev is not None:
926 if rev is not None:
911 ctx = repo[rev or None]
927 ctx = repo[rev or None]
912 m = matchmod.match(repo.root, repo.getcwd(), pats, include=inc,
928 m = matchmod.match(repo.root, repo.getcwd(), pats, include=inc,
913 exclude=exc, ctx=ctx, default=default)
929 exclude=exc, ctx=ctx, default=default)
914 for f in c.files():
930 for f in c.files():
915 if m(f):
931 if m(f):
916 return True
932 return True
917 return False
933 return False
918
934
919 return subset.filter(matches)
935 return subset.filter(matches)
920
936
921 def hasfile(repo, subset, x):
937 def hasfile(repo, subset, x):
922 """``file(pattern)``
938 """``file(pattern)``
923 Changesets affecting files matched by pattern.
939 Changesets affecting files matched by pattern.
924
940
925 For a faster but less accurate result, consider using ``filelog()``
941 For a faster but less accurate result, consider using ``filelog()``
926 instead.
942 instead.
927
943
928 This predicate uses ``glob:`` as the default kind of pattern.
944 This predicate uses ``glob:`` as the default kind of pattern.
929 """
945 """
930 # i18n: "file" is a keyword
946 # i18n: "file" is a keyword
931 pat = getstring(x, _("file requires a pattern"))
947 pat = getstring(x, _("file requires a pattern"))
932 return _matchfiles(repo, subset, ('string', 'p:' + pat))
948 return _matchfiles(repo, subset, ('string', 'p:' + pat))
933
949
934 def head(repo, subset, x):
950 def head(repo, subset, x):
935 """``head()``
951 """``head()``
936 Changeset is a named branch head.
952 Changeset is a named branch head.
937 """
953 """
938 # i18n: "head" is a keyword
954 # i18n: "head" is a keyword
939 getargs(x, 0, 0, _("head takes no arguments"))
955 getargs(x, 0, 0, _("head takes no arguments"))
940 hs = set()
956 hs = set()
941 for b, ls in repo.branchmap().iteritems():
957 for b, ls in repo.branchmap().iteritems():
942 hs.update(repo[h].rev() for h in ls)
958 hs.update(repo[h].rev() for h in ls)
943 return baseset(hs).filter(subset.__contains__)
959 return baseset(hs).filter(subset.__contains__)
944
960
945 def heads(repo, subset, x):
961 def heads(repo, subset, x):
946 """``heads(set)``
962 """``heads(set)``
947 Members of set with no children in set.
963 Members of set with no children in set.
948 """
964 """
949 s = getset(repo, subset, x)
965 s = getset(repo, subset, x)
950 ps = parents(repo, subset, x)
966 ps = parents(repo, subset, x)
951 return s - ps
967 return s - ps
952
968
953 def hidden(repo, subset, x):
969 def hidden(repo, subset, x):
954 """``hidden()``
970 """``hidden()``
955 Hidden changesets.
971 Hidden changesets.
956 """
972 """
957 # i18n: "hidden" is a keyword
973 # i18n: "hidden" is a keyword
958 getargs(x, 0, 0, _("hidden takes no arguments"))
974 getargs(x, 0, 0, _("hidden takes no arguments"))
959 hiddenrevs = repoview.filterrevs(repo, 'visible')
975 hiddenrevs = repoview.filterrevs(repo, 'visible')
960 return subset & hiddenrevs
976 return subset & hiddenrevs
961
977
962 def keyword(repo, subset, x):
978 def keyword(repo, subset, x):
963 """``keyword(string)``
979 """``keyword(string)``
964 Search commit message, user name, and names of changed files for
980 Search commit message, user name, and names of changed files for
965 string. The match is case-insensitive.
981 string. The match is case-insensitive.
966 """
982 """
967 # i18n: "keyword" is a keyword
983 # i18n: "keyword" is a keyword
968 kw = encoding.lower(getstring(x, _("keyword requires a string")))
984 kw = encoding.lower(getstring(x, _("keyword requires a string")))
969
985
970 def matches(r):
986 def matches(r):
971 c = repo[r]
987 c = repo[r]
972 return util.any(kw in encoding.lower(t) for t in c.files() + [c.user(),
988 return util.any(kw in encoding.lower(t) for t in c.files() + [c.user(),
973 c.description()])
989 c.description()])
974
990
975 return subset.filter(matches)
991 return subset.filter(matches)
976
992
977 def limit(repo, subset, x):
993 def limit(repo, subset, x):
978 """``limit(set, [n])``
994 """``limit(set, [n])``
979 First n members of set, defaulting to 1.
995 First n members of set, defaulting to 1.
980 """
996 """
981 # i18n: "limit" is a keyword
997 # i18n: "limit" is a keyword
982 l = getargs(x, 1, 2, _("limit requires one or two arguments"))
998 l = getargs(x, 1, 2, _("limit requires one or two arguments"))
983 try:
999 try:
984 lim = 1
1000 lim = 1
985 if len(l) == 2:
1001 if len(l) == 2:
986 # i18n: "limit" is a keyword
1002 # i18n: "limit" is a keyword
987 lim = int(getstring(l[1], _("limit requires a number")))
1003 lim = int(getstring(l[1], _("limit requires a number")))
988 except (TypeError, ValueError):
1004 except (TypeError, ValueError):
989 # i18n: "limit" is a keyword
1005 # i18n: "limit" is a keyword
990 raise error.ParseError(_("limit expects a number"))
1006 raise error.ParseError(_("limit expects a number"))
991 ss = subset.set()
1007 ss = subset.set()
992 os = getset(repo, spanset(repo), l[0])
1008 os = getset(repo, spanset(repo), l[0])
993 bs = baseset([])
1009 bs = baseset([])
994 it = iter(os)
1010 it = iter(os)
995 for x in xrange(lim):
1011 for x in xrange(lim):
996 try:
1012 try:
997 y = it.next()
1013 y = it.next()
998 if y in ss:
1014 if y in ss:
999 bs.append(y)
1015 bs.append(y)
1000 except (StopIteration):
1016 except (StopIteration):
1001 break
1017 break
1002 return bs
1018 return bs
1003
1019
1004 def last(repo, subset, x):
1020 def last(repo, subset, x):
1005 """``last(set, [n])``
1021 """``last(set, [n])``
1006 Last n members of set, defaulting to 1.
1022 Last n members of set, defaulting to 1.
1007 """
1023 """
1008 # i18n: "last" is a keyword
1024 # i18n: "last" is a keyword
1009 l = getargs(x, 1, 2, _("last requires one or two arguments"))
1025 l = getargs(x, 1, 2, _("last requires one or two arguments"))
1010 try:
1026 try:
1011 lim = 1
1027 lim = 1
1012 if len(l) == 2:
1028 if len(l) == 2:
1013 # i18n: "last" is a keyword
1029 # i18n: "last" is a keyword
1014 lim = int(getstring(l[1], _("last requires a number")))
1030 lim = int(getstring(l[1], _("last requires a number")))
1015 except (TypeError, ValueError):
1031 except (TypeError, ValueError):
1016 # i18n: "last" is a keyword
1032 # i18n: "last" is a keyword
1017 raise error.ParseError(_("last expects a number"))
1033 raise error.ParseError(_("last expects a number"))
1018 ss = subset.set()
1034 ss = subset.set()
1019 os = getset(repo, spanset(repo), l[0])
1035 os = getset(repo, spanset(repo), l[0])
1020 os.reverse()
1036 os.reverse()
1021 bs = baseset([])
1037 bs = baseset([])
1022 it = iter(os)
1038 it = iter(os)
1023 for x in xrange(lim):
1039 for x in xrange(lim):
1024 try:
1040 try:
1025 y = it.next()
1041 y = it.next()
1026 if y in ss:
1042 if y in ss:
1027 bs.append(y)
1043 bs.append(y)
1028 except (StopIteration):
1044 except (StopIteration):
1029 break
1045 break
1030 return bs
1046 return bs
1031
1047
1032 def maxrev(repo, subset, x):
1048 def maxrev(repo, subset, x):
1033 """``max(set)``
1049 """``max(set)``
1034 Changeset with highest revision number in set.
1050 Changeset with highest revision number in set.
1035 """
1051 """
1036 os = getset(repo, spanset(repo), x)
1052 os = getset(repo, spanset(repo), x)
1037 if os:
1053 if os:
1038 m = os.max()
1054 m = os.max()
1039 if m in subset:
1055 if m in subset:
1040 return baseset([m])
1056 return baseset([m])
1041 return baseset([])
1057 return baseset([])
1042
1058
1043 def merge(repo, subset, x):
1059 def merge(repo, subset, x):
1044 """``merge()``
1060 """``merge()``
1045 Changeset is a merge changeset.
1061 Changeset is a merge changeset.
1046 """
1062 """
1047 # i18n: "merge" is a keyword
1063 # i18n: "merge" is a keyword
1048 getargs(x, 0, 0, _("merge takes no arguments"))
1064 getargs(x, 0, 0, _("merge takes no arguments"))
1049 cl = repo.changelog
1065 cl = repo.changelog
1050 return subset.filter(lambda r: cl.parentrevs(r)[1] != -1)
1066 return subset.filter(lambda r: cl.parentrevs(r)[1] != -1)
1051
1067
1052 def branchpoint(repo, subset, x):
1068 def branchpoint(repo, subset, x):
1053 """``branchpoint()``
1069 """``branchpoint()``
1054 Changesets with more than one child.
1070 Changesets with more than one child.
1055 """
1071 """
1056 # i18n: "branchpoint" is a keyword
1072 # i18n: "branchpoint" is a keyword
1057 getargs(x, 0, 0, _("branchpoint takes no arguments"))
1073 getargs(x, 0, 0, _("branchpoint takes no arguments"))
1058 cl = repo.changelog
1074 cl = repo.changelog
1059 if not subset:
1075 if not subset:
1060 return baseset([])
1076 return baseset([])
1061 baserev = min(subset)
1077 baserev = min(subset)
1062 parentscount = [0]*(len(repo) - baserev)
1078 parentscount = [0]*(len(repo) - baserev)
1063 for r in cl.revs(start=baserev + 1):
1079 for r in cl.revs(start=baserev + 1):
1064 for p in cl.parentrevs(r):
1080 for p in cl.parentrevs(r):
1065 if p >= baserev:
1081 if p >= baserev:
1066 parentscount[p - baserev] += 1
1082 parentscount[p - baserev] += 1
1067 return subset.filter(lambda r: parentscount[r - baserev] > 1)
1083 return subset.filter(lambda r: parentscount[r - baserev] > 1)
1068
1084
1069 def minrev(repo, subset, x):
1085 def minrev(repo, subset, x):
1070 """``min(set)``
1086 """``min(set)``
1071 Changeset with lowest revision number in set.
1087 Changeset with lowest revision number in set.
1072 """
1088 """
1073 os = getset(repo, spanset(repo), x)
1089 os = getset(repo, spanset(repo), x)
1074 if os:
1090 if os:
1075 m = os.min()
1091 m = os.min()
1076 if m in subset:
1092 if m in subset:
1077 return baseset([m])
1093 return baseset([m])
1078 return baseset([])
1094 return baseset([])
1079
1095
1080 def _missingancestors(repo, subset, x):
1096 def _missingancestors(repo, subset, x):
1081 # i18n: "_missingancestors" is a keyword
1097 # i18n: "_missingancestors" is a keyword
1082 revs, bases = getargs(x, 2, 2,
1098 revs, bases = getargs(x, 2, 2,
1083 _("_missingancestors requires two arguments"))
1099 _("_missingancestors requires two arguments"))
1084 rs = baseset(repo)
1100 rs = baseset(repo)
1085 revs = getset(repo, rs, revs)
1101 revs = getset(repo, rs, revs)
1086 bases = getset(repo, rs, bases)
1102 bases = getset(repo, rs, bases)
1087 missing = set(repo.changelog.findmissingrevs(bases, revs))
1103 missing = set(repo.changelog.findmissingrevs(bases, revs))
1088 return baseset([r for r in subset if r in missing])
1104 return baseset([r for r in subset if r in missing])
1089
1105
1090 def modifies(repo, subset, x):
1106 def modifies(repo, subset, x):
1091 """``modifies(pattern)``
1107 """``modifies(pattern)``
1092 Changesets modifying files matched by pattern.
1108 Changesets modifying files matched by pattern.
1093
1109
1094 The pattern without explicit kind like ``glob:`` is expected to be
1110 The pattern without explicit kind like ``glob:`` is expected to be
1095 relative to the current directory and match against a file or a
1111 relative to the current directory and match against a file or a
1096 directory.
1112 directory.
1097 """
1113 """
1098 # i18n: "modifies" is a keyword
1114 # i18n: "modifies" is a keyword
1099 pat = getstring(x, _("modifies requires a pattern"))
1115 pat = getstring(x, _("modifies requires a pattern"))
1100 return checkstatus(repo, subset, pat, 0)
1116 return checkstatus(repo, subset, pat, 0)
1101
1117
1102 def node_(repo, subset, x):
1118 def node_(repo, subset, x):
1103 """``id(string)``
1119 """``id(string)``
1104 Revision non-ambiguously specified by the given hex string prefix.
1120 Revision non-ambiguously specified by the given hex string prefix.
1105 """
1121 """
1106 # i18n: "id" is a keyword
1122 # i18n: "id" is a keyword
1107 l = getargs(x, 1, 1, _("id requires one argument"))
1123 l = getargs(x, 1, 1, _("id requires one argument"))
1108 # i18n: "id" is a keyword
1124 # i18n: "id" is a keyword
1109 n = getstring(l[0], _("id requires a string"))
1125 n = getstring(l[0], _("id requires a string"))
1110 if len(n) == 40:
1126 if len(n) == 40:
1111 rn = repo[n].rev()
1127 rn = repo[n].rev()
1112 else:
1128 else:
1113 rn = None
1129 rn = None
1114 pm = repo.changelog._partialmatch(n)
1130 pm = repo.changelog._partialmatch(n)
1115 if pm is not None:
1131 if pm is not None:
1116 rn = repo.changelog.rev(pm)
1132 rn = repo.changelog.rev(pm)
1117
1133
1118 return subset.filter(lambda r: r == rn)
1134 return subset.filter(lambda r: r == rn)
1119
1135
1120 def obsolete(repo, subset, x):
1136 def obsolete(repo, subset, x):
1121 """``obsolete()``
1137 """``obsolete()``
1122 Mutable changeset with a newer version."""
1138 Mutable changeset with a newer version."""
1123 # i18n: "obsolete" is a keyword
1139 # i18n: "obsolete" is a keyword
1124 getargs(x, 0, 0, _("obsolete takes no arguments"))
1140 getargs(x, 0, 0, _("obsolete takes no arguments"))
1125 obsoletes = obsmod.getrevs(repo, 'obsolete')
1141 obsoletes = obsmod.getrevs(repo, 'obsolete')
1126 return subset & obsoletes
1142 return subset & obsoletes
1127
1143
1128 def origin(repo, subset, x):
1144 def origin(repo, subset, x):
1129 """``origin([set])``
1145 """``origin([set])``
1130 Changesets that were specified as a source for the grafts, transplants or
1146 Changesets that were specified as a source for the grafts, transplants or
1131 rebases that created the given revisions. Omitting the optional set is the
1147 rebases that created the given revisions. Omitting the optional set is the
1132 same as passing all(). If a changeset created by these operations is itself
1148 same as passing all(). If a changeset created by these operations is itself
1133 specified as a source for one of these operations, only the source changeset
1149 specified as a source for one of these operations, only the source changeset
1134 for the first operation is selected.
1150 for the first operation is selected.
1135 """
1151 """
1136 if x is not None:
1152 if x is not None:
1137 args = getset(repo, spanset(repo), x).set()
1153 args = getset(repo, spanset(repo), x).set()
1138 else:
1154 else:
1139 args = getall(repo, spanset(repo), x).set()
1155 args = getall(repo, spanset(repo), x).set()
1140
1156
1141 def _firstsrc(rev):
1157 def _firstsrc(rev):
1142 src = _getrevsource(repo, rev)
1158 src = _getrevsource(repo, rev)
1143 if src is None:
1159 if src is None:
1144 return None
1160 return None
1145
1161
1146 while True:
1162 while True:
1147 prev = _getrevsource(repo, src)
1163 prev = _getrevsource(repo, src)
1148
1164
1149 if prev is None:
1165 if prev is None:
1150 return src
1166 return src
1151 src = prev
1167 src = prev
1152
1168
1153 o = set([_firstsrc(r) for r in args])
1169 o = set([_firstsrc(r) for r in args])
1154 return subset.filter(lambda r: r in o)
1170 return subset.filter(lambda r: r in o)
1155
1171
1156 def outgoing(repo, subset, x):
1172 def outgoing(repo, subset, x):
1157 """``outgoing([path])``
1173 """``outgoing([path])``
1158 Changesets not found in the specified destination repository, or the
1174 Changesets not found in the specified destination repository, or the
1159 default push location.
1175 default push location.
1160 """
1176 """
1161 import hg # avoid start-up nasties
1177 import hg # avoid start-up nasties
1162 # i18n: "outgoing" is a keyword
1178 # i18n: "outgoing" is a keyword
1163 l = getargs(x, 0, 1, _("outgoing takes one or no arguments"))
1179 l = getargs(x, 0, 1, _("outgoing takes one or no arguments"))
1164 # i18n: "outgoing" is a keyword
1180 # i18n: "outgoing" is a keyword
1165 dest = l and getstring(l[0], _("outgoing requires a repository path")) or ''
1181 dest = l and getstring(l[0], _("outgoing requires a repository path")) or ''
1166 dest = repo.ui.expandpath(dest or 'default-push', dest or 'default')
1182 dest = repo.ui.expandpath(dest or 'default-push', dest or 'default')
1167 dest, branches = hg.parseurl(dest)
1183 dest, branches = hg.parseurl(dest)
1168 revs, checkout = hg.addbranchrevs(repo, repo, branches, [])
1184 revs, checkout = hg.addbranchrevs(repo, repo, branches, [])
1169 if revs:
1185 if revs:
1170 revs = [repo.lookup(rev) for rev in revs]
1186 revs = [repo.lookup(rev) for rev in revs]
1171 other = hg.peer(repo, {}, dest)
1187 other = hg.peer(repo, {}, dest)
1172 repo.ui.pushbuffer()
1188 repo.ui.pushbuffer()
1173 outgoing = discovery.findcommonoutgoing(repo, other, onlyheads=revs)
1189 outgoing = discovery.findcommonoutgoing(repo, other, onlyheads=revs)
1174 repo.ui.popbuffer()
1190 repo.ui.popbuffer()
1175 cl = repo.changelog
1191 cl = repo.changelog
1176 o = set([cl.rev(r) for r in outgoing.missing])
1192 o = set([cl.rev(r) for r in outgoing.missing])
1177 return subset.filter(lambda r: r in o)
1193 return subset.filter(lambda r: r in o)
1178
1194
1179 def p1(repo, subset, x):
1195 def p1(repo, subset, x):
1180 """``p1([set])``
1196 """``p1([set])``
1181 First parent of changesets in set, or the working directory.
1197 First parent of changesets in set, or the working directory.
1182 """
1198 """
1183 if x is None:
1199 if x is None:
1184 p = repo[x].p1().rev()
1200 p = repo[x].p1().rev()
1185 return subset.filter(lambda r: r == p)
1201 return subset.filter(lambda r: r == p)
1186
1202
1187 ps = set()
1203 ps = set()
1188 cl = repo.changelog
1204 cl = repo.changelog
1189 for r in getset(repo, spanset(repo), x):
1205 for r in getset(repo, spanset(repo), x):
1190 ps.add(cl.parentrevs(r)[0])
1206 ps.add(cl.parentrevs(r)[0])
1191 return subset & ps
1207 return subset & ps
1192
1208
1193 def p2(repo, subset, x):
1209 def p2(repo, subset, x):
1194 """``p2([set])``
1210 """``p2([set])``
1195 Second parent of changesets in set, or the working directory.
1211 Second parent of changesets in set, or the working directory.
1196 """
1212 """
1197 if x is None:
1213 if x is None:
1198 ps = repo[x].parents()
1214 ps = repo[x].parents()
1199 try:
1215 try:
1200 p = ps[1].rev()
1216 p = ps[1].rev()
1201 return subset.filter(lambda r: r == p)
1217 return subset.filter(lambda r: r == p)
1202 except IndexError:
1218 except IndexError:
1203 return baseset([])
1219 return baseset([])
1204
1220
1205 ps = set()
1221 ps = set()
1206 cl = repo.changelog
1222 cl = repo.changelog
1207 for r in getset(repo, spanset(repo), x):
1223 for r in getset(repo, spanset(repo), x):
1208 ps.add(cl.parentrevs(r)[1])
1224 ps.add(cl.parentrevs(r)[1])
1209 return subset & ps
1225 return subset & ps
1210
1226
1211 def parents(repo, subset, x):
1227 def parents(repo, subset, x):
1212 """``parents([set])``
1228 """``parents([set])``
1213 The set of all parents for all changesets in set, or the working directory.
1229 The set of all parents for all changesets in set, or the working directory.
1214 """
1230 """
1215 if x is None:
1231 if x is None:
1216 ps = tuple(p.rev() for p in repo[x].parents())
1232 ps = tuple(p.rev() for p in repo[x].parents())
1217 return subset & ps
1233 return subset & ps
1218
1234
1219 ps = set()
1235 ps = set()
1220 cl = repo.changelog
1236 cl = repo.changelog
1221 for r in getset(repo, spanset(repo), x):
1237 for r in getset(repo, spanset(repo), x):
1222 ps.update(cl.parentrevs(r))
1238 ps.update(cl.parentrevs(r))
1223 return subset & ps
1239 return subset & ps
1224
1240
1225 def parentspec(repo, subset, x, n):
1241 def parentspec(repo, subset, x, n):
1226 """``set^0``
1242 """``set^0``
1227 The set.
1243 The set.
1228 ``set^1`` (or ``set^``), ``set^2``
1244 ``set^1`` (or ``set^``), ``set^2``
1229 First or second parent, respectively, of all changesets in set.
1245 First or second parent, respectively, of all changesets in set.
1230 """
1246 """
1231 try:
1247 try:
1232 n = int(n[1])
1248 n = int(n[1])
1233 if n not in (0, 1, 2):
1249 if n not in (0, 1, 2):
1234 raise ValueError
1250 raise ValueError
1235 except (TypeError, ValueError):
1251 except (TypeError, ValueError):
1236 raise error.ParseError(_("^ expects a number 0, 1, or 2"))
1252 raise error.ParseError(_("^ expects a number 0, 1, or 2"))
1237 ps = set()
1253 ps = set()
1238 cl = repo.changelog
1254 cl = repo.changelog
1239 for r in getset(repo, baseset(cl), x):
1255 for r in getset(repo, baseset(cl), x):
1240 if n == 0:
1256 if n == 0:
1241 ps.add(r)
1257 ps.add(r)
1242 elif n == 1:
1258 elif n == 1:
1243 ps.add(cl.parentrevs(r)[0])
1259 ps.add(cl.parentrevs(r)[0])
1244 elif n == 2:
1260 elif n == 2:
1245 parents = cl.parentrevs(r)
1261 parents = cl.parentrevs(r)
1246 if len(parents) > 1:
1262 if len(parents) > 1:
1247 ps.add(parents[1])
1263 ps.add(parents[1])
1248 return subset & ps
1264 return subset & ps
1249
1265
1250 def present(repo, subset, x):
1266 def present(repo, subset, x):
1251 """``present(set)``
1267 """``present(set)``
1252 An empty set, if any revision in set isn't found; otherwise,
1268 An empty set, if any revision in set isn't found; otherwise,
1253 all revisions in set.
1269 all revisions in set.
1254
1270
1255 If any of specified revisions is not present in the local repository,
1271 If any of specified revisions is not present in the local repository,
1256 the query is normally aborted. But this predicate allows the query
1272 the query is normally aborted. But this predicate allows the query
1257 to continue even in such cases.
1273 to continue even in such cases.
1258 """
1274 """
1259 try:
1275 try:
1260 return getset(repo, subset, x)
1276 return getset(repo, subset, x)
1261 except error.RepoLookupError:
1277 except error.RepoLookupError:
1262 return baseset([])
1278 return baseset([])
1263
1279
1264 def public(repo, subset, x):
1280 def public(repo, subset, x):
1265 """``public()``
1281 """``public()``
1266 Changeset in public phase."""
1282 Changeset in public phase."""
1267 # i18n: "public" is a keyword
1283 # i18n: "public" is a keyword
1268 getargs(x, 0, 0, _("public takes no arguments"))
1284 getargs(x, 0, 0, _("public takes no arguments"))
1269 pc = repo._phasecache
1285 pc = repo._phasecache
1270 return subset.filter(lambda r: pc.phase(repo, r) == phases.public)
1286 return subset.filter(lambda r: pc.phase(repo, r) == phases.public)
1271
1287
1272 def remote(repo, subset, x):
1288 def remote(repo, subset, x):
1273 """``remote([id [,path]])``
1289 """``remote([id [,path]])``
1274 Local revision that corresponds to the given identifier in a
1290 Local revision that corresponds to the given identifier in a
1275 remote repository, if present. Here, the '.' identifier is a
1291 remote repository, if present. Here, the '.' identifier is a
1276 synonym for the current local branch.
1292 synonym for the current local branch.
1277 """
1293 """
1278
1294
1279 import hg # avoid start-up nasties
1295 import hg # avoid start-up nasties
1280 # i18n: "remote" is a keyword
1296 # i18n: "remote" is a keyword
1281 l = getargs(x, 0, 2, _("remote takes one, two or no arguments"))
1297 l = getargs(x, 0, 2, _("remote takes one, two or no arguments"))
1282
1298
1283 q = '.'
1299 q = '.'
1284 if len(l) > 0:
1300 if len(l) > 0:
1285 # i18n: "remote" is a keyword
1301 # i18n: "remote" is a keyword
1286 q = getstring(l[0], _("remote requires a string id"))
1302 q = getstring(l[0], _("remote requires a string id"))
1287 if q == '.':
1303 if q == '.':
1288 q = repo['.'].branch()
1304 q = repo['.'].branch()
1289
1305
1290 dest = ''
1306 dest = ''
1291 if len(l) > 1:
1307 if len(l) > 1:
1292 # i18n: "remote" is a keyword
1308 # i18n: "remote" is a keyword
1293 dest = getstring(l[1], _("remote requires a repository path"))
1309 dest = getstring(l[1], _("remote requires a repository path"))
1294 dest = repo.ui.expandpath(dest or 'default')
1310 dest = repo.ui.expandpath(dest or 'default')
1295 dest, branches = hg.parseurl(dest)
1311 dest, branches = hg.parseurl(dest)
1296 revs, checkout = hg.addbranchrevs(repo, repo, branches, [])
1312 revs, checkout = hg.addbranchrevs(repo, repo, branches, [])
1297 if revs:
1313 if revs:
1298 revs = [repo.lookup(rev) for rev in revs]
1314 revs = [repo.lookup(rev) for rev in revs]
1299 other = hg.peer(repo, {}, dest)
1315 other = hg.peer(repo, {}, dest)
1300 n = other.lookup(q)
1316 n = other.lookup(q)
1301 if n in repo:
1317 if n in repo:
1302 r = repo[n].rev()
1318 r = repo[n].rev()
1303 if r in subset:
1319 if r in subset:
1304 return baseset([r])
1320 return baseset([r])
1305 return baseset([])
1321 return baseset([])
1306
1322
1307 def removes(repo, subset, x):
1323 def removes(repo, subset, x):
1308 """``removes(pattern)``
1324 """``removes(pattern)``
1309 Changesets which remove files matching pattern.
1325 Changesets which remove files matching pattern.
1310
1326
1311 The pattern without explicit kind like ``glob:`` is expected to be
1327 The pattern without explicit kind like ``glob:`` is expected to be
1312 relative to the current directory and match against a file or a
1328 relative to the current directory and match against a file or a
1313 directory.
1329 directory.
1314 """
1330 """
1315 # i18n: "removes" is a keyword
1331 # i18n: "removes" is a keyword
1316 pat = getstring(x, _("removes requires a pattern"))
1332 pat = getstring(x, _("removes requires a pattern"))
1317 return checkstatus(repo, subset, pat, 2)
1333 return checkstatus(repo, subset, pat, 2)
1318
1334
1319 def rev(repo, subset, x):
1335 def rev(repo, subset, x):
1320 """``rev(number)``
1336 """``rev(number)``
1321 Revision with the given numeric identifier.
1337 Revision with the given numeric identifier.
1322 """
1338 """
1323 # i18n: "rev" is a keyword
1339 # i18n: "rev" is a keyword
1324 l = getargs(x, 1, 1, _("rev requires one argument"))
1340 l = getargs(x, 1, 1, _("rev requires one argument"))
1325 try:
1341 try:
1326 # i18n: "rev" is a keyword
1342 # i18n: "rev" is a keyword
1327 l = int(getstring(l[0], _("rev requires a number")))
1343 l = int(getstring(l[0], _("rev requires a number")))
1328 except (TypeError, ValueError):
1344 except (TypeError, ValueError):
1329 # i18n: "rev" is a keyword
1345 # i18n: "rev" is a keyword
1330 raise error.ParseError(_("rev expects a number"))
1346 raise error.ParseError(_("rev expects a number"))
1331 return subset.filter(lambda r: r == l)
1347 return subset.filter(lambda r: r == l)
1332
1348
1333 def matching(repo, subset, x):
1349 def matching(repo, subset, x):
1334 """``matching(revision [, field])``
1350 """``matching(revision [, field])``
1335 Changesets in which a given set of fields match the set of fields in the
1351 Changesets in which a given set of fields match the set of fields in the
1336 selected revision or set.
1352 selected revision or set.
1337
1353
1338 To match more than one field pass the list of fields to match separated
1354 To match more than one field pass the list of fields to match separated
1339 by spaces (e.g. ``author description``).
1355 by spaces (e.g. ``author description``).
1340
1356
1341 Valid fields are most regular revision fields and some special fields.
1357 Valid fields are most regular revision fields and some special fields.
1342
1358
1343 Regular revision fields are ``description``, ``author``, ``branch``,
1359 Regular revision fields are ``description``, ``author``, ``branch``,
1344 ``date``, ``files``, ``phase``, ``parents``, ``substate``, ``user``
1360 ``date``, ``files``, ``phase``, ``parents``, ``substate``, ``user``
1345 and ``diff``.
1361 and ``diff``.
1346 Note that ``author`` and ``user`` are synonyms. ``diff`` refers to the
1362 Note that ``author`` and ``user`` are synonyms. ``diff`` refers to the
1347 contents of the revision. Two revisions matching their ``diff`` will
1363 contents of the revision. Two revisions matching their ``diff`` will
1348 also match their ``files``.
1364 also match their ``files``.
1349
1365
1350 Special fields are ``summary`` and ``metadata``:
1366 Special fields are ``summary`` and ``metadata``:
1351 ``summary`` matches the first line of the description.
1367 ``summary`` matches the first line of the description.
1352 ``metadata`` is equivalent to matching ``description user date``
1368 ``metadata`` is equivalent to matching ``description user date``
1353 (i.e. it matches the main metadata fields).
1369 (i.e. it matches the main metadata fields).
1354
1370
1355 ``metadata`` is the default field which is used when no fields are
1371 ``metadata`` is the default field which is used when no fields are
1356 specified. You can match more than one field at a time.
1372 specified. You can match more than one field at a time.
1357 """
1373 """
1358 # i18n: "matching" is a keyword
1374 # i18n: "matching" is a keyword
1359 l = getargs(x, 1, 2, _("matching takes 1 or 2 arguments"))
1375 l = getargs(x, 1, 2, _("matching takes 1 or 2 arguments"))
1360
1376
1361 revs = getset(repo, baseset(repo.changelog), l[0])
1377 revs = getset(repo, baseset(repo.changelog), l[0])
1362
1378
1363 fieldlist = ['metadata']
1379 fieldlist = ['metadata']
1364 if len(l) > 1:
1380 if len(l) > 1:
1365 fieldlist = getstring(l[1],
1381 fieldlist = getstring(l[1],
1366 # i18n: "matching" is a keyword
1382 # i18n: "matching" is a keyword
1367 _("matching requires a string "
1383 _("matching requires a string "
1368 "as its second argument")).split()
1384 "as its second argument")).split()
1369
1385
1370 # Make sure that there are no repeated fields,
1386 # Make sure that there are no repeated fields,
1371 # expand the 'special' 'metadata' field type
1387 # expand the 'special' 'metadata' field type
1372 # and check the 'files' whenever we check the 'diff'
1388 # and check the 'files' whenever we check the 'diff'
1373 fields = []
1389 fields = []
1374 for field in fieldlist:
1390 for field in fieldlist:
1375 if field == 'metadata':
1391 if field == 'metadata':
1376 fields += ['user', 'description', 'date']
1392 fields += ['user', 'description', 'date']
1377 elif field == 'diff':
1393 elif field == 'diff':
1378 # a revision matching the diff must also match the files
1394 # a revision matching the diff must also match the files
1379 # since matching the diff is very costly, make sure to
1395 # since matching the diff is very costly, make sure to
1380 # also match the files first
1396 # also match the files first
1381 fields += ['files', 'diff']
1397 fields += ['files', 'diff']
1382 else:
1398 else:
1383 if field == 'author':
1399 if field == 'author':
1384 field = 'user'
1400 field = 'user'
1385 fields.append(field)
1401 fields.append(field)
1386 fields = set(fields)
1402 fields = set(fields)
1387 if 'summary' in fields and 'description' in fields:
1403 if 'summary' in fields and 'description' in fields:
1388 # If a revision matches its description it also matches its summary
1404 # If a revision matches its description it also matches its summary
1389 fields.discard('summary')
1405 fields.discard('summary')
1390
1406
1391 # We may want to match more than one field
1407 # We may want to match more than one field
1392 # Not all fields take the same amount of time to be matched
1408 # Not all fields take the same amount of time to be matched
1393 # Sort the selected fields in order of increasing matching cost
1409 # Sort the selected fields in order of increasing matching cost
1394 fieldorder = ['phase', 'parents', 'user', 'date', 'branch', 'summary',
1410 fieldorder = ['phase', 'parents', 'user', 'date', 'branch', 'summary',
1395 'files', 'description', 'substate', 'diff']
1411 'files', 'description', 'substate', 'diff']
1396 def fieldkeyfunc(f):
1412 def fieldkeyfunc(f):
1397 try:
1413 try:
1398 return fieldorder.index(f)
1414 return fieldorder.index(f)
1399 except ValueError:
1415 except ValueError:
1400 # assume an unknown field is very costly
1416 # assume an unknown field is very costly
1401 return len(fieldorder)
1417 return len(fieldorder)
1402 fields = list(fields)
1418 fields = list(fields)
1403 fields.sort(key=fieldkeyfunc)
1419 fields.sort(key=fieldkeyfunc)
1404
1420
1405 # Each field will be matched with its own "getfield" function
1421 # Each field will be matched with its own "getfield" function
1406 # which will be added to the getfieldfuncs array of functions
1422 # which will be added to the getfieldfuncs array of functions
1407 getfieldfuncs = []
1423 getfieldfuncs = []
1408 _funcs = {
1424 _funcs = {
1409 'user': lambda r: repo[r].user(),
1425 'user': lambda r: repo[r].user(),
1410 'branch': lambda r: repo[r].branch(),
1426 'branch': lambda r: repo[r].branch(),
1411 'date': lambda r: repo[r].date(),
1427 'date': lambda r: repo[r].date(),
1412 'description': lambda r: repo[r].description(),
1428 'description': lambda r: repo[r].description(),
1413 'files': lambda r: repo[r].files(),
1429 'files': lambda r: repo[r].files(),
1414 'parents': lambda r: repo[r].parents(),
1430 'parents': lambda r: repo[r].parents(),
1415 'phase': lambda r: repo[r].phase(),
1431 'phase': lambda r: repo[r].phase(),
1416 'substate': lambda r: repo[r].substate,
1432 'substate': lambda r: repo[r].substate,
1417 'summary': lambda r: repo[r].description().splitlines()[0],
1433 'summary': lambda r: repo[r].description().splitlines()[0],
1418 'diff': lambda r: list(repo[r].diff(git=True),)
1434 'diff': lambda r: list(repo[r].diff(git=True),)
1419 }
1435 }
1420 for info in fields:
1436 for info in fields:
1421 getfield = _funcs.get(info, None)
1437 getfield = _funcs.get(info, None)
1422 if getfield is None:
1438 if getfield is None:
1423 raise error.ParseError(
1439 raise error.ParseError(
1424 # i18n: "matching" is a keyword
1440 # i18n: "matching" is a keyword
1425 _("unexpected field name passed to matching: %s") % info)
1441 _("unexpected field name passed to matching: %s") % info)
1426 getfieldfuncs.append(getfield)
1442 getfieldfuncs.append(getfield)
1427 # convert the getfield array of functions into a "getinfo" function
1443 # convert the getfield array of functions into a "getinfo" function
1428 # which returns an array of field values (or a single value if there
1444 # which returns an array of field values (or a single value if there
1429 # is only one field to match)
1445 # is only one field to match)
1430 getinfo = lambda r: [f(r) for f in getfieldfuncs]
1446 getinfo = lambda r: [f(r) for f in getfieldfuncs]
1431
1447
1432 def matches(x):
1448 def matches(x):
1433 for rev in revs:
1449 for rev in revs:
1434 target = getinfo(rev)
1450 target = getinfo(rev)
1435 match = True
1451 match = True
1436 for n, f in enumerate(getfieldfuncs):
1452 for n, f in enumerate(getfieldfuncs):
1437 if target[n] != f(x):
1453 if target[n] != f(x):
1438 match = False
1454 match = False
1439 if match:
1455 if match:
1440 return True
1456 return True
1441 return False
1457 return False
1442
1458
1443 return subset.filter(matches)
1459 return subset.filter(matches)
1444
1460
1445 def reverse(repo, subset, x):
1461 def reverse(repo, subset, x):
1446 """``reverse(set)``
1462 """``reverse(set)``
1447 Reverse order of set.
1463 Reverse order of set.
1448 """
1464 """
1449 l = getset(repo, subset, x)
1465 l = getset(repo, subset, x)
1450 l.reverse()
1466 l.reverse()
1451 return l
1467 return l
1452
1468
1453 def roots(repo, subset, x):
1469 def roots(repo, subset, x):
1454 """``roots(set)``
1470 """``roots(set)``
1455 Changesets in set with no parent changeset in set.
1471 Changesets in set with no parent changeset in set.
1456 """
1472 """
1457 s = getset(repo, baseset(repo.changelog), x).set()
1473 s = getset(repo, baseset(repo.changelog), x).set()
1458 subset = baseset([r for r in subset if r in s])
1474 subset = baseset([r for r in subset if r in s])
1459 cs = _children(repo, subset, s)
1475 cs = _children(repo, subset, s)
1460 return subset - cs
1476 return subset - cs
1461
1477
1462 def secret(repo, subset, x):
1478 def secret(repo, subset, x):
1463 """``secret()``
1479 """``secret()``
1464 Changeset in secret phase."""
1480 Changeset in secret phase."""
1465 # i18n: "secret" is a keyword
1481 # i18n: "secret" is a keyword
1466 getargs(x, 0, 0, _("secret takes no arguments"))
1482 getargs(x, 0, 0, _("secret takes no arguments"))
1467 pc = repo._phasecache
1483 pc = repo._phasecache
1468 return subset.filter(lambda x: pc.phase(repo, x) == phases.secret)
1484 return subset.filter(lambda x: pc.phase(repo, x) == phases.secret)
1469
1485
1470 def sort(repo, subset, x):
1486 def sort(repo, subset, x):
1471 """``sort(set[, [-]key...])``
1487 """``sort(set[, [-]key...])``
1472 Sort set by keys. The default sort order is ascending, specify a key
1488 Sort set by keys. The default sort order is ascending, specify a key
1473 as ``-key`` to sort in descending order.
1489 as ``-key`` to sort in descending order.
1474
1490
1475 The keys can be:
1491 The keys can be:
1476
1492
1477 - ``rev`` for the revision number,
1493 - ``rev`` for the revision number,
1478 - ``branch`` for the branch name,
1494 - ``branch`` for the branch name,
1479 - ``desc`` for the commit message (description),
1495 - ``desc`` for the commit message (description),
1480 - ``user`` for user name (``author`` can be used as an alias),
1496 - ``user`` for user name (``author`` can be used as an alias),
1481 - ``date`` for the commit date
1497 - ``date`` for the commit date
1482 """
1498 """
1483 # i18n: "sort" is a keyword
1499 # i18n: "sort" is a keyword
1484 l = getargs(x, 1, 2, _("sort requires one or two arguments"))
1500 l = getargs(x, 1, 2, _("sort requires one or two arguments"))
1485 keys = "rev"
1501 keys = "rev"
1486 if len(l) == 2:
1502 if len(l) == 2:
1487 # i18n: "sort" is a keyword
1503 # i18n: "sort" is a keyword
1488 keys = getstring(l[1], _("sort spec must be a string"))
1504 keys = getstring(l[1], _("sort spec must be a string"))
1489
1505
1490 s = l[0]
1506 s = l[0]
1491 keys = keys.split()
1507 keys = keys.split()
1492 l = []
1508 l = []
1493 def invert(s):
1509 def invert(s):
1494 return "".join(chr(255 - ord(c)) for c in s)
1510 return "".join(chr(255 - ord(c)) for c in s)
1495 revs = getset(repo, subset, s)
1511 revs = getset(repo, subset, s)
1496 if keys == ["rev"]:
1512 if keys == ["rev"]:
1497 revs.sort()
1513 revs.sort()
1498 return revs
1514 return revs
1499 elif keys == ["-rev"]:
1515 elif keys == ["-rev"]:
1500 revs.sort(reverse=True)
1516 revs.sort(reverse=True)
1501 return revs
1517 return revs
1502 for r in revs:
1518 for r in revs:
1503 c = repo[r]
1519 c = repo[r]
1504 e = []
1520 e = []
1505 for k in keys:
1521 for k in keys:
1506 if k == 'rev':
1522 if k == 'rev':
1507 e.append(r)
1523 e.append(r)
1508 elif k == '-rev':
1524 elif k == '-rev':
1509 e.append(-r)
1525 e.append(-r)
1510 elif k == 'branch':
1526 elif k == 'branch':
1511 e.append(c.branch())
1527 e.append(c.branch())
1512 elif k == '-branch':
1528 elif k == '-branch':
1513 e.append(invert(c.branch()))
1529 e.append(invert(c.branch()))
1514 elif k == 'desc':
1530 elif k == 'desc':
1515 e.append(c.description())
1531 e.append(c.description())
1516 elif k == '-desc':
1532 elif k == '-desc':
1517 e.append(invert(c.description()))
1533 e.append(invert(c.description()))
1518 elif k in 'user author':
1534 elif k in 'user author':
1519 e.append(c.user())
1535 e.append(c.user())
1520 elif k in '-user -author':
1536 elif k in '-user -author':
1521 e.append(invert(c.user()))
1537 e.append(invert(c.user()))
1522 elif k == 'date':
1538 elif k == 'date':
1523 e.append(c.date()[0])
1539 e.append(c.date()[0])
1524 elif k == '-date':
1540 elif k == '-date':
1525 e.append(-c.date()[0])
1541 e.append(-c.date()[0])
1526 else:
1542 else:
1527 raise error.ParseError(_("unknown sort key %r") % k)
1543 raise error.ParseError(_("unknown sort key %r") % k)
1528 e.append(r)
1544 e.append(r)
1529 l.append(e)
1545 l.append(e)
1530 l.sort()
1546 l.sort()
1531 return baseset([e[-1] for e in l])
1547 return baseset([e[-1] for e in l])
1532
1548
1533 def _stringmatcher(pattern):
1549 def _stringmatcher(pattern):
1534 """
1550 """
1535 accepts a string, possibly starting with 're:' or 'literal:' prefix.
1551 accepts a string, possibly starting with 're:' or 'literal:' prefix.
1536 returns the matcher name, pattern, and matcher function.
1552 returns the matcher name, pattern, and matcher function.
1537 missing or unknown prefixes are treated as literal matches.
1553 missing or unknown prefixes are treated as literal matches.
1538
1554
1539 helper for tests:
1555 helper for tests:
1540 >>> def test(pattern, *tests):
1556 >>> def test(pattern, *tests):
1541 ... kind, pattern, matcher = _stringmatcher(pattern)
1557 ... kind, pattern, matcher = _stringmatcher(pattern)
1542 ... return (kind, pattern, [bool(matcher(t)) for t in tests])
1558 ... return (kind, pattern, [bool(matcher(t)) for t in tests])
1543
1559
1544 exact matching (no prefix):
1560 exact matching (no prefix):
1545 >>> test('abcdefg', 'abc', 'def', 'abcdefg')
1561 >>> test('abcdefg', 'abc', 'def', 'abcdefg')
1546 ('literal', 'abcdefg', [False, False, True])
1562 ('literal', 'abcdefg', [False, False, True])
1547
1563
1548 regex matching ('re:' prefix)
1564 regex matching ('re:' prefix)
1549 >>> test('re:a.+b', 'nomatch', 'fooadef', 'fooadefbar')
1565 >>> test('re:a.+b', 'nomatch', 'fooadef', 'fooadefbar')
1550 ('re', 'a.+b', [False, False, True])
1566 ('re', 'a.+b', [False, False, True])
1551
1567
1552 force exact matches ('literal:' prefix)
1568 force exact matches ('literal:' prefix)
1553 >>> test('literal:re:foobar', 'foobar', 're:foobar')
1569 >>> test('literal:re:foobar', 'foobar', 're:foobar')
1554 ('literal', 're:foobar', [False, True])
1570 ('literal', 're:foobar', [False, True])
1555
1571
1556 unknown prefixes are ignored and treated as literals
1572 unknown prefixes are ignored and treated as literals
1557 >>> test('foo:bar', 'foo', 'bar', 'foo:bar')
1573 >>> test('foo:bar', 'foo', 'bar', 'foo:bar')
1558 ('literal', 'foo:bar', [False, False, True])
1574 ('literal', 'foo:bar', [False, False, True])
1559 """
1575 """
1560 if pattern.startswith('re:'):
1576 if pattern.startswith('re:'):
1561 pattern = pattern[3:]
1577 pattern = pattern[3:]
1562 try:
1578 try:
1563 regex = re.compile(pattern)
1579 regex = re.compile(pattern)
1564 except re.error, e:
1580 except re.error, e:
1565 raise error.ParseError(_('invalid regular expression: %s')
1581 raise error.ParseError(_('invalid regular expression: %s')
1566 % e)
1582 % e)
1567 return 're', pattern, regex.search
1583 return 're', pattern, regex.search
1568 elif pattern.startswith('literal:'):
1584 elif pattern.startswith('literal:'):
1569 pattern = pattern[8:]
1585 pattern = pattern[8:]
1570 return 'literal', pattern, pattern.__eq__
1586 return 'literal', pattern, pattern.__eq__
1571
1587
1572 def _substringmatcher(pattern):
1588 def _substringmatcher(pattern):
1573 kind, pattern, matcher = _stringmatcher(pattern)
1589 kind, pattern, matcher = _stringmatcher(pattern)
1574 if kind == 'literal':
1590 if kind == 'literal':
1575 matcher = lambda s: pattern in s
1591 matcher = lambda s: pattern in s
1576 return kind, pattern, matcher
1592 return kind, pattern, matcher
1577
1593
1578 def tag(repo, subset, x):
1594 def tag(repo, subset, x):
1579 """``tag([name])``
1595 """``tag([name])``
1580 The specified tag by name, or all tagged revisions if no name is given.
1596 The specified tag by name, or all tagged revisions if no name is given.
1581 """
1597 """
1582 # i18n: "tag" is a keyword
1598 # i18n: "tag" is a keyword
1583 args = getargs(x, 0, 1, _("tag takes one or no arguments"))
1599 args = getargs(x, 0, 1, _("tag takes one or no arguments"))
1584 cl = repo.changelog
1600 cl = repo.changelog
1585 if args:
1601 if args:
1586 pattern = getstring(args[0],
1602 pattern = getstring(args[0],
1587 # i18n: "tag" is a keyword
1603 # i18n: "tag" is a keyword
1588 _('the argument to tag must be a string'))
1604 _('the argument to tag must be a string'))
1589 kind, pattern, matcher = _stringmatcher(pattern)
1605 kind, pattern, matcher = _stringmatcher(pattern)
1590 if kind == 'literal':
1606 if kind == 'literal':
1591 # avoid resolving all tags
1607 # avoid resolving all tags
1592 tn = repo._tagscache.tags.get(pattern, None)
1608 tn = repo._tagscache.tags.get(pattern, None)
1593 if tn is None:
1609 if tn is None:
1594 raise util.Abort(_("tag '%s' does not exist") % pattern)
1610 raise util.Abort(_("tag '%s' does not exist") % pattern)
1595 s = set([repo[tn].rev()])
1611 s = set([repo[tn].rev()])
1596 else:
1612 else:
1597 s = set([cl.rev(n) for t, n in repo.tagslist() if matcher(t)])
1613 s = set([cl.rev(n) for t, n in repo.tagslist() if matcher(t)])
1598 else:
1614 else:
1599 s = set([cl.rev(n) for t, n in repo.tagslist() if t != 'tip'])
1615 s = set([cl.rev(n) for t, n in repo.tagslist() if t != 'tip'])
1600 return subset & s
1616 return subset & s
1601
1617
1602 def tagged(repo, subset, x):
1618 def tagged(repo, subset, x):
1603 return tag(repo, subset, x)
1619 return tag(repo, subset, x)
1604
1620
1605 def unstable(repo, subset, x):
1621 def unstable(repo, subset, x):
1606 """``unstable()``
1622 """``unstable()``
1607 Non-obsolete changesets with obsolete ancestors.
1623 Non-obsolete changesets with obsolete ancestors.
1608 """
1624 """
1609 # i18n: "unstable" is a keyword
1625 # i18n: "unstable" is a keyword
1610 getargs(x, 0, 0, _("unstable takes no arguments"))
1626 getargs(x, 0, 0, _("unstable takes no arguments"))
1611 unstables = obsmod.getrevs(repo, 'unstable')
1627 unstables = obsmod.getrevs(repo, 'unstable')
1612 return subset & unstables
1628 return subset & unstables
1613
1629
1614
1630
1615 def user(repo, subset, x):
1631 def user(repo, subset, x):
1616 """``user(string)``
1632 """``user(string)``
1617 User name contains string. The match is case-insensitive.
1633 User name contains string. The match is case-insensitive.
1618
1634
1619 If `string` starts with `re:`, the remainder of the string is treated as
1635 If `string` starts with `re:`, the remainder of the string is treated as
1620 a regular expression. To match a user that actually contains `re:`, use
1636 a regular expression. To match a user that actually contains `re:`, use
1621 the prefix `literal:`.
1637 the prefix `literal:`.
1622 """
1638 """
1623 return author(repo, subset, x)
1639 return author(repo, subset, x)
1624
1640
1625 # for internal use
1641 # for internal use
1626 def _list(repo, subset, x):
1642 def _list(repo, subset, x):
1627 s = getstring(x, "internal error")
1643 s = getstring(x, "internal error")
1628 if not s:
1644 if not s:
1629 return baseset([])
1645 return baseset([])
1630 ls = [repo[r].rev() for r in s.split('\0')]
1646 ls = [repo[r].rev() for r in s.split('\0')]
1631 s = subset.set()
1647 s = subset.set()
1632 return baseset([r for r in ls if r in s])
1648 return baseset([r for r in ls if r in s])
1633
1649
1634 # for internal use
1650 # for internal use
1635 def _intlist(repo, subset, x):
1651 def _intlist(repo, subset, x):
1636 s = getstring(x, "internal error")
1652 s = getstring(x, "internal error")
1637 if not s:
1653 if not s:
1638 return baseset([])
1654 return baseset([])
1639 ls = [int(r) for r in s.split('\0')]
1655 ls = [int(r) for r in s.split('\0')]
1640 s = subset.set()
1656 s = subset.set()
1641 return baseset([r for r in ls if r in s])
1657 return baseset([r for r in ls if r in s])
1642
1658
1643 # for internal use
1659 # for internal use
1644 def _hexlist(repo, subset, x):
1660 def _hexlist(repo, subset, x):
1645 s = getstring(x, "internal error")
1661 s = getstring(x, "internal error")
1646 if not s:
1662 if not s:
1647 return baseset([])
1663 return baseset([])
1648 cl = repo.changelog
1664 cl = repo.changelog
1649 ls = [cl.rev(node.bin(r)) for r in s.split('\0')]
1665 ls = [cl.rev(node.bin(r)) for r in s.split('\0')]
1650 s = subset.set()
1666 s = subset.set()
1651 return baseset([r for r in ls if r in s])
1667 return baseset([r for r in ls if r in s])
1652
1668
1653 symbols = {
1669 symbols = {
1654 "adds": adds,
1670 "adds": adds,
1655 "all": getall,
1671 "all": getall,
1656 "ancestor": ancestor,
1672 "ancestor": ancestor,
1657 "ancestors": ancestors,
1673 "ancestors": ancestors,
1658 "_firstancestors": _firstancestors,
1674 "_firstancestors": _firstancestors,
1659 "author": author,
1675 "author": author,
1660 "only": only,
1676 "only": only,
1661 "bisect": bisect,
1677 "bisect": bisect,
1662 "bisected": bisected,
1678 "bisected": bisected,
1663 "bookmark": bookmark,
1679 "bookmark": bookmark,
1664 "branch": branch,
1680 "branch": branch,
1665 "branchpoint": branchpoint,
1681 "branchpoint": branchpoint,
1666 "bumped": bumped,
1682 "bumped": bumped,
1667 "bundle": bundle,
1683 "bundle": bundle,
1668 "children": children,
1684 "children": children,
1669 "closed": closed,
1685 "closed": closed,
1670 "contains": contains,
1686 "contains": contains,
1671 "converted": converted,
1687 "converted": converted,
1672 "date": date,
1688 "date": date,
1673 "desc": desc,
1689 "desc": desc,
1674 "descendants": descendants,
1690 "descendants": descendants,
1675 "_firstdescendants": _firstdescendants,
1691 "_firstdescendants": _firstdescendants,
1676 "destination": destination,
1692 "destination": destination,
1677 "divergent": divergent,
1693 "divergent": divergent,
1678 "draft": draft,
1694 "draft": draft,
1679 "extinct": extinct,
1695 "extinct": extinct,
1680 "extra": extra,
1696 "extra": extra,
1681 "file": hasfile,
1697 "file": hasfile,
1682 "filelog": filelog,
1698 "filelog": filelog,
1683 "first": first,
1699 "first": first,
1684 "follow": follow,
1700 "follow": follow,
1685 "_followfirst": _followfirst,
1701 "_followfirst": _followfirst,
1686 "grep": grep,
1702 "grep": grep,
1687 "head": head,
1703 "head": head,
1688 "heads": heads,
1704 "heads": heads,
1689 "hidden": hidden,
1705 "hidden": hidden,
1690 "id": node_,
1706 "id": node_,
1691 "keyword": keyword,
1707 "keyword": keyword,
1692 "last": last,
1708 "last": last,
1693 "limit": limit,
1709 "limit": limit,
1694 "_matchfiles": _matchfiles,
1710 "_matchfiles": _matchfiles,
1695 "max": maxrev,
1711 "max": maxrev,
1696 "merge": merge,
1712 "merge": merge,
1697 "min": minrev,
1713 "min": minrev,
1698 "_missingancestors": _missingancestors,
1714 "_missingancestors": _missingancestors,
1699 "modifies": modifies,
1715 "modifies": modifies,
1700 "obsolete": obsolete,
1716 "obsolete": obsolete,
1701 "origin": origin,
1717 "origin": origin,
1702 "outgoing": outgoing,
1718 "outgoing": outgoing,
1703 "p1": p1,
1719 "p1": p1,
1704 "p2": p2,
1720 "p2": p2,
1705 "parents": parents,
1721 "parents": parents,
1706 "present": present,
1722 "present": present,
1707 "public": public,
1723 "public": public,
1708 "remote": remote,
1724 "remote": remote,
1709 "removes": removes,
1725 "removes": removes,
1710 "rev": rev,
1726 "rev": rev,
1711 "reverse": reverse,
1727 "reverse": reverse,
1712 "roots": roots,
1728 "roots": roots,
1713 "sort": sort,
1729 "sort": sort,
1714 "secret": secret,
1730 "secret": secret,
1715 "matching": matching,
1731 "matching": matching,
1716 "tag": tag,
1732 "tag": tag,
1717 "tagged": tagged,
1733 "tagged": tagged,
1718 "user": user,
1734 "user": user,
1719 "unstable": unstable,
1735 "unstable": unstable,
1720 "_list": _list,
1736 "_list": _list,
1721 "_intlist": _intlist,
1737 "_intlist": _intlist,
1722 "_hexlist": _hexlist,
1738 "_hexlist": _hexlist,
1723 }
1739 }
1724
1740
1725 # symbols which can't be used for a DoS attack for any given input
1741 # symbols which can't be used for a DoS attack for any given input
1726 # (e.g. those which accept regexes as plain strings shouldn't be included)
1742 # (e.g. those which accept regexes as plain strings shouldn't be included)
1727 # functions that just return a lot of changesets (like all) don't count here
1743 # functions that just return a lot of changesets (like all) don't count here
1728 safesymbols = set([
1744 safesymbols = set([
1729 "adds",
1745 "adds",
1730 "all",
1746 "all",
1731 "ancestor",
1747 "ancestor",
1732 "ancestors",
1748 "ancestors",
1733 "_firstancestors",
1749 "_firstancestors",
1734 "author",
1750 "author",
1735 "bisect",
1751 "bisect",
1736 "bisected",
1752 "bisected",
1737 "bookmark",
1753 "bookmark",
1738 "branch",
1754 "branch",
1739 "branchpoint",
1755 "branchpoint",
1740 "bumped",
1756 "bumped",
1741 "bundle",
1757 "bundle",
1742 "children",
1758 "children",
1743 "closed",
1759 "closed",
1744 "converted",
1760 "converted",
1745 "date",
1761 "date",
1746 "desc",
1762 "desc",
1747 "descendants",
1763 "descendants",
1748 "_firstdescendants",
1764 "_firstdescendants",
1749 "destination",
1765 "destination",
1750 "divergent",
1766 "divergent",
1751 "draft",
1767 "draft",
1752 "extinct",
1768 "extinct",
1753 "extra",
1769 "extra",
1754 "file",
1770 "file",
1755 "filelog",
1771 "filelog",
1756 "first",
1772 "first",
1757 "follow",
1773 "follow",
1758 "_followfirst",
1774 "_followfirst",
1759 "head",
1775 "head",
1760 "heads",
1776 "heads",
1761 "hidden",
1777 "hidden",
1762 "id",
1778 "id",
1763 "keyword",
1779 "keyword",
1764 "last",
1780 "last",
1765 "limit",
1781 "limit",
1766 "_matchfiles",
1782 "_matchfiles",
1767 "max",
1783 "max",
1768 "merge",
1784 "merge",
1769 "min",
1785 "min",
1770 "_missingancestors",
1786 "_missingancestors",
1771 "modifies",
1787 "modifies",
1772 "obsolete",
1788 "obsolete",
1773 "origin",
1789 "origin",
1774 "outgoing",
1790 "outgoing",
1775 "p1",
1791 "p1",
1776 "p2",
1792 "p2",
1777 "parents",
1793 "parents",
1778 "present",
1794 "present",
1779 "public",
1795 "public",
1780 "remote",
1796 "remote",
1781 "removes",
1797 "removes",
1782 "rev",
1798 "rev",
1783 "reverse",
1799 "reverse",
1784 "roots",
1800 "roots",
1785 "sort",
1801 "sort",
1786 "secret",
1802 "secret",
1787 "matching",
1803 "matching",
1788 "tag",
1804 "tag",
1789 "tagged",
1805 "tagged",
1790 "user",
1806 "user",
1791 "unstable",
1807 "unstable",
1792 "_list",
1808 "_list",
1793 "_intlist",
1809 "_intlist",
1794 "_hexlist",
1810 "_hexlist",
1795 ])
1811 ])
1796
1812
1797 methods = {
1813 methods = {
1798 "range": rangeset,
1814 "range": rangeset,
1799 "dagrange": dagrange,
1815 "dagrange": dagrange,
1800 "string": stringset,
1816 "string": stringset,
1801 "symbol": symbolset,
1817 "symbol": symbolset,
1802 "and": andset,
1818 "and": andset,
1803 "or": orset,
1819 "or": orset,
1804 "not": notset,
1820 "not": notset,
1805 "list": listset,
1821 "list": listset,
1806 "func": func,
1822 "func": func,
1807 "ancestor": ancestorspec,
1823 "ancestor": ancestorspec,
1808 "parent": parentspec,
1824 "parent": parentspec,
1809 "parentpost": p1,
1825 "parentpost": p1,
1810 }
1826 }
1811
1827
1812 def optimize(x, small):
1828 def optimize(x, small):
1813 if x is None:
1829 if x is None:
1814 return 0, x
1830 return 0, x
1815
1831
1816 smallbonus = 1
1832 smallbonus = 1
1817 if small:
1833 if small:
1818 smallbonus = .5
1834 smallbonus = .5
1819
1835
1820 op = x[0]
1836 op = x[0]
1821 if op == 'minus':
1837 if op == 'minus':
1822 return optimize(('and', x[1], ('not', x[2])), small)
1838 return optimize(('and', x[1], ('not', x[2])), small)
1823 elif op == 'dagrangepre':
1839 elif op == 'dagrangepre':
1824 return optimize(('func', ('symbol', 'ancestors'), x[1]), small)
1840 return optimize(('func', ('symbol', 'ancestors'), x[1]), small)
1825 elif op == 'dagrangepost':
1841 elif op == 'dagrangepost':
1826 return optimize(('func', ('symbol', 'descendants'), x[1]), small)
1842 return optimize(('func', ('symbol', 'descendants'), x[1]), small)
1827 elif op == 'rangepre':
1843 elif op == 'rangepre':
1828 return optimize(('range', ('string', '0'), x[1]), small)
1844 return optimize(('range', ('string', '0'), x[1]), small)
1829 elif op == 'rangepost':
1845 elif op == 'rangepost':
1830 return optimize(('range', x[1], ('string', 'tip')), small)
1846 return optimize(('range', x[1], ('string', 'tip')), small)
1831 elif op == 'negate':
1847 elif op == 'negate':
1832 return optimize(('string',
1848 return optimize(('string',
1833 '-' + getstring(x[1], _("can't negate that"))), small)
1849 '-' + getstring(x[1], _("can't negate that"))), small)
1834 elif op in 'string symbol negate':
1850 elif op in 'string symbol negate':
1835 return smallbonus, x # single revisions are small
1851 return smallbonus, x # single revisions are small
1836 elif op == 'and':
1852 elif op == 'and':
1837 wa, ta = optimize(x[1], True)
1853 wa, ta = optimize(x[1], True)
1838 wb, tb = optimize(x[2], True)
1854 wb, tb = optimize(x[2], True)
1839
1855
1840 # (::x and not ::y)/(not ::y and ::x) have a fast path
1856 # (::x and not ::y)/(not ::y and ::x) have a fast path
1841 def ismissingancestors(revs, bases):
1857 def ismissingancestors(revs, bases):
1842 return (
1858 return (
1843 revs[0] == 'func'
1859 revs[0] == 'func'
1844 and getstring(revs[1], _('not a symbol')) == 'ancestors'
1860 and getstring(revs[1], _('not a symbol')) == 'ancestors'
1845 and bases[0] == 'not'
1861 and bases[0] == 'not'
1846 and bases[1][0] == 'func'
1862 and bases[1][0] == 'func'
1847 and getstring(bases[1][1], _('not a symbol')) == 'ancestors')
1863 and getstring(bases[1][1], _('not a symbol')) == 'ancestors')
1848
1864
1849 w = min(wa, wb)
1865 w = min(wa, wb)
1850 if ismissingancestors(ta, tb):
1866 if ismissingancestors(ta, tb):
1851 return w, ('func', ('symbol', '_missingancestors'),
1867 return w, ('func', ('symbol', '_missingancestors'),
1852 ('list', ta[2], tb[1][2]))
1868 ('list', ta[2], tb[1][2]))
1853 if ismissingancestors(tb, ta):
1869 if ismissingancestors(tb, ta):
1854 return w, ('func', ('symbol', '_missingancestors'),
1870 return w, ('func', ('symbol', '_missingancestors'),
1855 ('list', tb[2], ta[1][2]))
1871 ('list', tb[2], ta[1][2]))
1856
1872
1857 if wa > wb:
1873 if wa > wb:
1858 return w, (op, tb, ta)
1874 return w, (op, tb, ta)
1859 return w, (op, ta, tb)
1875 return w, (op, ta, tb)
1860 elif op == 'or':
1876 elif op == 'or':
1861 wa, ta = optimize(x[1], False)
1877 wa, ta = optimize(x[1], False)
1862 wb, tb = optimize(x[2], False)
1878 wb, tb = optimize(x[2], False)
1863 if wb < wa:
1879 if wb < wa:
1864 wb, wa = wa, wb
1880 wb, wa = wa, wb
1865 return max(wa, wb), (op, ta, tb)
1881 return max(wa, wb), (op, ta, tb)
1866 elif op == 'not':
1882 elif op == 'not':
1867 o = optimize(x[1], not small)
1883 o = optimize(x[1], not small)
1868 return o[0], (op, o[1])
1884 return o[0], (op, o[1])
1869 elif op == 'parentpost':
1885 elif op == 'parentpost':
1870 o = optimize(x[1], small)
1886 o = optimize(x[1], small)
1871 return o[0], (op, o[1])
1887 return o[0], (op, o[1])
1872 elif op == 'group':
1888 elif op == 'group':
1873 return optimize(x[1], small)
1889 return optimize(x[1], small)
1874 elif op in 'dagrange range list parent ancestorspec':
1890 elif op in 'dagrange range list parent ancestorspec':
1875 if op == 'parent':
1891 if op == 'parent':
1876 # x^:y means (x^) : y, not x ^ (:y)
1892 # x^:y means (x^) : y, not x ^ (:y)
1877 post = ('parentpost', x[1])
1893 post = ('parentpost', x[1])
1878 if x[2][0] == 'dagrangepre':
1894 if x[2][0] == 'dagrangepre':
1879 return optimize(('dagrange', post, x[2][1]), small)
1895 return optimize(('dagrange', post, x[2][1]), small)
1880 elif x[2][0] == 'rangepre':
1896 elif x[2][0] == 'rangepre':
1881 return optimize(('range', post, x[2][1]), small)
1897 return optimize(('range', post, x[2][1]), small)
1882
1898
1883 wa, ta = optimize(x[1], small)
1899 wa, ta = optimize(x[1], small)
1884 wb, tb = optimize(x[2], small)
1900 wb, tb = optimize(x[2], small)
1885 return wa + wb, (op, ta, tb)
1901 return wa + wb, (op, ta, tb)
1886 elif op == 'func':
1902 elif op == 'func':
1887 f = getstring(x[1], _("not a symbol"))
1903 f = getstring(x[1], _("not a symbol"))
1888 wa, ta = optimize(x[2], small)
1904 wa, ta = optimize(x[2], small)
1889 if f in ("author branch closed date desc file grep keyword "
1905 if f in ("author branch closed date desc file grep keyword "
1890 "outgoing user"):
1906 "outgoing user"):
1891 w = 10 # slow
1907 w = 10 # slow
1892 elif f in "modifies adds removes":
1908 elif f in "modifies adds removes":
1893 w = 30 # slower
1909 w = 30 # slower
1894 elif f == "contains":
1910 elif f == "contains":
1895 w = 100 # very slow
1911 w = 100 # very slow
1896 elif f == "ancestor":
1912 elif f == "ancestor":
1897 w = 1 * smallbonus
1913 w = 1 * smallbonus
1898 elif f in "reverse limit first":
1914 elif f in "reverse limit first":
1899 w = 0
1915 w = 0
1900 elif f in "sort":
1916 elif f in "sort":
1901 w = 10 # assume most sorts look at changelog
1917 w = 10 # assume most sorts look at changelog
1902 else:
1918 else:
1903 w = 1
1919 w = 1
1904 return w + wa, (op, x[1], ta)
1920 return w + wa, (op, x[1], ta)
1905 return 1, x
1921 return 1, x
1906
1922
1907 _aliasarg = ('func', ('symbol', '_aliasarg'))
1923 _aliasarg = ('func', ('symbol', '_aliasarg'))
1908 def _getaliasarg(tree):
1924 def _getaliasarg(tree):
1909 """If tree matches ('func', ('symbol', '_aliasarg'), ('string', X))
1925 """If tree matches ('func', ('symbol', '_aliasarg'), ('string', X))
1910 return X, None otherwise.
1926 return X, None otherwise.
1911 """
1927 """
1912 if (len(tree) == 3 and tree[:2] == _aliasarg
1928 if (len(tree) == 3 and tree[:2] == _aliasarg
1913 and tree[2][0] == 'string'):
1929 and tree[2][0] == 'string'):
1914 return tree[2][1]
1930 return tree[2][1]
1915 return None
1931 return None
1916
1932
1917 def _checkaliasarg(tree, known=None):
1933 def _checkaliasarg(tree, known=None):
1918 """Check tree contains no _aliasarg construct or only ones which
1934 """Check tree contains no _aliasarg construct or only ones which
1919 value is in known. Used to avoid alias placeholders injection.
1935 value is in known. Used to avoid alias placeholders injection.
1920 """
1936 """
1921 if isinstance(tree, tuple):
1937 if isinstance(tree, tuple):
1922 arg = _getaliasarg(tree)
1938 arg = _getaliasarg(tree)
1923 if arg is not None and (not known or arg not in known):
1939 if arg is not None and (not known or arg not in known):
1924 raise error.ParseError(_("not a function: %s") % '_aliasarg')
1940 raise error.ParseError(_("not a function: %s") % '_aliasarg')
1925 for t in tree:
1941 for t in tree:
1926 _checkaliasarg(t, known)
1942 _checkaliasarg(t, known)
1927
1943
1928 class revsetalias(object):
1944 class revsetalias(object):
1929 funcre = re.compile('^([^(]+)\(([^)]+)\)$')
1945 funcre = re.compile('^([^(]+)\(([^)]+)\)$')
1930 args = None
1946 args = None
1931
1947
1932 def __init__(self, name, value):
1948 def __init__(self, name, value):
1933 '''Aliases like:
1949 '''Aliases like:
1934
1950
1935 h = heads(default)
1951 h = heads(default)
1936 b($1) = ancestors($1) - ancestors(default)
1952 b($1) = ancestors($1) - ancestors(default)
1937 '''
1953 '''
1938 m = self.funcre.search(name)
1954 m = self.funcre.search(name)
1939 if m:
1955 if m:
1940 self.name = m.group(1)
1956 self.name = m.group(1)
1941 self.tree = ('func', ('symbol', m.group(1)))
1957 self.tree = ('func', ('symbol', m.group(1)))
1942 self.args = [x.strip() for x in m.group(2).split(',')]
1958 self.args = [x.strip() for x in m.group(2).split(',')]
1943 for arg in self.args:
1959 for arg in self.args:
1944 # _aliasarg() is an unknown symbol only used separate
1960 # _aliasarg() is an unknown symbol only used separate
1945 # alias argument placeholders from regular strings.
1961 # alias argument placeholders from regular strings.
1946 value = value.replace(arg, '_aliasarg(%r)' % (arg,))
1962 value = value.replace(arg, '_aliasarg(%r)' % (arg,))
1947 else:
1963 else:
1948 self.name = name
1964 self.name = name
1949 self.tree = ('symbol', name)
1965 self.tree = ('symbol', name)
1950
1966
1951 self.replacement, pos = parse(value)
1967 self.replacement, pos = parse(value)
1952 if pos != len(value):
1968 if pos != len(value):
1953 raise error.ParseError(_('invalid token'), pos)
1969 raise error.ParseError(_('invalid token'), pos)
1954 # Check for placeholder injection
1970 # Check for placeholder injection
1955 _checkaliasarg(self.replacement, self.args)
1971 _checkaliasarg(self.replacement, self.args)
1956
1972
1957 def _getalias(aliases, tree):
1973 def _getalias(aliases, tree):
1958 """If tree looks like an unexpanded alias, return it. Return None
1974 """If tree looks like an unexpanded alias, return it. Return None
1959 otherwise.
1975 otherwise.
1960 """
1976 """
1961 if isinstance(tree, tuple) and tree:
1977 if isinstance(tree, tuple) and tree:
1962 if tree[0] == 'symbol' and len(tree) == 2:
1978 if tree[0] == 'symbol' and len(tree) == 2:
1963 name = tree[1]
1979 name = tree[1]
1964 alias = aliases.get(name)
1980 alias = aliases.get(name)
1965 if alias and alias.args is None and alias.tree == tree:
1981 if alias and alias.args is None and alias.tree == tree:
1966 return alias
1982 return alias
1967 if tree[0] == 'func' and len(tree) > 1:
1983 if tree[0] == 'func' and len(tree) > 1:
1968 if tree[1][0] == 'symbol' and len(tree[1]) == 2:
1984 if tree[1][0] == 'symbol' and len(tree[1]) == 2:
1969 name = tree[1][1]
1985 name = tree[1][1]
1970 alias = aliases.get(name)
1986 alias = aliases.get(name)
1971 if alias and alias.args is not None and alias.tree == tree[:2]:
1987 if alias and alias.args is not None and alias.tree == tree[:2]:
1972 return alias
1988 return alias
1973 return None
1989 return None
1974
1990
1975 def _expandargs(tree, args):
1991 def _expandargs(tree, args):
1976 """Replace _aliasarg instances with the substitution value of the
1992 """Replace _aliasarg instances with the substitution value of the
1977 same name in args, recursively.
1993 same name in args, recursively.
1978 """
1994 """
1979 if not tree or not isinstance(tree, tuple):
1995 if not tree or not isinstance(tree, tuple):
1980 return tree
1996 return tree
1981 arg = _getaliasarg(tree)
1997 arg = _getaliasarg(tree)
1982 if arg is not None:
1998 if arg is not None:
1983 return args[arg]
1999 return args[arg]
1984 return tuple(_expandargs(t, args) for t in tree)
2000 return tuple(_expandargs(t, args) for t in tree)
1985
2001
1986 def _expandaliases(aliases, tree, expanding, cache):
2002 def _expandaliases(aliases, tree, expanding, cache):
1987 """Expand aliases in tree, recursively.
2003 """Expand aliases in tree, recursively.
1988
2004
1989 'aliases' is a dictionary mapping user defined aliases to
2005 'aliases' is a dictionary mapping user defined aliases to
1990 revsetalias objects.
2006 revsetalias objects.
1991 """
2007 """
1992 if not isinstance(tree, tuple):
2008 if not isinstance(tree, tuple):
1993 # Do not expand raw strings
2009 # Do not expand raw strings
1994 return tree
2010 return tree
1995 alias = _getalias(aliases, tree)
2011 alias = _getalias(aliases, tree)
1996 if alias is not None:
2012 if alias is not None:
1997 if alias in expanding:
2013 if alias in expanding:
1998 raise error.ParseError(_('infinite expansion of revset alias "%s" '
2014 raise error.ParseError(_('infinite expansion of revset alias "%s" '
1999 'detected') % alias.name)
2015 'detected') % alias.name)
2000 expanding.append(alias)
2016 expanding.append(alias)
2001 if alias.name not in cache:
2017 if alias.name not in cache:
2002 cache[alias.name] = _expandaliases(aliases, alias.replacement,
2018 cache[alias.name] = _expandaliases(aliases, alias.replacement,
2003 expanding, cache)
2019 expanding, cache)
2004 result = cache[alias.name]
2020 result = cache[alias.name]
2005 expanding.pop()
2021 expanding.pop()
2006 if alias.args is not None:
2022 if alias.args is not None:
2007 l = getlist(tree[2])
2023 l = getlist(tree[2])
2008 if len(l) != len(alias.args):
2024 if len(l) != len(alias.args):
2009 raise error.ParseError(
2025 raise error.ParseError(
2010 _('invalid number of arguments: %s') % len(l))
2026 _('invalid number of arguments: %s') % len(l))
2011 l = [_expandaliases(aliases, a, [], cache) for a in l]
2027 l = [_expandaliases(aliases, a, [], cache) for a in l]
2012 result = _expandargs(result, dict(zip(alias.args, l)))
2028 result = _expandargs(result, dict(zip(alias.args, l)))
2013 else:
2029 else:
2014 result = tuple(_expandaliases(aliases, t, expanding, cache)
2030 result = tuple(_expandaliases(aliases, t, expanding, cache)
2015 for t in tree)
2031 for t in tree)
2016 return result
2032 return result
2017
2033
2018 def findaliases(ui, tree):
2034 def findaliases(ui, tree):
2019 _checkaliasarg(tree)
2035 _checkaliasarg(tree)
2020 aliases = {}
2036 aliases = {}
2021 for k, v in ui.configitems('revsetalias'):
2037 for k, v in ui.configitems('revsetalias'):
2022 alias = revsetalias(k, v)
2038 alias = revsetalias(k, v)
2023 aliases[alias.name] = alias
2039 aliases[alias.name] = alias
2024 return _expandaliases(aliases, tree, [], {})
2040 return _expandaliases(aliases, tree, [], {})
2025
2041
2026 def parse(spec, lookup=None):
2042 def parse(spec, lookup=None):
2027 p = parser.parser(tokenize, elements)
2043 p = parser.parser(tokenize, elements)
2028 return p.parse(spec, lookup=lookup)
2044 return p.parse(spec, lookup=lookup)
2029
2045
2030 def match(ui, spec, repo=None):
2046 def match(ui, spec, repo=None):
2031 if not spec:
2047 if not spec:
2032 raise error.ParseError(_("empty query"))
2048 raise error.ParseError(_("empty query"))
2033 lookup = None
2049 lookup = None
2034 if repo:
2050 if repo:
2035 lookup = repo.__contains__
2051 lookup = repo.__contains__
2036 tree, pos = parse(spec, lookup)
2052 tree, pos = parse(spec, lookup)
2037 if (pos != len(spec)):
2053 if (pos != len(spec)):
2038 raise error.ParseError(_("invalid token"), pos)
2054 raise error.ParseError(_("invalid token"), pos)
2039 if ui:
2055 if ui:
2040 tree = findaliases(ui, tree)
2056 tree = findaliases(ui, tree)
2041 weight, tree = optimize(tree, True)
2057 weight, tree = optimize(tree, True)
2042 def mfunc(repo, subset):
2058 def mfunc(repo, subset):
2043 if util.safehasattr(subset, 'set'):
2059 if util.safehasattr(subset, 'set'):
2044 return getset(repo, subset, tree)
2060 return getset(repo, subset, tree)
2045 return getset(repo, baseset(subset), tree)
2061 return getset(repo, baseset(subset), tree)
2046 return mfunc
2062 return mfunc
2047
2063
2048 def formatspec(expr, *args):
2064 def formatspec(expr, *args):
2049 '''
2065 '''
2050 This is a convenience function for using revsets internally, and
2066 This is a convenience function for using revsets internally, and
2051 escapes arguments appropriately. Aliases are intentionally ignored
2067 escapes arguments appropriately. Aliases are intentionally ignored
2052 so that intended expression behavior isn't accidentally subverted.
2068 so that intended expression behavior isn't accidentally subverted.
2053
2069
2054 Supported arguments:
2070 Supported arguments:
2055
2071
2056 %r = revset expression, parenthesized
2072 %r = revset expression, parenthesized
2057 %d = int(arg), no quoting
2073 %d = int(arg), no quoting
2058 %s = string(arg), escaped and single-quoted
2074 %s = string(arg), escaped and single-quoted
2059 %b = arg.branch(), escaped and single-quoted
2075 %b = arg.branch(), escaped and single-quoted
2060 %n = hex(arg), single-quoted
2076 %n = hex(arg), single-quoted
2061 %% = a literal '%'
2077 %% = a literal '%'
2062
2078
2063 Prefixing the type with 'l' specifies a parenthesized list of that type.
2079 Prefixing the type with 'l' specifies a parenthesized list of that type.
2064
2080
2065 >>> formatspec('%r:: and %lr', '10 or 11', ("this()", "that()"))
2081 >>> formatspec('%r:: and %lr', '10 or 11', ("this()", "that()"))
2066 '(10 or 11):: and ((this()) or (that()))'
2082 '(10 or 11):: and ((this()) or (that()))'
2067 >>> formatspec('%d:: and not %d::', 10, 20)
2083 >>> formatspec('%d:: and not %d::', 10, 20)
2068 '10:: and not 20::'
2084 '10:: and not 20::'
2069 >>> formatspec('%ld or %ld', [], [1])
2085 >>> formatspec('%ld or %ld', [], [1])
2070 "_list('') or 1"
2086 "_list('') or 1"
2071 >>> formatspec('keyword(%s)', 'foo\\xe9')
2087 >>> formatspec('keyword(%s)', 'foo\\xe9')
2072 "keyword('foo\\\\xe9')"
2088 "keyword('foo\\\\xe9')"
2073 >>> b = lambda: 'default'
2089 >>> b = lambda: 'default'
2074 >>> b.branch = b
2090 >>> b.branch = b
2075 >>> formatspec('branch(%b)', b)
2091 >>> formatspec('branch(%b)', b)
2076 "branch('default')"
2092 "branch('default')"
2077 >>> formatspec('root(%ls)', ['a', 'b', 'c', 'd'])
2093 >>> formatspec('root(%ls)', ['a', 'b', 'c', 'd'])
2078 "root(_list('a\\x00b\\x00c\\x00d'))"
2094 "root(_list('a\\x00b\\x00c\\x00d'))"
2079 '''
2095 '''
2080
2096
2081 def quote(s):
2097 def quote(s):
2082 return repr(str(s))
2098 return repr(str(s))
2083
2099
2084 def argtype(c, arg):
2100 def argtype(c, arg):
2085 if c == 'd':
2101 if c == 'd':
2086 return str(int(arg))
2102 return str(int(arg))
2087 elif c == 's':
2103 elif c == 's':
2088 return quote(arg)
2104 return quote(arg)
2089 elif c == 'r':
2105 elif c == 'r':
2090 parse(arg) # make sure syntax errors are confined
2106 parse(arg) # make sure syntax errors are confined
2091 return '(%s)' % arg
2107 return '(%s)' % arg
2092 elif c == 'n':
2108 elif c == 'n':
2093 return quote(node.hex(arg))
2109 return quote(node.hex(arg))
2094 elif c == 'b':
2110 elif c == 'b':
2095 return quote(arg.branch())
2111 return quote(arg.branch())
2096
2112
2097 def listexp(s, t):
2113 def listexp(s, t):
2098 l = len(s)
2114 l = len(s)
2099 if l == 0:
2115 if l == 0:
2100 return "_list('')"
2116 return "_list('')"
2101 elif l == 1:
2117 elif l == 1:
2102 return argtype(t, s[0])
2118 return argtype(t, s[0])
2103 elif t == 'd':
2119 elif t == 'd':
2104 return "_intlist('%s')" % "\0".join(str(int(a)) for a in s)
2120 return "_intlist('%s')" % "\0".join(str(int(a)) for a in s)
2105 elif t == 's':
2121 elif t == 's':
2106 return "_list('%s')" % "\0".join(s)
2122 return "_list('%s')" % "\0".join(s)
2107 elif t == 'n':
2123 elif t == 'n':
2108 return "_hexlist('%s')" % "\0".join(node.hex(a) for a in s)
2124 return "_hexlist('%s')" % "\0".join(node.hex(a) for a in s)
2109 elif t == 'b':
2125 elif t == 'b':
2110 return "_list('%s')" % "\0".join(a.branch() for a in s)
2126 return "_list('%s')" % "\0".join(a.branch() for a in s)
2111
2127
2112 m = l // 2
2128 m = l // 2
2113 return '(%s or %s)' % (listexp(s[:m], t), listexp(s[m:], t))
2129 return '(%s or %s)' % (listexp(s[:m], t), listexp(s[m:], t))
2114
2130
2115 ret = ''
2131 ret = ''
2116 pos = 0
2132 pos = 0
2117 arg = 0
2133 arg = 0
2118 while pos < len(expr):
2134 while pos < len(expr):
2119 c = expr[pos]
2135 c = expr[pos]
2120 if c == '%':
2136 if c == '%':
2121 pos += 1
2137 pos += 1
2122 d = expr[pos]
2138 d = expr[pos]
2123 if d == '%':
2139 if d == '%':
2124 ret += d
2140 ret += d
2125 elif d in 'dsnbr':
2141 elif d in 'dsnbr':
2126 ret += argtype(d, args[arg])
2142 ret += argtype(d, args[arg])
2127 arg += 1
2143 arg += 1
2128 elif d == 'l':
2144 elif d == 'l':
2129 # a list of some type
2145 # a list of some type
2130 pos += 1
2146 pos += 1
2131 d = expr[pos]
2147 d = expr[pos]
2132 ret += listexp(list(args[arg]), d)
2148 ret += listexp(list(args[arg]), d)
2133 arg += 1
2149 arg += 1
2134 else:
2150 else:
2135 raise util.Abort('unexpected revspec format character %s' % d)
2151 raise util.Abort('unexpected revspec format character %s' % d)
2136 else:
2152 else:
2137 ret += c
2153 ret += c
2138 pos += 1
2154 pos += 1
2139
2155
2140 return ret
2156 return ret
2141
2157
2142 def prettyformat(tree):
2158 def prettyformat(tree):
2143 def _prettyformat(tree, level, lines):
2159 def _prettyformat(tree, level, lines):
2144 if not isinstance(tree, tuple) or tree[0] in ('string', 'symbol'):
2160 if not isinstance(tree, tuple) or tree[0] in ('string', 'symbol'):
2145 lines.append((level, str(tree)))
2161 lines.append((level, str(tree)))
2146 else:
2162 else:
2147 lines.append((level, '(%s' % tree[0]))
2163 lines.append((level, '(%s' % tree[0]))
2148 for s in tree[1:]:
2164 for s in tree[1:]:
2149 _prettyformat(s, level + 1, lines)
2165 _prettyformat(s, level + 1, lines)
2150 lines[-1:] = [(lines[-1][0], lines[-1][1] + ')')]
2166 lines[-1:] = [(lines[-1][0], lines[-1][1] + ')')]
2151
2167
2152 lines = []
2168 lines = []
2153 _prettyformat(tree, 0, lines)
2169 _prettyformat(tree, 0, lines)
2154 output = '\n'.join((' '*l + s) for l, s in lines)
2170 output = '\n'.join((' '*l + s) for l, s in lines)
2155 return output
2171 return output
2156
2172
2157 def depth(tree):
2173 def depth(tree):
2158 if isinstance(tree, tuple):
2174 if isinstance(tree, tuple):
2159 return max(map(depth, tree)) + 1
2175 return max(map(depth, tree)) + 1
2160 else:
2176 else:
2161 return 0
2177 return 0
2162
2178
2163 def funcsused(tree):
2179 def funcsused(tree):
2164 if not isinstance(tree, tuple) or tree[0] in ('string', 'symbol'):
2180 if not isinstance(tree, tuple) or tree[0] in ('string', 'symbol'):
2165 return set()
2181 return set()
2166 else:
2182 else:
2167 funcs = set()
2183 funcs = set()
2168 for s in tree[1:]:
2184 for s in tree[1:]:
2169 funcs |= funcsused(s)
2185 funcs |= funcsused(s)
2170 if tree[0] == 'func':
2186 if tree[0] == 'func':
2171 funcs.add(tree[1][1])
2187 funcs.add(tree[1][1])
2172 return funcs
2188 return funcs
2173
2189
2174 class baseset(list):
2190 class baseset(list):
2175 """Basic data structure that represents a revset and contains the basic
2191 """Basic data structure that represents a revset and contains the basic
2176 operation that it should be able to perform.
2192 operation that it should be able to perform.
2177
2193
2178 Every method in this class should be implemented by any smartset class.
2194 Every method in this class should be implemented by any smartset class.
2179 """
2195 """
2180 def __init__(self, data=()):
2196 def __init__(self, data=()):
2181 super(baseset, self).__init__(data)
2197 super(baseset, self).__init__(data)
2182 self._set = None
2198 self._set = None
2183
2199
2184 def ascending(self):
2200 def ascending(self):
2185 """Sorts the set in ascending order (in place).
2201 """Sorts the set in ascending order (in place).
2186
2202
2187 This is part of the mandatory API for smartset."""
2203 This is part of the mandatory API for smartset."""
2188 self.sort()
2204 self.sort()
2189
2205
2190 def descending(self):
2206 def descending(self):
2191 """Sorts the set in descending order (in place).
2207 """Sorts the set in descending order (in place).
2192
2208
2193 This is part of the mandatory API for smartset."""
2209 This is part of the mandatory API for smartset."""
2194 self.sort(reverse=True)
2210 self.sort(reverse=True)
2195
2211
2196 def min(self):
2212 def min(self):
2197 return min(self)
2213 return min(self)
2198
2214
2199 def max(self):
2215 def max(self):
2200 return max(self)
2216 return max(self)
2201
2217
2202 def set(self):
2218 def set(self):
2203 """Returns a set or a smartset containing all the elements.
2219 """Returns a set or a smartset containing all the elements.
2204
2220
2205 The returned structure should be the fastest option for membership
2221 The returned structure should be the fastest option for membership
2206 testing.
2222 testing.
2207
2223
2208 This is part of the mandatory API for smartset."""
2224 This is part of the mandatory API for smartset."""
2209 if not self._set:
2225 if not self._set:
2210 self._set = set(self)
2226 self._set = set(self)
2211 return self._set
2227 return self._set
2212
2228
2213 def __sub__(self, other):
2229 def __sub__(self, other):
2214 """Returns a new object with the substraction of the two collections.
2230 """Returns a new object with the substraction of the two collections.
2215
2231
2216 This is part of the mandatory API for smartset."""
2232 This is part of the mandatory API for smartset."""
2217 if isinstance(other, baseset):
2233 if isinstance(other, baseset):
2218 s = other.set()
2234 s = other.set()
2219 else:
2235 else:
2220 s = set(other)
2236 s = set(other)
2221 return baseset(self.set() - s)
2237 return baseset(self.set() - s)
2222
2238
2223 def __and__(self, other):
2239 def __and__(self, other):
2224 """Returns a new object with the intersection of the two collections.
2240 """Returns a new object with the intersection of the two collections.
2225
2241
2226 This is part of the mandatory API for smartset."""
2242 This is part of the mandatory API for smartset."""
2227 if isinstance(other, baseset):
2243 if isinstance(other, baseset):
2228 other = other.set()
2244 other = other.set()
2229 return baseset([y for y in self if y in other])
2245 return baseset([y for y in self if y in other])
2230
2246
2231 def __add__(self, other):
2247 def __add__(self, other):
2232 """Returns a new object with the union of the two collections.
2248 """Returns a new object with the union of the two collections.
2233
2249
2234 This is part of the mandatory API for smartset."""
2250 This is part of the mandatory API for smartset."""
2235 s = self.set()
2251 s = self.set()
2236 l = [r for r in other if r not in s]
2252 l = [r for r in other if r not in s]
2237 return baseset(list(self) + l)
2253 return baseset(list(self) + l)
2238
2254
2239 def isascending(self):
2255 def isascending(self):
2240 """Returns True if the collection is ascending order, False if not.
2256 """Returns True if the collection is ascending order, False if not.
2241
2257
2242 This is part of the mandatory API for smartset."""
2258 This is part of the mandatory API for smartset."""
2243 return False
2259 return False
2244
2260
2245 def isdescending(self):
2261 def isdescending(self):
2246 """Returns True if the collection is descending order, False if not.
2262 """Returns True if the collection is descending order, False if not.
2247
2263
2248 This is part of the mandatory API for smartset."""
2264 This is part of the mandatory API for smartset."""
2249 return False
2265 return False
2250
2266
2251 def filter(self, condition):
2267 def filter(self, condition):
2252 """Returns this smartset filtered by condition as a new smartset.
2268 """Returns this smartset filtered by condition as a new smartset.
2253
2269
2254 `condition` is a callable which takes a revision number and returns a
2270 `condition` is a callable which takes a revision number and returns a
2255 boolean.
2271 boolean.
2256
2272
2257 This is part of the mandatory API for smartset."""
2273 This is part of the mandatory API for smartset."""
2258 return lazyset(self, condition)
2274 return lazyset(self, condition)
2259
2275
2260 class _orderedsetmixin(object):
2276 class _orderedsetmixin(object):
2261 """Mixin class with utility methods for smartsets
2277 """Mixin class with utility methods for smartsets
2262
2278
2263 This should be extended by smartsets which have the isascending(),
2279 This should be extended by smartsets which have the isascending(),
2264 isdescending() and reverse() methods"""
2280 isdescending() and reverse() methods"""
2265
2281
2266 def _first(self):
2282 def _first(self):
2267 """return the first revision in the set"""
2283 """return the first revision in the set"""
2268 for r in self:
2284 for r in self:
2269 return r
2285 return r
2270 return None
2286 return None
2271
2287
2272 def _last(self):
2288 def _last(self):
2273 """return the last revision in the set"""
2289 """return the last revision in the set"""
2274 self.reverse()
2290 self.reverse()
2275 m = self._first()
2291 m = self._first()
2276 self.reverse()
2292 self.reverse()
2277 return m
2293 return m
2278
2294
2279 def min(self):
2295 def min(self):
2280 """return the smallest element in the set"""
2296 """return the smallest element in the set"""
2281 if self.isascending():
2297 if self.isascending():
2282 return self._first()
2298 return self._first()
2283 return self._last()
2299 return self._last()
2284
2300
2285 def max(self):
2301 def max(self):
2286 """return the largest element in the set"""
2302 """return the largest element in the set"""
2287 if self.isascending():
2303 if self.isascending():
2288 return self._last()
2304 return self._last()
2289 return self._first()
2305 return self._first()
2290
2306
2291 class lazyset(object):
2307 class lazyset(object):
2292 """Duck type for baseset class which iterates lazily over the revisions in
2308 """Duck type for baseset class which iterates lazily over the revisions in
2293 the subset and contains a function which tests for membership in the
2309 the subset and contains a function which tests for membership in the
2294 revset
2310 revset
2295 """
2311 """
2296 def __init__(self, subset, condition=lambda x: True):
2312 def __init__(self, subset, condition=lambda x: True):
2297 """
2313 """
2298 condition: a function that decide whether a revision in the subset
2314 condition: a function that decide whether a revision in the subset
2299 belongs to the revset or not.
2315 belongs to the revset or not.
2300 """
2316 """
2301 self._subset = subset
2317 self._subset = subset
2302 self._condition = condition
2318 self._condition = condition
2303 self._cache = {}
2319 self._cache = {}
2304
2320
2305 def ascending(self):
2321 def ascending(self):
2306 self._subset.sort()
2322 self._subset.sort()
2307
2323
2308 def descending(self):
2324 def descending(self):
2309 self._subset.sort(reverse=True)
2325 self._subset.sort(reverse=True)
2310
2326
2311 def min(self):
2327 def min(self):
2312 return min(self)
2328 return min(self)
2313
2329
2314 def max(self):
2330 def max(self):
2315 return max(self)
2331 return max(self)
2316
2332
2317 def __contains__(self, x):
2333 def __contains__(self, x):
2318 c = self._cache
2334 c = self._cache
2319 if x not in c:
2335 if x not in c:
2320 c[x] = x in self._subset and self._condition(x)
2336 c[x] = x in self._subset and self._condition(x)
2321 return c[x]
2337 return c[x]
2322
2338
2323 def __iter__(self):
2339 def __iter__(self):
2324 cond = self._condition
2340 cond = self._condition
2325 for x in self._subset:
2341 for x in self._subset:
2326 if cond(x):
2342 if cond(x):
2327 yield x
2343 yield x
2328
2344
2329 def __and__(self, x):
2345 def __and__(self, x):
2330 return lazyset(self, lambda r: r in x)
2346 return lazyset(self, lambda r: r in x)
2331
2347
2332 def __sub__(self, x):
2348 def __sub__(self, x):
2333 return lazyset(self, lambda r: r not in x)
2349 return lazyset(self, lambda r: r not in x)
2334
2350
2335 def __add__(self, x):
2351 def __add__(self, x):
2336 return _addset(self, x)
2352 return _addset(self, x)
2337
2353
2338 def __nonzero__(self):
2354 def __nonzero__(self):
2339 for r in self:
2355 for r in self:
2340 return True
2356 return True
2341 return False
2357 return False
2342
2358
2343 def __len__(self):
2359 def __len__(self):
2344 # Basic implementation to be changed in future patches.
2360 # Basic implementation to be changed in future patches.
2345 l = baseset([r for r in self])
2361 l = baseset([r for r in self])
2346 return len(l)
2362 return len(l)
2347
2363
2348 def __getitem__(self, x):
2364 def __getitem__(self, x):
2349 # Basic implementation to be changed in future patches.
2365 # Basic implementation to be changed in future patches.
2350 l = baseset([r for r in self])
2366 l = baseset([r for r in self])
2351 return l[x]
2367 return l[x]
2352
2368
2353 def sort(self, reverse=False):
2369 def sort(self, reverse=False):
2354 if not util.safehasattr(self._subset, 'sort'):
2370 if not util.safehasattr(self._subset, 'sort'):
2355 self._subset = baseset(self._subset)
2371 self._subset = baseset(self._subset)
2356 self._subset.sort(reverse=reverse)
2372 self._subset.sort(reverse=reverse)
2357
2373
2358 def reverse(self):
2374 def reverse(self):
2359 self._subset.reverse()
2375 self._subset.reverse()
2360
2376
2361 def set(self):
2377 def set(self):
2362 return set([r for r in self])
2378 return set([r for r in self])
2363
2379
2364 def isascending(self):
2380 def isascending(self):
2365 return False
2381 return False
2366
2382
2367 def isdescending(self):
2383 def isdescending(self):
2368 return False
2384 return False
2369
2385
2370 def filter(self, l):
2386 def filter(self, l):
2371 return lazyset(self, l)
2387 return lazyset(self, l)
2372
2388
2373 class orderedlazyset(_orderedsetmixin, lazyset):
2389 class orderedlazyset(_orderedsetmixin, lazyset):
2374 """Subclass of lazyset which subset can be ordered either ascending or
2390 """Subclass of lazyset which subset can be ordered either ascending or
2375 descendingly
2391 descendingly
2376 """
2392 """
2377 def __init__(self, subset, condition, ascending=True):
2393 def __init__(self, subset, condition, ascending=True):
2378 super(orderedlazyset, self).__init__(subset, condition)
2394 super(orderedlazyset, self).__init__(subset, condition)
2379 self._ascending = ascending
2395 self._ascending = ascending
2380
2396
2381 def filter(self, l):
2397 def filter(self, l):
2382 return orderedlazyset(self, l, ascending=self._ascending)
2398 return orderedlazyset(self, l, ascending=self._ascending)
2383
2399
2384 def ascending(self):
2400 def ascending(self):
2385 if not self._ascending:
2401 if not self._ascending:
2386 self.reverse()
2402 self.reverse()
2387
2403
2388 def descending(self):
2404 def descending(self):
2389 if self._ascending:
2405 if self._ascending:
2390 self.reverse()
2406 self.reverse()
2391
2407
2392 def __and__(self, x):
2408 def __and__(self, x):
2393 return orderedlazyset(self, lambda r: r in x,
2409 return orderedlazyset(self, lambda r: r in x,
2394 ascending=self._ascending)
2410 ascending=self._ascending)
2395
2411
2396 def __sub__(self, x):
2412 def __sub__(self, x):
2397 return orderedlazyset(self, lambda r: r not in x,
2413 return orderedlazyset(self, lambda r: r not in x,
2398 ascending=self._ascending)
2414 ascending=self._ascending)
2399
2415
2400 def __add__(self, x):
2416 def __add__(self, x):
2401 kwargs = {}
2417 kwargs = {}
2402 if self.isascending() and x.isascending():
2418 if self.isascending() and x.isascending():
2403 kwargs['ascending'] = True
2419 kwargs['ascending'] = True
2404 if self.isdescending() and x.isdescending():
2420 if self.isdescending() and x.isdescending():
2405 kwargs['ascending'] = False
2421 kwargs['ascending'] = False
2406 return _addset(self, x, **kwargs)
2422 return _addset(self, x, **kwargs)
2407
2423
2408 def sort(self, reverse=False):
2424 def sort(self, reverse=False):
2409 if reverse:
2425 if reverse:
2410 if self._ascending:
2426 if self._ascending:
2411 self._subset.sort(reverse=reverse)
2427 self._subset.sort(reverse=reverse)
2412 else:
2428 else:
2413 if not self._ascending:
2429 if not self._ascending:
2414 self._subset.sort(reverse=reverse)
2430 self._subset.sort(reverse=reverse)
2415 self._ascending = not reverse
2431 self._ascending = not reverse
2416
2432
2417 def isascending(self):
2433 def isascending(self):
2418 return self._ascending
2434 return self._ascending
2419
2435
2420 def isdescending(self):
2436 def isdescending(self):
2421 return not self._ascending
2437 return not self._ascending
2422
2438
2423 def reverse(self):
2439 def reverse(self):
2424 self._subset.reverse()
2440 self._subset.reverse()
2425 self._ascending = not self._ascending
2441 self._ascending = not self._ascending
2426
2442
2427 class _addset(_orderedsetmixin):
2443 class _addset(_orderedsetmixin):
2428 """Represent the addition of two sets
2444 """Represent the addition of two sets
2429
2445
2430 Wrapper structure for lazily adding two structures without losing much
2446 Wrapper structure for lazily adding two structures without losing much
2431 performance on the __contains__ method
2447 performance on the __contains__ method
2432
2448
2433 If the ascending attribute is set, that means the two structures are
2449 If the ascending attribute is set, that means the two structures are
2434 ordered in either an ascending or descending way. Therefore, we can add
2450 ordered in either an ascending or descending way. Therefore, we can add
2435 them mantaining the order by iterating over both at the same time
2451 them mantaining the order by iterating over both at the same time
2436
2452
2437 This class does not duck-type baseset and it's only supposed to be used
2453 This class does not duck-type baseset and it's only supposed to be used
2438 internally
2454 internally
2439 """
2455 """
2440 def __init__(self, revs1, revs2, ascending=None):
2456 def __init__(self, revs1, revs2, ascending=None):
2441 self._r1 = revs1
2457 self._r1 = revs1
2442 self._r2 = revs2
2458 self._r2 = revs2
2443 self._iter = None
2459 self._iter = None
2444 self._ascending = ascending
2460 self._ascending = ascending
2445 self._genlist = None
2461 self._genlist = None
2446
2462
2447 @util.propertycache
2463 @util.propertycache
2448 def _list(self):
2464 def _list(self):
2449 if not self._genlist:
2465 if not self._genlist:
2450 self._genlist = baseset(self._iterator())
2466 self._genlist = baseset(self._iterator())
2451 return self._genlist
2467 return self._genlist
2452
2468
2453 def filter(self, condition):
2469 def filter(self, condition):
2454 if self._ascending is not None:
2470 if self._ascending is not None:
2455 return orderedlazyset(self, condition, ascending=self._ascending)
2471 return orderedlazyset(self, condition, ascending=self._ascending)
2456 return lazyset(self, condition)
2472 return lazyset(self, condition)
2457
2473
2458 def ascending(self):
2474 def ascending(self):
2459 if self._ascending is None:
2475 if self._ascending is None:
2460 self.sort()
2476 self.sort()
2461 self._ascending = True
2477 self._ascending = True
2462 else:
2478 else:
2463 if not self._ascending:
2479 if not self._ascending:
2464 self.reverse()
2480 self.reverse()
2465
2481
2466 def descending(self):
2482 def descending(self):
2467 if self._ascending is None:
2483 if self._ascending is None:
2468 self.sort(reverse=True)
2484 self.sort(reverse=True)
2469 self._ascending = False
2485 self._ascending = False
2470 else:
2486 else:
2471 if self._ascending:
2487 if self._ascending:
2472 self.reverse()
2488 self.reverse()
2473
2489
2474 def __and__(self, other):
2490 def __and__(self, other):
2475 filterfunc = other.__contains__
2491 filterfunc = other.__contains__
2476 if self._ascending is not None:
2492 if self._ascending is not None:
2477 return orderedlazyset(self, filterfunc, ascending=self._ascending)
2493 return orderedlazyset(self, filterfunc, ascending=self._ascending)
2478 return lazyset(self, filterfunc)
2494 return lazyset(self, filterfunc)
2479
2495
2480 def __sub__(self, other):
2496 def __sub__(self, other):
2481 filterfunc = lambda r: r not in other
2497 filterfunc = lambda r: r not in other
2482 if self._ascending is not None:
2498 if self._ascending is not None:
2483 return orderedlazyset(self, filterfunc, ascending=self._ascending)
2499 return orderedlazyset(self, filterfunc, ascending=self._ascending)
2484 return lazyset(self, filterfunc)
2500 return lazyset(self, filterfunc)
2485
2501
2486 def __add__(self, other):
2502 def __add__(self, other):
2487 """When both collections are ascending or descending, preserve the order
2503 """When both collections are ascending or descending, preserve the order
2488 """
2504 """
2489 kwargs = {}
2505 kwargs = {}
2490 if self._ascending is not None:
2506 if self._ascending is not None:
2491 if self.isascending() and other.isascending():
2507 if self.isascending() and other.isascending():
2492 kwargs['ascending'] = True
2508 kwargs['ascending'] = True
2493 if self.isdescending() and other.isdescending():
2509 if self.isdescending() and other.isdescending():
2494 kwargs['ascending'] = False
2510 kwargs['ascending'] = False
2495 return _addset(self, other, **kwargs)
2511 return _addset(self, other, **kwargs)
2496
2512
2497 def _iterator(self):
2513 def _iterator(self):
2498 """Iterate over both collections without repeating elements
2514 """Iterate over both collections without repeating elements
2499
2515
2500 If the ascending attribute is not set, iterate over the first one and
2516 If the ascending attribute is not set, iterate over the first one and
2501 then over the second one checking for membership on the first one so we
2517 then over the second one checking for membership on the first one so we
2502 dont yield any duplicates.
2518 dont yield any duplicates.
2503
2519
2504 If the ascending attribute is set, iterate over both collections at the
2520 If the ascending attribute is set, iterate over both collections at the
2505 same time, yielding only one value at a time in the given order.
2521 same time, yielding only one value at a time in the given order.
2506 """
2522 """
2507 if not self._iter:
2523 if not self._iter:
2508 def gen():
2524 def gen():
2509 if self._ascending is None:
2525 if self._ascending is None:
2510 for r in self._r1:
2526 for r in self._r1:
2511 yield r
2527 yield r
2512 s = self._r1.set()
2528 s = self._r1.set()
2513 for r in self._r2:
2529 for r in self._r2:
2514 if r not in s:
2530 if r not in s:
2515 yield r
2531 yield r
2516 else:
2532 else:
2517 iter1 = iter(self._r1)
2533 iter1 = iter(self._r1)
2518 iter2 = iter(self._r2)
2534 iter2 = iter(self._r2)
2519
2535
2520 val1 = None
2536 val1 = None
2521 val2 = None
2537 val2 = None
2522
2538
2523 choice = max
2539 choice = max
2524 if self._ascending:
2540 if self._ascending:
2525 choice = min
2541 choice = min
2526 try:
2542 try:
2527 # Consume both iterators in an ordered way until one is
2543 # Consume both iterators in an ordered way until one is
2528 # empty
2544 # empty
2529 while True:
2545 while True:
2530 if val1 is None:
2546 if val1 is None:
2531 val1 = iter1.next()
2547 val1 = iter1.next()
2532 if val2 is None:
2548 if val2 is None:
2533 val2 = iter2.next()
2549 val2 = iter2.next()
2534 next = choice(val1, val2)
2550 next = choice(val1, val2)
2535 yield next
2551 yield next
2536 if val1 == next:
2552 if val1 == next:
2537 val1 = None
2553 val1 = None
2538 if val2 == next:
2554 if val2 == next:
2539 val2 = None
2555 val2 = None
2540 except StopIteration:
2556 except StopIteration:
2541 # Flush any remaining values and consume the other one
2557 # Flush any remaining values and consume the other one
2542 it = iter2
2558 it = iter2
2543 if val1 is not None:
2559 if val1 is not None:
2544 yield val1
2560 yield val1
2545 it = iter1
2561 it = iter1
2546 elif val2 is not None:
2562 elif val2 is not None:
2547 # might have been equality and both are empty
2563 # might have been equality and both are empty
2548 yield val2
2564 yield val2
2549 for val in it:
2565 for val in it:
2550 yield val
2566 yield val
2551
2567
2552 self._iter = _generatorset(gen())
2568 self._iter = _generatorset(gen())
2553
2569
2554 return self._iter
2570 return self._iter
2555
2571
2556 def __iter__(self):
2572 def __iter__(self):
2557 if self._genlist:
2573 if self._genlist:
2558 return iter(self._genlist)
2574 return iter(self._genlist)
2559 return iter(self._iterator())
2575 return iter(self._iterator())
2560
2576
2561 def __contains__(self, x):
2577 def __contains__(self, x):
2562 return x in self._r1 or x in self._r2
2578 return x in self._r1 or x in self._r2
2563
2579
2564 def set(self):
2580 def set(self):
2565 return self
2581 return self
2566
2582
2567 def sort(self, reverse=False):
2583 def sort(self, reverse=False):
2568 """Sort the added set
2584 """Sort the added set
2569
2585
2570 For this we use the cached list with all the generated values and if we
2586 For this we use the cached list with all the generated values and if we
2571 know they are ascending or descending we can sort them in a smart way.
2587 know they are ascending or descending we can sort them in a smart way.
2572 """
2588 """
2573 if self._ascending is None:
2589 if self._ascending is None:
2574 self._list.sort(reverse=reverse)
2590 self._list.sort(reverse=reverse)
2575 self._ascending = not reverse
2591 self._ascending = not reverse
2576 else:
2592 else:
2577 if bool(self._ascending) == bool(reverse):
2593 if bool(self._ascending) == bool(reverse):
2578 self.reverse()
2594 self.reverse()
2579
2595
2580 def isascending(self):
2596 def isascending(self):
2581 return self._ascending is not None and self._ascending
2597 return self._ascending is not None and self._ascending
2582
2598
2583 def isdescending(self):
2599 def isdescending(self):
2584 return self._ascending is not None and not self._ascending
2600 return self._ascending is not None and not self._ascending
2585
2601
2586 def reverse(self):
2602 def reverse(self):
2587 self._list.reverse()
2603 self._list.reverse()
2588 if self._ascending is not None:
2604 if self._ascending is not None:
2589 self._ascending = not self._ascending
2605 self._ascending = not self._ascending
2590
2606
2591 class _generatorset(object):
2607 class _generatorset(object):
2592 """Wrap a generator for lazy iteration
2608 """Wrap a generator for lazy iteration
2593
2609
2594 Wrapper structure for generators that provides lazy membership and can
2610 Wrapper structure for generators that provides lazy membership and can
2595 be iterated more than once.
2611 be iterated more than once.
2596 When asked for membership it generates values until either it finds the
2612 When asked for membership it generates values until either it finds the
2597 requested one or has gone through all the elements in the generator
2613 requested one or has gone through all the elements in the generator
2598
2614
2599 This class does not duck-type baseset and it's only supposed to be used
2615 This class does not duck-type baseset and it's only supposed to be used
2600 internally
2616 internally
2601 """
2617 """
2602 def __init__(self, gen):
2618 def __init__(self, gen):
2603 """
2619 """
2604 gen: a generator producing the values for the generatorset.
2620 gen: a generator producing the values for the generatorset.
2605 """
2621 """
2606 self._gen = gen
2622 self._gen = gen
2607 self._iter = iter(gen)
2623 self._iter = iter(gen)
2608 self._cache = {}
2624 self._cache = {}
2609 self._genlist = baseset([])
2625 self._genlist = baseset([])
2610 self._iterated = False
2626 self._iterated = False
2611 self._finished = False
2627 self._finished = False
2612
2628
2613 def __contains__(self, x):
2629 def __contains__(self, x):
2614 if x in self._cache:
2630 if x in self._cache:
2615 return self._cache[x]
2631 return self._cache[x]
2616
2632
2617 # Use __iter__ which caches values and stores them into self._genlist
2633 # Use __iter__ which caches values and stores them into self._genlist
2618 for l in self:
2634 for l in self:
2619 if l == x:
2635 if l == x:
2620 return True
2636 return True
2621
2637
2622 self._finished = True
2638 self._finished = True
2623 self._cache[x] = False
2639 self._cache[x] = False
2624 return False
2640 return False
2625
2641
2626 def __iter__(self):
2642 def __iter__(self):
2627 if self._iterated:
2643 if self._iterated:
2628 # At least a part of the list should be cached if iteration has
2644 # At least a part of the list should be cached if iteration has
2629 # started over the generatorset.
2645 # started over the generatorset.
2630 for l in self._genlist:
2646 for l in self._genlist:
2631 yield l
2647 yield l
2632 else:
2648 else:
2633 # Starting iteration over the generatorset.
2649 # Starting iteration over the generatorset.
2634 self._iterated = True
2650 self._iterated = True
2635
2651
2636 for item in self._gen:
2652 for item in self._gen:
2637 self._cache[item] = True
2653 self._cache[item] = True
2638 self._genlist.append(item)
2654 self._genlist.append(item)
2639 yield item
2655 yield item
2640
2656
2641 # Iteration over the generator has finished. Whole value list should be
2657 # Iteration over the generator has finished. Whole value list should be
2642 # cached in self._genlist
2658 # cached in self._genlist
2643 self._finished = True
2659 self._finished = True
2644
2660
2645 def set(self):
2661 def set(self):
2646 return self
2662 return self
2647
2663
2648 def sort(self, reverse=False):
2664 def sort(self, reverse=False):
2649 if not self._finished:
2665 if not self._finished:
2650 for i in self:
2666 for i in self:
2651 continue
2667 continue
2652 self._genlist.sort(reverse=reverse)
2668 self._genlist.sort(reverse=reverse)
2653
2669
2654 class _ascgeneratorset(_generatorset):
2670 class _ascgeneratorset(_generatorset):
2655 """Wrap a generator of ascending elements for lazy iteration
2671 """Wrap a generator of ascending elements for lazy iteration
2656
2672
2657 Same structure as _generatorset but stops iterating after it goes past
2673 Same structure as _generatorset but stops iterating after it goes past
2658 the value when asked for membership and the element is not contained
2674 the value when asked for membership and the element is not contained
2659
2675
2660 This class does not duck-type baseset and it's only supposed to be used
2676 This class does not duck-type baseset and it's only supposed to be used
2661 internally
2677 internally
2662 """
2678 """
2663 def __contains__(self, x):
2679 def __contains__(self, x):
2664 if x in self._cache:
2680 if x in self._cache:
2665 return self._cache[x]
2681 return self._cache[x]
2666
2682
2667 for l in self:
2683 for l in self:
2668 if l == x:
2684 if l == x:
2669 return True
2685 return True
2670 if l > x:
2686 if l > x:
2671 break
2687 break
2672
2688
2673 self._cache[x] = False
2689 self._cache[x] = False
2674 return False
2690 return False
2675
2691
2676 class _descgeneratorset(_generatorset):
2692 class _descgeneratorset(_generatorset):
2677 """Wrap a generator of descending elements for lazy iteration
2693 """Wrap a generator of descending elements for lazy iteration
2678
2694
2679 Same structure as _generatorset but stops iterating after it goes past
2695 Same structure as _generatorset but stops iterating after it goes past
2680 the value when asked for membership and the element is not contained
2696 the value when asked for membership and the element is not contained
2681
2697
2682 This class does not duck-type baseset and it's only supposed to be used
2698 This class does not duck-type baseset and it's only supposed to be used
2683 internally
2699 internally
2684 """
2700 """
2685 def __contains__(self, x):
2701 def __contains__(self, x):
2686 if x in self._cache:
2702 if x in self._cache:
2687 return self._cache[x]
2703 return self._cache[x]
2688
2704
2689 for l in self:
2705 for l in self:
2690 if l == x:
2706 if l == x:
2691 return True
2707 return True
2692 if l < x:
2708 if l < x:
2693 break
2709 break
2694
2710
2695 self._cache[x] = False
2711 self._cache[x] = False
2696 return False
2712 return False
2697
2713
2698 class spanset(_orderedsetmixin):
2714 class spanset(_orderedsetmixin):
2699 """Duck type for baseset class which represents a range of revisions and
2715 """Duck type for baseset class which represents a range of revisions and
2700 can work lazily and without having all the range in memory
2716 can work lazily and without having all the range in memory
2701
2717
2702 Note that spanset(x, y) behave almost like xrange(x, y) except for two
2718 Note that spanset(x, y) behave almost like xrange(x, y) except for two
2703 notable points:
2719 notable points:
2704 - when x < y it will be automatically descending,
2720 - when x < y it will be automatically descending,
2705 - revision filtered with this repoview will be skipped.
2721 - revision filtered with this repoview will be skipped.
2706
2722
2707 """
2723 """
2708 def __init__(self, repo, start=0, end=None):
2724 def __init__(self, repo, start=0, end=None):
2709 """
2725 """
2710 start: first revision included the set
2726 start: first revision included the set
2711 (default to 0)
2727 (default to 0)
2712 end: first revision excluded (last+1)
2728 end: first revision excluded (last+1)
2713 (default to len(repo)
2729 (default to len(repo)
2714
2730
2715 Spanset will be descending if `end` < `start`.
2731 Spanset will be descending if `end` < `start`.
2716 """
2732 """
2717 self._start = start
2733 self._start = start
2718 if end is not None:
2734 if end is not None:
2719 self._end = end
2735 self._end = end
2720 else:
2736 else:
2721 self._end = len(repo)
2737 self._end = len(repo)
2722 self._hiddenrevs = repo.changelog.filteredrevs
2738 self._hiddenrevs = repo.changelog.filteredrevs
2723
2739
2724 def ascending(self):
2740 def ascending(self):
2725 if self._start > self._end:
2741 if self._start > self._end:
2726 self.reverse()
2742 self.reverse()
2727
2743
2728 def descending(self):
2744 def descending(self):
2729 if self._start < self._end:
2745 if self._start < self._end:
2730 self.reverse()
2746 self.reverse()
2731
2747
2732 def _contained(self, rev):
2748 def _contained(self, rev):
2733 return (rev <= self._start and rev > self._end) or (rev >= self._start
2749 return (rev <= self._start and rev > self._end) or (rev >= self._start
2734 and rev < self._end)
2750 and rev < self._end)
2735
2751
2736 def __iter__(self):
2752 def __iter__(self):
2737 if self._start <= self._end:
2753 if self._start <= self._end:
2738 iterrange = xrange(self._start, self._end)
2754 iterrange = xrange(self._start, self._end)
2739 else:
2755 else:
2740 iterrange = xrange(self._start, self._end, -1)
2756 iterrange = xrange(self._start, self._end, -1)
2741
2757
2742 if self._hiddenrevs:
2758 if self._hiddenrevs:
2743 s = self._hiddenrevs
2759 s = self._hiddenrevs
2744 for r in iterrange:
2760 for r in iterrange:
2745 if r not in s:
2761 if r not in s:
2746 yield r
2762 yield r
2747 else:
2763 else:
2748 for r in iterrange:
2764 for r in iterrange:
2749 yield r
2765 yield r
2750
2766
2751 def __contains__(self, x):
2767 def __contains__(self, x):
2752 return self._contained(x) and not (self._hiddenrevs and rev in
2768 return self._contained(x) and not (self._hiddenrevs and rev in
2753 self._hiddenrevs)
2769 self._hiddenrevs)
2754
2770
2755 def __nonzero__(self):
2771 def __nonzero__(self):
2756 for r in self:
2772 for r in self:
2757 return True
2773 return True
2758 return False
2774 return False
2759
2775
2760 def __and__(self, x):
2776 def __and__(self, x):
2761 if isinstance(x, baseset):
2777 if isinstance(x, baseset):
2762 x = x.set()
2778 x = x.set()
2763 if self._start <= self._end:
2779 if self._start <= self._end:
2764 return orderedlazyset(self, lambda r: r in x)
2780 return orderedlazyset(self, lambda r: r in x)
2765 else:
2781 else:
2766 return orderedlazyset(self, lambda r: r in x, ascending=False)
2782 return orderedlazyset(self, lambda r: r in x, ascending=False)
2767
2783
2768 def __sub__(self, x):
2784 def __sub__(self, x):
2769 if isinstance(x, baseset):
2785 if isinstance(x, baseset):
2770 x = x.set()
2786 x = x.set()
2771 if self._start <= self._end:
2787 if self._start <= self._end:
2772 return orderedlazyset(self, lambda r: r not in x)
2788 return orderedlazyset(self, lambda r: r not in x)
2773 else:
2789 else:
2774 return orderedlazyset(self, lambda r: r not in x, ascending=False)
2790 return orderedlazyset(self, lambda r: r not in x, ascending=False)
2775
2791
2776 def __add__(self, x):
2792 def __add__(self, x):
2777 kwargs = {}
2793 kwargs = {}
2778 if self.isascending() and x.isascending():
2794 if self.isascending() and x.isascending():
2779 kwargs['ascending'] = True
2795 kwargs['ascending'] = True
2780 if self.isdescending() and x.isdescending():
2796 if self.isdescending() and x.isdescending():
2781 kwargs['ascending'] = False
2797 kwargs['ascending'] = False
2782 return _addset(self, x, **kwargs)
2798 return _addset(self, x, **kwargs)
2783
2799
2784 def __len__(self):
2800 def __len__(self):
2785 if not self._hiddenrevs:
2801 if not self._hiddenrevs:
2786 return abs(self._end - self._start)
2802 return abs(self._end - self._start)
2787 else:
2803 else:
2788 count = 0
2804 count = 0
2789 for rev in self._hiddenrevs:
2805 for rev in self._hiddenrevs:
2790 if self._contained(rev):
2806 if self._contained(rev):
2791 count += 1
2807 count += 1
2792 return abs(self._end - self._start) - count
2808 return abs(self._end - self._start) - count
2793
2809
2794 def __getitem__(self, x):
2810 def __getitem__(self, x):
2795 # Basic implementation to be changed in future patches.
2811 # Basic implementation to be changed in future patches.
2796 l = baseset([r for r in self])
2812 l = baseset([r for r in self])
2797 return l[x]
2813 return l[x]
2798
2814
2799 def sort(self, reverse=False):
2815 def sort(self, reverse=False):
2800 if bool(reverse) != (self._start > self._end):
2816 if bool(reverse) != (self._start > self._end):
2801 self.reverse()
2817 self.reverse()
2802
2818
2803 def reverse(self):
2819 def reverse(self):
2804 # Just switch the _start and _end parameters
2820 # Just switch the _start and _end parameters
2805 if self._start <= self._end:
2821 if self._start <= self._end:
2806 self._start, self._end = self._end - 1, self._start - 1
2822 self._start, self._end = self._end - 1, self._start - 1
2807 else:
2823 else:
2808 self._start, self._end = self._end + 1, self._start + 1
2824 self._start, self._end = self._end + 1, self._start + 1
2809
2825
2810 def set(self):
2826 def set(self):
2811 return self
2827 return self
2812
2828
2813 def isascending(self):
2829 def isascending(self):
2814 return self._start < self._end
2830 return self._start < self._end
2815
2831
2816 def isdescending(self):
2832 def isdescending(self):
2817 return self._start > self._end
2833 return self._start > self._end
2818
2834
2819 def filter(self, l):
2835 def filter(self, l):
2820 if self._start <= self._end:
2836 if self._start <= self._end:
2821 return orderedlazyset(self, l)
2837 return orderedlazyset(self, l)
2822 else:
2838 else:
2823 return orderedlazyset(self, l, ascending=False)
2839 return orderedlazyset(self, l, ascending=False)
2824
2840
2825 # tell hggettext to extract docstrings from these functions:
2841 # tell hggettext to extract docstrings from these functions:
2826 i18nfunctions = symbols.values()
2842 i18nfunctions = symbols.values()
General Comments 0
You need to be logged in to leave comments. Login now