##// END OF EJS Templates
revset: use 'next()' to detect end of iteration in 'limit'...
Pierre-Yves David -
r25144:81a39544 default
parent child Browse files
Show More
@@ -1,3506 +1,3505
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, hbisect, phases
9 import parser, util, error, 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 from i18n import _
13 from i18n import _
14 import encoding
14 import encoding
15 import obsolete as obsmod
15 import obsolete as obsmod
16 import pathutil
16 import pathutil
17 import repoview
17 import repoview
18
18
19 def _revancestors(repo, revs, followfirst):
19 def _revancestors(repo, revs, followfirst):
20 """Like revlog.ancestors(), but supports followfirst."""
20 """Like revlog.ancestors(), but supports followfirst."""
21 if followfirst:
21 if followfirst:
22 cut = 1
22 cut = 1
23 else:
23 else:
24 cut = None
24 cut = None
25 cl = repo.changelog
25 cl = repo.changelog
26
26
27 def iterate():
27 def iterate():
28 revs.sort(reverse=True)
28 revs.sort(reverse=True)
29 irevs = iter(revs)
29 irevs = iter(revs)
30 h = []
30 h = []
31
31
32 inputrev = next(irevs, None)
32 inputrev = next(irevs, None)
33 if inputrev is not None:
33 if inputrev is not None:
34 heapq.heappush(h, -inputrev)
34 heapq.heappush(h, -inputrev)
35
35
36 seen = set()
36 seen = set()
37 while h:
37 while h:
38 current = -heapq.heappop(h)
38 current = -heapq.heappop(h)
39 if current == inputrev:
39 if current == inputrev:
40 inputrev = next(irevs, None)
40 inputrev = next(irevs, None)
41 if inputrev is not None:
41 if inputrev is not None:
42 heapq.heappush(h, -inputrev)
42 heapq.heappush(h, -inputrev)
43 if current not in seen:
43 if current not in seen:
44 seen.add(current)
44 seen.add(current)
45 yield current
45 yield current
46 for parent in cl.parentrevs(current)[:cut]:
46 for parent in cl.parentrevs(current)[:cut]:
47 if parent != node.nullrev:
47 if parent != node.nullrev:
48 heapq.heappush(h, -parent)
48 heapq.heappush(h, -parent)
49
49
50 return generatorset(iterate(), iterasc=False)
50 return generatorset(iterate(), iterasc=False)
51
51
52 def _revdescendants(repo, revs, followfirst):
52 def _revdescendants(repo, revs, followfirst):
53 """Like revlog.descendants() but supports followfirst."""
53 """Like revlog.descendants() but supports followfirst."""
54 if followfirst:
54 if followfirst:
55 cut = 1
55 cut = 1
56 else:
56 else:
57 cut = None
57 cut = None
58
58
59 def iterate():
59 def iterate():
60 cl = repo.changelog
60 cl = repo.changelog
61 first = min(revs)
61 first = min(revs)
62 nullrev = node.nullrev
62 nullrev = node.nullrev
63 if first == nullrev:
63 if first == nullrev:
64 # Are there nodes with a null first parent and a non-null
64 # Are there nodes with a null first parent and a non-null
65 # second one? Maybe. Do we care? Probably not.
65 # second one? Maybe. Do we care? Probably not.
66 for i in cl:
66 for i in cl:
67 yield i
67 yield i
68 else:
68 else:
69 seen = set(revs)
69 seen = set(revs)
70 for i in cl.revs(first + 1):
70 for i in cl.revs(first + 1):
71 for x in cl.parentrevs(i)[:cut]:
71 for x in cl.parentrevs(i)[:cut]:
72 if x != nullrev and x in seen:
72 if x != nullrev and x in seen:
73 seen.add(i)
73 seen.add(i)
74 yield i
74 yield i
75 break
75 break
76
76
77 return generatorset(iterate(), iterasc=True)
77 return generatorset(iterate(), iterasc=True)
78
78
79 def _revsbetween(repo, roots, heads):
79 def _revsbetween(repo, roots, heads):
80 """Return all paths between roots and heads, inclusive of both endpoint
80 """Return all paths between roots and heads, inclusive of both endpoint
81 sets."""
81 sets."""
82 if not roots:
82 if not roots:
83 return baseset()
83 return baseset()
84 parentrevs = repo.changelog.parentrevs
84 parentrevs = repo.changelog.parentrevs
85 visit = list(heads)
85 visit = list(heads)
86 reachable = set()
86 reachable = set()
87 seen = {}
87 seen = {}
88 minroot = min(roots)
88 minroot = min(roots)
89 roots = set(roots)
89 roots = set(roots)
90 # open-code the post-order traversal due to the tiny size of
90 # open-code the post-order traversal due to the tiny size of
91 # sys.getrecursionlimit()
91 # sys.getrecursionlimit()
92 while visit:
92 while visit:
93 rev = visit.pop()
93 rev = visit.pop()
94 if rev in roots:
94 if rev in roots:
95 reachable.add(rev)
95 reachable.add(rev)
96 parents = parentrevs(rev)
96 parents = parentrevs(rev)
97 seen[rev] = parents
97 seen[rev] = parents
98 for parent in parents:
98 for parent in parents:
99 if parent >= minroot and parent not in seen:
99 if parent >= minroot and parent not in seen:
100 visit.append(parent)
100 visit.append(parent)
101 if not reachable:
101 if not reachable:
102 return baseset()
102 return baseset()
103 for rev in sorted(seen):
103 for rev in sorted(seen):
104 for parent in seen[rev]:
104 for parent in seen[rev]:
105 if parent in reachable:
105 if parent in reachable:
106 reachable.add(rev)
106 reachable.add(rev)
107 return baseset(sorted(reachable))
107 return baseset(sorted(reachable))
108
108
109 elements = {
109 elements = {
110 "(": (21, ("group", 1, ")"), ("func", 1, ")")),
110 "(": (21, ("group", 1, ")"), ("func", 1, ")")),
111 "##": (20, None, ("_concat", 20)),
111 "##": (20, None, ("_concat", 20)),
112 "~": (18, None, ("ancestor", 18)),
112 "~": (18, None, ("ancestor", 18)),
113 "^": (18, None, ("parent", 18), ("parentpost", 18)),
113 "^": (18, None, ("parent", 18), ("parentpost", 18)),
114 "-": (5, ("negate", 19), ("minus", 5)),
114 "-": (5, ("negate", 19), ("minus", 5)),
115 "::": (17, ("dagrangepre", 17), ("dagrange", 17),
115 "::": (17, ("dagrangepre", 17), ("dagrange", 17),
116 ("dagrangepost", 17)),
116 ("dagrangepost", 17)),
117 "..": (17, ("dagrangepre", 17), ("dagrange", 17),
117 "..": (17, ("dagrangepre", 17), ("dagrange", 17),
118 ("dagrangepost", 17)),
118 ("dagrangepost", 17)),
119 ":": (15, ("rangepre", 15), ("range", 15), ("rangepost", 15)),
119 ":": (15, ("rangepre", 15), ("range", 15), ("rangepost", 15)),
120 "not": (10, ("not", 10)),
120 "not": (10, ("not", 10)),
121 "!": (10, ("not", 10)),
121 "!": (10, ("not", 10)),
122 "and": (5, None, ("and", 5)),
122 "and": (5, None, ("and", 5)),
123 "&": (5, None, ("and", 5)),
123 "&": (5, None, ("and", 5)),
124 "%": (5, None, ("only", 5), ("onlypost", 5)),
124 "%": (5, None, ("only", 5), ("onlypost", 5)),
125 "or": (4, None, ("or", 4)),
125 "or": (4, None, ("or", 4)),
126 "|": (4, None, ("or", 4)),
126 "|": (4, None, ("or", 4)),
127 "+": (4, None, ("or", 4)),
127 "+": (4, None, ("or", 4)),
128 ",": (2, None, ("list", 2)),
128 ",": (2, None, ("list", 2)),
129 ")": (0, None, None),
129 ")": (0, None, None),
130 "symbol": (0, ("symbol",), None),
130 "symbol": (0, ("symbol",), None),
131 "string": (0, ("string",), None),
131 "string": (0, ("string",), None),
132 "end": (0, None, None),
132 "end": (0, None, None),
133 }
133 }
134
134
135 keywords = set(['and', 'or', 'not'])
135 keywords = set(['and', 'or', 'not'])
136
136
137 # default set of valid characters for the initial letter of symbols
137 # default set of valid characters for the initial letter of symbols
138 _syminitletters = set(c for c in [chr(i) for i in xrange(256)]
138 _syminitletters = set(c for c in [chr(i) for i in xrange(256)]
139 if c.isalnum() or c in '._@' or ord(c) > 127)
139 if c.isalnum() or c in '._@' or ord(c) > 127)
140
140
141 # default set of valid characters for non-initial letters of symbols
141 # default set of valid characters for non-initial letters of symbols
142 _symletters = set(c for c in [chr(i) for i in xrange(256)]
142 _symletters = set(c for c in [chr(i) for i in xrange(256)]
143 if c.isalnum() or c in '-._/@' or ord(c) > 127)
143 if c.isalnum() or c in '-._/@' or ord(c) > 127)
144
144
145 def tokenize(program, lookup=None, syminitletters=None, symletters=None):
145 def tokenize(program, lookup=None, syminitletters=None, symletters=None):
146 '''
146 '''
147 Parse a revset statement into a stream of tokens
147 Parse a revset statement into a stream of tokens
148
148
149 ``syminitletters`` is the set of valid characters for the initial
149 ``syminitletters`` is the set of valid characters for the initial
150 letter of symbols.
150 letter of symbols.
151
151
152 By default, character ``c`` is recognized as valid for initial
152 By default, character ``c`` is recognized as valid for initial
153 letter of symbols, if ``c.isalnum() or c in '._@' or ord(c) > 127``.
153 letter of symbols, if ``c.isalnum() or c in '._@' or ord(c) > 127``.
154
154
155 ``symletters`` is the set of valid characters for non-initial
155 ``symletters`` is the set of valid characters for non-initial
156 letters of symbols.
156 letters of symbols.
157
157
158 By default, character ``c`` is recognized as valid for non-initial
158 By default, character ``c`` is recognized as valid for non-initial
159 letters of symbols, if ``c.isalnum() or c in '-._/@' or ord(c) > 127``.
159 letters of symbols, if ``c.isalnum() or c in '-._/@' or ord(c) > 127``.
160
160
161 Check that @ is a valid unquoted token character (issue3686):
161 Check that @ is a valid unquoted token character (issue3686):
162 >>> list(tokenize("@::"))
162 >>> list(tokenize("@::"))
163 [('symbol', '@', 0), ('::', None, 1), ('end', None, 3)]
163 [('symbol', '@', 0), ('::', None, 1), ('end', None, 3)]
164
164
165 '''
165 '''
166 if syminitletters is None:
166 if syminitletters is None:
167 syminitletters = _syminitletters
167 syminitletters = _syminitletters
168 if symletters is None:
168 if symletters is None:
169 symletters = _symletters
169 symletters = _symletters
170
170
171 pos, l = 0, len(program)
171 pos, l = 0, len(program)
172 while pos < l:
172 while pos < l:
173 c = program[pos]
173 c = program[pos]
174 if c.isspace(): # skip inter-token whitespace
174 if c.isspace(): # skip inter-token whitespace
175 pass
175 pass
176 elif c == ':' and program[pos:pos + 2] == '::': # look ahead carefully
176 elif c == ':' and program[pos:pos + 2] == '::': # look ahead carefully
177 yield ('::', None, pos)
177 yield ('::', None, pos)
178 pos += 1 # skip ahead
178 pos += 1 # skip ahead
179 elif c == '.' and program[pos:pos + 2] == '..': # look ahead carefully
179 elif c == '.' and program[pos:pos + 2] == '..': # look ahead carefully
180 yield ('..', None, pos)
180 yield ('..', None, pos)
181 pos += 1 # skip ahead
181 pos += 1 # skip ahead
182 elif c == '#' and program[pos:pos + 2] == '##': # look ahead carefully
182 elif c == '#' and program[pos:pos + 2] == '##': # look ahead carefully
183 yield ('##', None, pos)
183 yield ('##', None, pos)
184 pos += 1 # skip ahead
184 pos += 1 # skip ahead
185 elif c in "():,-|&+!~^%": # handle simple operators
185 elif c in "():,-|&+!~^%": # handle simple operators
186 yield (c, None, pos)
186 yield (c, None, pos)
187 elif (c in '"\'' or c == 'r' and
187 elif (c in '"\'' or c == 'r' and
188 program[pos:pos + 2] in ("r'", 'r"')): # handle quoted strings
188 program[pos:pos + 2] in ("r'", 'r"')): # handle quoted strings
189 if c == 'r':
189 if c == 'r':
190 pos += 1
190 pos += 1
191 c = program[pos]
191 c = program[pos]
192 decode = lambda x: x
192 decode = lambda x: x
193 else:
193 else:
194 decode = lambda x: x.decode('string-escape')
194 decode = lambda x: x.decode('string-escape')
195 pos += 1
195 pos += 1
196 s = pos
196 s = pos
197 while pos < l: # find closing quote
197 while pos < l: # find closing quote
198 d = program[pos]
198 d = program[pos]
199 if d == '\\': # skip over escaped characters
199 if d == '\\': # skip over escaped characters
200 pos += 2
200 pos += 2
201 continue
201 continue
202 if d == c:
202 if d == c:
203 yield ('string', decode(program[s:pos]), s)
203 yield ('string', decode(program[s:pos]), s)
204 break
204 break
205 pos += 1
205 pos += 1
206 else:
206 else:
207 raise error.ParseError(_("unterminated string"), s)
207 raise error.ParseError(_("unterminated string"), s)
208 # gather up a symbol/keyword
208 # gather up a symbol/keyword
209 elif c in syminitletters:
209 elif c in syminitletters:
210 s = pos
210 s = pos
211 pos += 1
211 pos += 1
212 while pos < l: # find end of symbol
212 while pos < l: # find end of symbol
213 d = program[pos]
213 d = program[pos]
214 if d not in symletters:
214 if d not in symletters:
215 break
215 break
216 if d == '.' and program[pos - 1] == '.': # special case for ..
216 if d == '.' and program[pos - 1] == '.': # special case for ..
217 pos -= 1
217 pos -= 1
218 break
218 break
219 pos += 1
219 pos += 1
220 sym = program[s:pos]
220 sym = program[s:pos]
221 if sym in keywords: # operator keywords
221 if sym in keywords: # operator keywords
222 yield (sym, None, s)
222 yield (sym, None, s)
223 elif '-' in sym:
223 elif '-' in sym:
224 # some jerk gave us foo-bar-baz, try to check if it's a symbol
224 # some jerk gave us foo-bar-baz, try to check if it's a symbol
225 if lookup and lookup(sym):
225 if lookup and lookup(sym):
226 # looks like a real symbol
226 # looks like a real symbol
227 yield ('symbol', sym, s)
227 yield ('symbol', sym, s)
228 else:
228 else:
229 # looks like an expression
229 # looks like an expression
230 parts = sym.split('-')
230 parts = sym.split('-')
231 for p in parts[:-1]:
231 for p in parts[:-1]:
232 if p: # possible consecutive -
232 if p: # possible consecutive -
233 yield ('symbol', p, s)
233 yield ('symbol', p, s)
234 s += len(p)
234 s += len(p)
235 yield ('-', None, pos)
235 yield ('-', None, pos)
236 s += 1
236 s += 1
237 if parts[-1]: # possible trailing -
237 if parts[-1]: # possible trailing -
238 yield ('symbol', parts[-1], s)
238 yield ('symbol', parts[-1], s)
239 else:
239 else:
240 yield ('symbol', sym, s)
240 yield ('symbol', sym, s)
241 pos -= 1
241 pos -= 1
242 else:
242 else:
243 raise error.ParseError(_("syntax error in revset '%s'") %
243 raise error.ParseError(_("syntax error in revset '%s'") %
244 program, pos)
244 program, pos)
245 pos += 1
245 pos += 1
246 yield ('end', None, pos)
246 yield ('end', None, pos)
247
247
248 def parseerrordetail(inst):
248 def parseerrordetail(inst):
249 """Compose error message from specified ParseError object
249 """Compose error message from specified ParseError object
250 """
250 """
251 if len(inst.args) > 1:
251 if len(inst.args) > 1:
252 return _('at %s: %s') % (inst.args[1], inst.args[0])
252 return _('at %s: %s') % (inst.args[1], inst.args[0])
253 else:
253 else:
254 return inst.args[0]
254 return inst.args[0]
255
255
256 # helpers
256 # helpers
257
257
258 def getstring(x, err):
258 def getstring(x, err):
259 if x and (x[0] == 'string' or x[0] == 'symbol'):
259 if x and (x[0] == 'string' or x[0] == 'symbol'):
260 return x[1]
260 return x[1]
261 raise error.ParseError(err)
261 raise error.ParseError(err)
262
262
263 def getlist(x):
263 def getlist(x):
264 if not x:
264 if not x:
265 return []
265 return []
266 if x[0] == 'list':
266 if x[0] == 'list':
267 return getlist(x[1]) + [x[2]]
267 return getlist(x[1]) + [x[2]]
268 return [x]
268 return [x]
269
269
270 def getargs(x, min, max, err):
270 def getargs(x, min, max, err):
271 l = getlist(x)
271 l = getlist(x)
272 if len(l) < min or (max >= 0 and len(l) > max):
272 if len(l) < min or (max >= 0 and len(l) > max):
273 raise error.ParseError(err)
273 raise error.ParseError(err)
274 return l
274 return l
275
275
276 def isvalidsymbol(tree):
276 def isvalidsymbol(tree):
277 """Examine whether specified ``tree`` is valid ``symbol`` or not
277 """Examine whether specified ``tree`` is valid ``symbol`` or not
278 """
278 """
279 return tree[0] == 'symbol' and len(tree) > 1
279 return tree[0] == 'symbol' and len(tree) > 1
280
280
281 def getsymbol(tree):
281 def getsymbol(tree):
282 """Get symbol name from valid ``symbol`` in ``tree``
282 """Get symbol name from valid ``symbol`` in ``tree``
283
283
284 This assumes that ``tree`` is already examined by ``isvalidsymbol``.
284 This assumes that ``tree`` is already examined by ``isvalidsymbol``.
285 """
285 """
286 return tree[1]
286 return tree[1]
287
287
288 def isvalidfunc(tree):
288 def isvalidfunc(tree):
289 """Examine whether specified ``tree`` is valid ``func`` or not
289 """Examine whether specified ``tree`` is valid ``func`` or not
290 """
290 """
291 return tree[0] == 'func' and len(tree) > 1 and isvalidsymbol(tree[1])
291 return tree[0] == 'func' and len(tree) > 1 and isvalidsymbol(tree[1])
292
292
293 def getfuncname(tree):
293 def getfuncname(tree):
294 """Get function name from valid ``func`` in ``tree``
294 """Get function name from valid ``func`` in ``tree``
295
295
296 This assumes that ``tree`` is already examined by ``isvalidfunc``.
296 This assumes that ``tree`` is already examined by ``isvalidfunc``.
297 """
297 """
298 return getsymbol(tree[1])
298 return getsymbol(tree[1])
299
299
300 def getfuncargs(tree):
300 def getfuncargs(tree):
301 """Get list of function arguments from valid ``func`` in ``tree``
301 """Get list of function arguments from valid ``func`` in ``tree``
302
302
303 This assumes that ``tree`` is already examined by ``isvalidfunc``.
303 This assumes that ``tree`` is already examined by ``isvalidfunc``.
304 """
304 """
305 if len(tree) > 2:
305 if len(tree) > 2:
306 return getlist(tree[2])
306 return getlist(tree[2])
307 else:
307 else:
308 return []
308 return []
309
309
310 def getset(repo, subset, x):
310 def getset(repo, subset, x):
311 if not x:
311 if not x:
312 raise error.ParseError(_("missing argument"))
312 raise error.ParseError(_("missing argument"))
313 s = methods[x[0]](repo, subset, *x[1:])
313 s = methods[x[0]](repo, subset, *x[1:])
314 if util.safehasattr(s, 'isascending'):
314 if util.safehasattr(s, 'isascending'):
315 return s
315 return s
316 return baseset(s)
316 return baseset(s)
317
317
318 def _getrevsource(repo, r):
318 def _getrevsource(repo, r):
319 extra = repo[r].extra()
319 extra = repo[r].extra()
320 for label in ('source', 'transplant_source', 'rebase_source'):
320 for label in ('source', 'transplant_source', 'rebase_source'):
321 if label in extra:
321 if label in extra:
322 try:
322 try:
323 return repo[extra[label]].rev()
323 return repo[extra[label]].rev()
324 except error.RepoLookupError:
324 except error.RepoLookupError:
325 pass
325 pass
326 return None
326 return None
327
327
328 # operator methods
328 # operator methods
329
329
330 def stringset(repo, subset, x):
330 def stringset(repo, subset, x):
331 x = repo[x].rev()
331 x = repo[x].rev()
332 if x in subset:
332 if x in subset:
333 return baseset([x])
333 return baseset([x])
334 return baseset()
334 return baseset()
335
335
336 def rangeset(repo, subset, x, y):
336 def rangeset(repo, subset, x, y):
337 m = getset(repo, fullreposet(repo), x)
337 m = getset(repo, fullreposet(repo), x)
338 n = getset(repo, fullreposet(repo), y)
338 n = getset(repo, fullreposet(repo), y)
339
339
340 if not m or not n:
340 if not m or not n:
341 return baseset()
341 return baseset()
342 m, n = m.first(), n.last()
342 m, n = m.first(), n.last()
343
343
344 if m < n:
344 if m < n:
345 r = spanset(repo, m, n + 1)
345 r = spanset(repo, m, n + 1)
346 else:
346 else:
347 r = spanset(repo, m, n - 1)
347 r = spanset(repo, m, n - 1)
348 return r & subset
348 return r & subset
349
349
350 def dagrange(repo, subset, x, y):
350 def dagrange(repo, subset, x, y):
351 r = fullreposet(repo)
351 r = fullreposet(repo)
352 xs = _revsbetween(repo, getset(repo, r, x), getset(repo, r, y))
352 xs = _revsbetween(repo, getset(repo, r, x), getset(repo, r, y))
353 return xs & subset
353 return xs & subset
354
354
355 def andset(repo, subset, x, y):
355 def andset(repo, subset, x, y):
356 return getset(repo, getset(repo, subset, x), y)
356 return getset(repo, getset(repo, subset, x), y)
357
357
358 def orset(repo, subset, x, y):
358 def orset(repo, subset, x, y):
359 xl = getset(repo, subset, x)
359 xl = getset(repo, subset, x)
360 yl = getset(repo, subset, y)
360 yl = getset(repo, subset, y)
361 return xl + yl
361 return xl + yl
362
362
363 def notset(repo, subset, x):
363 def notset(repo, subset, x):
364 return subset - getset(repo, subset, x)
364 return subset - getset(repo, subset, x)
365
365
366 def listset(repo, subset, a, b):
366 def listset(repo, subset, a, b):
367 raise error.ParseError(_("can't use a list in this context"))
367 raise error.ParseError(_("can't use a list in this context"))
368
368
369 def func(repo, subset, a, b):
369 def func(repo, subset, a, b):
370 if a[0] == 'symbol' and a[1] in symbols:
370 if a[0] == 'symbol' and a[1] in symbols:
371 return symbols[a[1]](repo, subset, b)
371 return symbols[a[1]](repo, subset, b)
372 raise error.UnknownIdentifier(a[1], symbols.keys())
372 raise error.UnknownIdentifier(a[1], symbols.keys())
373
373
374 # functions
374 # functions
375
375
376 def adds(repo, subset, x):
376 def adds(repo, subset, x):
377 """``adds(pattern)``
377 """``adds(pattern)``
378 Changesets that add a file matching pattern.
378 Changesets that add a file matching pattern.
379
379
380 The pattern without explicit kind like ``glob:`` is expected to be
380 The pattern without explicit kind like ``glob:`` is expected to be
381 relative to the current directory and match against a file or a
381 relative to the current directory and match against a file or a
382 directory.
382 directory.
383 """
383 """
384 # i18n: "adds" is a keyword
384 # i18n: "adds" is a keyword
385 pat = getstring(x, _("adds requires a pattern"))
385 pat = getstring(x, _("adds requires a pattern"))
386 return checkstatus(repo, subset, pat, 1)
386 return checkstatus(repo, subset, pat, 1)
387
387
388 def ancestor(repo, subset, x):
388 def ancestor(repo, subset, x):
389 """``ancestor(*changeset)``
389 """``ancestor(*changeset)``
390 A greatest common ancestor of the changesets.
390 A greatest common ancestor of the changesets.
391
391
392 Accepts 0 or more changesets.
392 Accepts 0 or more changesets.
393 Will return empty list when passed no args.
393 Will return empty list when passed no args.
394 Greatest common ancestor of a single changeset is that changeset.
394 Greatest common ancestor of a single changeset is that changeset.
395 """
395 """
396 # i18n: "ancestor" is a keyword
396 # i18n: "ancestor" is a keyword
397 l = getlist(x)
397 l = getlist(x)
398 rl = fullreposet(repo)
398 rl = fullreposet(repo)
399 anc = None
399 anc = None
400
400
401 # (getset(repo, rl, i) for i in l) generates a list of lists
401 # (getset(repo, rl, i) for i in l) generates a list of lists
402 for revs in (getset(repo, rl, i) for i in l):
402 for revs in (getset(repo, rl, i) for i in l):
403 for r in revs:
403 for r in revs:
404 if anc is None:
404 if anc is None:
405 anc = repo[r]
405 anc = repo[r]
406 else:
406 else:
407 anc = anc.ancestor(repo[r])
407 anc = anc.ancestor(repo[r])
408
408
409 if anc is not None and anc.rev() in subset:
409 if anc is not None and anc.rev() in subset:
410 return baseset([anc.rev()])
410 return baseset([anc.rev()])
411 return baseset()
411 return baseset()
412
412
413 def _ancestors(repo, subset, x, followfirst=False):
413 def _ancestors(repo, subset, x, followfirst=False):
414 heads = getset(repo, fullreposet(repo), x)
414 heads = getset(repo, fullreposet(repo), x)
415 if not heads:
415 if not heads:
416 return baseset()
416 return baseset()
417 s = _revancestors(repo, heads, followfirst)
417 s = _revancestors(repo, heads, followfirst)
418 return subset & s
418 return subset & s
419
419
420 def ancestors(repo, subset, x):
420 def ancestors(repo, subset, x):
421 """``ancestors(set)``
421 """``ancestors(set)``
422 Changesets that are ancestors of a changeset in set.
422 Changesets that are ancestors of a changeset in set.
423 """
423 """
424 return _ancestors(repo, subset, x)
424 return _ancestors(repo, subset, x)
425
425
426 def _firstancestors(repo, subset, x):
426 def _firstancestors(repo, subset, x):
427 # ``_firstancestors(set)``
427 # ``_firstancestors(set)``
428 # Like ``ancestors(set)`` but follows only the first parents.
428 # Like ``ancestors(set)`` but follows only the first parents.
429 return _ancestors(repo, subset, x, followfirst=True)
429 return _ancestors(repo, subset, x, followfirst=True)
430
430
431 def ancestorspec(repo, subset, x, n):
431 def ancestorspec(repo, subset, x, n):
432 """``set~n``
432 """``set~n``
433 Changesets that are the Nth ancestor (first parents only) of a changeset
433 Changesets that are the Nth ancestor (first parents only) of a changeset
434 in set.
434 in set.
435 """
435 """
436 try:
436 try:
437 n = int(n[1])
437 n = int(n[1])
438 except (TypeError, ValueError):
438 except (TypeError, ValueError):
439 raise error.ParseError(_("~ expects a number"))
439 raise error.ParseError(_("~ expects a number"))
440 ps = set()
440 ps = set()
441 cl = repo.changelog
441 cl = repo.changelog
442 for r in getset(repo, fullreposet(repo), x):
442 for r in getset(repo, fullreposet(repo), x):
443 for i in range(n):
443 for i in range(n):
444 r = cl.parentrevs(r)[0]
444 r = cl.parentrevs(r)[0]
445 ps.add(r)
445 ps.add(r)
446 return subset & ps
446 return subset & ps
447
447
448 def author(repo, subset, x):
448 def author(repo, subset, x):
449 """``author(string)``
449 """``author(string)``
450 Alias for ``user(string)``.
450 Alias for ``user(string)``.
451 """
451 """
452 # i18n: "author" is a keyword
452 # i18n: "author" is a keyword
453 n = encoding.lower(getstring(x, _("author requires a string")))
453 n = encoding.lower(getstring(x, _("author requires a string")))
454 kind, pattern, matcher = _substringmatcher(n)
454 kind, pattern, matcher = _substringmatcher(n)
455 return subset.filter(lambda x: matcher(encoding.lower(repo[x].user())))
455 return subset.filter(lambda x: matcher(encoding.lower(repo[x].user())))
456
456
457 def bisect(repo, subset, x):
457 def bisect(repo, subset, x):
458 """``bisect(string)``
458 """``bisect(string)``
459 Changesets marked in the specified bisect status:
459 Changesets marked in the specified bisect status:
460
460
461 - ``good``, ``bad``, ``skip``: csets explicitly marked as good/bad/skip
461 - ``good``, ``bad``, ``skip``: csets explicitly marked as good/bad/skip
462 - ``goods``, ``bads`` : csets topologically good/bad
462 - ``goods``, ``bads`` : csets topologically good/bad
463 - ``range`` : csets taking part in the bisection
463 - ``range`` : csets taking part in the bisection
464 - ``pruned`` : csets that are goods, bads or skipped
464 - ``pruned`` : csets that are goods, bads or skipped
465 - ``untested`` : csets whose fate is yet unknown
465 - ``untested`` : csets whose fate is yet unknown
466 - ``ignored`` : csets ignored due to DAG topology
466 - ``ignored`` : csets ignored due to DAG topology
467 - ``current`` : the cset currently being bisected
467 - ``current`` : the cset currently being bisected
468 """
468 """
469 # i18n: "bisect" is a keyword
469 # i18n: "bisect" is a keyword
470 status = getstring(x, _("bisect requires a string")).lower()
470 status = getstring(x, _("bisect requires a string")).lower()
471 state = set(hbisect.get(repo, status))
471 state = set(hbisect.get(repo, status))
472 return subset & state
472 return subset & state
473
473
474 # Backward-compatibility
474 # Backward-compatibility
475 # - no help entry so that we do not advertise it any more
475 # - no help entry so that we do not advertise it any more
476 def bisected(repo, subset, x):
476 def bisected(repo, subset, x):
477 return bisect(repo, subset, x)
477 return bisect(repo, subset, x)
478
478
479 def bookmark(repo, subset, x):
479 def bookmark(repo, subset, x):
480 """``bookmark([name])``
480 """``bookmark([name])``
481 The named bookmark or all bookmarks.
481 The named bookmark or all bookmarks.
482
482
483 If `name` starts with `re:`, the remainder of the name is treated as
483 If `name` starts with `re:`, the remainder of the name is treated as
484 a regular expression. To match a bookmark that actually starts with `re:`,
484 a regular expression. To match a bookmark that actually starts with `re:`,
485 use the prefix `literal:`.
485 use the prefix `literal:`.
486 """
486 """
487 # i18n: "bookmark" is a keyword
487 # i18n: "bookmark" is a keyword
488 args = getargs(x, 0, 1, _('bookmark takes one or no arguments'))
488 args = getargs(x, 0, 1, _('bookmark takes one or no arguments'))
489 if args:
489 if args:
490 bm = getstring(args[0],
490 bm = getstring(args[0],
491 # i18n: "bookmark" is a keyword
491 # i18n: "bookmark" is a keyword
492 _('the argument to bookmark must be a string'))
492 _('the argument to bookmark must be a string'))
493 kind, pattern, matcher = _stringmatcher(bm)
493 kind, pattern, matcher = _stringmatcher(bm)
494 bms = set()
494 bms = set()
495 if kind == 'literal':
495 if kind == 'literal':
496 bmrev = repo._bookmarks.get(pattern, None)
496 bmrev = repo._bookmarks.get(pattern, None)
497 if not bmrev:
497 if not bmrev:
498 raise error.RepoLookupError(_("bookmark '%s' does not exist")
498 raise error.RepoLookupError(_("bookmark '%s' does not exist")
499 % bm)
499 % bm)
500 bms.add(repo[bmrev].rev())
500 bms.add(repo[bmrev].rev())
501 else:
501 else:
502 matchrevs = set()
502 matchrevs = set()
503 for name, bmrev in repo._bookmarks.iteritems():
503 for name, bmrev in repo._bookmarks.iteritems():
504 if matcher(name):
504 if matcher(name):
505 matchrevs.add(bmrev)
505 matchrevs.add(bmrev)
506 if not matchrevs:
506 if not matchrevs:
507 raise error.RepoLookupError(_("no bookmarks exist"
507 raise error.RepoLookupError(_("no bookmarks exist"
508 " that match '%s'") % pattern)
508 " that match '%s'") % pattern)
509 for bmrev in matchrevs:
509 for bmrev in matchrevs:
510 bms.add(repo[bmrev].rev())
510 bms.add(repo[bmrev].rev())
511 else:
511 else:
512 bms = set([repo[r].rev()
512 bms = set([repo[r].rev()
513 for r in repo._bookmarks.values()])
513 for r in repo._bookmarks.values()])
514 bms -= set([node.nullrev])
514 bms -= set([node.nullrev])
515 return subset & bms
515 return subset & bms
516
516
517 def branch(repo, subset, x):
517 def branch(repo, subset, x):
518 """``branch(string or set)``
518 """``branch(string or set)``
519 All changesets belonging to the given branch or the branches of the given
519 All changesets belonging to the given branch or the branches of the given
520 changesets.
520 changesets.
521
521
522 If `string` starts with `re:`, the remainder of the name is treated as
522 If `string` starts with `re:`, the remainder of the name is treated as
523 a regular expression. To match a branch that actually starts with `re:`,
523 a regular expression. To match a branch that actually starts with `re:`,
524 use the prefix `literal:`.
524 use the prefix `literal:`.
525 """
525 """
526 getbi = repo.revbranchcache().branchinfo
526 getbi = repo.revbranchcache().branchinfo
527
527
528 try:
528 try:
529 b = getstring(x, '')
529 b = getstring(x, '')
530 except error.ParseError:
530 except error.ParseError:
531 # not a string, but another revspec, e.g. tip()
531 # not a string, but another revspec, e.g. tip()
532 pass
532 pass
533 else:
533 else:
534 kind, pattern, matcher = _stringmatcher(b)
534 kind, pattern, matcher = _stringmatcher(b)
535 if kind == 'literal':
535 if kind == 'literal':
536 # note: falls through to the revspec case if no branch with
536 # note: falls through to the revspec case if no branch with
537 # this name exists
537 # this name exists
538 if pattern in repo.branchmap():
538 if pattern in repo.branchmap():
539 return subset.filter(lambda r: matcher(getbi(r)[0]))
539 return subset.filter(lambda r: matcher(getbi(r)[0]))
540 else:
540 else:
541 return subset.filter(lambda r: matcher(getbi(r)[0]))
541 return subset.filter(lambda r: matcher(getbi(r)[0]))
542
542
543 s = getset(repo, fullreposet(repo), x)
543 s = getset(repo, fullreposet(repo), x)
544 b = set()
544 b = set()
545 for r in s:
545 for r in s:
546 b.add(getbi(r)[0])
546 b.add(getbi(r)[0])
547 c = s.__contains__
547 c = s.__contains__
548 return subset.filter(lambda r: c(r) or getbi(r)[0] in b)
548 return subset.filter(lambda r: c(r) or getbi(r)[0] in b)
549
549
550 def bumped(repo, subset, x):
550 def bumped(repo, subset, x):
551 """``bumped()``
551 """``bumped()``
552 Mutable changesets marked as successors of public changesets.
552 Mutable changesets marked as successors of public changesets.
553
553
554 Only non-public and non-obsolete changesets can be `bumped`.
554 Only non-public and non-obsolete changesets can be `bumped`.
555 """
555 """
556 # i18n: "bumped" is a keyword
556 # i18n: "bumped" is a keyword
557 getargs(x, 0, 0, _("bumped takes no arguments"))
557 getargs(x, 0, 0, _("bumped takes no arguments"))
558 bumped = obsmod.getrevs(repo, 'bumped')
558 bumped = obsmod.getrevs(repo, 'bumped')
559 return subset & bumped
559 return subset & bumped
560
560
561 def bundle(repo, subset, x):
561 def bundle(repo, subset, x):
562 """``bundle()``
562 """``bundle()``
563 Changesets in the bundle.
563 Changesets in the bundle.
564
564
565 Bundle must be specified by the -R option."""
565 Bundle must be specified by the -R option."""
566
566
567 try:
567 try:
568 bundlerevs = repo.changelog.bundlerevs
568 bundlerevs = repo.changelog.bundlerevs
569 except AttributeError:
569 except AttributeError:
570 raise util.Abort(_("no bundle provided - specify with -R"))
570 raise util.Abort(_("no bundle provided - specify with -R"))
571 return subset & bundlerevs
571 return subset & bundlerevs
572
572
573 def checkstatus(repo, subset, pat, field):
573 def checkstatus(repo, subset, pat, field):
574 hasset = matchmod.patkind(pat) == 'set'
574 hasset = matchmod.patkind(pat) == 'set'
575
575
576 mcache = [None]
576 mcache = [None]
577 def matches(x):
577 def matches(x):
578 c = repo[x]
578 c = repo[x]
579 if not mcache[0] or hasset:
579 if not mcache[0] or hasset:
580 mcache[0] = matchmod.match(repo.root, repo.getcwd(), [pat], ctx=c)
580 mcache[0] = matchmod.match(repo.root, repo.getcwd(), [pat], ctx=c)
581 m = mcache[0]
581 m = mcache[0]
582 fname = None
582 fname = None
583 if not m.anypats() and len(m.files()) == 1:
583 if not m.anypats() and len(m.files()) == 1:
584 fname = m.files()[0]
584 fname = m.files()[0]
585 if fname is not None:
585 if fname is not None:
586 if fname not in c.files():
586 if fname not in c.files():
587 return False
587 return False
588 else:
588 else:
589 for f in c.files():
589 for f in c.files():
590 if m(f):
590 if m(f):
591 break
591 break
592 else:
592 else:
593 return False
593 return False
594 files = repo.status(c.p1().node(), c.node())[field]
594 files = repo.status(c.p1().node(), c.node())[field]
595 if fname is not None:
595 if fname is not None:
596 if fname in files:
596 if fname in files:
597 return True
597 return True
598 else:
598 else:
599 for f in files:
599 for f in files:
600 if m(f):
600 if m(f):
601 return True
601 return True
602
602
603 return subset.filter(matches)
603 return subset.filter(matches)
604
604
605 def _children(repo, narrow, parentset):
605 def _children(repo, narrow, parentset):
606 cs = set()
606 cs = set()
607 if not parentset:
607 if not parentset:
608 return baseset(cs)
608 return baseset(cs)
609 pr = repo.changelog.parentrevs
609 pr = repo.changelog.parentrevs
610 minrev = min(parentset)
610 minrev = min(parentset)
611 for r in narrow:
611 for r in narrow:
612 if r <= minrev:
612 if r <= minrev:
613 continue
613 continue
614 for p in pr(r):
614 for p in pr(r):
615 if p in parentset:
615 if p in parentset:
616 cs.add(r)
616 cs.add(r)
617 return baseset(cs)
617 return baseset(cs)
618
618
619 def children(repo, subset, x):
619 def children(repo, subset, x):
620 """``children(set)``
620 """``children(set)``
621 Child changesets of changesets in set.
621 Child changesets of changesets in set.
622 """
622 """
623 s = getset(repo, fullreposet(repo), x)
623 s = getset(repo, fullreposet(repo), x)
624 cs = _children(repo, subset, s)
624 cs = _children(repo, subset, s)
625 return subset & cs
625 return subset & cs
626
626
627 def closed(repo, subset, x):
627 def closed(repo, subset, x):
628 """``closed()``
628 """``closed()``
629 Changeset is closed.
629 Changeset is closed.
630 """
630 """
631 # i18n: "closed" is a keyword
631 # i18n: "closed" is a keyword
632 getargs(x, 0, 0, _("closed takes no arguments"))
632 getargs(x, 0, 0, _("closed takes no arguments"))
633 return subset.filter(lambda r: repo[r].closesbranch())
633 return subset.filter(lambda r: repo[r].closesbranch())
634
634
635 def contains(repo, subset, x):
635 def contains(repo, subset, x):
636 """``contains(pattern)``
636 """``contains(pattern)``
637 The revision's manifest contains a file matching pattern (but might not
637 The revision's manifest contains a file matching pattern (but might not
638 modify it). See :hg:`help patterns` for information about file patterns.
638 modify it). See :hg:`help patterns` for information about file patterns.
639
639
640 The pattern without explicit kind like ``glob:`` is expected to be
640 The pattern without explicit kind like ``glob:`` is expected to be
641 relative to the current directory and match against a file exactly
641 relative to the current directory and match against a file exactly
642 for efficiency.
642 for efficiency.
643 """
643 """
644 # i18n: "contains" is a keyword
644 # i18n: "contains" is a keyword
645 pat = getstring(x, _("contains requires a pattern"))
645 pat = getstring(x, _("contains requires a pattern"))
646
646
647 def matches(x):
647 def matches(x):
648 if not matchmod.patkind(pat):
648 if not matchmod.patkind(pat):
649 pats = pathutil.canonpath(repo.root, repo.getcwd(), pat)
649 pats = pathutil.canonpath(repo.root, repo.getcwd(), pat)
650 if pats in repo[x]:
650 if pats in repo[x]:
651 return True
651 return True
652 else:
652 else:
653 c = repo[x]
653 c = repo[x]
654 m = matchmod.match(repo.root, repo.getcwd(), [pat], ctx=c)
654 m = matchmod.match(repo.root, repo.getcwd(), [pat], ctx=c)
655 for f in c.manifest():
655 for f in c.manifest():
656 if m(f):
656 if m(f):
657 return True
657 return True
658 return False
658 return False
659
659
660 return subset.filter(matches)
660 return subset.filter(matches)
661
661
662 def converted(repo, subset, x):
662 def converted(repo, subset, x):
663 """``converted([id])``
663 """``converted([id])``
664 Changesets converted from the given identifier in the old repository if
664 Changesets converted from the given identifier in the old repository if
665 present, or all converted changesets if no identifier is specified.
665 present, or all converted changesets if no identifier is specified.
666 """
666 """
667
667
668 # There is exactly no chance of resolving the revision, so do a simple
668 # There is exactly no chance of resolving the revision, so do a simple
669 # string compare and hope for the best
669 # string compare and hope for the best
670
670
671 rev = None
671 rev = None
672 # i18n: "converted" is a keyword
672 # i18n: "converted" is a keyword
673 l = getargs(x, 0, 1, _('converted takes one or no arguments'))
673 l = getargs(x, 0, 1, _('converted takes one or no arguments'))
674 if l:
674 if l:
675 # i18n: "converted" is a keyword
675 # i18n: "converted" is a keyword
676 rev = getstring(l[0], _('converted requires a revision'))
676 rev = getstring(l[0], _('converted requires a revision'))
677
677
678 def _matchvalue(r):
678 def _matchvalue(r):
679 source = repo[r].extra().get('convert_revision', None)
679 source = repo[r].extra().get('convert_revision', None)
680 return source is not None and (rev is None or source.startswith(rev))
680 return source is not None and (rev is None or source.startswith(rev))
681
681
682 return subset.filter(lambda r: _matchvalue(r))
682 return subset.filter(lambda r: _matchvalue(r))
683
683
684 def date(repo, subset, x):
684 def date(repo, subset, x):
685 """``date(interval)``
685 """``date(interval)``
686 Changesets within the interval, see :hg:`help dates`.
686 Changesets within the interval, see :hg:`help dates`.
687 """
687 """
688 # i18n: "date" is a keyword
688 # i18n: "date" is a keyword
689 ds = getstring(x, _("date requires a string"))
689 ds = getstring(x, _("date requires a string"))
690 dm = util.matchdate(ds)
690 dm = util.matchdate(ds)
691 return subset.filter(lambda x: dm(repo[x].date()[0]))
691 return subset.filter(lambda x: dm(repo[x].date()[0]))
692
692
693 def desc(repo, subset, x):
693 def desc(repo, subset, x):
694 """``desc(string)``
694 """``desc(string)``
695 Search commit message for string. The match is case-insensitive.
695 Search commit message for string. The match is case-insensitive.
696 """
696 """
697 # i18n: "desc" is a keyword
697 # i18n: "desc" is a keyword
698 ds = encoding.lower(getstring(x, _("desc requires a string")))
698 ds = encoding.lower(getstring(x, _("desc requires a string")))
699
699
700 def matches(x):
700 def matches(x):
701 c = repo[x]
701 c = repo[x]
702 return ds in encoding.lower(c.description())
702 return ds in encoding.lower(c.description())
703
703
704 return subset.filter(matches)
704 return subset.filter(matches)
705
705
706 def _descendants(repo, subset, x, followfirst=False):
706 def _descendants(repo, subset, x, followfirst=False):
707 roots = getset(repo, fullreposet(repo), x)
707 roots = getset(repo, fullreposet(repo), x)
708 if not roots:
708 if not roots:
709 return baseset()
709 return baseset()
710 s = _revdescendants(repo, roots, followfirst)
710 s = _revdescendants(repo, roots, followfirst)
711
711
712 # Both sets need to be ascending in order to lazily return the union
712 # Both sets need to be ascending in order to lazily return the union
713 # in the correct order.
713 # in the correct order.
714 base = subset & roots
714 base = subset & roots
715 desc = subset & s
715 desc = subset & s
716 result = base + desc
716 result = base + desc
717 if subset.isascending():
717 if subset.isascending():
718 result.sort()
718 result.sort()
719 elif subset.isdescending():
719 elif subset.isdescending():
720 result.sort(reverse=True)
720 result.sort(reverse=True)
721 else:
721 else:
722 result = subset & result
722 result = subset & result
723 return result
723 return result
724
724
725 def descendants(repo, subset, x):
725 def descendants(repo, subset, x):
726 """``descendants(set)``
726 """``descendants(set)``
727 Changesets which are descendants of changesets in set.
727 Changesets which are descendants of changesets in set.
728 """
728 """
729 return _descendants(repo, subset, x)
729 return _descendants(repo, subset, x)
730
730
731 def _firstdescendants(repo, subset, x):
731 def _firstdescendants(repo, subset, x):
732 # ``_firstdescendants(set)``
732 # ``_firstdescendants(set)``
733 # Like ``descendants(set)`` but follows only the first parents.
733 # Like ``descendants(set)`` but follows only the first parents.
734 return _descendants(repo, subset, x, followfirst=True)
734 return _descendants(repo, subset, x, followfirst=True)
735
735
736 def destination(repo, subset, x):
736 def destination(repo, subset, x):
737 """``destination([set])``
737 """``destination([set])``
738 Changesets that were created by a graft, transplant or rebase operation,
738 Changesets that were created by a graft, transplant or rebase operation,
739 with the given revisions specified as the source. Omitting the optional set
739 with the given revisions specified as the source. Omitting the optional set
740 is the same as passing all().
740 is the same as passing all().
741 """
741 """
742 if x is not None:
742 if x is not None:
743 sources = getset(repo, fullreposet(repo), x)
743 sources = getset(repo, fullreposet(repo), x)
744 else:
744 else:
745 sources = fullreposet(repo)
745 sources = fullreposet(repo)
746
746
747 dests = set()
747 dests = set()
748
748
749 # subset contains all of the possible destinations that can be returned, so
749 # subset contains all of the possible destinations that can be returned, so
750 # iterate over them and see if their source(s) were provided in the arg set.
750 # iterate over them and see if their source(s) were provided in the arg set.
751 # Even if the immediate src of r is not in the arg set, src's source (or
751 # Even if the immediate src of r is not in the arg set, src's source (or
752 # further back) may be. Scanning back further than the immediate src allows
752 # further back) may be. Scanning back further than the immediate src allows
753 # transitive transplants and rebases to yield the same results as transitive
753 # transitive transplants and rebases to yield the same results as transitive
754 # grafts.
754 # grafts.
755 for r in subset:
755 for r in subset:
756 src = _getrevsource(repo, r)
756 src = _getrevsource(repo, r)
757 lineage = None
757 lineage = None
758
758
759 while src is not None:
759 while src is not None:
760 if lineage is None:
760 if lineage is None:
761 lineage = list()
761 lineage = list()
762
762
763 lineage.append(r)
763 lineage.append(r)
764
764
765 # The visited lineage is a match if the current source is in the arg
765 # The visited lineage is a match if the current source is in the arg
766 # set. Since every candidate dest is visited by way of iterating
766 # set. Since every candidate dest is visited by way of iterating
767 # subset, any dests further back in the lineage will be tested by a
767 # subset, any dests further back in the lineage will be tested by a
768 # different iteration over subset. Likewise, if the src was already
768 # different iteration over subset. Likewise, if the src was already
769 # selected, the current lineage can be selected without going back
769 # selected, the current lineage can be selected without going back
770 # further.
770 # further.
771 if src in sources or src in dests:
771 if src in sources or src in dests:
772 dests.update(lineage)
772 dests.update(lineage)
773 break
773 break
774
774
775 r = src
775 r = src
776 src = _getrevsource(repo, r)
776 src = _getrevsource(repo, r)
777
777
778 return subset.filter(dests.__contains__)
778 return subset.filter(dests.__contains__)
779
779
780 def divergent(repo, subset, x):
780 def divergent(repo, subset, x):
781 """``divergent()``
781 """``divergent()``
782 Final successors of changesets with an alternative set of final successors.
782 Final successors of changesets with an alternative set of final successors.
783 """
783 """
784 # i18n: "divergent" is a keyword
784 # i18n: "divergent" is a keyword
785 getargs(x, 0, 0, _("divergent takes no arguments"))
785 getargs(x, 0, 0, _("divergent takes no arguments"))
786 divergent = obsmod.getrevs(repo, 'divergent')
786 divergent = obsmod.getrevs(repo, 'divergent')
787 return subset & divergent
787 return subset & divergent
788
788
789 def draft(repo, subset, x):
789 def draft(repo, subset, x):
790 """``draft()``
790 """``draft()``
791 Changeset in draft phase."""
791 Changeset in draft phase."""
792 # i18n: "draft" is a keyword
792 # i18n: "draft" is a keyword
793 getargs(x, 0, 0, _("draft takes no arguments"))
793 getargs(x, 0, 0, _("draft takes no arguments"))
794 phase = repo._phasecache.phase
794 phase = repo._phasecache.phase
795 target = phases.draft
795 target = phases.draft
796 condition = lambda r: phase(repo, r) == target
796 condition = lambda r: phase(repo, r) == target
797 return subset.filter(condition, cache=False)
797 return subset.filter(condition, cache=False)
798
798
799 def extinct(repo, subset, x):
799 def extinct(repo, subset, x):
800 """``extinct()``
800 """``extinct()``
801 Obsolete changesets with obsolete descendants only.
801 Obsolete changesets with obsolete descendants only.
802 """
802 """
803 # i18n: "extinct" is a keyword
803 # i18n: "extinct" is a keyword
804 getargs(x, 0, 0, _("extinct takes no arguments"))
804 getargs(x, 0, 0, _("extinct takes no arguments"))
805 extincts = obsmod.getrevs(repo, 'extinct')
805 extincts = obsmod.getrevs(repo, 'extinct')
806 return subset & extincts
806 return subset & extincts
807
807
808 def extra(repo, subset, x):
808 def extra(repo, subset, x):
809 """``extra(label, [value])``
809 """``extra(label, [value])``
810 Changesets with the given label in the extra metadata, with the given
810 Changesets with the given label in the extra metadata, with the given
811 optional value.
811 optional value.
812
812
813 If `value` starts with `re:`, the remainder of the value is treated as
813 If `value` starts with `re:`, the remainder of the value is treated as
814 a regular expression. To match a value that actually starts with `re:`,
814 a regular expression. To match a value that actually starts with `re:`,
815 use the prefix `literal:`.
815 use the prefix `literal:`.
816 """
816 """
817
817
818 # i18n: "extra" is a keyword
818 # i18n: "extra" is a keyword
819 l = getargs(x, 1, 2, _('extra takes at least 1 and at most 2 arguments'))
819 l = getargs(x, 1, 2, _('extra takes at least 1 and at most 2 arguments'))
820 # i18n: "extra" is a keyword
820 # i18n: "extra" is a keyword
821 label = getstring(l[0], _('first argument to extra must be a string'))
821 label = getstring(l[0], _('first argument to extra must be a string'))
822 value = None
822 value = None
823
823
824 if len(l) > 1:
824 if len(l) > 1:
825 # i18n: "extra" is a keyword
825 # i18n: "extra" is a keyword
826 value = getstring(l[1], _('second argument to extra must be a string'))
826 value = getstring(l[1], _('second argument to extra must be a string'))
827 kind, value, matcher = _stringmatcher(value)
827 kind, value, matcher = _stringmatcher(value)
828
828
829 def _matchvalue(r):
829 def _matchvalue(r):
830 extra = repo[r].extra()
830 extra = repo[r].extra()
831 return label in extra and (value is None or matcher(extra[label]))
831 return label in extra and (value is None or matcher(extra[label]))
832
832
833 return subset.filter(lambda r: _matchvalue(r))
833 return subset.filter(lambda r: _matchvalue(r))
834
834
835 def filelog(repo, subset, x):
835 def filelog(repo, subset, x):
836 """``filelog(pattern)``
836 """``filelog(pattern)``
837 Changesets connected to the specified filelog.
837 Changesets connected to the specified filelog.
838
838
839 For performance reasons, visits only revisions mentioned in the file-level
839 For performance reasons, visits only revisions mentioned in the file-level
840 filelog, rather than filtering through all changesets (much faster, but
840 filelog, rather than filtering through all changesets (much faster, but
841 doesn't include deletes or duplicate changes). For a slower, more accurate
841 doesn't include deletes or duplicate changes). For a slower, more accurate
842 result, use ``file()``.
842 result, use ``file()``.
843
843
844 The pattern without explicit kind like ``glob:`` is expected to be
844 The pattern without explicit kind like ``glob:`` is expected to be
845 relative to the current directory and match against a file exactly
845 relative to the current directory and match against a file exactly
846 for efficiency.
846 for efficiency.
847
847
848 If some linkrev points to revisions filtered by the current repoview, we'll
848 If some linkrev points to revisions filtered by the current repoview, we'll
849 work around it to return a non-filtered value.
849 work around it to return a non-filtered value.
850 """
850 """
851
851
852 # i18n: "filelog" is a keyword
852 # i18n: "filelog" is a keyword
853 pat = getstring(x, _("filelog requires a pattern"))
853 pat = getstring(x, _("filelog requires a pattern"))
854 s = set()
854 s = set()
855 cl = repo.changelog
855 cl = repo.changelog
856
856
857 if not matchmod.patkind(pat):
857 if not matchmod.patkind(pat):
858 f = pathutil.canonpath(repo.root, repo.getcwd(), pat)
858 f = pathutil.canonpath(repo.root, repo.getcwd(), pat)
859 files = [f]
859 files = [f]
860 else:
860 else:
861 m = matchmod.match(repo.root, repo.getcwd(), [pat], ctx=repo[None])
861 m = matchmod.match(repo.root, repo.getcwd(), [pat], ctx=repo[None])
862 files = (f for f in repo[None] if m(f))
862 files = (f for f in repo[None] if m(f))
863
863
864 for f in files:
864 for f in files:
865 backrevref = {} # final value for: filerev -> changerev
865 backrevref = {} # final value for: filerev -> changerev
866 lowestchild = {} # lowest known filerev child of a filerev
866 lowestchild = {} # lowest known filerev child of a filerev
867 delayed = [] # filerev with filtered linkrev, for post-processing
867 delayed = [] # filerev with filtered linkrev, for post-processing
868 lowesthead = None # cache for manifest content of all head revisions
868 lowesthead = None # cache for manifest content of all head revisions
869 fl = repo.file(f)
869 fl = repo.file(f)
870 for fr in list(fl):
870 for fr in list(fl):
871 rev = fl.linkrev(fr)
871 rev = fl.linkrev(fr)
872 if rev not in cl:
872 if rev not in cl:
873 # changerev pointed in linkrev is filtered
873 # changerev pointed in linkrev is filtered
874 # record it for post processing.
874 # record it for post processing.
875 delayed.append((fr, rev))
875 delayed.append((fr, rev))
876 continue
876 continue
877 for p in fl.parentrevs(fr):
877 for p in fl.parentrevs(fr):
878 if 0 <= p and p not in lowestchild:
878 if 0 <= p and p not in lowestchild:
879 lowestchild[p] = fr
879 lowestchild[p] = fr
880 backrevref[fr] = rev
880 backrevref[fr] = rev
881 s.add(rev)
881 s.add(rev)
882
882
883 # Post-processing of all filerevs we skipped because they were
883 # Post-processing of all filerevs we skipped because they were
884 # filtered. If such filerevs have known and unfiltered children, this
884 # filtered. If such filerevs have known and unfiltered children, this
885 # means they have an unfiltered appearance out there. We'll use linkrev
885 # means they have an unfiltered appearance out there. We'll use linkrev
886 # adjustment to find one of these appearances. The lowest known child
886 # adjustment to find one of these appearances. The lowest known child
887 # will be used as a starting point because it is the best upper-bound we
887 # will be used as a starting point because it is the best upper-bound we
888 # have.
888 # have.
889 #
889 #
890 # This approach will fail when an unfiltered but linkrev-shadowed
890 # This approach will fail when an unfiltered but linkrev-shadowed
891 # appearance exists in a head changeset without unfiltered filerev
891 # appearance exists in a head changeset without unfiltered filerev
892 # children anywhere.
892 # children anywhere.
893 while delayed:
893 while delayed:
894 # must be a descending iteration. To slowly fill lowest child
894 # must be a descending iteration. To slowly fill lowest child
895 # information that is of potential use by the next item.
895 # information that is of potential use by the next item.
896 fr, rev = delayed.pop()
896 fr, rev = delayed.pop()
897 lkr = rev
897 lkr = rev
898
898
899 child = lowestchild.get(fr)
899 child = lowestchild.get(fr)
900
900
901 if child is None:
901 if child is None:
902 # search for existence of this file revision in a head revision.
902 # search for existence of this file revision in a head revision.
903 # There are three possibilities:
903 # There are three possibilities:
904 # - the revision exists in a head and we can find an
904 # - the revision exists in a head and we can find an
905 # introduction from there,
905 # introduction from there,
906 # - the revision does not exist in a head because it has been
906 # - the revision does not exist in a head because it has been
907 # changed since its introduction: we would have found a child
907 # changed since its introduction: we would have found a child
908 # and be in the other 'else' clause,
908 # and be in the other 'else' clause,
909 # - all versions of the revision are hidden.
909 # - all versions of the revision are hidden.
910 if lowesthead is None:
910 if lowesthead is None:
911 lowesthead = {}
911 lowesthead = {}
912 for h in repo.heads():
912 for h in repo.heads():
913 fnode = repo[h].manifest().get(f)
913 fnode = repo[h].manifest().get(f)
914 if fnode is not None:
914 if fnode is not None:
915 lowesthead[fl.rev(fnode)] = h
915 lowesthead[fl.rev(fnode)] = h
916 headrev = lowesthead.get(fr)
916 headrev = lowesthead.get(fr)
917 if headrev is None:
917 if headrev is None:
918 # content is nowhere unfiltered
918 # content is nowhere unfiltered
919 continue
919 continue
920 rev = repo[headrev][f].introrev()
920 rev = repo[headrev][f].introrev()
921 else:
921 else:
922 # the lowest known child is a good upper bound
922 # the lowest known child is a good upper bound
923 childcrev = backrevref[child]
923 childcrev = backrevref[child]
924 # XXX this does not guarantee returning the lowest
924 # XXX this does not guarantee returning the lowest
925 # introduction of this revision, but this gives a
925 # introduction of this revision, but this gives a
926 # result which is a good start and will fit in most
926 # result which is a good start and will fit in most
927 # cases. We probably need to fix the multiple
927 # cases. We probably need to fix the multiple
928 # introductions case properly (report each
928 # introductions case properly (report each
929 # introduction, even for identical file revisions)
929 # introduction, even for identical file revisions)
930 # once and for all at some point anyway.
930 # once and for all at some point anyway.
931 for p in repo[childcrev][f].parents():
931 for p in repo[childcrev][f].parents():
932 if p.filerev() == fr:
932 if p.filerev() == fr:
933 rev = p.rev()
933 rev = p.rev()
934 break
934 break
935 if rev == lkr: # no shadowed entry found
935 if rev == lkr: # no shadowed entry found
936 # XXX This should never happen unless some manifest points
936 # XXX This should never happen unless some manifest points
937 # to biggish file revisions (like a revision that uses a
937 # to biggish file revisions (like a revision that uses a
938 # parent that never appears in the manifest ancestors)
938 # parent that never appears in the manifest ancestors)
939 continue
939 continue
940
940
941 # Fill the data for the next iteration.
941 # Fill the data for the next iteration.
942 for p in fl.parentrevs(fr):
942 for p in fl.parentrevs(fr):
943 if 0 <= p and p not in lowestchild:
943 if 0 <= p and p not in lowestchild:
944 lowestchild[p] = fr
944 lowestchild[p] = fr
945 backrevref[fr] = rev
945 backrevref[fr] = rev
946 s.add(rev)
946 s.add(rev)
947
947
948 return subset & s
948 return subset & s
949
949
950 def first(repo, subset, x):
950 def first(repo, subset, x):
951 """``first(set, [n])``
951 """``first(set, [n])``
952 An alias for limit().
952 An alias for limit().
953 """
953 """
954 return limit(repo, subset, x)
954 return limit(repo, subset, x)
955
955
956 def _follow(repo, subset, x, name, followfirst=False):
956 def _follow(repo, subset, x, name, followfirst=False):
957 l = getargs(x, 0, 1, _("%s takes no arguments or a filename") % name)
957 l = getargs(x, 0, 1, _("%s takes no arguments or a filename") % name)
958 c = repo['.']
958 c = repo['.']
959 if l:
959 if l:
960 x = getstring(l[0], _("%s expected a filename") % name)
960 x = getstring(l[0], _("%s expected a filename") % name)
961 if x in c:
961 if x in c:
962 cx = c[x]
962 cx = c[x]
963 s = set(ctx.rev() for ctx in cx.ancestors(followfirst=followfirst))
963 s = set(ctx.rev() for ctx in cx.ancestors(followfirst=followfirst))
964 # include the revision responsible for the most recent version
964 # include the revision responsible for the most recent version
965 s.add(cx.introrev())
965 s.add(cx.introrev())
966 else:
966 else:
967 return baseset()
967 return baseset()
968 else:
968 else:
969 s = _revancestors(repo, baseset([c.rev()]), followfirst)
969 s = _revancestors(repo, baseset([c.rev()]), followfirst)
970
970
971 return subset & s
971 return subset & s
972
972
973 def follow(repo, subset, x):
973 def follow(repo, subset, x):
974 """``follow([file])``
974 """``follow([file])``
975 An alias for ``::.`` (ancestors of the working directory's first parent).
975 An alias for ``::.`` (ancestors of the working directory's first parent).
976 If a filename is specified, the history of the given file is followed,
976 If a filename is specified, the history of the given file is followed,
977 including copies.
977 including copies.
978 """
978 """
979 return _follow(repo, subset, x, 'follow')
979 return _follow(repo, subset, x, 'follow')
980
980
981 def _followfirst(repo, subset, x):
981 def _followfirst(repo, subset, x):
982 # ``followfirst([file])``
982 # ``followfirst([file])``
983 # Like ``follow([file])`` but follows only the first parent of
983 # Like ``follow([file])`` but follows only the first parent of
984 # every revision or file revision.
984 # every revision or file revision.
985 return _follow(repo, subset, x, '_followfirst', followfirst=True)
985 return _follow(repo, subset, x, '_followfirst', followfirst=True)
986
986
987 def getall(repo, subset, x):
987 def getall(repo, subset, x):
988 """``all()``
988 """``all()``
989 All changesets, the same as ``0:tip``.
989 All changesets, the same as ``0:tip``.
990 """
990 """
991 # i18n: "all" is a keyword
991 # i18n: "all" is a keyword
992 getargs(x, 0, 0, _("all takes no arguments"))
992 getargs(x, 0, 0, _("all takes no arguments"))
993 return subset & spanset(repo) # drop "null" if any
993 return subset & spanset(repo) # drop "null" if any
994
994
995 def grep(repo, subset, x):
995 def grep(repo, subset, x):
996 """``grep(regex)``
996 """``grep(regex)``
997 Like ``keyword(string)`` but accepts a regex. Use ``grep(r'...')``
997 Like ``keyword(string)`` but accepts a regex. Use ``grep(r'...')``
998 to ensure special escape characters are handled correctly. Unlike
998 to ensure special escape characters are handled correctly. Unlike
999 ``keyword(string)``, the match is case-sensitive.
999 ``keyword(string)``, the match is case-sensitive.
1000 """
1000 """
1001 try:
1001 try:
1002 # i18n: "grep" is a keyword
1002 # i18n: "grep" is a keyword
1003 gr = re.compile(getstring(x, _("grep requires a string")))
1003 gr = re.compile(getstring(x, _("grep requires a string")))
1004 except re.error, e:
1004 except re.error, e:
1005 raise error.ParseError(_('invalid match pattern: %s') % e)
1005 raise error.ParseError(_('invalid match pattern: %s') % e)
1006
1006
1007 def matches(x):
1007 def matches(x):
1008 c = repo[x]
1008 c = repo[x]
1009 for e in c.files() + [c.user(), c.description()]:
1009 for e in c.files() + [c.user(), c.description()]:
1010 if gr.search(e):
1010 if gr.search(e):
1011 return True
1011 return True
1012 return False
1012 return False
1013
1013
1014 return subset.filter(matches)
1014 return subset.filter(matches)
1015
1015
1016 def _matchfiles(repo, subset, x):
1016 def _matchfiles(repo, subset, x):
1017 # _matchfiles takes a revset list of prefixed arguments:
1017 # _matchfiles takes a revset list of prefixed arguments:
1018 #
1018 #
1019 # [p:foo, i:bar, x:baz]
1019 # [p:foo, i:bar, x:baz]
1020 #
1020 #
1021 # builds a match object from them and filters subset. Allowed
1021 # builds a match object from them and filters subset. Allowed
1022 # prefixes are 'p:' for regular patterns, 'i:' for include
1022 # prefixes are 'p:' for regular patterns, 'i:' for include
1023 # patterns and 'x:' for exclude patterns. Use 'r:' prefix to pass
1023 # patterns and 'x:' for exclude patterns. Use 'r:' prefix to pass
1024 # a revision identifier, or the empty string to reference the
1024 # a revision identifier, or the empty string to reference the
1025 # working directory, from which the match object is
1025 # working directory, from which the match object is
1026 # initialized. Use 'd:' to set the default matching mode, default
1026 # initialized. Use 'd:' to set the default matching mode, default
1027 # to 'glob'. At most one 'r:' and 'd:' argument can be passed.
1027 # to 'glob'. At most one 'r:' and 'd:' argument can be passed.
1028
1028
1029 # i18n: "_matchfiles" is a keyword
1029 # i18n: "_matchfiles" is a keyword
1030 l = getargs(x, 1, -1, _("_matchfiles requires at least one argument"))
1030 l = getargs(x, 1, -1, _("_matchfiles requires at least one argument"))
1031 pats, inc, exc = [], [], []
1031 pats, inc, exc = [], [], []
1032 rev, default = None, None
1032 rev, default = None, None
1033 for arg in l:
1033 for arg in l:
1034 # i18n: "_matchfiles" is a keyword
1034 # i18n: "_matchfiles" is a keyword
1035 s = getstring(arg, _("_matchfiles requires string arguments"))
1035 s = getstring(arg, _("_matchfiles requires string arguments"))
1036 prefix, value = s[:2], s[2:]
1036 prefix, value = s[:2], s[2:]
1037 if prefix == 'p:':
1037 if prefix == 'p:':
1038 pats.append(value)
1038 pats.append(value)
1039 elif prefix == 'i:':
1039 elif prefix == 'i:':
1040 inc.append(value)
1040 inc.append(value)
1041 elif prefix == 'x:':
1041 elif prefix == 'x:':
1042 exc.append(value)
1042 exc.append(value)
1043 elif prefix == 'r:':
1043 elif prefix == 'r:':
1044 if rev is not None:
1044 if rev is not None:
1045 # i18n: "_matchfiles" is a keyword
1045 # i18n: "_matchfiles" is a keyword
1046 raise error.ParseError(_('_matchfiles expected at most one '
1046 raise error.ParseError(_('_matchfiles expected at most one '
1047 'revision'))
1047 'revision'))
1048 if value != '': # empty means working directory; leave rev as None
1048 if value != '': # empty means working directory; leave rev as None
1049 rev = value
1049 rev = value
1050 elif prefix == 'd:':
1050 elif prefix == 'd:':
1051 if default is not None:
1051 if default is not None:
1052 # i18n: "_matchfiles" is a keyword
1052 # i18n: "_matchfiles" is a keyword
1053 raise error.ParseError(_('_matchfiles expected at most one '
1053 raise error.ParseError(_('_matchfiles expected at most one '
1054 'default mode'))
1054 'default mode'))
1055 default = value
1055 default = value
1056 else:
1056 else:
1057 # i18n: "_matchfiles" is a keyword
1057 # i18n: "_matchfiles" is a keyword
1058 raise error.ParseError(_('invalid _matchfiles prefix: %s') % prefix)
1058 raise error.ParseError(_('invalid _matchfiles prefix: %s') % prefix)
1059 if not default:
1059 if not default:
1060 default = 'glob'
1060 default = 'glob'
1061
1061
1062 m = matchmod.match(repo.root, repo.getcwd(), pats, include=inc,
1062 m = matchmod.match(repo.root, repo.getcwd(), pats, include=inc,
1063 exclude=exc, ctx=repo[rev], default=default)
1063 exclude=exc, ctx=repo[rev], default=default)
1064
1064
1065 def matches(x):
1065 def matches(x):
1066 for f in repo[x].files():
1066 for f in repo[x].files():
1067 if m(f):
1067 if m(f):
1068 return True
1068 return True
1069 return False
1069 return False
1070
1070
1071 return subset.filter(matches)
1071 return subset.filter(matches)
1072
1072
1073 def hasfile(repo, subset, x):
1073 def hasfile(repo, subset, x):
1074 """``file(pattern)``
1074 """``file(pattern)``
1075 Changesets affecting files matched by pattern.
1075 Changesets affecting files matched by pattern.
1076
1076
1077 For a faster but less accurate result, consider using ``filelog()``
1077 For a faster but less accurate result, consider using ``filelog()``
1078 instead.
1078 instead.
1079
1079
1080 This predicate uses ``glob:`` as the default kind of pattern.
1080 This predicate uses ``glob:`` as the default kind of pattern.
1081 """
1081 """
1082 # i18n: "file" is a keyword
1082 # i18n: "file" is a keyword
1083 pat = getstring(x, _("file requires a pattern"))
1083 pat = getstring(x, _("file requires a pattern"))
1084 return _matchfiles(repo, subset, ('string', 'p:' + pat))
1084 return _matchfiles(repo, subset, ('string', 'p:' + pat))
1085
1085
1086 def head(repo, subset, x):
1086 def head(repo, subset, x):
1087 """``head()``
1087 """``head()``
1088 Changeset is a named branch head.
1088 Changeset is a named branch head.
1089 """
1089 """
1090 # i18n: "head" is a keyword
1090 # i18n: "head" is a keyword
1091 getargs(x, 0, 0, _("head takes no arguments"))
1091 getargs(x, 0, 0, _("head takes no arguments"))
1092 hs = set()
1092 hs = set()
1093 for b, ls in repo.branchmap().iteritems():
1093 for b, ls in repo.branchmap().iteritems():
1094 hs.update(repo[h].rev() for h in ls)
1094 hs.update(repo[h].rev() for h in ls)
1095 return baseset(hs).filter(subset.__contains__)
1095 return baseset(hs).filter(subset.__contains__)
1096
1096
1097 def heads(repo, subset, x):
1097 def heads(repo, subset, x):
1098 """``heads(set)``
1098 """``heads(set)``
1099 Members of set with no children in set.
1099 Members of set with no children in set.
1100 """
1100 """
1101 s = getset(repo, subset, x)
1101 s = getset(repo, subset, x)
1102 ps = parents(repo, subset, x)
1102 ps = parents(repo, subset, x)
1103 return s - ps
1103 return s - ps
1104
1104
1105 def hidden(repo, subset, x):
1105 def hidden(repo, subset, x):
1106 """``hidden()``
1106 """``hidden()``
1107 Hidden changesets.
1107 Hidden changesets.
1108 """
1108 """
1109 # i18n: "hidden" is a keyword
1109 # i18n: "hidden" is a keyword
1110 getargs(x, 0, 0, _("hidden takes no arguments"))
1110 getargs(x, 0, 0, _("hidden takes no arguments"))
1111 hiddenrevs = repoview.filterrevs(repo, 'visible')
1111 hiddenrevs = repoview.filterrevs(repo, 'visible')
1112 return subset & hiddenrevs
1112 return subset & hiddenrevs
1113
1113
1114 def keyword(repo, subset, x):
1114 def keyword(repo, subset, x):
1115 """``keyword(string)``
1115 """``keyword(string)``
1116 Search commit message, user name, and names of changed files for
1116 Search commit message, user name, and names of changed files for
1117 string. The match is case-insensitive.
1117 string. The match is case-insensitive.
1118 """
1118 """
1119 # i18n: "keyword" is a keyword
1119 # i18n: "keyword" is a keyword
1120 kw = encoding.lower(getstring(x, _("keyword requires a string")))
1120 kw = encoding.lower(getstring(x, _("keyword requires a string")))
1121
1121
1122 def matches(r):
1122 def matches(r):
1123 c = repo[r]
1123 c = repo[r]
1124 return util.any(kw in encoding.lower(t) for t in c.files() + [c.user(),
1124 return util.any(kw in encoding.lower(t) for t in c.files() + [c.user(),
1125 c.description()])
1125 c.description()])
1126
1126
1127 return subset.filter(matches)
1127 return subset.filter(matches)
1128
1128
1129 def limit(repo, subset, x):
1129 def limit(repo, subset, x):
1130 """``limit(set, [n])``
1130 """``limit(set, [n])``
1131 First n members of set, defaulting to 1.
1131 First n members of set, defaulting to 1.
1132 """
1132 """
1133 # i18n: "limit" is a keyword
1133 # i18n: "limit" is a keyword
1134 l = getargs(x, 1, 2, _("limit requires one or two arguments"))
1134 l = getargs(x, 1, 2, _("limit requires one or two arguments"))
1135 try:
1135 try:
1136 lim = 1
1136 lim = 1
1137 if len(l) == 2:
1137 if len(l) == 2:
1138 # i18n: "limit" is a keyword
1138 # i18n: "limit" is a keyword
1139 lim = int(getstring(l[1], _("limit requires a number")))
1139 lim = int(getstring(l[1], _("limit requires a number")))
1140 except (TypeError, ValueError):
1140 except (TypeError, ValueError):
1141 # i18n: "limit" is a keyword
1141 # i18n: "limit" is a keyword
1142 raise error.ParseError(_("limit expects a number"))
1142 raise error.ParseError(_("limit expects a number"))
1143 ss = subset
1143 ss = subset
1144 os = getset(repo, fullreposet(repo), l[0])
1144 os = getset(repo, fullreposet(repo), l[0])
1145 result = []
1145 result = []
1146 it = iter(os)
1146 it = iter(os)
1147 for x in xrange(lim):
1147 for x in xrange(lim):
1148 try:
1148 y = next(it, None)
1149 y = it.next()
1149 if y is None:
1150 if y in ss:
1150 break
1151 elif y in ss:
1151 result.append(y)
1152 result.append(y)
1152 except (StopIteration):
1153 break
1154 return baseset(result)
1153 return baseset(result)
1155
1154
1156 def last(repo, subset, x):
1155 def last(repo, subset, x):
1157 """``last(set, [n])``
1156 """``last(set, [n])``
1158 Last n members of set, defaulting to 1.
1157 Last n members of set, defaulting to 1.
1159 """
1158 """
1160 # i18n: "last" is a keyword
1159 # i18n: "last" is a keyword
1161 l = getargs(x, 1, 2, _("last requires one or two arguments"))
1160 l = getargs(x, 1, 2, _("last requires one or two arguments"))
1162 try:
1161 try:
1163 lim = 1
1162 lim = 1
1164 if len(l) == 2:
1163 if len(l) == 2:
1165 # i18n: "last" is a keyword
1164 # i18n: "last" is a keyword
1166 lim = int(getstring(l[1], _("last requires a number")))
1165 lim = int(getstring(l[1], _("last requires a number")))
1167 except (TypeError, ValueError):
1166 except (TypeError, ValueError):
1168 # i18n: "last" is a keyword
1167 # i18n: "last" is a keyword
1169 raise error.ParseError(_("last expects a number"))
1168 raise error.ParseError(_("last expects a number"))
1170 ss = subset
1169 ss = subset
1171 os = getset(repo, fullreposet(repo), l[0])
1170 os = getset(repo, fullreposet(repo), l[0])
1172 os.reverse()
1171 os.reverse()
1173 result = []
1172 result = []
1174 it = iter(os)
1173 it = iter(os)
1175 for x in xrange(lim):
1174 for x in xrange(lim):
1176 try:
1175 try:
1177 y = it.next()
1176 y = it.next()
1178 if y in ss:
1177 if y in ss:
1179 result.append(y)
1178 result.append(y)
1180 except (StopIteration):
1179 except (StopIteration):
1181 break
1180 break
1182 return baseset(result)
1181 return baseset(result)
1183
1182
1184 def maxrev(repo, subset, x):
1183 def maxrev(repo, subset, x):
1185 """``max(set)``
1184 """``max(set)``
1186 Changeset with highest revision number in set.
1185 Changeset with highest revision number in set.
1187 """
1186 """
1188 os = getset(repo, fullreposet(repo), x)
1187 os = getset(repo, fullreposet(repo), x)
1189 if os:
1188 if os:
1190 m = os.max()
1189 m = os.max()
1191 if m in subset:
1190 if m in subset:
1192 return baseset([m])
1191 return baseset([m])
1193 return baseset()
1192 return baseset()
1194
1193
1195 def merge(repo, subset, x):
1194 def merge(repo, subset, x):
1196 """``merge()``
1195 """``merge()``
1197 Changeset is a merge changeset.
1196 Changeset is a merge changeset.
1198 """
1197 """
1199 # i18n: "merge" is a keyword
1198 # i18n: "merge" is a keyword
1200 getargs(x, 0, 0, _("merge takes no arguments"))
1199 getargs(x, 0, 0, _("merge takes no arguments"))
1201 cl = repo.changelog
1200 cl = repo.changelog
1202 return subset.filter(lambda r: cl.parentrevs(r)[1] != -1)
1201 return subset.filter(lambda r: cl.parentrevs(r)[1] != -1)
1203
1202
1204 def branchpoint(repo, subset, x):
1203 def branchpoint(repo, subset, x):
1205 """``branchpoint()``
1204 """``branchpoint()``
1206 Changesets with more than one child.
1205 Changesets with more than one child.
1207 """
1206 """
1208 # i18n: "branchpoint" is a keyword
1207 # i18n: "branchpoint" is a keyword
1209 getargs(x, 0, 0, _("branchpoint takes no arguments"))
1208 getargs(x, 0, 0, _("branchpoint takes no arguments"))
1210 cl = repo.changelog
1209 cl = repo.changelog
1211 if not subset:
1210 if not subset:
1212 return baseset()
1211 return baseset()
1213 baserev = min(subset)
1212 baserev = min(subset)
1214 parentscount = [0]*(len(repo) - baserev)
1213 parentscount = [0]*(len(repo) - baserev)
1215 for r in cl.revs(start=baserev + 1):
1214 for r in cl.revs(start=baserev + 1):
1216 for p in cl.parentrevs(r):
1215 for p in cl.parentrevs(r):
1217 if p >= baserev:
1216 if p >= baserev:
1218 parentscount[p - baserev] += 1
1217 parentscount[p - baserev] += 1
1219 return subset.filter(lambda r: parentscount[r - baserev] > 1)
1218 return subset.filter(lambda r: parentscount[r - baserev] > 1)
1220
1219
1221 def minrev(repo, subset, x):
1220 def minrev(repo, subset, x):
1222 """``min(set)``
1221 """``min(set)``
1223 Changeset with lowest revision number in set.
1222 Changeset with lowest revision number in set.
1224 """
1223 """
1225 os = getset(repo, fullreposet(repo), x)
1224 os = getset(repo, fullreposet(repo), x)
1226 if os:
1225 if os:
1227 m = os.min()
1226 m = os.min()
1228 if m in subset:
1227 if m in subset:
1229 return baseset([m])
1228 return baseset([m])
1230 return baseset()
1229 return baseset()
1231
1230
1232 def modifies(repo, subset, x):
1231 def modifies(repo, subset, x):
1233 """``modifies(pattern)``
1232 """``modifies(pattern)``
1234 Changesets modifying files matched by pattern.
1233 Changesets modifying files matched by pattern.
1235
1234
1236 The pattern without explicit kind like ``glob:`` is expected to be
1235 The pattern without explicit kind like ``glob:`` is expected to be
1237 relative to the current directory and match against a file or a
1236 relative to the current directory and match against a file or a
1238 directory.
1237 directory.
1239 """
1238 """
1240 # i18n: "modifies" is a keyword
1239 # i18n: "modifies" is a keyword
1241 pat = getstring(x, _("modifies requires a pattern"))
1240 pat = getstring(x, _("modifies requires a pattern"))
1242 return checkstatus(repo, subset, pat, 0)
1241 return checkstatus(repo, subset, pat, 0)
1243
1242
1244 def named(repo, subset, x):
1243 def named(repo, subset, x):
1245 """``named(namespace)``
1244 """``named(namespace)``
1246 The changesets in a given namespace.
1245 The changesets in a given namespace.
1247
1246
1248 If `namespace` starts with `re:`, the remainder of the string is treated as
1247 If `namespace` starts with `re:`, the remainder of the string is treated as
1249 a regular expression. To match a namespace that actually starts with `re:`,
1248 a regular expression. To match a namespace that actually starts with `re:`,
1250 use the prefix `literal:`.
1249 use the prefix `literal:`.
1251 """
1250 """
1252 # i18n: "named" is a keyword
1251 # i18n: "named" is a keyword
1253 args = getargs(x, 1, 1, _('named requires a namespace argument'))
1252 args = getargs(x, 1, 1, _('named requires a namespace argument'))
1254
1253
1255 ns = getstring(args[0],
1254 ns = getstring(args[0],
1256 # i18n: "named" is a keyword
1255 # i18n: "named" is a keyword
1257 _('the argument to named must be a string'))
1256 _('the argument to named must be a string'))
1258 kind, pattern, matcher = _stringmatcher(ns)
1257 kind, pattern, matcher = _stringmatcher(ns)
1259 namespaces = set()
1258 namespaces = set()
1260 if kind == 'literal':
1259 if kind == 'literal':
1261 if pattern not in repo.names:
1260 if pattern not in repo.names:
1262 raise error.RepoLookupError(_("namespace '%s' does not exist")
1261 raise error.RepoLookupError(_("namespace '%s' does not exist")
1263 % ns)
1262 % ns)
1264 namespaces.add(repo.names[pattern])
1263 namespaces.add(repo.names[pattern])
1265 else:
1264 else:
1266 for name, ns in repo.names.iteritems():
1265 for name, ns in repo.names.iteritems():
1267 if matcher(name):
1266 if matcher(name):
1268 namespaces.add(ns)
1267 namespaces.add(ns)
1269 if not namespaces:
1268 if not namespaces:
1270 raise error.RepoLookupError(_("no namespace exists"
1269 raise error.RepoLookupError(_("no namespace exists"
1271 " that match '%s'") % pattern)
1270 " that match '%s'") % pattern)
1272
1271
1273 names = set()
1272 names = set()
1274 for ns in namespaces:
1273 for ns in namespaces:
1275 for name in ns.listnames(repo):
1274 for name in ns.listnames(repo):
1276 if name not in ns.deprecated:
1275 if name not in ns.deprecated:
1277 names.update(repo[n].rev() for n in ns.nodes(repo, name))
1276 names.update(repo[n].rev() for n in ns.nodes(repo, name))
1278
1277
1279 names -= set([node.nullrev])
1278 names -= set([node.nullrev])
1280 return subset & names
1279 return subset & names
1281
1280
1282 def node_(repo, subset, x):
1281 def node_(repo, subset, x):
1283 """``id(string)``
1282 """``id(string)``
1284 Revision non-ambiguously specified by the given hex string prefix.
1283 Revision non-ambiguously specified by the given hex string prefix.
1285 """
1284 """
1286 # i18n: "id" is a keyword
1285 # i18n: "id" is a keyword
1287 l = getargs(x, 1, 1, _("id requires one argument"))
1286 l = getargs(x, 1, 1, _("id requires one argument"))
1288 # i18n: "id" is a keyword
1287 # i18n: "id" is a keyword
1289 n = getstring(l[0], _("id requires a string"))
1288 n = getstring(l[0], _("id requires a string"))
1290 if len(n) == 40:
1289 if len(n) == 40:
1291 try:
1290 try:
1292 rn = repo.changelog.rev(node.bin(n))
1291 rn = repo.changelog.rev(node.bin(n))
1293 except (LookupError, TypeError):
1292 except (LookupError, TypeError):
1294 rn = None
1293 rn = None
1295 else:
1294 else:
1296 rn = None
1295 rn = None
1297 pm = repo.changelog._partialmatch(n)
1296 pm = repo.changelog._partialmatch(n)
1298 if pm is not None:
1297 if pm is not None:
1299 rn = repo.changelog.rev(pm)
1298 rn = repo.changelog.rev(pm)
1300
1299
1301 if rn is None:
1300 if rn is None:
1302 return baseset()
1301 return baseset()
1303 result = baseset([rn])
1302 result = baseset([rn])
1304 return result & subset
1303 return result & subset
1305
1304
1306 def obsolete(repo, subset, x):
1305 def obsolete(repo, subset, x):
1307 """``obsolete()``
1306 """``obsolete()``
1308 Mutable changeset with a newer version."""
1307 Mutable changeset with a newer version."""
1309 # i18n: "obsolete" is a keyword
1308 # i18n: "obsolete" is a keyword
1310 getargs(x, 0, 0, _("obsolete takes no arguments"))
1309 getargs(x, 0, 0, _("obsolete takes no arguments"))
1311 obsoletes = obsmod.getrevs(repo, 'obsolete')
1310 obsoletes = obsmod.getrevs(repo, 'obsolete')
1312 return subset & obsoletes
1311 return subset & obsoletes
1313
1312
1314 def only(repo, subset, x):
1313 def only(repo, subset, x):
1315 """``only(set, [set])``
1314 """``only(set, [set])``
1316 Changesets that are ancestors of the first set that are not ancestors
1315 Changesets that are ancestors of the first set that are not ancestors
1317 of any other head in the repo. If a second set is specified, the result
1316 of any other head in the repo. If a second set is specified, the result
1318 is ancestors of the first set that are not ancestors of the second set
1317 is ancestors of the first set that are not ancestors of the second set
1319 (i.e. ::<set1> - ::<set2>).
1318 (i.e. ::<set1> - ::<set2>).
1320 """
1319 """
1321 cl = repo.changelog
1320 cl = repo.changelog
1322 # i18n: "only" is a keyword
1321 # i18n: "only" is a keyword
1323 args = getargs(x, 1, 2, _('only takes one or two arguments'))
1322 args = getargs(x, 1, 2, _('only takes one or two arguments'))
1324 include = getset(repo, fullreposet(repo), args[0])
1323 include = getset(repo, fullreposet(repo), args[0])
1325 if len(args) == 1:
1324 if len(args) == 1:
1326 if not include:
1325 if not include:
1327 return baseset()
1326 return baseset()
1328
1327
1329 descendants = set(_revdescendants(repo, include, False))
1328 descendants = set(_revdescendants(repo, include, False))
1330 exclude = [rev for rev in cl.headrevs()
1329 exclude = [rev for rev in cl.headrevs()
1331 if not rev in descendants and not rev in include]
1330 if not rev in descendants and not rev in include]
1332 else:
1331 else:
1333 exclude = getset(repo, fullreposet(repo), args[1])
1332 exclude = getset(repo, fullreposet(repo), args[1])
1334
1333
1335 results = set(cl.findmissingrevs(common=exclude, heads=include))
1334 results = set(cl.findmissingrevs(common=exclude, heads=include))
1336 return subset & results
1335 return subset & results
1337
1336
1338 def origin(repo, subset, x):
1337 def origin(repo, subset, x):
1339 """``origin([set])``
1338 """``origin([set])``
1340 Changesets that were specified as a source for the grafts, transplants or
1339 Changesets that were specified as a source for the grafts, transplants or
1341 rebases that created the given revisions. Omitting the optional set is the
1340 rebases that created the given revisions. Omitting the optional set is the
1342 same as passing all(). If a changeset created by these operations is itself
1341 same as passing all(). If a changeset created by these operations is itself
1343 specified as a source for one of these operations, only the source changeset
1342 specified as a source for one of these operations, only the source changeset
1344 for the first operation is selected.
1343 for the first operation is selected.
1345 """
1344 """
1346 if x is not None:
1345 if x is not None:
1347 dests = getset(repo, fullreposet(repo), x)
1346 dests = getset(repo, fullreposet(repo), x)
1348 else:
1347 else:
1349 dests = fullreposet(repo)
1348 dests = fullreposet(repo)
1350
1349
1351 def _firstsrc(rev):
1350 def _firstsrc(rev):
1352 src = _getrevsource(repo, rev)
1351 src = _getrevsource(repo, rev)
1353 if src is None:
1352 if src is None:
1354 return None
1353 return None
1355
1354
1356 while True:
1355 while True:
1357 prev = _getrevsource(repo, src)
1356 prev = _getrevsource(repo, src)
1358
1357
1359 if prev is None:
1358 if prev is None:
1360 return src
1359 return src
1361 src = prev
1360 src = prev
1362
1361
1363 o = set([_firstsrc(r) for r in dests])
1362 o = set([_firstsrc(r) for r in dests])
1364 o -= set([None])
1363 o -= set([None])
1365 return subset & o
1364 return subset & o
1366
1365
1367 def outgoing(repo, subset, x):
1366 def outgoing(repo, subset, x):
1368 """``outgoing([path])``
1367 """``outgoing([path])``
1369 Changesets not found in the specified destination repository, or the
1368 Changesets not found in the specified destination repository, or the
1370 default push location.
1369 default push location.
1371 """
1370 """
1372 # Avoid cycles.
1371 # Avoid cycles.
1373 import discovery
1372 import discovery
1374 import hg
1373 import hg
1375 # i18n: "outgoing" is a keyword
1374 # i18n: "outgoing" is a keyword
1376 l = getargs(x, 0, 1, _("outgoing takes one or no arguments"))
1375 l = getargs(x, 0, 1, _("outgoing takes one or no arguments"))
1377 # i18n: "outgoing" is a keyword
1376 # i18n: "outgoing" is a keyword
1378 dest = l and getstring(l[0], _("outgoing requires a repository path")) or ''
1377 dest = l and getstring(l[0], _("outgoing requires a repository path")) or ''
1379 dest = repo.ui.expandpath(dest or 'default-push', dest or 'default')
1378 dest = repo.ui.expandpath(dest or 'default-push', dest or 'default')
1380 dest, branches = hg.parseurl(dest)
1379 dest, branches = hg.parseurl(dest)
1381 revs, checkout = hg.addbranchrevs(repo, repo, branches, [])
1380 revs, checkout = hg.addbranchrevs(repo, repo, branches, [])
1382 if revs:
1381 if revs:
1383 revs = [repo.lookup(rev) for rev in revs]
1382 revs = [repo.lookup(rev) for rev in revs]
1384 other = hg.peer(repo, {}, dest)
1383 other = hg.peer(repo, {}, dest)
1385 repo.ui.pushbuffer()
1384 repo.ui.pushbuffer()
1386 outgoing = discovery.findcommonoutgoing(repo, other, onlyheads=revs)
1385 outgoing = discovery.findcommonoutgoing(repo, other, onlyheads=revs)
1387 repo.ui.popbuffer()
1386 repo.ui.popbuffer()
1388 cl = repo.changelog
1387 cl = repo.changelog
1389 o = set([cl.rev(r) for r in outgoing.missing])
1388 o = set([cl.rev(r) for r in outgoing.missing])
1390 return subset & o
1389 return subset & o
1391
1390
1392 def p1(repo, subset, x):
1391 def p1(repo, subset, x):
1393 """``p1([set])``
1392 """``p1([set])``
1394 First parent of changesets in set, or the working directory.
1393 First parent of changesets in set, or the working directory.
1395 """
1394 """
1396 if x is None:
1395 if x is None:
1397 p = repo[x].p1().rev()
1396 p = repo[x].p1().rev()
1398 if p >= 0:
1397 if p >= 0:
1399 return subset & baseset([p])
1398 return subset & baseset([p])
1400 return baseset()
1399 return baseset()
1401
1400
1402 ps = set()
1401 ps = set()
1403 cl = repo.changelog
1402 cl = repo.changelog
1404 for r in getset(repo, fullreposet(repo), x):
1403 for r in getset(repo, fullreposet(repo), x):
1405 ps.add(cl.parentrevs(r)[0])
1404 ps.add(cl.parentrevs(r)[0])
1406 ps -= set([node.nullrev])
1405 ps -= set([node.nullrev])
1407 return subset & ps
1406 return subset & ps
1408
1407
1409 def p2(repo, subset, x):
1408 def p2(repo, subset, x):
1410 """``p2([set])``
1409 """``p2([set])``
1411 Second parent of changesets in set, or the working directory.
1410 Second parent of changesets in set, or the working directory.
1412 """
1411 """
1413 if x is None:
1412 if x is None:
1414 ps = repo[x].parents()
1413 ps = repo[x].parents()
1415 try:
1414 try:
1416 p = ps[1].rev()
1415 p = ps[1].rev()
1417 if p >= 0:
1416 if p >= 0:
1418 return subset & baseset([p])
1417 return subset & baseset([p])
1419 return baseset()
1418 return baseset()
1420 except IndexError:
1419 except IndexError:
1421 return baseset()
1420 return baseset()
1422
1421
1423 ps = set()
1422 ps = set()
1424 cl = repo.changelog
1423 cl = repo.changelog
1425 for r in getset(repo, fullreposet(repo), x):
1424 for r in getset(repo, fullreposet(repo), x):
1426 ps.add(cl.parentrevs(r)[1])
1425 ps.add(cl.parentrevs(r)[1])
1427 ps -= set([node.nullrev])
1426 ps -= set([node.nullrev])
1428 return subset & ps
1427 return subset & ps
1429
1428
1430 def parents(repo, subset, x):
1429 def parents(repo, subset, x):
1431 """``parents([set])``
1430 """``parents([set])``
1432 The set of all parents for all changesets in set, or the working directory.
1431 The set of all parents for all changesets in set, or the working directory.
1433 """
1432 """
1434 if x is None:
1433 if x is None:
1435 ps = set(p.rev() for p in repo[x].parents())
1434 ps = set(p.rev() for p in repo[x].parents())
1436 else:
1435 else:
1437 ps = set()
1436 ps = set()
1438 cl = repo.changelog
1437 cl = repo.changelog
1439 for r in getset(repo, fullreposet(repo), x):
1438 for r in getset(repo, fullreposet(repo), x):
1440 ps.update(cl.parentrevs(r))
1439 ps.update(cl.parentrevs(r))
1441 ps -= set([node.nullrev])
1440 ps -= set([node.nullrev])
1442 return subset & ps
1441 return subset & ps
1443
1442
1444 def parentspec(repo, subset, x, n):
1443 def parentspec(repo, subset, x, n):
1445 """``set^0``
1444 """``set^0``
1446 The set.
1445 The set.
1447 ``set^1`` (or ``set^``), ``set^2``
1446 ``set^1`` (or ``set^``), ``set^2``
1448 First or second parent, respectively, of all changesets in set.
1447 First or second parent, respectively, of all changesets in set.
1449 """
1448 """
1450 try:
1449 try:
1451 n = int(n[1])
1450 n = int(n[1])
1452 if n not in (0, 1, 2):
1451 if n not in (0, 1, 2):
1453 raise ValueError
1452 raise ValueError
1454 except (TypeError, ValueError):
1453 except (TypeError, ValueError):
1455 raise error.ParseError(_("^ expects a number 0, 1, or 2"))
1454 raise error.ParseError(_("^ expects a number 0, 1, or 2"))
1456 ps = set()
1455 ps = set()
1457 cl = repo.changelog
1456 cl = repo.changelog
1458 for r in getset(repo, fullreposet(repo), x):
1457 for r in getset(repo, fullreposet(repo), x):
1459 if n == 0:
1458 if n == 0:
1460 ps.add(r)
1459 ps.add(r)
1461 elif n == 1:
1460 elif n == 1:
1462 ps.add(cl.parentrevs(r)[0])
1461 ps.add(cl.parentrevs(r)[0])
1463 elif n == 2:
1462 elif n == 2:
1464 parents = cl.parentrevs(r)
1463 parents = cl.parentrevs(r)
1465 if len(parents) > 1:
1464 if len(parents) > 1:
1466 ps.add(parents[1])
1465 ps.add(parents[1])
1467 return subset & ps
1466 return subset & ps
1468
1467
1469 def present(repo, subset, x):
1468 def present(repo, subset, x):
1470 """``present(set)``
1469 """``present(set)``
1471 An empty set, if any revision in set isn't found; otherwise,
1470 An empty set, if any revision in set isn't found; otherwise,
1472 all revisions in set.
1471 all revisions in set.
1473
1472
1474 If any of specified revisions is not present in the local repository,
1473 If any of specified revisions is not present in the local repository,
1475 the query is normally aborted. But this predicate allows the query
1474 the query is normally aborted. But this predicate allows the query
1476 to continue even in such cases.
1475 to continue even in such cases.
1477 """
1476 """
1478 try:
1477 try:
1479 return getset(repo, subset, x)
1478 return getset(repo, subset, x)
1480 except error.RepoLookupError:
1479 except error.RepoLookupError:
1481 return baseset()
1480 return baseset()
1482
1481
1483 def public(repo, subset, x):
1482 def public(repo, subset, x):
1484 """``public()``
1483 """``public()``
1485 Changeset in public phase."""
1484 Changeset in public phase."""
1486 # i18n: "public" is a keyword
1485 # i18n: "public" is a keyword
1487 getargs(x, 0, 0, _("public takes no arguments"))
1486 getargs(x, 0, 0, _("public takes no arguments"))
1488 phase = repo._phasecache.phase
1487 phase = repo._phasecache.phase
1489 target = phases.public
1488 target = phases.public
1490 condition = lambda r: phase(repo, r) == target
1489 condition = lambda r: phase(repo, r) == target
1491 return subset.filter(condition, cache=False)
1490 return subset.filter(condition, cache=False)
1492
1491
1493 def remote(repo, subset, x):
1492 def remote(repo, subset, x):
1494 """``remote([id [,path]])``
1493 """``remote([id [,path]])``
1495 Local revision that corresponds to the given identifier in a
1494 Local revision that corresponds to the given identifier in a
1496 remote repository, if present. Here, the '.' identifier is a
1495 remote repository, if present. Here, the '.' identifier is a
1497 synonym for the current local branch.
1496 synonym for the current local branch.
1498 """
1497 """
1499
1498
1500 import hg # avoid start-up nasties
1499 import hg # avoid start-up nasties
1501 # i18n: "remote" is a keyword
1500 # i18n: "remote" is a keyword
1502 l = getargs(x, 0, 2, _("remote takes one, two or no arguments"))
1501 l = getargs(x, 0, 2, _("remote takes one, two or no arguments"))
1503
1502
1504 q = '.'
1503 q = '.'
1505 if len(l) > 0:
1504 if len(l) > 0:
1506 # i18n: "remote" is a keyword
1505 # i18n: "remote" is a keyword
1507 q = getstring(l[0], _("remote requires a string id"))
1506 q = getstring(l[0], _("remote requires a string id"))
1508 if q == '.':
1507 if q == '.':
1509 q = repo['.'].branch()
1508 q = repo['.'].branch()
1510
1509
1511 dest = ''
1510 dest = ''
1512 if len(l) > 1:
1511 if len(l) > 1:
1513 # i18n: "remote" is a keyword
1512 # i18n: "remote" is a keyword
1514 dest = getstring(l[1], _("remote requires a repository path"))
1513 dest = getstring(l[1], _("remote requires a repository path"))
1515 dest = repo.ui.expandpath(dest or 'default')
1514 dest = repo.ui.expandpath(dest or 'default')
1516 dest, branches = hg.parseurl(dest)
1515 dest, branches = hg.parseurl(dest)
1517 revs, checkout = hg.addbranchrevs(repo, repo, branches, [])
1516 revs, checkout = hg.addbranchrevs(repo, repo, branches, [])
1518 if revs:
1517 if revs:
1519 revs = [repo.lookup(rev) for rev in revs]
1518 revs = [repo.lookup(rev) for rev in revs]
1520 other = hg.peer(repo, {}, dest)
1519 other = hg.peer(repo, {}, dest)
1521 n = other.lookup(q)
1520 n = other.lookup(q)
1522 if n in repo:
1521 if n in repo:
1523 r = repo[n].rev()
1522 r = repo[n].rev()
1524 if r in subset:
1523 if r in subset:
1525 return baseset([r])
1524 return baseset([r])
1526 return baseset()
1525 return baseset()
1527
1526
1528 def removes(repo, subset, x):
1527 def removes(repo, subset, x):
1529 """``removes(pattern)``
1528 """``removes(pattern)``
1530 Changesets which remove files matching pattern.
1529 Changesets which remove files matching pattern.
1531
1530
1532 The pattern without explicit kind like ``glob:`` is expected to be
1531 The pattern without explicit kind like ``glob:`` is expected to be
1533 relative to the current directory and match against a file or a
1532 relative to the current directory and match against a file or a
1534 directory.
1533 directory.
1535 """
1534 """
1536 # i18n: "removes" is a keyword
1535 # i18n: "removes" is a keyword
1537 pat = getstring(x, _("removes requires a pattern"))
1536 pat = getstring(x, _("removes requires a pattern"))
1538 return checkstatus(repo, subset, pat, 2)
1537 return checkstatus(repo, subset, pat, 2)
1539
1538
1540 def rev(repo, subset, x):
1539 def rev(repo, subset, x):
1541 """``rev(number)``
1540 """``rev(number)``
1542 Revision with the given numeric identifier.
1541 Revision with the given numeric identifier.
1543 """
1542 """
1544 # i18n: "rev" is a keyword
1543 # i18n: "rev" is a keyword
1545 l = getargs(x, 1, 1, _("rev requires one argument"))
1544 l = getargs(x, 1, 1, _("rev requires one argument"))
1546 try:
1545 try:
1547 # i18n: "rev" is a keyword
1546 # i18n: "rev" is a keyword
1548 l = int(getstring(l[0], _("rev requires a number")))
1547 l = int(getstring(l[0], _("rev requires a number")))
1549 except (TypeError, ValueError):
1548 except (TypeError, ValueError):
1550 # i18n: "rev" is a keyword
1549 # i18n: "rev" is a keyword
1551 raise error.ParseError(_("rev expects a number"))
1550 raise error.ParseError(_("rev expects a number"))
1552 if l not in repo.changelog and l != node.nullrev:
1551 if l not in repo.changelog and l != node.nullrev:
1553 return baseset()
1552 return baseset()
1554 return subset & baseset([l])
1553 return subset & baseset([l])
1555
1554
1556 def matching(repo, subset, x):
1555 def matching(repo, subset, x):
1557 """``matching(revision [, field])``
1556 """``matching(revision [, field])``
1558 Changesets in which a given set of fields match the set of fields in the
1557 Changesets in which a given set of fields match the set of fields in the
1559 selected revision or set.
1558 selected revision or set.
1560
1559
1561 To match more than one field pass the list of fields to match separated
1560 To match more than one field pass the list of fields to match separated
1562 by spaces (e.g. ``author description``).
1561 by spaces (e.g. ``author description``).
1563
1562
1564 Valid fields are most regular revision fields and some special fields.
1563 Valid fields are most regular revision fields and some special fields.
1565
1564
1566 Regular revision fields are ``description``, ``author``, ``branch``,
1565 Regular revision fields are ``description``, ``author``, ``branch``,
1567 ``date``, ``files``, ``phase``, ``parents``, ``substate``, ``user``
1566 ``date``, ``files``, ``phase``, ``parents``, ``substate``, ``user``
1568 and ``diff``.
1567 and ``diff``.
1569 Note that ``author`` and ``user`` are synonyms. ``diff`` refers to the
1568 Note that ``author`` and ``user`` are synonyms. ``diff`` refers to the
1570 contents of the revision. Two revisions matching their ``diff`` will
1569 contents of the revision. Two revisions matching their ``diff`` will
1571 also match their ``files``.
1570 also match their ``files``.
1572
1571
1573 Special fields are ``summary`` and ``metadata``:
1572 Special fields are ``summary`` and ``metadata``:
1574 ``summary`` matches the first line of the description.
1573 ``summary`` matches the first line of the description.
1575 ``metadata`` is equivalent to matching ``description user date``
1574 ``metadata`` is equivalent to matching ``description user date``
1576 (i.e. it matches the main metadata fields).
1575 (i.e. it matches the main metadata fields).
1577
1576
1578 ``metadata`` is the default field which is used when no fields are
1577 ``metadata`` is the default field which is used when no fields are
1579 specified. You can match more than one field at a time.
1578 specified. You can match more than one field at a time.
1580 """
1579 """
1581 # i18n: "matching" is a keyword
1580 # i18n: "matching" is a keyword
1582 l = getargs(x, 1, 2, _("matching takes 1 or 2 arguments"))
1581 l = getargs(x, 1, 2, _("matching takes 1 or 2 arguments"))
1583
1582
1584 revs = getset(repo, fullreposet(repo), l[0])
1583 revs = getset(repo, fullreposet(repo), l[0])
1585
1584
1586 fieldlist = ['metadata']
1585 fieldlist = ['metadata']
1587 if len(l) > 1:
1586 if len(l) > 1:
1588 fieldlist = getstring(l[1],
1587 fieldlist = getstring(l[1],
1589 # i18n: "matching" is a keyword
1588 # i18n: "matching" is a keyword
1590 _("matching requires a string "
1589 _("matching requires a string "
1591 "as its second argument")).split()
1590 "as its second argument")).split()
1592
1591
1593 # Make sure that there are no repeated fields,
1592 # Make sure that there are no repeated fields,
1594 # expand the 'special' 'metadata' field type
1593 # expand the 'special' 'metadata' field type
1595 # and check the 'files' whenever we check the 'diff'
1594 # and check the 'files' whenever we check the 'diff'
1596 fields = []
1595 fields = []
1597 for field in fieldlist:
1596 for field in fieldlist:
1598 if field == 'metadata':
1597 if field == 'metadata':
1599 fields += ['user', 'description', 'date']
1598 fields += ['user', 'description', 'date']
1600 elif field == 'diff':
1599 elif field == 'diff':
1601 # a revision matching the diff must also match the files
1600 # a revision matching the diff must also match the files
1602 # since matching the diff is very costly, make sure to
1601 # since matching the diff is very costly, make sure to
1603 # also match the files first
1602 # also match the files first
1604 fields += ['files', 'diff']
1603 fields += ['files', 'diff']
1605 else:
1604 else:
1606 if field == 'author':
1605 if field == 'author':
1607 field = 'user'
1606 field = 'user'
1608 fields.append(field)
1607 fields.append(field)
1609 fields = set(fields)
1608 fields = set(fields)
1610 if 'summary' in fields and 'description' in fields:
1609 if 'summary' in fields and 'description' in fields:
1611 # If a revision matches its description it also matches its summary
1610 # If a revision matches its description it also matches its summary
1612 fields.discard('summary')
1611 fields.discard('summary')
1613
1612
1614 # We may want to match more than one field
1613 # We may want to match more than one field
1615 # Not all fields take the same amount of time to be matched
1614 # Not all fields take the same amount of time to be matched
1616 # Sort the selected fields in order of increasing matching cost
1615 # Sort the selected fields in order of increasing matching cost
1617 fieldorder = ['phase', 'parents', 'user', 'date', 'branch', 'summary',
1616 fieldorder = ['phase', 'parents', 'user', 'date', 'branch', 'summary',
1618 'files', 'description', 'substate', 'diff']
1617 'files', 'description', 'substate', 'diff']
1619 def fieldkeyfunc(f):
1618 def fieldkeyfunc(f):
1620 try:
1619 try:
1621 return fieldorder.index(f)
1620 return fieldorder.index(f)
1622 except ValueError:
1621 except ValueError:
1623 # assume an unknown field is very costly
1622 # assume an unknown field is very costly
1624 return len(fieldorder)
1623 return len(fieldorder)
1625 fields = list(fields)
1624 fields = list(fields)
1626 fields.sort(key=fieldkeyfunc)
1625 fields.sort(key=fieldkeyfunc)
1627
1626
1628 # Each field will be matched with its own "getfield" function
1627 # Each field will be matched with its own "getfield" function
1629 # which will be added to the getfieldfuncs array of functions
1628 # which will be added to the getfieldfuncs array of functions
1630 getfieldfuncs = []
1629 getfieldfuncs = []
1631 _funcs = {
1630 _funcs = {
1632 'user': lambda r: repo[r].user(),
1631 'user': lambda r: repo[r].user(),
1633 'branch': lambda r: repo[r].branch(),
1632 'branch': lambda r: repo[r].branch(),
1634 'date': lambda r: repo[r].date(),
1633 'date': lambda r: repo[r].date(),
1635 'description': lambda r: repo[r].description(),
1634 'description': lambda r: repo[r].description(),
1636 'files': lambda r: repo[r].files(),
1635 'files': lambda r: repo[r].files(),
1637 'parents': lambda r: repo[r].parents(),
1636 'parents': lambda r: repo[r].parents(),
1638 'phase': lambda r: repo[r].phase(),
1637 'phase': lambda r: repo[r].phase(),
1639 'substate': lambda r: repo[r].substate,
1638 'substate': lambda r: repo[r].substate,
1640 'summary': lambda r: repo[r].description().splitlines()[0],
1639 'summary': lambda r: repo[r].description().splitlines()[0],
1641 'diff': lambda r: list(repo[r].diff(git=True),)
1640 'diff': lambda r: list(repo[r].diff(git=True),)
1642 }
1641 }
1643 for info in fields:
1642 for info in fields:
1644 getfield = _funcs.get(info, None)
1643 getfield = _funcs.get(info, None)
1645 if getfield is None:
1644 if getfield is None:
1646 raise error.ParseError(
1645 raise error.ParseError(
1647 # i18n: "matching" is a keyword
1646 # i18n: "matching" is a keyword
1648 _("unexpected field name passed to matching: %s") % info)
1647 _("unexpected field name passed to matching: %s") % info)
1649 getfieldfuncs.append(getfield)
1648 getfieldfuncs.append(getfield)
1650 # convert the getfield array of functions into a "getinfo" function
1649 # convert the getfield array of functions into a "getinfo" function
1651 # which returns an array of field values (or a single value if there
1650 # which returns an array of field values (or a single value if there
1652 # is only one field to match)
1651 # is only one field to match)
1653 getinfo = lambda r: [f(r) for f in getfieldfuncs]
1652 getinfo = lambda r: [f(r) for f in getfieldfuncs]
1654
1653
1655 def matches(x):
1654 def matches(x):
1656 for rev in revs:
1655 for rev in revs:
1657 target = getinfo(rev)
1656 target = getinfo(rev)
1658 match = True
1657 match = True
1659 for n, f in enumerate(getfieldfuncs):
1658 for n, f in enumerate(getfieldfuncs):
1660 if target[n] != f(x):
1659 if target[n] != f(x):
1661 match = False
1660 match = False
1662 if match:
1661 if match:
1663 return True
1662 return True
1664 return False
1663 return False
1665
1664
1666 return subset.filter(matches)
1665 return subset.filter(matches)
1667
1666
1668 def reverse(repo, subset, x):
1667 def reverse(repo, subset, x):
1669 """``reverse(set)``
1668 """``reverse(set)``
1670 Reverse order of set.
1669 Reverse order of set.
1671 """
1670 """
1672 l = getset(repo, subset, x)
1671 l = getset(repo, subset, x)
1673 l.reverse()
1672 l.reverse()
1674 return l
1673 return l
1675
1674
1676 def roots(repo, subset, x):
1675 def roots(repo, subset, x):
1677 """``roots(set)``
1676 """``roots(set)``
1678 Changesets in set with no parent changeset in set.
1677 Changesets in set with no parent changeset in set.
1679 """
1678 """
1680 s = getset(repo, fullreposet(repo), x)
1679 s = getset(repo, fullreposet(repo), x)
1681 subset = subset & s# baseset([r for r in s if r in subset])
1680 subset = subset & s# baseset([r for r in s if r in subset])
1682 cs = _children(repo, subset, s)
1681 cs = _children(repo, subset, s)
1683 return subset - cs
1682 return subset - cs
1684
1683
1685 def secret(repo, subset, x):
1684 def secret(repo, subset, x):
1686 """``secret()``
1685 """``secret()``
1687 Changeset in secret phase."""
1686 Changeset in secret phase."""
1688 # i18n: "secret" is a keyword
1687 # i18n: "secret" is a keyword
1689 getargs(x, 0, 0, _("secret takes no arguments"))
1688 getargs(x, 0, 0, _("secret takes no arguments"))
1690 phase = repo._phasecache.phase
1689 phase = repo._phasecache.phase
1691 target = phases.secret
1690 target = phases.secret
1692 condition = lambda r: phase(repo, r) == target
1691 condition = lambda r: phase(repo, r) == target
1693 return subset.filter(condition, cache=False)
1692 return subset.filter(condition, cache=False)
1694
1693
1695 def sort(repo, subset, x):
1694 def sort(repo, subset, x):
1696 """``sort(set[, [-]key...])``
1695 """``sort(set[, [-]key...])``
1697 Sort set by keys. The default sort order is ascending, specify a key
1696 Sort set by keys. The default sort order is ascending, specify a key
1698 as ``-key`` to sort in descending order.
1697 as ``-key`` to sort in descending order.
1699
1698
1700 The keys can be:
1699 The keys can be:
1701
1700
1702 - ``rev`` for the revision number,
1701 - ``rev`` for the revision number,
1703 - ``branch`` for the branch name,
1702 - ``branch`` for the branch name,
1704 - ``desc`` for the commit message (description),
1703 - ``desc`` for the commit message (description),
1705 - ``user`` for user name (``author`` can be used as an alias),
1704 - ``user`` for user name (``author`` can be used as an alias),
1706 - ``date`` for the commit date
1705 - ``date`` for the commit date
1707 """
1706 """
1708 # i18n: "sort" is a keyword
1707 # i18n: "sort" is a keyword
1709 l = getargs(x, 1, 2, _("sort requires one or two arguments"))
1708 l = getargs(x, 1, 2, _("sort requires one or two arguments"))
1710 keys = "rev"
1709 keys = "rev"
1711 if len(l) == 2:
1710 if len(l) == 2:
1712 # i18n: "sort" is a keyword
1711 # i18n: "sort" is a keyword
1713 keys = getstring(l[1], _("sort spec must be a string"))
1712 keys = getstring(l[1], _("sort spec must be a string"))
1714
1713
1715 s = l[0]
1714 s = l[0]
1716 keys = keys.split()
1715 keys = keys.split()
1717 l = []
1716 l = []
1718 def invert(s):
1717 def invert(s):
1719 return "".join(chr(255 - ord(c)) for c in s)
1718 return "".join(chr(255 - ord(c)) for c in s)
1720 revs = getset(repo, subset, s)
1719 revs = getset(repo, subset, s)
1721 if keys == ["rev"]:
1720 if keys == ["rev"]:
1722 revs.sort()
1721 revs.sort()
1723 return revs
1722 return revs
1724 elif keys == ["-rev"]:
1723 elif keys == ["-rev"]:
1725 revs.sort(reverse=True)
1724 revs.sort(reverse=True)
1726 return revs
1725 return revs
1727 for r in revs:
1726 for r in revs:
1728 c = repo[r]
1727 c = repo[r]
1729 e = []
1728 e = []
1730 for k in keys:
1729 for k in keys:
1731 if k == 'rev':
1730 if k == 'rev':
1732 e.append(r)
1731 e.append(r)
1733 elif k == '-rev':
1732 elif k == '-rev':
1734 e.append(-r)
1733 e.append(-r)
1735 elif k == 'branch':
1734 elif k == 'branch':
1736 e.append(c.branch())
1735 e.append(c.branch())
1737 elif k == '-branch':
1736 elif k == '-branch':
1738 e.append(invert(c.branch()))
1737 e.append(invert(c.branch()))
1739 elif k == 'desc':
1738 elif k == 'desc':
1740 e.append(c.description())
1739 e.append(c.description())
1741 elif k == '-desc':
1740 elif k == '-desc':
1742 e.append(invert(c.description()))
1741 e.append(invert(c.description()))
1743 elif k in 'user author':
1742 elif k in 'user author':
1744 e.append(c.user())
1743 e.append(c.user())
1745 elif k in '-user -author':
1744 elif k in '-user -author':
1746 e.append(invert(c.user()))
1745 e.append(invert(c.user()))
1747 elif k == 'date':
1746 elif k == 'date':
1748 e.append(c.date()[0])
1747 e.append(c.date()[0])
1749 elif k == '-date':
1748 elif k == '-date':
1750 e.append(-c.date()[0])
1749 e.append(-c.date()[0])
1751 else:
1750 else:
1752 raise error.ParseError(_("unknown sort key %r") % k)
1751 raise error.ParseError(_("unknown sort key %r") % k)
1753 e.append(r)
1752 e.append(r)
1754 l.append(e)
1753 l.append(e)
1755 l.sort()
1754 l.sort()
1756 return baseset([e[-1] for e in l])
1755 return baseset([e[-1] for e in l])
1757
1756
1758 def subrepo(repo, subset, x):
1757 def subrepo(repo, subset, x):
1759 """``subrepo([pattern])``
1758 """``subrepo([pattern])``
1760 Changesets that add, modify or remove the given subrepo. If no subrepo
1759 Changesets that add, modify or remove the given subrepo. If no subrepo
1761 pattern is named, any subrepo changes are returned.
1760 pattern is named, any subrepo changes are returned.
1762 """
1761 """
1763 # i18n: "subrepo" is a keyword
1762 # i18n: "subrepo" is a keyword
1764 args = getargs(x, 0, 1, _('subrepo takes at most one argument'))
1763 args = getargs(x, 0, 1, _('subrepo takes at most one argument'))
1765 if len(args) != 0:
1764 if len(args) != 0:
1766 pat = getstring(args[0], _("subrepo requires a pattern"))
1765 pat = getstring(args[0], _("subrepo requires a pattern"))
1767
1766
1768 m = matchmod.exact(repo.root, repo.root, ['.hgsubstate'])
1767 m = matchmod.exact(repo.root, repo.root, ['.hgsubstate'])
1769
1768
1770 def submatches(names):
1769 def submatches(names):
1771 k, p, m = _stringmatcher(pat)
1770 k, p, m = _stringmatcher(pat)
1772 for name in names:
1771 for name in names:
1773 if m(name):
1772 if m(name):
1774 yield name
1773 yield name
1775
1774
1776 def matches(x):
1775 def matches(x):
1777 c = repo[x]
1776 c = repo[x]
1778 s = repo.status(c.p1().node(), c.node(), match=m)
1777 s = repo.status(c.p1().node(), c.node(), match=m)
1779
1778
1780 if len(args) == 0:
1779 if len(args) == 0:
1781 return s.added or s.modified or s.removed
1780 return s.added or s.modified or s.removed
1782
1781
1783 if s.added:
1782 if s.added:
1784 return util.any(submatches(c.substate.keys()))
1783 return util.any(submatches(c.substate.keys()))
1785
1784
1786 if s.modified:
1785 if s.modified:
1787 subs = set(c.p1().substate.keys())
1786 subs = set(c.p1().substate.keys())
1788 subs.update(c.substate.keys())
1787 subs.update(c.substate.keys())
1789
1788
1790 for path in submatches(subs):
1789 for path in submatches(subs):
1791 if c.p1().substate.get(path) != c.substate.get(path):
1790 if c.p1().substate.get(path) != c.substate.get(path):
1792 return True
1791 return True
1793
1792
1794 if s.removed:
1793 if s.removed:
1795 return util.any(submatches(c.p1().substate.keys()))
1794 return util.any(submatches(c.p1().substate.keys()))
1796
1795
1797 return False
1796 return False
1798
1797
1799 return subset.filter(matches)
1798 return subset.filter(matches)
1800
1799
1801 def _stringmatcher(pattern):
1800 def _stringmatcher(pattern):
1802 """
1801 """
1803 accepts a string, possibly starting with 're:' or 'literal:' prefix.
1802 accepts a string, possibly starting with 're:' or 'literal:' prefix.
1804 returns the matcher name, pattern, and matcher function.
1803 returns the matcher name, pattern, and matcher function.
1805 missing or unknown prefixes are treated as literal matches.
1804 missing or unknown prefixes are treated as literal matches.
1806
1805
1807 helper for tests:
1806 helper for tests:
1808 >>> def test(pattern, *tests):
1807 >>> def test(pattern, *tests):
1809 ... kind, pattern, matcher = _stringmatcher(pattern)
1808 ... kind, pattern, matcher = _stringmatcher(pattern)
1810 ... return (kind, pattern, [bool(matcher(t)) for t in tests])
1809 ... return (kind, pattern, [bool(matcher(t)) for t in tests])
1811
1810
1812 exact matching (no prefix):
1811 exact matching (no prefix):
1813 >>> test('abcdefg', 'abc', 'def', 'abcdefg')
1812 >>> test('abcdefg', 'abc', 'def', 'abcdefg')
1814 ('literal', 'abcdefg', [False, False, True])
1813 ('literal', 'abcdefg', [False, False, True])
1815
1814
1816 regex matching ('re:' prefix)
1815 regex matching ('re:' prefix)
1817 >>> test('re:a.+b', 'nomatch', 'fooadef', 'fooadefbar')
1816 >>> test('re:a.+b', 'nomatch', 'fooadef', 'fooadefbar')
1818 ('re', 'a.+b', [False, False, True])
1817 ('re', 'a.+b', [False, False, True])
1819
1818
1820 force exact matches ('literal:' prefix)
1819 force exact matches ('literal:' prefix)
1821 >>> test('literal:re:foobar', 'foobar', 're:foobar')
1820 >>> test('literal:re:foobar', 'foobar', 're:foobar')
1822 ('literal', 're:foobar', [False, True])
1821 ('literal', 're:foobar', [False, True])
1823
1822
1824 unknown prefixes are ignored and treated as literals
1823 unknown prefixes are ignored and treated as literals
1825 >>> test('foo:bar', 'foo', 'bar', 'foo:bar')
1824 >>> test('foo:bar', 'foo', 'bar', 'foo:bar')
1826 ('literal', 'foo:bar', [False, False, True])
1825 ('literal', 'foo:bar', [False, False, True])
1827 """
1826 """
1828 if pattern.startswith('re:'):
1827 if pattern.startswith('re:'):
1829 pattern = pattern[3:]
1828 pattern = pattern[3:]
1830 try:
1829 try:
1831 regex = re.compile(pattern)
1830 regex = re.compile(pattern)
1832 except re.error, e:
1831 except re.error, e:
1833 raise error.ParseError(_('invalid regular expression: %s')
1832 raise error.ParseError(_('invalid regular expression: %s')
1834 % e)
1833 % e)
1835 return 're', pattern, regex.search
1834 return 're', pattern, regex.search
1836 elif pattern.startswith('literal:'):
1835 elif pattern.startswith('literal:'):
1837 pattern = pattern[8:]
1836 pattern = pattern[8:]
1838 return 'literal', pattern, pattern.__eq__
1837 return 'literal', pattern, pattern.__eq__
1839
1838
1840 def _substringmatcher(pattern):
1839 def _substringmatcher(pattern):
1841 kind, pattern, matcher = _stringmatcher(pattern)
1840 kind, pattern, matcher = _stringmatcher(pattern)
1842 if kind == 'literal':
1841 if kind == 'literal':
1843 matcher = lambda s: pattern in s
1842 matcher = lambda s: pattern in s
1844 return kind, pattern, matcher
1843 return kind, pattern, matcher
1845
1844
1846 def tag(repo, subset, x):
1845 def tag(repo, subset, x):
1847 """``tag([name])``
1846 """``tag([name])``
1848 The specified tag by name, or all tagged revisions if no name is given.
1847 The specified tag by name, or all tagged revisions if no name is given.
1849
1848
1850 If `name` starts with `re:`, the remainder of the name is treated as
1849 If `name` starts with `re:`, the remainder of the name is treated as
1851 a regular expression. To match a tag that actually starts with `re:`,
1850 a regular expression. To match a tag that actually starts with `re:`,
1852 use the prefix `literal:`.
1851 use the prefix `literal:`.
1853 """
1852 """
1854 # i18n: "tag" is a keyword
1853 # i18n: "tag" is a keyword
1855 args = getargs(x, 0, 1, _("tag takes one or no arguments"))
1854 args = getargs(x, 0, 1, _("tag takes one or no arguments"))
1856 cl = repo.changelog
1855 cl = repo.changelog
1857 if args:
1856 if args:
1858 pattern = getstring(args[0],
1857 pattern = getstring(args[0],
1859 # i18n: "tag" is a keyword
1858 # i18n: "tag" is a keyword
1860 _('the argument to tag must be a string'))
1859 _('the argument to tag must be a string'))
1861 kind, pattern, matcher = _stringmatcher(pattern)
1860 kind, pattern, matcher = _stringmatcher(pattern)
1862 if kind == 'literal':
1861 if kind == 'literal':
1863 # avoid resolving all tags
1862 # avoid resolving all tags
1864 tn = repo._tagscache.tags.get(pattern, None)
1863 tn = repo._tagscache.tags.get(pattern, None)
1865 if tn is None:
1864 if tn is None:
1866 raise error.RepoLookupError(_("tag '%s' does not exist")
1865 raise error.RepoLookupError(_("tag '%s' does not exist")
1867 % pattern)
1866 % pattern)
1868 s = set([repo[tn].rev()])
1867 s = set([repo[tn].rev()])
1869 else:
1868 else:
1870 s = set([cl.rev(n) for t, n in repo.tagslist() if matcher(t)])
1869 s = set([cl.rev(n) for t, n in repo.tagslist() if matcher(t)])
1871 else:
1870 else:
1872 s = set([cl.rev(n) for t, n in repo.tagslist() if t != 'tip'])
1871 s = set([cl.rev(n) for t, n in repo.tagslist() if t != 'tip'])
1873 return subset & s
1872 return subset & s
1874
1873
1875 def tagged(repo, subset, x):
1874 def tagged(repo, subset, x):
1876 return tag(repo, subset, x)
1875 return tag(repo, subset, x)
1877
1876
1878 def unstable(repo, subset, x):
1877 def unstable(repo, subset, x):
1879 """``unstable()``
1878 """``unstable()``
1880 Non-obsolete changesets with obsolete ancestors.
1879 Non-obsolete changesets with obsolete ancestors.
1881 """
1880 """
1882 # i18n: "unstable" is a keyword
1881 # i18n: "unstable" is a keyword
1883 getargs(x, 0, 0, _("unstable takes no arguments"))
1882 getargs(x, 0, 0, _("unstable takes no arguments"))
1884 unstables = obsmod.getrevs(repo, 'unstable')
1883 unstables = obsmod.getrevs(repo, 'unstable')
1885 return subset & unstables
1884 return subset & unstables
1886
1885
1887
1886
1888 def user(repo, subset, x):
1887 def user(repo, subset, x):
1889 """``user(string)``
1888 """``user(string)``
1890 User name contains string. The match is case-insensitive.
1889 User name contains string. The match is case-insensitive.
1891
1890
1892 If `string` starts with `re:`, the remainder of the string is treated as
1891 If `string` starts with `re:`, the remainder of the string is treated as
1893 a regular expression. To match a user that actually contains `re:`, use
1892 a regular expression. To match a user that actually contains `re:`, use
1894 the prefix `literal:`.
1893 the prefix `literal:`.
1895 """
1894 """
1896 return author(repo, subset, x)
1895 return author(repo, subset, x)
1897
1896
1898 # experimental
1897 # experimental
1899 def wdir(repo, subset, x):
1898 def wdir(repo, subset, x):
1900 # i18n: "wdir" is a keyword
1899 # i18n: "wdir" is a keyword
1901 getargs(x, 0, 0, _("wdir takes no arguments"))
1900 getargs(x, 0, 0, _("wdir takes no arguments"))
1902 if None in subset:
1901 if None in subset:
1903 return baseset([None])
1902 return baseset([None])
1904 return baseset()
1903 return baseset()
1905
1904
1906 # for internal use
1905 # for internal use
1907 def _list(repo, subset, x):
1906 def _list(repo, subset, x):
1908 s = getstring(x, "internal error")
1907 s = getstring(x, "internal error")
1909 if not s:
1908 if not s:
1910 return baseset()
1909 return baseset()
1911 ls = [repo[r].rev() for r in s.split('\0')]
1910 ls = [repo[r].rev() for r in s.split('\0')]
1912 s = subset
1911 s = subset
1913 return baseset([r for r in ls if r in s])
1912 return baseset([r for r in ls if r in s])
1914
1913
1915 # for internal use
1914 # for internal use
1916 def _intlist(repo, subset, x):
1915 def _intlist(repo, subset, x):
1917 s = getstring(x, "internal error")
1916 s = getstring(x, "internal error")
1918 if not s:
1917 if not s:
1919 return baseset()
1918 return baseset()
1920 ls = [int(r) for r in s.split('\0')]
1919 ls = [int(r) for r in s.split('\0')]
1921 s = subset
1920 s = subset
1922 return baseset([r for r in ls if r in s])
1921 return baseset([r for r in ls if r in s])
1923
1922
1924 # for internal use
1923 # for internal use
1925 def _hexlist(repo, subset, x):
1924 def _hexlist(repo, subset, x):
1926 s = getstring(x, "internal error")
1925 s = getstring(x, "internal error")
1927 if not s:
1926 if not s:
1928 return baseset()
1927 return baseset()
1929 cl = repo.changelog
1928 cl = repo.changelog
1930 ls = [cl.rev(node.bin(r)) for r in s.split('\0')]
1929 ls = [cl.rev(node.bin(r)) for r in s.split('\0')]
1931 s = subset
1930 s = subset
1932 return baseset([r for r in ls if r in s])
1931 return baseset([r for r in ls if r in s])
1933
1932
1934 symbols = {
1933 symbols = {
1935 "adds": adds,
1934 "adds": adds,
1936 "all": getall,
1935 "all": getall,
1937 "ancestor": ancestor,
1936 "ancestor": ancestor,
1938 "ancestors": ancestors,
1937 "ancestors": ancestors,
1939 "_firstancestors": _firstancestors,
1938 "_firstancestors": _firstancestors,
1940 "author": author,
1939 "author": author,
1941 "bisect": bisect,
1940 "bisect": bisect,
1942 "bisected": bisected,
1941 "bisected": bisected,
1943 "bookmark": bookmark,
1942 "bookmark": bookmark,
1944 "branch": branch,
1943 "branch": branch,
1945 "branchpoint": branchpoint,
1944 "branchpoint": branchpoint,
1946 "bumped": bumped,
1945 "bumped": bumped,
1947 "bundle": bundle,
1946 "bundle": bundle,
1948 "children": children,
1947 "children": children,
1949 "closed": closed,
1948 "closed": closed,
1950 "contains": contains,
1949 "contains": contains,
1951 "converted": converted,
1950 "converted": converted,
1952 "date": date,
1951 "date": date,
1953 "desc": desc,
1952 "desc": desc,
1954 "descendants": descendants,
1953 "descendants": descendants,
1955 "_firstdescendants": _firstdescendants,
1954 "_firstdescendants": _firstdescendants,
1956 "destination": destination,
1955 "destination": destination,
1957 "divergent": divergent,
1956 "divergent": divergent,
1958 "draft": draft,
1957 "draft": draft,
1959 "extinct": extinct,
1958 "extinct": extinct,
1960 "extra": extra,
1959 "extra": extra,
1961 "file": hasfile,
1960 "file": hasfile,
1962 "filelog": filelog,
1961 "filelog": filelog,
1963 "first": first,
1962 "first": first,
1964 "follow": follow,
1963 "follow": follow,
1965 "_followfirst": _followfirst,
1964 "_followfirst": _followfirst,
1966 "grep": grep,
1965 "grep": grep,
1967 "head": head,
1966 "head": head,
1968 "heads": heads,
1967 "heads": heads,
1969 "hidden": hidden,
1968 "hidden": hidden,
1970 "id": node_,
1969 "id": node_,
1971 "keyword": keyword,
1970 "keyword": keyword,
1972 "last": last,
1971 "last": last,
1973 "limit": limit,
1972 "limit": limit,
1974 "_matchfiles": _matchfiles,
1973 "_matchfiles": _matchfiles,
1975 "max": maxrev,
1974 "max": maxrev,
1976 "merge": merge,
1975 "merge": merge,
1977 "min": minrev,
1976 "min": minrev,
1978 "modifies": modifies,
1977 "modifies": modifies,
1979 "named": named,
1978 "named": named,
1980 "obsolete": obsolete,
1979 "obsolete": obsolete,
1981 "only": only,
1980 "only": only,
1982 "origin": origin,
1981 "origin": origin,
1983 "outgoing": outgoing,
1982 "outgoing": outgoing,
1984 "p1": p1,
1983 "p1": p1,
1985 "p2": p2,
1984 "p2": p2,
1986 "parents": parents,
1985 "parents": parents,
1987 "present": present,
1986 "present": present,
1988 "public": public,
1987 "public": public,
1989 "remote": remote,
1988 "remote": remote,
1990 "removes": removes,
1989 "removes": removes,
1991 "rev": rev,
1990 "rev": rev,
1992 "reverse": reverse,
1991 "reverse": reverse,
1993 "roots": roots,
1992 "roots": roots,
1994 "sort": sort,
1993 "sort": sort,
1995 "secret": secret,
1994 "secret": secret,
1996 "subrepo": subrepo,
1995 "subrepo": subrepo,
1997 "matching": matching,
1996 "matching": matching,
1998 "tag": tag,
1997 "tag": tag,
1999 "tagged": tagged,
1998 "tagged": tagged,
2000 "user": user,
1999 "user": user,
2001 "unstable": unstable,
2000 "unstable": unstable,
2002 "wdir": wdir,
2001 "wdir": wdir,
2003 "_list": _list,
2002 "_list": _list,
2004 "_intlist": _intlist,
2003 "_intlist": _intlist,
2005 "_hexlist": _hexlist,
2004 "_hexlist": _hexlist,
2006 }
2005 }
2007
2006
2008 # symbols which can't be used for a DoS attack for any given input
2007 # symbols which can't be used for a DoS attack for any given input
2009 # (e.g. those which accept regexes as plain strings shouldn't be included)
2008 # (e.g. those which accept regexes as plain strings shouldn't be included)
2010 # functions that just return a lot of changesets (like all) don't count here
2009 # functions that just return a lot of changesets (like all) don't count here
2011 safesymbols = set([
2010 safesymbols = set([
2012 "adds",
2011 "adds",
2013 "all",
2012 "all",
2014 "ancestor",
2013 "ancestor",
2015 "ancestors",
2014 "ancestors",
2016 "_firstancestors",
2015 "_firstancestors",
2017 "author",
2016 "author",
2018 "bisect",
2017 "bisect",
2019 "bisected",
2018 "bisected",
2020 "bookmark",
2019 "bookmark",
2021 "branch",
2020 "branch",
2022 "branchpoint",
2021 "branchpoint",
2023 "bumped",
2022 "bumped",
2024 "bundle",
2023 "bundle",
2025 "children",
2024 "children",
2026 "closed",
2025 "closed",
2027 "converted",
2026 "converted",
2028 "date",
2027 "date",
2029 "desc",
2028 "desc",
2030 "descendants",
2029 "descendants",
2031 "_firstdescendants",
2030 "_firstdescendants",
2032 "destination",
2031 "destination",
2033 "divergent",
2032 "divergent",
2034 "draft",
2033 "draft",
2035 "extinct",
2034 "extinct",
2036 "extra",
2035 "extra",
2037 "file",
2036 "file",
2038 "filelog",
2037 "filelog",
2039 "first",
2038 "first",
2040 "follow",
2039 "follow",
2041 "_followfirst",
2040 "_followfirst",
2042 "head",
2041 "head",
2043 "heads",
2042 "heads",
2044 "hidden",
2043 "hidden",
2045 "id",
2044 "id",
2046 "keyword",
2045 "keyword",
2047 "last",
2046 "last",
2048 "limit",
2047 "limit",
2049 "_matchfiles",
2048 "_matchfiles",
2050 "max",
2049 "max",
2051 "merge",
2050 "merge",
2052 "min",
2051 "min",
2053 "modifies",
2052 "modifies",
2054 "obsolete",
2053 "obsolete",
2055 "only",
2054 "only",
2056 "origin",
2055 "origin",
2057 "outgoing",
2056 "outgoing",
2058 "p1",
2057 "p1",
2059 "p2",
2058 "p2",
2060 "parents",
2059 "parents",
2061 "present",
2060 "present",
2062 "public",
2061 "public",
2063 "remote",
2062 "remote",
2064 "removes",
2063 "removes",
2065 "rev",
2064 "rev",
2066 "reverse",
2065 "reverse",
2067 "roots",
2066 "roots",
2068 "sort",
2067 "sort",
2069 "secret",
2068 "secret",
2070 "matching",
2069 "matching",
2071 "tag",
2070 "tag",
2072 "tagged",
2071 "tagged",
2073 "user",
2072 "user",
2074 "unstable",
2073 "unstable",
2075 "wdir",
2074 "wdir",
2076 "_list",
2075 "_list",
2077 "_intlist",
2076 "_intlist",
2078 "_hexlist",
2077 "_hexlist",
2079 ])
2078 ])
2080
2079
2081 methods = {
2080 methods = {
2082 "range": rangeset,
2081 "range": rangeset,
2083 "dagrange": dagrange,
2082 "dagrange": dagrange,
2084 "string": stringset,
2083 "string": stringset,
2085 "symbol": stringset,
2084 "symbol": stringset,
2086 "and": andset,
2085 "and": andset,
2087 "or": orset,
2086 "or": orset,
2088 "not": notset,
2087 "not": notset,
2089 "list": listset,
2088 "list": listset,
2090 "func": func,
2089 "func": func,
2091 "ancestor": ancestorspec,
2090 "ancestor": ancestorspec,
2092 "parent": parentspec,
2091 "parent": parentspec,
2093 "parentpost": p1,
2092 "parentpost": p1,
2094 }
2093 }
2095
2094
2096 def optimize(x, small):
2095 def optimize(x, small):
2097 if x is None:
2096 if x is None:
2098 return 0, x
2097 return 0, x
2099
2098
2100 smallbonus = 1
2099 smallbonus = 1
2101 if small:
2100 if small:
2102 smallbonus = .5
2101 smallbonus = .5
2103
2102
2104 op = x[0]
2103 op = x[0]
2105 if op == 'minus':
2104 if op == 'minus':
2106 return optimize(('and', x[1], ('not', x[2])), small)
2105 return optimize(('and', x[1], ('not', x[2])), small)
2107 elif op == 'only':
2106 elif op == 'only':
2108 return optimize(('func', ('symbol', 'only'),
2107 return optimize(('func', ('symbol', 'only'),
2109 ('list', x[1], x[2])), small)
2108 ('list', x[1], x[2])), small)
2110 elif op == 'onlypost':
2109 elif op == 'onlypost':
2111 return optimize(('func', ('symbol', 'only'), x[1]), small)
2110 return optimize(('func', ('symbol', 'only'), x[1]), small)
2112 elif op == 'dagrangepre':
2111 elif op == 'dagrangepre':
2113 return optimize(('func', ('symbol', 'ancestors'), x[1]), small)
2112 return optimize(('func', ('symbol', 'ancestors'), x[1]), small)
2114 elif op == 'dagrangepost':
2113 elif op == 'dagrangepost':
2115 return optimize(('func', ('symbol', 'descendants'), x[1]), small)
2114 return optimize(('func', ('symbol', 'descendants'), x[1]), small)
2116 elif op == 'rangepre':
2115 elif op == 'rangepre':
2117 return optimize(('range', ('string', '0'), x[1]), small)
2116 return optimize(('range', ('string', '0'), x[1]), small)
2118 elif op == 'rangepost':
2117 elif op == 'rangepost':
2119 return optimize(('range', x[1], ('string', 'tip')), small)
2118 return optimize(('range', x[1], ('string', 'tip')), small)
2120 elif op == 'negate':
2119 elif op == 'negate':
2121 return optimize(('string',
2120 return optimize(('string',
2122 '-' + getstring(x[1], _("can't negate that"))), small)
2121 '-' + getstring(x[1], _("can't negate that"))), small)
2123 elif op in 'string symbol negate':
2122 elif op in 'string symbol negate':
2124 return smallbonus, x # single revisions are small
2123 return smallbonus, x # single revisions are small
2125 elif op == 'and':
2124 elif op == 'and':
2126 wa, ta = optimize(x[1], True)
2125 wa, ta = optimize(x[1], True)
2127 wb, tb = optimize(x[2], True)
2126 wb, tb = optimize(x[2], True)
2128
2127
2129 # (::x and not ::y)/(not ::y and ::x) have a fast path
2128 # (::x and not ::y)/(not ::y and ::x) have a fast path
2130 def isonly(revs, bases):
2129 def isonly(revs, bases):
2131 return (
2130 return (
2132 revs[0] == 'func'
2131 revs[0] == 'func'
2133 and getstring(revs[1], _('not a symbol')) == 'ancestors'
2132 and getstring(revs[1], _('not a symbol')) == 'ancestors'
2134 and bases[0] == 'not'
2133 and bases[0] == 'not'
2135 and bases[1][0] == 'func'
2134 and bases[1][0] == 'func'
2136 and getstring(bases[1][1], _('not a symbol')) == 'ancestors')
2135 and getstring(bases[1][1], _('not a symbol')) == 'ancestors')
2137
2136
2138 w = min(wa, wb)
2137 w = min(wa, wb)
2139 if isonly(ta, tb):
2138 if isonly(ta, tb):
2140 return w, ('func', ('symbol', 'only'), ('list', ta[2], tb[1][2]))
2139 return w, ('func', ('symbol', 'only'), ('list', ta[2], tb[1][2]))
2141 if isonly(tb, ta):
2140 if isonly(tb, ta):
2142 return w, ('func', ('symbol', 'only'), ('list', tb[2], ta[1][2]))
2141 return w, ('func', ('symbol', 'only'), ('list', tb[2], ta[1][2]))
2143
2142
2144 if wa > wb:
2143 if wa > wb:
2145 return w, (op, tb, ta)
2144 return w, (op, tb, ta)
2146 return w, (op, ta, tb)
2145 return w, (op, ta, tb)
2147 elif op == 'or':
2146 elif op == 'or':
2148 wa, ta = optimize(x[1], False)
2147 wa, ta = optimize(x[1], False)
2149 wb, tb = optimize(x[2], False)
2148 wb, tb = optimize(x[2], False)
2150 if wb < wa:
2149 if wb < wa:
2151 wb, wa = wa, wb
2150 wb, wa = wa, wb
2152 return max(wa, wb), (op, ta, tb)
2151 return max(wa, wb), (op, ta, tb)
2153 elif op == 'not':
2152 elif op == 'not':
2154 o = optimize(x[1], not small)
2153 o = optimize(x[1], not small)
2155 return o[0], (op, o[1])
2154 return o[0], (op, o[1])
2156 elif op == 'parentpost':
2155 elif op == 'parentpost':
2157 o = optimize(x[1], small)
2156 o = optimize(x[1], small)
2158 return o[0], (op, o[1])
2157 return o[0], (op, o[1])
2159 elif op == 'group':
2158 elif op == 'group':
2160 return optimize(x[1], small)
2159 return optimize(x[1], small)
2161 elif op in 'dagrange range list parent ancestorspec':
2160 elif op in 'dagrange range list parent ancestorspec':
2162 if op == 'parent':
2161 if op == 'parent':
2163 # x^:y means (x^) : y, not x ^ (:y)
2162 # x^:y means (x^) : y, not x ^ (:y)
2164 post = ('parentpost', x[1])
2163 post = ('parentpost', x[1])
2165 if x[2][0] == 'dagrangepre':
2164 if x[2][0] == 'dagrangepre':
2166 return optimize(('dagrange', post, x[2][1]), small)
2165 return optimize(('dagrange', post, x[2][1]), small)
2167 elif x[2][0] == 'rangepre':
2166 elif x[2][0] == 'rangepre':
2168 return optimize(('range', post, x[2][1]), small)
2167 return optimize(('range', post, x[2][1]), small)
2169
2168
2170 wa, ta = optimize(x[1], small)
2169 wa, ta = optimize(x[1], small)
2171 wb, tb = optimize(x[2], small)
2170 wb, tb = optimize(x[2], small)
2172 return wa + wb, (op, ta, tb)
2171 return wa + wb, (op, ta, tb)
2173 elif op == 'func':
2172 elif op == 'func':
2174 f = getstring(x[1], _("not a symbol"))
2173 f = getstring(x[1], _("not a symbol"))
2175 wa, ta = optimize(x[2], small)
2174 wa, ta = optimize(x[2], small)
2176 if f in ("author branch closed date desc file grep keyword "
2175 if f in ("author branch closed date desc file grep keyword "
2177 "outgoing user"):
2176 "outgoing user"):
2178 w = 10 # slow
2177 w = 10 # slow
2179 elif f in "modifies adds removes":
2178 elif f in "modifies adds removes":
2180 w = 30 # slower
2179 w = 30 # slower
2181 elif f == "contains":
2180 elif f == "contains":
2182 w = 100 # very slow
2181 w = 100 # very slow
2183 elif f == "ancestor":
2182 elif f == "ancestor":
2184 w = 1 * smallbonus
2183 w = 1 * smallbonus
2185 elif f in "reverse limit first _intlist":
2184 elif f in "reverse limit first _intlist":
2186 w = 0
2185 w = 0
2187 elif f in "sort":
2186 elif f in "sort":
2188 w = 10 # assume most sorts look at changelog
2187 w = 10 # assume most sorts look at changelog
2189 else:
2188 else:
2190 w = 1
2189 w = 1
2191 return w + wa, (op, x[1], ta)
2190 return w + wa, (op, x[1], ta)
2192 return 1, x
2191 return 1, x
2193
2192
2194 _aliasarg = ('func', ('symbol', '_aliasarg'))
2193 _aliasarg = ('func', ('symbol', '_aliasarg'))
2195 def _getaliasarg(tree):
2194 def _getaliasarg(tree):
2196 """If tree matches ('func', ('symbol', '_aliasarg'), ('string', X))
2195 """If tree matches ('func', ('symbol', '_aliasarg'), ('string', X))
2197 return X, None otherwise.
2196 return X, None otherwise.
2198 """
2197 """
2199 if (len(tree) == 3 and tree[:2] == _aliasarg
2198 if (len(tree) == 3 and tree[:2] == _aliasarg
2200 and tree[2][0] == 'string'):
2199 and tree[2][0] == 'string'):
2201 return tree[2][1]
2200 return tree[2][1]
2202 return None
2201 return None
2203
2202
2204 def _checkaliasarg(tree, known=None):
2203 def _checkaliasarg(tree, known=None):
2205 """Check tree contains no _aliasarg construct or only ones which
2204 """Check tree contains no _aliasarg construct or only ones which
2206 value is in known. Used to avoid alias placeholders injection.
2205 value is in known. Used to avoid alias placeholders injection.
2207 """
2206 """
2208 if isinstance(tree, tuple):
2207 if isinstance(tree, tuple):
2209 arg = _getaliasarg(tree)
2208 arg = _getaliasarg(tree)
2210 if arg is not None and (not known or arg not in known):
2209 if arg is not None and (not known or arg not in known):
2211 raise error.UnknownIdentifier('_aliasarg', [])
2210 raise error.UnknownIdentifier('_aliasarg', [])
2212 for t in tree:
2211 for t in tree:
2213 _checkaliasarg(t, known)
2212 _checkaliasarg(t, known)
2214
2213
2215 # the set of valid characters for the initial letter of symbols in
2214 # the set of valid characters for the initial letter of symbols in
2216 # alias declarations and definitions
2215 # alias declarations and definitions
2217 _aliassyminitletters = set(c for c in [chr(i) for i in xrange(256)]
2216 _aliassyminitletters = set(c for c in [chr(i) for i in xrange(256)]
2218 if c.isalnum() or c in '._@$' or ord(c) > 127)
2217 if c.isalnum() or c in '._@$' or ord(c) > 127)
2219
2218
2220 def _tokenizealias(program, lookup=None):
2219 def _tokenizealias(program, lookup=None):
2221 """Parse alias declaration/definition into a stream of tokens
2220 """Parse alias declaration/definition into a stream of tokens
2222
2221
2223 This allows symbol names to use also ``$`` as an initial letter
2222 This allows symbol names to use also ``$`` as an initial letter
2224 (for backward compatibility), and callers of this function should
2223 (for backward compatibility), and callers of this function should
2225 examine whether ``$`` is used also for unexpected symbols or not.
2224 examine whether ``$`` is used also for unexpected symbols or not.
2226 """
2225 """
2227 return tokenize(program, lookup=lookup,
2226 return tokenize(program, lookup=lookup,
2228 syminitletters=_aliassyminitletters)
2227 syminitletters=_aliassyminitletters)
2229
2228
2230 def _parsealiasdecl(decl):
2229 def _parsealiasdecl(decl):
2231 """Parse alias declaration ``decl``
2230 """Parse alias declaration ``decl``
2232
2231
2233 This returns ``(name, tree, args, errorstr)`` tuple:
2232 This returns ``(name, tree, args, errorstr)`` tuple:
2234
2233
2235 - ``name``: of declared alias (may be ``decl`` itself at error)
2234 - ``name``: of declared alias (may be ``decl`` itself at error)
2236 - ``tree``: parse result (or ``None`` at error)
2235 - ``tree``: parse result (or ``None`` at error)
2237 - ``args``: list of alias argument names (or None for symbol declaration)
2236 - ``args``: list of alias argument names (or None for symbol declaration)
2238 - ``errorstr``: detail about detected error (or None)
2237 - ``errorstr``: detail about detected error (or None)
2239
2238
2240 >>> _parsealiasdecl('foo')
2239 >>> _parsealiasdecl('foo')
2241 ('foo', ('symbol', 'foo'), None, None)
2240 ('foo', ('symbol', 'foo'), None, None)
2242 >>> _parsealiasdecl('$foo')
2241 >>> _parsealiasdecl('$foo')
2243 ('$foo', None, None, "'$' not for alias arguments")
2242 ('$foo', None, None, "'$' not for alias arguments")
2244 >>> _parsealiasdecl('foo::bar')
2243 >>> _parsealiasdecl('foo::bar')
2245 ('foo::bar', None, None, 'invalid format')
2244 ('foo::bar', None, None, 'invalid format')
2246 >>> _parsealiasdecl('foo bar')
2245 >>> _parsealiasdecl('foo bar')
2247 ('foo bar', None, None, 'at 4: invalid token')
2246 ('foo bar', None, None, 'at 4: invalid token')
2248 >>> _parsealiasdecl('foo()')
2247 >>> _parsealiasdecl('foo()')
2249 ('foo', ('func', ('symbol', 'foo')), [], None)
2248 ('foo', ('func', ('symbol', 'foo')), [], None)
2250 >>> _parsealiasdecl('$foo()')
2249 >>> _parsealiasdecl('$foo()')
2251 ('$foo()', None, None, "'$' not for alias arguments")
2250 ('$foo()', None, None, "'$' not for alias arguments")
2252 >>> _parsealiasdecl('foo($1, $2)')
2251 >>> _parsealiasdecl('foo($1, $2)')
2253 ('foo', ('func', ('symbol', 'foo')), ['$1', '$2'], None)
2252 ('foo', ('func', ('symbol', 'foo')), ['$1', '$2'], None)
2254 >>> _parsealiasdecl('foo(bar_bar, baz.baz)')
2253 >>> _parsealiasdecl('foo(bar_bar, baz.baz)')
2255 ('foo', ('func', ('symbol', 'foo')), ['bar_bar', 'baz.baz'], None)
2254 ('foo', ('func', ('symbol', 'foo')), ['bar_bar', 'baz.baz'], None)
2256 >>> _parsealiasdecl('foo($1, $2, nested($1, $2))')
2255 >>> _parsealiasdecl('foo($1, $2, nested($1, $2))')
2257 ('foo($1, $2, nested($1, $2))', None, None, 'invalid argument list')
2256 ('foo($1, $2, nested($1, $2))', None, None, 'invalid argument list')
2258 >>> _parsealiasdecl('foo(bar($1, $2))')
2257 >>> _parsealiasdecl('foo(bar($1, $2))')
2259 ('foo(bar($1, $2))', None, None, 'invalid argument list')
2258 ('foo(bar($1, $2))', None, None, 'invalid argument list')
2260 >>> _parsealiasdecl('foo("string")')
2259 >>> _parsealiasdecl('foo("string")')
2261 ('foo("string")', None, None, 'invalid argument list')
2260 ('foo("string")', None, None, 'invalid argument list')
2262 >>> _parsealiasdecl('foo($1, $2')
2261 >>> _parsealiasdecl('foo($1, $2')
2263 ('foo($1, $2', None, None, 'at 10: unexpected token: end')
2262 ('foo($1, $2', None, None, 'at 10: unexpected token: end')
2264 >>> _parsealiasdecl('foo("string')
2263 >>> _parsealiasdecl('foo("string')
2265 ('foo("string', None, None, 'at 5: unterminated string')
2264 ('foo("string', None, None, 'at 5: unterminated string')
2266 >>> _parsealiasdecl('foo($1, $2, $1)')
2265 >>> _parsealiasdecl('foo($1, $2, $1)')
2267 ('foo', None, None, 'argument names collide with each other')
2266 ('foo', None, None, 'argument names collide with each other')
2268 """
2267 """
2269 p = parser.parser(_tokenizealias, elements)
2268 p = parser.parser(_tokenizealias, elements)
2270 try:
2269 try:
2271 tree, pos = p.parse(decl)
2270 tree, pos = p.parse(decl)
2272 if (pos != len(decl)):
2271 if (pos != len(decl)):
2273 raise error.ParseError(_('invalid token'), pos)
2272 raise error.ParseError(_('invalid token'), pos)
2274
2273
2275 if isvalidsymbol(tree):
2274 if isvalidsymbol(tree):
2276 # "name = ...." style
2275 # "name = ...." style
2277 name = getsymbol(tree)
2276 name = getsymbol(tree)
2278 if name.startswith('$'):
2277 if name.startswith('$'):
2279 return (decl, None, None, _("'$' not for alias arguments"))
2278 return (decl, None, None, _("'$' not for alias arguments"))
2280 return (name, ('symbol', name), None, None)
2279 return (name, ('symbol', name), None, None)
2281
2280
2282 if isvalidfunc(tree):
2281 if isvalidfunc(tree):
2283 # "name(arg, ....) = ...." style
2282 # "name(arg, ....) = ...." style
2284 name = getfuncname(tree)
2283 name = getfuncname(tree)
2285 if name.startswith('$'):
2284 if name.startswith('$'):
2286 return (decl, None, None, _("'$' not for alias arguments"))
2285 return (decl, None, None, _("'$' not for alias arguments"))
2287 args = []
2286 args = []
2288 for arg in getfuncargs(tree):
2287 for arg in getfuncargs(tree):
2289 if not isvalidsymbol(arg):
2288 if not isvalidsymbol(arg):
2290 return (decl, None, None, _("invalid argument list"))
2289 return (decl, None, None, _("invalid argument list"))
2291 args.append(getsymbol(arg))
2290 args.append(getsymbol(arg))
2292 if len(args) != len(set(args)):
2291 if len(args) != len(set(args)):
2293 return (name, None, None,
2292 return (name, None, None,
2294 _("argument names collide with each other"))
2293 _("argument names collide with each other"))
2295 return (name, ('func', ('symbol', name)), args, None)
2294 return (name, ('func', ('symbol', name)), args, None)
2296
2295
2297 return (decl, None, None, _("invalid format"))
2296 return (decl, None, None, _("invalid format"))
2298 except error.ParseError, inst:
2297 except error.ParseError, inst:
2299 return (decl, None, None, parseerrordetail(inst))
2298 return (decl, None, None, parseerrordetail(inst))
2300
2299
2301 def _parsealiasdefn(defn, args):
2300 def _parsealiasdefn(defn, args):
2302 """Parse alias definition ``defn``
2301 """Parse alias definition ``defn``
2303
2302
2304 This function also replaces alias argument references in the
2303 This function also replaces alias argument references in the
2305 specified definition by ``_aliasarg(ARGNAME)``.
2304 specified definition by ``_aliasarg(ARGNAME)``.
2306
2305
2307 ``args`` is a list of alias argument names, or None if the alias
2306 ``args`` is a list of alias argument names, or None if the alias
2308 is declared as a symbol.
2307 is declared as a symbol.
2309
2308
2310 This returns "tree" as parsing result.
2309 This returns "tree" as parsing result.
2311
2310
2312 >>> args = ['$1', '$2', 'foo']
2311 >>> args = ['$1', '$2', 'foo']
2313 >>> print prettyformat(_parsealiasdefn('$1 or foo', args))
2312 >>> print prettyformat(_parsealiasdefn('$1 or foo', args))
2314 (or
2313 (or
2315 (func
2314 (func
2316 ('symbol', '_aliasarg')
2315 ('symbol', '_aliasarg')
2317 ('string', '$1'))
2316 ('string', '$1'))
2318 (func
2317 (func
2319 ('symbol', '_aliasarg')
2318 ('symbol', '_aliasarg')
2320 ('string', 'foo')))
2319 ('string', 'foo')))
2321 >>> try:
2320 >>> try:
2322 ... _parsealiasdefn('$1 or $bar', args)
2321 ... _parsealiasdefn('$1 or $bar', args)
2323 ... except error.ParseError, inst:
2322 ... except error.ParseError, inst:
2324 ... print parseerrordetail(inst)
2323 ... print parseerrordetail(inst)
2325 at 6: '$' not for alias arguments
2324 at 6: '$' not for alias arguments
2326 >>> args = ['$1', '$10', 'foo']
2325 >>> args = ['$1', '$10', 'foo']
2327 >>> print prettyformat(_parsealiasdefn('$10 or foobar', args))
2326 >>> print prettyformat(_parsealiasdefn('$10 or foobar', args))
2328 (or
2327 (or
2329 (func
2328 (func
2330 ('symbol', '_aliasarg')
2329 ('symbol', '_aliasarg')
2331 ('string', '$10'))
2330 ('string', '$10'))
2332 ('symbol', 'foobar'))
2331 ('symbol', 'foobar'))
2333 >>> print prettyformat(_parsealiasdefn('"$1" or "foo"', args))
2332 >>> print prettyformat(_parsealiasdefn('"$1" or "foo"', args))
2334 (or
2333 (or
2335 ('string', '$1')
2334 ('string', '$1')
2336 ('string', 'foo'))
2335 ('string', 'foo'))
2337 """
2336 """
2338 def tokenizedefn(program, lookup=None):
2337 def tokenizedefn(program, lookup=None):
2339 if args:
2338 if args:
2340 argset = set(args)
2339 argset = set(args)
2341 else:
2340 else:
2342 argset = set()
2341 argset = set()
2343
2342
2344 for t, value, pos in _tokenizealias(program, lookup=lookup):
2343 for t, value, pos in _tokenizealias(program, lookup=lookup):
2345 if t == 'symbol':
2344 if t == 'symbol':
2346 if value in argset:
2345 if value in argset:
2347 # emulate tokenization of "_aliasarg('ARGNAME')":
2346 # emulate tokenization of "_aliasarg('ARGNAME')":
2348 # "_aliasarg()" is an unknown symbol only used separate
2347 # "_aliasarg()" is an unknown symbol only used separate
2349 # alias argument placeholders from regular strings.
2348 # alias argument placeholders from regular strings.
2350 yield ('symbol', '_aliasarg', pos)
2349 yield ('symbol', '_aliasarg', pos)
2351 yield ('(', None, pos)
2350 yield ('(', None, pos)
2352 yield ('string', value, pos)
2351 yield ('string', value, pos)
2353 yield (')', None, pos)
2352 yield (')', None, pos)
2354 continue
2353 continue
2355 elif value.startswith('$'):
2354 elif value.startswith('$'):
2356 raise error.ParseError(_("'$' not for alias arguments"),
2355 raise error.ParseError(_("'$' not for alias arguments"),
2357 pos)
2356 pos)
2358 yield (t, value, pos)
2357 yield (t, value, pos)
2359
2358
2360 p = parser.parser(tokenizedefn, elements)
2359 p = parser.parser(tokenizedefn, elements)
2361 tree, pos = p.parse(defn)
2360 tree, pos = p.parse(defn)
2362 if pos != len(defn):
2361 if pos != len(defn):
2363 raise error.ParseError(_('invalid token'), pos)
2362 raise error.ParseError(_('invalid token'), pos)
2364 return tree
2363 return tree
2365
2364
2366 class revsetalias(object):
2365 class revsetalias(object):
2367 # whether own `error` information is already shown or not.
2366 # whether own `error` information is already shown or not.
2368 # this avoids showing same warning multiple times at each `findaliases`.
2367 # this avoids showing same warning multiple times at each `findaliases`.
2369 warned = False
2368 warned = False
2370
2369
2371 def __init__(self, name, value):
2370 def __init__(self, name, value):
2372 '''Aliases like:
2371 '''Aliases like:
2373
2372
2374 h = heads(default)
2373 h = heads(default)
2375 b($1) = ancestors($1) - ancestors(default)
2374 b($1) = ancestors($1) - ancestors(default)
2376 '''
2375 '''
2377 self.name, self.tree, self.args, self.error = _parsealiasdecl(name)
2376 self.name, self.tree, self.args, self.error = _parsealiasdecl(name)
2378 if self.error:
2377 if self.error:
2379 self.error = _('failed to parse the declaration of revset alias'
2378 self.error = _('failed to parse the declaration of revset alias'
2380 ' "%s": %s') % (self.name, self.error)
2379 ' "%s": %s') % (self.name, self.error)
2381 return
2380 return
2382
2381
2383 try:
2382 try:
2384 self.replacement = _parsealiasdefn(value, self.args)
2383 self.replacement = _parsealiasdefn(value, self.args)
2385 # Check for placeholder injection
2384 # Check for placeholder injection
2386 _checkaliasarg(self.replacement, self.args)
2385 _checkaliasarg(self.replacement, self.args)
2387 except error.ParseError, inst:
2386 except error.ParseError, inst:
2388 self.error = _('failed to parse the definition of revset alias'
2387 self.error = _('failed to parse the definition of revset alias'
2389 ' "%s": %s') % (self.name, parseerrordetail(inst))
2388 ' "%s": %s') % (self.name, parseerrordetail(inst))
2390
2389
2391 def _getalias(aliases, tree):
2390 def _getalias(aliases, tree):
2392 """If tree looks like an unexpanded alias, return it. Return None
2391 """If tree looks like an unexpanded alias, return it. Return None
2393 otherwise.
2392 otherwise.
2394 """
2393 """
2395 if isinstance(tree, tuple) and tree:
2394 if isinstance(tree, tuple) and tree:
2396 if tree[0] == 'symbol' and len(tree) == 2:
2395 if tree[0] == 'symbol' and len(tree) == 2:
2397 name = tree[1]
2396 name = tree[1]
2398 alias = aliases.get(name)
2397 alias = aliases.get(name)
2399 if alias and alias.args is None and alias.tree == tree:
2398 if alias and alias.args is None and alias.tree == tree:
2400 return alias
2399 return alias
2401 if tree[0] == 'func' and len(tree) > 1:
2400 if tree[0] == 'func' and len(tree) > 1:
2402 if tree[1][0] == 'symbol' and len(tree[1]) == 2:
2401 if tree[1][0] == 'symbol' and len(tree[1]) == 2:
2403 name = tree[1][1]
2402 name = tree[1][1]
2404 alias = aliases.get(name)
2403 alias = aliases.get(name)
2405 if alias and alias.args is not None and alias.tree == tree[:2]:
2404 if alias and alias.args is not None and alias.tree == tree[:2]:
2406 return alias
2405 return alias
2407 return None
2406 return None
2408
2407
2409 def _expandargs(tree, args):
2408 def _expandargs(tree, args):
2410 """Replace _aliasarg instances with the substitution value of the
2409 """Replace _aliasarg instances with the substitution value of the
2411 same name in args, recursively.
2410 same name in args, recursively.
2412 """
2411 """
2413 if not tree or not isinstance(tree, tuple):
2412 if not tree or not isinstance(tree, tuple):
2414 return tree
2413 return tree
2415 arg = _getaliasarg(tree)
2414 arg = _getaliasarg(tree)
2416 if arg is not None:
2415 if arg is not None:
2417 return args[arg]
2416 return args[arg]
2418 return tuple(_expandargs(t, args) for t in tree)
2417 return tuple(_expandargs(t, args) for t in tree)
2419
2418
2420 def _expandaliases(aliases, tree, expanding, cache):
2419 def _expandaliases(aliases, tree, expanding, cache):
2421 """Expand aliases in tree, recursively.
2420 """Expand aliases in tree, recursively.
2422
2421
2423 'aliases' is a dictionary mapping user defined aliases to
2422 'aliases' is a dictionary mapping user defined aliases to
2424 revsetalias objects.
2423 revsetalias objects.
2425 """
2424 """
2426 if not isinstance(tree, tuple):
2425 if not isinstance(tree, tuple):
2427 # Do not expand raw strings
2426 # Do not expand raw strings
2428 return tree
2427 return tree
2429 alias = _getalias(aliases, tree)
2428 alias = _getalias(aliases, tree)
2430 if alias is not None:
2429 if alias is not None:
2431 if alias.error:
2430 if alias.error:
2432 raise util.Abort(alias.error)
2431 raise util.Abort(alias.error)
2433 if alias in expanding:
2432 if alias in expanding:
2434 raise error.ParseError(_('infinite expansion of revset alias "%s" '
2433 raise error.ParseError(_('infinite expansion of revset alias "%s" '
2435 'detected') % alias.name)
2434 'detected') % alias.name)
2436 expanding.append(alias)
2435 expanding.append(alias)
2437 if alias.name not in cache:
2436 if alias.name not in cache:
2438 cache[alias.name] = _expandaliases(aliases, alias.replacement,
2437 cache[alias.name] = _expandaliases(aliases, alias.replacement,
2439 expanding, cache)
2438 expanding, cache)
2440 result = cache[alias.name]
2439 result = cache[alias.name]
2441 expanding.pop()
2440 expanding.pop()
2442 if alias.args is not None:
2441 if alias.args is not None:
2443 l = getlist(tree[2])
2442 l = getlist(tree[2])
2444 if len(l) != len(alias.args):
2443 if len(l) != len(alias.args):
2445 raise error.ParseError(
2444 raise error.ParseError(
2446 _('invalid number of arguments: %s') % len(l))
2445 _('invalid number of arguments: %s') % len(l))
2447 l = [_expandaliases(aliases, a, [], cache) for a in l]
2446 l = [_expandaliases(aliases, a, [], cache) for a in l]
2448 result = _expandargs(result, dict(zip(alias.args, l)))
2447 result = _expandargs(result, dict(zip(alias.args, l)))
2449 else:
2448 else:
2450 result = tuple(_expandaliases(aliases, t, expanding, cache)
2449 result = tuple(_expandaliases(aliases, t, expanding, cache)
2451 for t in tree)
2450 for t in tree)
2452 return result
2451 return result
2453
2452
2454 def findaliases(ui, tree, showwarning=None):
2453 def findaliases(ui, tree, showwarning=None):
2455 _checkaliasarg(tree)
2454 _checkaliasarg(tree)
2456 aliases = {}
2455 aliases = {}
2457 for k, v in ui.configitems('revsetalias'):
2456 for k, v in ui.configitems('revsetalias'):
2458 alias = revsetalias(k, v)
2457 alias = revsetalias(k, v)
2459 aliases[alias.name] = alias
2458 aliases[alias.name] = alias
2460 tree = _expandaliases(aliases, tree, [], {})
2459 tree = _expandaliases(aliases, tree, [], {})
2461 if showwarning:
2460 if showwarning:
2462 # warn about problematic (but not referred) aliases
2461 # warn about problematic (but not referred) aliases
2463 for name, alias in sorted(aliases.iteritems()):
2462 for name, alias in sorted(aliases.iteritems()):
2464 if alias.error and not alias.warned:
2463 if alias.error and not alias.warned:
2465 showwarning(_('warning: %s\n') % (alias.error))
2464 showwarning(_('warning: %s\n') % (alias.error))
2466 alias.warned = True
2465 alias.warned = True
2467 return tree
2466 return tree
2468
2467
2469 def foldconcat(tree):
2468 def foldconcat(tree):
2470 """Fold elements to be concatenated by `##`
2469 """Fold elements to be concatenated by `##`
2471 """
2470 """
2472 if not isinstance(tree, tuple) or tree[0] in ('string', 'symbol'):
2471 if not isinstance(tree, tuple) or tree[0] in ('string', 'symbol'):
2473 return tree
2472 return tree
2474 if tree[0] == '_concat':
2473 if tree[0] == '_concat':
2475 pending = [tree]
2474 pending = [tree]
2476 l = []
2475 l = []
2477 while pending:
2476 while pending:
2478 e = pending.pop()
2477 e = pending.pop()
2479 if e[0] == '_concat':
2478 if e[0] == '_concat':
2480 pending.extend(reversed(e[1:]))
2479 pending.extend(reversed(e[1:]))
2481 elif e[0] in ('string', 'symbol'):
2480 elif e[0] in ('string', 'symbol'):
2482 l.append(e[1])
2481 l.append(e[1])
2483 else:
2482 else:
2484 msg = _("\"##\" can't concatenate \"%s\" element") % (e[0])
2483 msg = _("\"##\" can't concatenate \"%s\" element") % (e[0])
2485 raise error.ParseError(msg)
2484 raise error.ParseError(msg)
2486 return ('string', ''.join(l))
2485 return ('string', ''.join(l))
2487 else:
2486 else:
2488 return tuple(foldconcat(t) for t in tree)
2487 return tuple(foldconcat(t) for t in tree)
2489
2488
2490 def parse(spec, lookup=None):
2489 def parse(spec, lookup=None):
2491 p = parser.parser(tokenize, elements)
2490 p = parser.parser(tokenize, elements)
2492 return p.parse(spec, lookup=lookup)
2491 return p.parse(spec, lookup=lookup)
2493
2492
2494 def posttreebuilthook(tree, repo):
2493 def posttreebuilthook(tree, repo):
2495 # hook for extensions to execute code on the optimized tree
2494 # hook for extensions to execute code on the optimized tree
2496 pass
2495 pass
2497
2496
2498 def match(ui, spec, repo=None):
2497 def match(ui, spec, repo=None):
2499 if not spec:
2498 if not spec:
2500 raise error.ParseError(_("empty query"))
2499 raise error.ParseError(_("empty query"))
2501 lookup = None
2500 lookup = None
2502 if repo:
2501 if repo:
2503 lookup = repo.__contains__
2502 lookup = repo.__contains__
2504 tree, pos = parse(spec, lookup)
2503 tree, pos = parse(spec, lookup)
2505 if (pos != len(spec)):
2504 if (pos != len(spec)):
2506 raise error.ParseError(_("invalid token"), pos)
2505 raise error.ParseError(_("invalid token"), pos)
2507 if ui:
2506 if ui:
2508 tree = findaliases(ui, tree, showwarning=ui.warn)
2507 tree = findaliases(ui, tree, showwarning=ui.warn)
2509 tree = foldconcat(tree)
2508 tree = foldconcat(tree)
2510 weight, tree = optimize(tree, True)
2509 weight, tree = optimize(tree, True)
2511 posttreebuilthook(tree, repo)
2510 posttreebuilthook(tree, repo)
2512 def mfunc(repo, subset=None):
2511 def mfunc(repo, subset=None):
2513 if subset is None:
2512 if subset is None:
2514 subset = fullreposet(repo)
2513 subset = fullreposet(repo)
2515 if util.safehasattr(subset, 'isascending'):
2514 if util.safehasattr(subset, 'isascending'):
2516 result = getset(repo, subset, tree)
2515 result = getset(repo, subset, tree)
2517 else:
2516 else:
2518 result = getset(repo, baseset(subset), tree)
2517 result = getset(repo, baseset(subset), tree)
2519 return result
2518 return result
2520 return mfunc
2519 return mfunc
2521
2520
2522 def formatspec(expr, *args):
2521 def formatspec(expr, *args):
2523 '''
2522 '''
2524 This is a convenience function for using revsets internally, and
2523 This is a convenience function for using revsets internally, and
2525 escapes arguments appropriately. Aliases are intentionally ignored
2524 escapes arguments appropriately. Aliases are intentionally ignored
2526 so that intended expression behavior isn't accidentally subverted.
2525 so that intended expression behavior isn't accidentally subverted.
2527
2526
2528 Supported arguments:
2527 Supported arguments:
2529
2528
2530 %r = revset expression, parenthesized
2529 %r = revset expression, parenthesized
2531 %d = int(arg), no quoting
2530 %d = int(arg), no quoting
2532 %s = string(arg), escaped and single-quoted
2531 %s = string(arg), escaped and single-quoted
2533 %b = arg.branch(), escaped and single-quoted
2532 %b = arg.branch(), escaped and single-quoted
2534 %n = hex(arg), single-quoted
2533 %n = hex(arg), single-quoted
2535 %% = a literal '%'
2534 %% = a literal '%'
2536
2535
2537 Prefixing the type with 'l' specifies a parenthesized list of that type.
2536 Prefixing the type with 'l' specifies a parenthesized list of that type.
2538
2537
2539 >>> formatspec('%r:: and %lr', '10 or 11', ("this()", "that()"))
2538 >>> formatspec('%r:: and %lr', '10 or 11', ("this()", "that()"))
2540 '(10 or 11):: and ((this()) or (that()))'
2539 '(10 or 11):: and ((this()) or (that()))'
2541 >>> formatspec('%d:: and not %d::', 10, 20)
2540 >>> formatspec('%d:: and not %d::', 10, 20)
2542 '10:: and not 20::'
2541 '10:: and not 20::'
2543 >>> formatspec('%ld or %ld', [], [1])
2542 >>> formatspec('%ld or %ld', [], [1])
2544 "_list('') or 1"
2543 "_list('') or 1"
2545 >>> formatspec('keyword(%s)', 'foo\\xe9')
2544 >>> formatspec('keyword(%s)', 'foo\\xe9')
2546 "keyword('foo\\\\xe9')"
2545 "keyword('foo\\\\xe9')"
2547 >>> b = lambda: 'default'
2546 >>> b = lambda: 'default'
2548 >>> b.branch = b
2547 >>> b.branch = b
2549 >>> formatspec('branch(%b)', b)
2548 >>> formatspec('branch(%b)', b)
2550 "branch('default')"
2549 "branch('default')"
2551 >>> formatspec('root(%ls)', ['a', 'b', 'c', 'd'])
2550 >>> formatspec('root(%ls)', ['a', 'b', 'c', 'd'])
2552 "root(_list('a\\x00b\\x00c\\x00d'))"
2551 "root(_list('a\\x00b\\x00c\\x00d'))"
2553 '''
2552 '''
2554
2553
2555 def quote(s):
2554 def quote(s):
2556 return repr(str(s))
2555 return repr(str(s))
2557
2556
2558 def argtype(c, arg):
2557 def argtype(c, arg):
2559 if c == 'd':
2558 if c == 'd':
2560 return str(int(arg))
2559 return str(int(arg))
2561 elif c == 's':
2560 elif c == 's':
2562 return quote(arg)
2561 return quote(arg)
2563 elif c == 'r':
2562 elif c == 'r':
2564 parse(arg) # make sure syntax errors are confined
2563 parse(arg) # make sure syntax errors are confined
2565 return '(%s)' % arg
2564 return '(%s)' % arg
2566 elif c == 'n':
2565 elif c == 'n':
2567 return quote(node.hex(arg))
2566 return quote(node.hex(arg))
2568 elif c == 'b':
2567 elif c == 'b':
2569 return quote(arg.branch())
2568 return quote(arg.branch())
2570
2569
2571 def listexp(s, t):
2570 def listexp(s, t):
2572 l = len(s)
2571 l = len(s)
2573 if l == 0:
2572 if l == 0:
2574 return "_list('')"
2573 return "_list('')"
2575 elif l == 1:
2574 elif l == 1:
2576 return argtype(t, s[0])
2575 return argtype(t, s[0])
2577 elif t == 'd':
2576 elif t == 'd':
2578 return "_intlist('%s')" % "\0".join(str(int(a)) for a in s)
2577 return "_intlist('%s')" % "\0".join(str(int(a)) for a in s)
2579 elif t == 's':
2578 elif t == 's':
2580 return "_list('%s')" % "\0".join(s)
2579 return "_list('%s')" % "\0".join(s)
2581 elif t == 'n':
2580 elif t == 'n':
2582 return "_hexlist('%s')" % "\0".join(node.hex(a) for a in s)
2581 return "_hexlist('%s')" % "\0".join(node.hex(a) for a in s)
2583 elif t == 'b':
2582 elif t == 'b':
2584 return "_list('%s')" % "\0".join(a.branch() for a in s)
2583 return "_list('%s')" % "\0".join(a.branch() for a in s)
2585
2584
2586 m = l // 2
2585 m = l // 2
2587 return '(%s or %s)' % (listexp(s[:m], t), listexp(s[m:], t))
2586 return '(%s or %s)' % (listexp(s[:m], t), listexp(s[m:], t))
2588
2587
2589 ret = ''
2588 ret = ''
2590 pos = 0
2589 pos = 0
2591 arg = 0
2590 arg = 0
2592 while pos < len(expr):
2591 while pos < len(expr):
2593 c = expr[pos]
2592 c = expr[pos]
2594 if c == '%':
2593 if c == '%':
2595 pos += 1
2594 pos += 1
2596 d = expr[pos]
2595 d = expr[pos]
2597 if d == '%':
2596 if d == '%':
2598 ret += d
2597 ret += d
2599 elif d in 'dsnbr':
2598 elif d in 'dsnbr':
2600 ret += argtype(d, args[arg])
2599 ret += argtype(d, args[arg])
2601 arg += 1
2600 arg += 1
2602 elif d == 'l':
2601 elif d == 'l':
2603 # a list of some type
2602 # a list of some type
2604 pos += 1
2603 pos += 1
2605 d = expr[pos]
2604 d = expr[pos]
2606 ret += listexp(list(args[arg]), d)
2605 ret += listexp(list(args[arg]), d)
2607 arg += 1
2606 arg += 1
2608 else:
2607 else:
2609 raise util.Abort('unexpected revspec format character %s' % d)
2608 raise util.Abort('unexpected revspec format character %s' % d)
2610 else:
2609 else:
2611 ret += c
2610 ret += c
2612 pos += 1
2611 pos += 1
2613
2612
2614 return ret
2613 return ret
2615
2614
2616 def prettyformat(tree):
2615 def prettyformat(tree):
2617 def _prettyformat(tree, level, lines):
2616 def _prettyformat(tree, level, lines):
2618 if not isinstance(tree, tuple) or tree[0] in ('string', 'symbol'):
2617 if not isinstance(tree, tuple) or tree[0] in ('string', 'symbol'):
2619 lines.append((level, str(tree)))
2618 lines.append((level, str(tree)))
2620 else:
2619 else:
2621 lines.append((level, '(%s' % tree[0]))
2620 lines.append((level, '(%s' % tree[0]))
2622 for s in tree[1:]:
2621 for s in tree[1:]:
2623 _prettyformat(s, level + 1, lines)
2622 _prettyformat(s, level + 1, lines)
2624 lines[-1:] = [(lines[-1][0], lines[-1][1] + ')')]
2623 lines[-1:] = [(lines[-1][0], lines[-1][1] + ')')]
2625
2624
2626 lines = []
2625 lines = []
2627 _prettyformat(tree, 0, lines)
2626 _prettyformat(tree, 0, lines)
2628 output = '\n'.join((' '*l + s) for l, s in lines)
2627 output = '\n'.join((' '*l + s) for l, s in lines)
2629 return output
2628 return output
2630
2629
2631 def depth(tree):
2630 def depth(tree):
2632 if isinstance(tree, tuple):
2631 if isinstance(tree, tuple):
2633 return max(map(depth, tree)) + 1
2632 return max(map(depth, tree)) + 1
2634 else:
2633 else:
2635 return 0
2634 return 0
2636
2635
2637 def funcsused(tree):
2636 def funcsused(tree):
2638 if not isinstance(tree, tuple) or tree[0] in ('string', 'symbol'):
2637 if not isinstance(tree, tuple) or tree[0] in ('string', 'symbol'):
2639 return set()
2638 return set()
2640 else:
2639 else:
2641 funcs = set()
2640 funcs = set()
2642 for s in tree[1:]:
2641 for s in tree[1:]:
2643 funcs |= funcsused(s)
2642 funcs |= funcsused(s)
2644 if tree[0] == 'func':
2643 if tree[0] == 'func':
2645 funcs.add(tree[1][1])
2644 funcs.add(tree[1][1])
2646 return funcs
2645 return funcs
2647
2646
2648 class abstractsmartset(object):
2647 class abstractsmartset(object):
2649
2648
2650 def __nonzero__(self):
2649 def __nonzero__(self):
2651 """True if the smartset is not empty"""
2650 """True if the smartset is not empty"""
2652 raise NotImplementedError()
2651 raise NotImplementedError()
2653
2652
2654 def __contains__(self, rev):
2653 def __contains__(self, rev):
2655 """provide fast membership testing"""
2654 """provide fast membership testing"""
2656 raise NotImplementedError()
2655 raise NotImplementedError()
2657
2656
2658 def __iter__(self):
2657 def __iter__(self):
2659 """iterate the set in the order it is supposed to be iterated"""
2658 """iterate the set in the order it is supposed to be iterated"""
2660 raise NotImplementedError()
2659 raise NotImplementedError()
2661
2660
2662 # Attributes containing a function to perform a fast iteration in a given
2661 # Attributes containing a function to perform a fast iteration in a given
2663 # direction. A smartset can have none, one, or both defined.
2662 # direction. A smartset can have none, one, or both defined.
2664 #
2663 #
2665 # Default value is None instead of a function returning None to avoid
2664 # Default value is None instead of a function returning None to avoid
2666 # initializing an iterator just for testing if a fast method exists.
2665 # initializing an iterator just for testing if a fast method exists.
2667 fastasc = None
2666 fastasc = None
2668 fastdesc = None
2667 fastdesc = None
2669
2668
2670 def isascending(self):
2669 def isascending(self):
2671 """True if the set will iterate in ascending order"""
2670 """True if the set will iterate in ascending order"""
2672 raise NotImplementedError()
2671 raise NotImplementedError()
2673
2672
2674 def isdescending(self):
2673 def isdescending(self):
2675 """True if the set will iterate in descending order"""
2674 """True if the set will iterate in descending order"""
2676 raise NotImplementedError()
2675 raise NotImplementedError()
2677
2676
2678 def min(self):
2677 def min(self):
2679 """return the minimum element in the set"""
2678 """return the minimum element in the set"""
2680 if self.fastasc is not None:
2679 if self.fastasc is not None:
2681 for r in self.fastasc():
2680 for r in self.fastasc():
2682 return r
2681 return r
2683 raise ValueError('arg is an empty sequence')
2682 raise ValueError('arg is an empty sequence')
2684 return min(self)
2683 return min(self)
2685
2684
2686 def max(self):
2685 def max(self):
2687 """return the maximum element in the set"""
2686 """return the maximum element in the set"""
2688 if self.fastdesc is not None:
2687 if self.fastdesc is not None:
2689 for r in self.fastdesc():
2688 for r in self.fastdesc():
2690 return r
2689 return r
2691 raise ValueError('arg is an empty sequence')
2690 raise ValueError('arg is an empty sequence')
2692 return max(self)
2691 return max(self)
2693
2692
2694 def first(self):
2693 def first(self):
2695 """return the first element in the set (user iteration perspective)
2694 """return the first element in the set (user iteration perspective)
2696
2695
2697 Return None if the set is empty"""
2696 Return None if the set is empty"""
2698 raise NotImplementedError()
2697 raise NotImplementedError()
2699
2698
2700 def last(self):
2699 def last(self):
2701 """return the last element in the set (user iteration perspective)
2700 """return the last element in the set (user iteration perspective)
2702
2701
2703 Return None if the set is empty"""
2702 Return None if the set is empty"""
2704 raise NotImplementedError()
2703 raise NotImplementedError()
2705
2704
2706 def __len__(self):
2705 def __len__(self):
2707 """return the length of the smartsets
2706 """return the length of the smartsets
2708
2707
2709 This can be expensive on smartset that could be lazy otherwise."""
2708 This can be expensive on smartset that could be lazy otherwise."""
2710 raise NotImplementedError()
2709 raise NotImplementedError()
2711
2710
2712 def reverse(self):
2711 def reverse(self):
2713 """reverse the expected iteration order"""
2712 """reverse the expected iteration order"""
2714 raise NotImplementedError()
2713 raise NotImplementedError()
2715
2714
2716 def sort(self, reverse=True):
2715 def sort(self, reverse=True):
2717 """get the set to iterate in an ascending or descending order"""
2716 """get the set to iterate in an ascending or descending order"""
2718 raise NotImplementedError()
2717 raise NotImplementedError()
2719
2718
2720 def __and__(self, other):
2719 def __and__(self, other):
2721 """Returns a new object with the intersection of the two collections.
2720 """Returns a new object with the intersection of the two collections.
2722
2721
2723 This is part of the mandatory API for smartset."""
2722 This is part of the mandatory API for smartset."""
2724 if isinstance(other, fullreposet):
2723 if isinstance(other, fullreposet):
2725 return self
2724 return self
2726 return self.filter(other.__contains__, cache=False)
2725 return self.filter(other.__contains__, cache=False)
2727
2726
2728 def __add__(self, other):
2727 def __add__(self, other):
2729 """Returns a new object with the union of the two collections.
2728 """Returns a new object with the union of the two collections.
2730
2729
2731 This is part of the mandatory API for smartset."""
2730 This is part of the mandatory API for smartset."""
2732 return addset(self, other)
2731 return addset(self, other)
2733
2732
2734 def __sub__(self, other):
2733 def __sub__(self, other):
2735 """Returns a new object with the substraction of the two collections.
2734 """Returns a new object with the substraction of the two collections.
2736
2735
2737 This is part of the mandatory API for smartset."""
2736 This is part of the mandatory API for smartset."""
2738 c = other.__contains__
2737 c = other.__contains__
2739 return self.filter(lambda r: not c(r), cache=False)
2738 return self.filter(lambda r: not c(r), cache=False)
2740
2739
2741 def filter(self, condition, cache=True):
2740 def filter(self, condition, cache=True):
2742 """Returns this smartset filtered by condition as a new smartset.
2741 """Returns this smartset filtered by condition as a new smartset.
2743
2742
2744 `condition` is a callable which takes a revision number and returns a
2743 `condition` is a callable which takes a revision number and returns a
2745 boolean.
2744 boolean.
2746
2745
2747 This is part of the mandatory API for smartset."""
2746 This is part of the mandatory API for smartset."""
2748 # builtin cannot be cached. but do not needs to
2747 # builtin cannot be cached. but do not needs to
2749 if cache and util.safehasattr(condition, 'func_code'):
2748 if cache and util.safehasattr(condition, 'func_code'):
2750 condition = util.cachefunc(condition)
2749 condition = util.cachefunc(condition)
2751 return filteredset(self, condition)
2750 return filteredset(self, condition)
2752
2751
2753 class baseset(abstractsmartset):
2752 class baseset(abstractsmartset):
2754 """Basic data structure that represents a revset and contains the basic
2753 """Basic data structure that represents a revset and contains the basic
2755 operation that it should be able to perform.
2754 operation that it should be able to perform.
2756
2755
2757 Every method in this class should be implemented by any smartset class.
2756 Every method in this class should be implemented by any smartset class.
2758 """
2757 """
2759 def __init__(self, data=()):
2758 def __init__(self, data=()):
2760 if not isinstance(data, list):
2759 if not isinstance(data, list):
2761 data = list(data)
2760 data = list(data)
2762 self._list = data
2761 self._list = data
2763 self._ascending = None
2762 self._ascending = None
2764
2763
2765 @util.propertycache
2764 @util.propertycache
2766 def _set(self):
2765 def _set(self):
2767 return set(self._list)
2766 return set(self._list)
2768
2767
2769 @util.propertycache
2768 @util.propertycache
2770 def _asclist(self):
2769 def _asclist(self):
2771 asclist = self._list[:]
2770 asclist = self._list[:]
2772 asclist.sort()
2771 asclist.sort()
2773 return asclist
2772 return asclist
2774
2773
2775 def __iter__(self):
2774 def __iter__(self):
2776 if self._ascending is None:
2775 if self._ascending is None:
2777 return iter(self._list)
2776 return iter(self._list)
2778 elif self._ascending:
2777 elif self._ascending:
2779 return iter(self._asclist)
2778 return iter(self._asclist)
2780 else:
2779 else:
2781 return reversed(self._asclist)
2780 return reversed(self._asclist)
2782
2781
2783 def fastasc(self):
2782 def fastasc(self):
2784 return iter(self._asclist)
2783 return iter(self._asclist)
2785
2784
2786 def fastdesc(self):
2785 def fastdesc(self):
2787 return reversed(self._asclist)
2786 return reversed(self._asclist)
2788
2787
2789 @util.propertycache
2788 @util.propertycache
2790 def __contains__(self):
2789 def __contains__(self):
2791 return self._set.__contains__
2790 return self._set.__contains__
2792
2791
2793 def __nonzero__(self):
2792 def __nonzero__(self):
2794 return bool(self._list)
2793 return bool(self._list)
2795
2794
2796 def sort(self, reverse=False):
2795 def sort(self, reverse=False):
2797 self._ascending = not bool(reverse)
2796 self._ascending = not bool(reverse)
2798
2797
2799 def reverse(self):
2798 def reverse(self):
2800 if self._ascending is None:
2799 if self._ascending is None:
2801 self._list.reverse()
2800 self._list.reverse()
2802 else:
2801 else:
2803 self._ascending = not self._ascending
2802 self._ascending = not self._ascending
2804
2803
2805 def __len__(self):
2804 def __len__(self):
2806 return len(self._list)
2805 return len(self._list)
2807
2806
2808 def isascending(self):
2807 def isascending(self):
2809 """Returns True if the collection is ascending order, False if not.
2808 """Returns True if the collection is ascending order, False if not.
2810
2809
2811 This is part of the mandatory API for smartset."""
2810 This is part of the mandatory API for smartset."""
2812 if len(self) <= 1:
2811 if len(self) <= 1:
2813 return True
2812 return True
2814 return self._ascending is not None and self._ascending
2813 return self._ascending is not None and self._ascending
2815
2814
2816 def isdescending(self):
2815 def isdescending(self):
2817 """Returns True if the collection is descending order, False if not.
2816 """Returns True if the collection is descending order, False if not.
2818
2817
2819 This is part of the mandatory API for smartset."""
2818 This is part of the mandatory API for smartset."""
2820 if len(self) <= 1:
2819 if len(self) <= 1:
2821 return True
2820 return True
2822 return self._ascending is not None and not self._ascending
2821 return self._ascending is not None and not self._ascending
2823
2822
2824 def first(self):
2823 def first(self):
2825 if self:
2824 if self:
2826 if self._ascending is None:
2825 if self._ascending is None:
2827 return self._list[0]
2826 return self._list[0]
2828 elif self._ascending:
2827 elif self._ascending:
2829 return self._asclist[0]
2828 return self._asclist[0]
2830 else:
2829 else:
2831 return self._asclist[-1]
2830 return self._asclist[-1]
2832 return None
2831 return None
2833
2832
2834 def last(self):
2833 def last(self):
2835 if self:
2834 if self:
2836 if self._ascending is None:
2835 if self._ascending is None:
2837 return self._list[-1]
2836 return self._list[-1]
2838 elif self._ascending:
2837 elif self._ascending:
2839 return self._asclist[-1]
2838 return self._asclist[-1]
2840 else:
2839 else:
2841 return self._asclist[0]
2840 return self._asclist[0]
2842 return None
2841 return None
2843
2842
2844 def __repr__(self):
2843 def __repr__(self):
2845 d = {None: '', False: '-', True: '+'}[self._ascending]
2844 d = {None: '', False: '-', True: '+'}[self._ascending]
2846 return '<%s%s %r>' % (type(self).__name__, d, self._list)
2845 return '<%s%s %r>' % (type(self).__name__, d, self._list)
2847
2846
2848 class filteredset(abstractsmartset):
2847 class filteredset(abstractsmartset):
2849 """Duck type for baseset class which iterates lazily over the revisions in
2848 """Duck type for baseset class which iterates lazily over the revisions in
2850 the subset and contains a function which tests for membership in the
2849 the subset and contains a function which tests for membership in the
2851 revset
2850 revset
2852 """
2851 """
2853 def __init__(self, subset, condition=lambda x: True):
2852 def __init__(self, subset, condition=lambda x: True):
2854 """
2853 """
2855 condition: a function that decide whether a revision in the subset
2854 condition: a function that decide whether a revision in the subset
2856 belongs to the revset or not.
2855 belongs to the revset or not.
2857 """
2856 """
2858 self._subset = subset
2857 self._subset = subset
2859 self._condition = condition
2858 self._condition = condition
2860 self._cache = {}
2859 self._cache = {}
2861
2860
2862 def __contains__(self, x):
2861 def __contains__(self, x):
2863 c = self._cache
2862 c = self._cache
2864 if x not in c:
2863 if x not in c:
2865 v = c[x] = x in self._subset and self._condition(x)
2864 v = c[x] = x in self._subset and self._condition(x)
2866 return v
2865 return v
2867 return c[x]
2866 return c[x]
2868
2867
2869 def __iter__(self):
2868 def __iter__(self):
2870 return self._iterfilter(self._subset)
2869 return self._iterfilter(self._subset)
2871
2870
2872 def _iterfilter(self, it):
2871 def _iterfilter(self, it):
2873 cond = self._condition
2872 cond = self._condition
2874 for x in it:
2873 for x in it:
2875 if cond(x):
2874 if cond(x):
2876 yield x
2875 yield x
2877
2876
2878 @property
2877 @property
2879 def fastasc(self):
2878 def fastasc(self):
2880 it = self._subset.fastasc
2879 it = self._subset.fastasc
2881 if it is None:
2880 if it is None:
2882 return None
2881 return None
2883 return lambda: self._iterfilter(it())
2882 return lambda: self._iterfilter(it())
2884
2883
2885 @property
2884 @property
2886 def fastdesc(self):
2885 def fastdesc(self):
2887 it = self._subset.fastdesc
2886 it = self._subset.fastdesc
2888 if it is None:
2887 if it is None:
2889 return None
2888 return None
2890 return lambda: self._iterfilter(it())
2889 return lambda: self._iterfilter(it())
2891
2890
2892 def __nonzero__(self):
2891 def __nonzero__(self):
2893 for r in self:
2892 for r in self:
2894 return True
2893 return True
2895 return False
2894 return False
2896
2895
2897 def __len__(self):
2896 def __len__(self):
2898 # Basic implementation to be changed in future patches.
2897 # Basic implementation to be changed in future patches.
2899 l = baseset([r for r in self])
2898 l = baseset([r for r in self])
2900 return len(l)
2899 return len(l)
2901
2900
2902 def sort(self, reverse=False):
2901 def sort(self, reverse=False):
2903 self._subset.sort(reverse=reverse)
2902 self._subset.sort(reverse=reverse)
2904
2903
2905 def reverse(self):
2904 def reverse(self):
2906 self._subset.reverse()
2905 self._subset.reverse()
2907
2906
2908 def isascending(self):
2907 def isascending(self):
2909 return self._subset.isascending()
2908 return self._subset.isascending()
2910
2909
2911 def isdescending(self):
2910 def isdescending(self):
2912 return self._subset.isdescending()
2911 return self._subset.isdescending()
2913
2912
2914 def first(self):
2913 def first(self):
2915 for x in self:
2914 for x in self:
2916 return x
2915 return x
2917 return None
2916 return None
2918
2917
2919 def last(self):
2918 def last(self):
2920 it = None
2919 it = None
2921 if self._subset.isascending:
2920 if self._subset.isascending:
2922 it = self.fastdesc
2921 it = self.fastdesc
2923 elif self._subset.isdescending:
2922 elif self._subset.isdescending:
2924 it = self.fastdesc
2923 it = self.fastdesc
2925 if it is None:
2924 if it is None:
2926 # slowly consume everything. This needs improvement
2925 # slowly consume everything. This needs improvement
2927 it = lambda: reversed(list(self))
2926 it = lambda: reversed(list(self))
2928 for x in it():
2927 for x in it():
2929 return x
2928 return x
2930 return None
2929 return None
2931
2930
2932 def __repr__(self):
2931 def __repr__(self):
2933 return '<%s %r>' % (type(self).__name__, self._subset)
2932 return '<%s %r>' % (type(self).__name__, self._subset)
2934
2933
2935 def _iterordered(ascending, iter1, iter2):
2934 def _iterordered(ascending, iter1, iter2):
2936 """produce an ordered iteration from two iterators with the same order
2935 """produce an ordered iteration from two iterators with the same order
2937
2936
2938 The ascending is used to indicated the iteration direction.
2937 The ascending is used to indicated the iteration direction.
2939 """
2938 """
2940 choice = max
2939 choice = max
2941 if ascending:
2940 if ascending:
2942 choice = min
2941 choice = min
2943
2942
2944 val1 = None
2943 val1 = None
2945 val2 = None
2944 val2 = None
2946 try:
2945 try:
2947 # Consume both iterators in an ordered way until one is empty
2946 # Consume both iterators in an ordered way until one is empty
2948 while True:
2947 while True:
2949 if val1 is None:
2948 if val1 is None:
2950 val1 = iter1.next()
2949 val1 = iter1.next()
2951 if val2 is None:
2950 if val2 is None:
2952 val2 = iter2.next()
2951 val2 = iter2.next()
2953 next = choice(val1, val2)
2952 next = choice(val1, val2)
2954 yield next
2953 yield next
2955 if val1 == next:
2954 if val1 == next:
2956 val1 = None
2955 val1 = None
2957 if val2 == next:
2956 if val2 == next:
2958 val2 = None
2957 val2 = None
2959 except StopIteration:
2958 except StopIteration:
2960 # Flush any remaining values and consume the other one
2959 # Flush any remaining values and consume the other one
2961 it = iter2
2960 it = iter2
2962 if val1 is not None:
2961 if val1 is not None:
2963 yield val1
2962 yield val1
2964 it = iter1
2963 it = iter1
2965 elif val2 is not None:
2964 elif val2 is not None:
2966 # might have been equality and both are empty
2965 # might have been equality and both are empty
2967 yield val2
2966 yield val2
2968 for val in it:
2967 for val in it:
2969 yield val
2968 yield val
2970
2969
2971 class addset(abstractsmartset):
2970 class addset(abstractsmartset):
2972 """Represent the addition of two sets
2971 """Represent the addition of two sets
2973
2972
2974 Wrapper structure for lazily adding two structures without losing much
2973 Wrapper structure for lazily adding two structures without losing much
2975 performance on the __contains__ method
2974 performance on the __contains__ method
2976
2975
2977 If the ascending attribute is set, that means the two structures are
2976 If the ascending attribute is set, that means the two structures are
2978 ordered in either an ascending or descending way. Therefore, we can add
2977 ordered in either an ascending or descending way. Therefore, we can add
2979 them maintaining the order by iterating over both at the same time
2978 them maintaining the order by iterating over both at the same time
2980
2979
2981 >>> xs = baseset([0, 3, 2])
2980 >>> xs = baseset([0, 3, 2])
2982 >>> ys = baseset([5, 2, 4])
2981 >>> ys = baseset([5, 2, 4])
2983
2982
2984 >>> rs = addset(xs, ys)
2983 >>> rs = addset(xs, ys)
2985 >>> bool(rs), 0 in rs, 1 in rs, 5 in rs, rs.first(), rs.last()
2984 >>> bool(rs), 0 in rs, 1 in rs, 5 in rs, rs.first(), rs.last()
2986 (True, True, False, True, 0, 4)
2985 (True, True, False, True, 0, 4)
2987 >>> rs = addset(xs, baseset([]))
2986 >>> rs = addset(xs, baseset([]))
2988 >>> bool(rs), 0 in rs, 1 in rs, rs.first(), rs.last()
2987 >>> bool(rs), 0 in rs, 1 in rs, rs.first(), rs.last()
2989 (True, True, False, 0, 2)
2988 (True, True, False, 0, 2)
2990 >>> rs = addset(baseset([]), baseset([]))
2989 >>> rs = addset(baseset([]), baseset([]))
2991 >>> bool(rs), 0 in rs, rs.first(), rs.last()
2990 >>> bool(rs), 0 in rs, rs.first(), rs.last()
2992 (False, False, None, None)
2991 (False, False, None, None)
2993
2992
2994 iterate unsorted:
2993 iterate unsorted:
2995 >>> rs = addset(xs, ys)
2994 >>> rs = addset(xs, ys)
2996 >>> [x for x in rs] # without _genlist
2995 >>> [x for x in rs] # without _genlist
2997 [0, 3, 2, 5, 4]
2996 [0, 3, 2, 5, 4]
2998 >>> assert not rs._genlist
2997 >>> assert not rs._genlist
2999 >>> len(rs)
2998 >>> len(rs)
3000 5
2999 5
3001 >>> [x for x in rs] # with _genlist
3000 >>> [x for x in rs] # with _genlist
3002 [0, 3, 2, 5, 4]
3001 [0, 3, 2, 5, 4]
3003 >>> assert rs._genlist
3002 >>> assert rs._genlist
3004
3003
3005 iterate ascending:
3004 iterate ascending:
3006 >>> rs = addset(xs, ys, ascending=True)
3005 >>> rs = addset(xs, ys, ascending=True)
3007 >>> [x for x in rs], [x for x in rs.fastasc()] # without _asclist
3006 >>> [x for x in rs], [x for x in rs.fastasc()] # without _asclist
3008 ([0, 2, 3, 4, 5], [0, 2, 3, 4, 5])
3007 ([0, 2, 3, 4, 5], [0, 2, 3, 4, 5])
3009 >>> assert not rs._asclist
3008 >>> assert not rs._asclist
3010 >>> len(rs)
3009 >>> len(rs)
3011 5
3010 5
3012 >>> [x for x in rs], [x for x in rs.fastasc()]
3011 >>> [x for x in rs], [x for x in rs.fastasc()]
3013 ([0, 2, 3, 4, 5], [0, 2, 3, 4, 5])
3012 ([0, 2, 3, 4, 5], [0, 2, 3, 4, 5])
3014 >>> assert rs._asclist
3013 >>> assert rs._asclist
3015
3014
3016 iterate descending:
3015 iterate descending:
3017 >>> rs = addset(xs, ys, ascending=False)
3016 >>> rs = addset(xs, ys, ascending=False)
3018 >>> [x for x in rs], [x for x in rs.fastdesc()] # without _asclist
3017 >>> [x for x in rs], [x for x in rs.fastdesc()] # without _asclist
3019 ([5, 4, 3, 2, 0], [5, 4, 3, 2, 0])
3018 ([5, 4, 3, 2, 0], [5, 4, 3, 2, 0])
3020 >>> assert not rs._asclist
3019 >>> assert not rs._asclist
3021 >>> len(rs)
3020 >>> len(rs)
3022 5
3021 5
3023 >>> [x for x in rs], [x for x in rs.fastdesc()]
3022 >>> [x for x in rs], [x for x in rs.fastdesc()]
3024 ([5, 4, 3, 2, 0], [5, 4, 3, 2, 0])
3023 ([5, 4, 3, 2, 0], [5, 4, 3, 2, 0])
3025 >>> assert rs._asclist
3024 >>> assert rs._asclist
3026
3025
3027 iterate ascending without fastasc:
3026 iterate ascending without fastasc:
3028 >>> rs = addset(xs, generatorset(ys), ascending=True)
3027 >>> rs = addset(xs, generatorset(ys), ascending=True)
3029 >>> assert rs.fastasc is None
3028 >>> assert rs.fastasc is None
3030 >>> [x for x in rs]
3029 >>> [x for x in rs]
3031 [0, 2, 3, 4, 5]
3030 [0, 2, 3, 4, 5]
3032
3031
3033 iterate descending without fastdesc:
3032 iterate descending without fastdesc:
3034 >>> rs = addset(generatorset(xs), ys, ascending=False)
3033 >>> rs = addset(generatorset(xs), ys, ascending=False)
3035 >>> assert rs.fastdesc is None
3034 >>> assert rs.fastdesc is None
3036 >>> [x for x in rs]
3035 >>> [x for x in rs]
3037 [5, 4, 3, 2, 0]
3036 [5, 4, 3, 2, 0]
3038 """
3037 """
3039 def __init__(self, revs1, revs2, ascending=None):
3038 def __init__(self, revs1, revs2, ascending=None):
3040 self._r1 = revs1
3039 self._r1 = revs1
3041 self._r2 = revs2
3040 self._r2 = revs2
3042 self._iter = None
3041 self._iter = None
3043 self._ascending = ascending
3042 self._ascending = ascending
3044 self._genlist = None
3043 self._genlist = None
3045 self._asclist = None
3044 self._asclist = None
3046
3045
3047 def __len__(self):
3046 def __len__(self):
3048 return len(self._list)
3047 return len(self._list)
3049
3048
3050 def __nonzero__(self):
3049 def __nonzero__(self):
3051 return bool(self._r1) or bool(self._r2)
3050 return bool(self._r1) or bool(self._r2)
3052
3051
3053 @util.propertycache
3052 @util.propertycache
3054 def _list(self):
3053 def _list(self):
3055 if not self._genlist:
3054 if not self._genlist:
3056 self._genlist = baseset(iter(self))
3055 self._genlist = baseset(iter(self))
3057 return self._genlist
3056 return self._genlist
3058
3057
3059 def __iter__(self):
3058 def __iter__(self):
3060 """Iterate over both collections without repeating elements
3059 """Iterate over both collections without repeating elements
3061
3060
3062 If the ascending attribute is not set, iterate over the first one and
3061 If the ascending attribute is not set, iterate over the first one and
3063 then over the second one checking for membership on the first one so we
3062 then over the second one checking for membership on the first one so we
3064 dont yield any duplicates.
3063 dont yield any duplicates.
3065
3064
3066 If the ascending attribute is set, iterate over both collections at the
3065 If the ascending attribute is set, iterate over both collections at the
3067 same time, yielding only one value at a time in the given order.
3066 same time, yielding only one value at a time in the given order.
3068 """
3067 """
3069 if self._ascending is None:
3068 if self._ascending is None:
3070 if self._genlist:
3069 if self._genlist:
3071 return iter(self._genlist)
3070 return iter(self._genlist)
3072 def arbitraryordergen():
3071 def arbitraryordergen():
3073 for r in self._r1:
3072 for r in self._r1:
3074 yield r
3073 yield r
3075 inr1 = self._r1.__contains__
3074 inr1 = self._r1.__contains__
3076 for r in self._r2:
3075 for r in self._r2:
3077 if not inr1(r):
3076 if not inr1(r):
3078 yield r
3077 yield r
3079 return arbitraryordergen()
3078 return arbitraryordergen()
3080 # try to use our own fast iterator if it exists
3079 # try to use our own fast iterator if it exists
3081 self._trysetasclist()
3080 self._trysetasclist()
3082 if self._ascending:
3081 if self._ascending:
3083 attr = 'fastasc'
3082 attr = 'fastasc'
3084 else:
3083 else:
3085 attr = 'fastdesc'
3084 attr = 'fastdesc'
3086 it = getattr(self, attr)
3085 it = getattr(self, attr)
3087 if it is not None:
3086 if it is not None:
3088 return it()
3087 return it()
3089 # maybe half of the component supports fast
3088 # maybe half of the component supports fast
3090 # get iterator for _r1
3089 # get iterator for _r1
3091 iter1 = getattr(self._r1, attr)
3090 iter1 = getattr(self._r1, attr)
3092 if iter1 is None:
3091 if iter1 is None:
3093 # let's avoid side effect (not sure it matters)
3092 # let's avoid side effect (not sure it matters)
3094 iter1 = iter(sorted(self._r1, reverse=not self._ascending))
3093 iter1 = iter(sorted(self._r1, reverse=not self._ascending))
3095 else:
3094 else:
3096 iter1 = iter1()
3095 iter1 = iter1()
3097 # get iterator for _r2
3096 # get iterator for _r2
3098 iter2 = getattr(self._r2, attr)
3097 iter2 = getattr(self._r2, attr)
3099 if iter2 is None:
3098 if iter2 is None:
3100 # let's avoid side effect (not sure it matters)
3099 # let's avoid side effect (not sure it matters)
3101 iter2 = iter(sorted(self._r2, reverse=not self._ascending))
3100 iter2 = iter(sorted(self._r2, reverse=not self._ascending))
3102 else:
3101 else:
3103 iter2 = iter2()
3102 iter2 = iter2()
3104 return _iterordered(self._ascending, iter1, iter2)
3103 return _iterordered(self._ascending, iter1, iter2)
3105
3104
3106 def _trysetasclist(self):
3105 def _trysetasclist(self):
3107 """populate the _asclist attribute if possible and necessary"""
3106 """populate the _asclist attribute if possible and necessary"""
3108 if self._genlist is not None and self._asclist is None:
3107 if self._genlist is not None and self._asclist is None:
3109 self._asclist = sorted(self._genlist)
3108 self._asclist = sorted(self._genlist)
3110
3109
3111 @property
3110 @property
3112 def fastasc(self):
3111 def fastasc(self):
3113 self._trysetasclist()
3112 self._trysetasclist()
3114 if self._asclist is not None:
3113 if self._asclist is not None:
3115 return self._asclist.__iter__
3114 return self._asclist.__iter__
3116 iter1 = self._r1.fastasc
3115 iter1 = self._r1.fastasc
3117 iter2 = self._r2.fastasc
3116 iter2 = self._r2.fastasc
3118 if None in (iter1, iter2):
3117 if None in (iter1, iter2):
3119 return None
3118 return None
3120 return lambda: _iterordered(True, iter1(), iter2())
3119 return lambda: _iterordered(True, iter1(), iter2())
3121
3120
3122 @property
3121 @property
3123 def fastdesc(self):
3122 def fastdesc(self):
3124 self._trysetasclist()
3123 self._trysetasclist()
3125 if self._asclist is not None:
3124 if self._asclist is not None:
3126 return self._asclist.__reversed__
3125 return self._asclist.__reversed__
3127 iter1 = self._r1.fastdesc
3126 iter1 = self._r1.fastdesc
3128 iter2 = self._r2.fastdesc
3127 iter2 = self._r2.fastdesc
3129 if None in (iter1, iter2):
3128 if None in (iter1, iter2):
3130 return None
3129 return None
3131 return lambda: _iterordered(False, iter1(), iter2())
3130 return lambda: _iterordered(False, iter1(), iter2())
3132
3131
3133 def __contains__(self, x):
3132 def __contains__(self, x):
3134 return x in self._r1 or x in self._r2
3133 return x in self._r1 or x in self._r2
3135
3134
3136 def sort(self, reverse=False):
3135 def sort(self, reverse=False):
3137 """Sort the added set
3136 """Sort the added set
3138
3137
3139 For this we use the cached list with all the generated values and if we
3138 For this we use the cached list with all the generated values and if we
3140 know they are ascending or descending we can sort them in a smart way.
3139 know they are ascending or descending we can sort them in a smart way.
3141 """
3140 """
3142 self._ascending = not reverse
3141 self._ascending = not reverse
3143
3142
3144 def isascending(self):
3143 def isascending(self):
3145 return self._ascending is not None and self._ascending
3144 return self._ascending is not None and self._ascending
3146
3145
3147 def isdescending(self):
3146 def isdescending(self):
3148 return self._ascending is not None and not self._ascending
3147 return self._ascending is not None and not self._ascending
3149
3148
3150 def reverse(self):
3149 def reverse(self):
3151 if self._ascending is None:
3150 if self._ascending is None:
3152 self._list.reverse()
3151 self._list.reverse()
3153 else:
3152 else:
3154 self._ascending = not self._ascending
3153 self._ascending = not self._ascending
3155
3154
3156 def first(self):
3155 def first(self):
3157 for x in self:
3156 for x in self:
3158 return x
3157 return x
3159 return None
3158 return None
3160
3159
3161 def last(self):
3160 def last(self):
3162 self.reverse()
3161 self.reverse()
3163 val = self.first()
3162 val = self.first()
3164 self.reverse()
3163 self.reverse()
3165 return val
3164 return val
3166
3165
3167 def __repr__(self):
3166 def __repr__(self):
3168 d = {None: '', False: '-', True: '+'}[self._ascending]
3167 d = {None: '', False: '-', True: '+'}[self._ascending]
3169 return '<%s%s %r, %r>' % (type(self).__name__, d, self._r1, self._r2)
3168 return '<%s%s %r, %r>' % (type(self).__name__, d, self._r1, self._r2)
3170
3169
3171 class generatorset(abstractsmartset):
3170 class generatorset(abstractsmartset):
3172 """Wrap a generator for lazy iteration
3171 """Wrap a generator for lazy iteration
3173
3172
3174 Wrapper structure for generators that provides lazy membership and can
3173 Wrapper structure for generators that provides lazy membership and can
3175 be iterated more than once.
3174 be iterated more than once.
3176 When asked for membership it generates values until either it finds the
3175 When asked for membership it generates values until either it finds the
3177 requested one or has gone through all the elements in the generator
3176 requested one or has gone through all the elements in the generator
3178 """
3177 """
3179 def __init__(self, gen, iterasc=None):
3178 def __init__(self, gen, iterasc=None):
3180 """
3179 """
3181 gen: a generator producing the values for the generatorset.
3180 gen: a generator producing the values for the generatorset.
3182 """
3181 """
3183 self._gen = gen
3182 self._gen = gen
3184 self._asclist = None
3183 self._asclist = None
3185 self._cache = {}
3184 self._cache = {}
3186 self._genlist = []
3185 self._genlist = []
3187 self._finished = False
3186 self._finished = False
3188 self._ascending = True
3187 self._ascending = True
3189 if iterasc is not None:
3188 if iterasc is not None:
3190 if iterasc:
3189 if iterasc:
3191 self.fastasc = self._iterator
3190 self.fastasc = self._iterator
3192 self.__contains__ = self._asccontains
3191 self.__contains__ = self._asccontains
3193 else:
3192 else:
3194 self.fastdesc = self._iterator
3193 self.fastdesc = self._iterator
3195 self.__contains__ = self._desccontains
3194 self.__contains__ = self._desccontains
3196
3195
3197 def __nonzero__(self):
3196 def __nonzero__(self):
3198 # Do not use 'for r in self' because it will enforce the iteration
3197 # Do not use 'for r in self' because it will enforce the iteration
3199 # order (default ascending), possibly unrolling a whole descending
3198 # order (default ascending), possibly unrolling a whole descending
3200 # iterator.
3199 # iterator.
3201 if self._genlist:
3200 if self._genlist:
3202 return True
3201 return True
3203 for r in self._consumegen():
3202 for r in self._consumegen():
3204 return True
3203 return True
3205 return False
3204 return False
3206
3205
3207 def __contains__(self, x):
3206 def __contains__(self, x):
3208 if x in self._cache:
3207 if x in self._cache:
3209 return self._cache[x]
3208 return self._cache[x]
3210
3209
3211 # Use new values only, as existing values would be cached.
3210 # Use new values only, as existing values would be cached.
3212 for l in self._consumegen():
3211 for l in self._consumegen():
3213 if l == x:
3212 if l == x:
3214 return True
3213 return True
3215
3214
3216 self._cache[x] = False
3215 self._cache[x] = False
3217 return False
3216 return False
3218
3217
3219 def _asccontains(self, x):
3218 def _asccontains(self, x):
3220 """version of contains optimised for ascending generator"""
3219 """version of contains optimised for ascending generator"""
3221 if x in self._cache:
3220 if x in self._cache:
3222 return self._cache[x]
3221 return self._cache[x]
3223
3222
3224 # Use new values only, as existing values would be cached.
3223 # Use new values only, as existing values would be cached.
3225 for l in self._consumegen():
3224 for l in self._consumegen():
3226 if l == x:
3225 if l == x:
3227 return True
3226 return True
3228 if l > x:
3227 if l > x:
3229 break
3228 break
3230
3229
3231 self._cache[x] = False
3230 self._cache[x] = False
3232 return False
3231 return False
3233
3232
3234 def _desccontains(self, x):
3233 def _desccontains(self, x):
3235 """version of contains optimised for descending generator"""
3234 """version of contains optimised for descending generator"""
3236 if x in self._cache:
3235 if x in self._cache:
3237 return self._cache[x]
3236 return self._cache[x]
3238
3237
3239 # Use new values only, as existing values would be cached.
3238 # Use new values only, as existing values would be cached.
3240 for l in self._consumegen():
3239 for l in self._consumegen():
3241 if l == x:
3240 if l == x:
3242 return True
3241 return True
3243 if l < x:
3242 if l < x:
3244 break
3243 break
3245
3244
3246 self._cache[x] = False
3245 self._cache[x] = False
3247 return False
3246 return False
3248
3247
3249 def __iter__(self):
3248 def __iter__(self):
3250 if self._ascending:
3249 if self._ascending:
3251 it = self.fastasc
3250 it = self.fastasc
3252 else:
3251 else:
3253 it = self.fastdesc
3252 it = self.fastdesc
3254 if it is not None:
3253 if it is not None:
3255 return it()
3254 return it()
3256 # we need to consume the iterator
3255 # we need to consume the iterator
3257 for x in self._consumegen():
3256 for x in self._consumegen():
3258 pass
3257 pass
3259 # recall the same code
3258 # recall the same code
3260 return iter(self)
3259 return iter(self)
3261
3260
3262 def _iterator(self):
3261 def _iterator(self):
3263 if self._finished:
3262 if self._finished:
3264 return iter(self._genlist)
3263 return iter(self._genlist)
3265
3264
3266 # We have to use this complex iteration strategy to allow multiple
3265 # We have to use this complex iteration strategy to allow multiple
3267 # iterations at the same time. We need to be able to catch revision
3266 # iterations at the same time. We need to be able to catch revision
3268 # removed from _consumegen and added to genlist in another instance.
3267 # removed from _consumegen and added to genlist in another instance.
3269 #
3268 #
3270 # Getting rid of it would provide an about 15% speed up on this
3269 # Getting rid of it would provide an about 15% speed up on this
3271 # iteration.
3270 # iteration.
3272 genlist = self._genlist
3271 genlist = self._genlist
3273 nextrev = self._consumegen().next
3272 nextrev = self._consumegen().next
3274 _len = len # cache global lookup
3273 _len = len # cache global lookup
3275 def gen():
3274 def gen():
3276 i = 0
3275 i = 0
3277 while True:
3276 while True:
3278 if i < _len(genlist):
3277 if i < _len(genlist):
3279 yield genlist[i]
3278 yield genlist[i]
3280 else:
3279 else:
3281 yield nextrev()
3280 yield nextrev()
3282 i += 1
3281 i += 1
3283 return gen()
3282 return gen()
3284
3283
3285 def _consumegen(self):
3284 def _consumegen(self):
3286 cache = self._cache
3285 cache = self._cache
3287 genlist = self._genlist.append
3286 genlist = self._genlist.append
3288 for item in self._gen:
3287 for item in self._gen:
3289 cache[item] = True
3288 cache[item] = True
3290 genlist(item)
3289 genlist(item)
3291 yield item
3290 yield item
3292 if not self._finished:
3291 if not self._finished:
3293 self._finished = True
3292 self._finished = True
3294 asc = self._genlist[:]
3293 asc = self._genlist[:]
3295 asc.sort()
3294 asc.sort()
3296 self._asclist = asc
3295 self._asclist = asc
3297 self.fastasc = asc.__iter__
3296 self.fastasc = asc.__iter__
3298 self.fastdesc = asc.__reversed__
3297 self.fastdesc = asc.__reversed__
3299
3298
3300 def __len__(self):
3299 def __len__(self):
3301 for x in self._consumegen():
3300 for x in self._consumegen():
3302 pass
3301 pass
3303 return len(self._genlist)
3302 return len(self._genlist)
3304
3303
3305 def sort(self, reverse=False):
3304 def sort(self, reverse=False):
3306 self._ascending = not reverse
3305 self._ascending = not reverse
3307
3306
3308 def reverse(self):
3307 def reverse(self):
3309 self._ascending = not self._ascending
3308 self._ascending = not self._ascending
3310
3309
3311 def isascending(self):
3310 def isascending(self):
3312 return self._ascending
3311 return self._ascending
3313
3312
3314 def isdescending(self):
3313 def isdescending(self):
3315 return not self._ascending
3314 return not self._ascending
3316
3315
3317 def first(self):
3316 def first(self):
3318 if self._ascending:
3317 if self._ascending:
3319 it = self.fastasc
3318 it = self.fastasc
3320 else:
3319 else:
3321 it = self.fastdesc
3320 it = self.fastdesc
3322 if it is None:
3321 if it is None:
3323 # we need to consume all and try again
3322 # we need to consume all and try again
3324 for x in self._consumegen():
3323 for x in self._consumegen():
3325 pass
3324 pass
3326 return self.first()
3325 return self.first()
3327 if self:
3326 if self:
3328 return it().next()
3327 return it().next()
3329 return None
3328 return None
3330
3329
3331 def last(self):
3330 def last(self):
3332 if self._ascending:
3331 if self._ascending:
3333 it = self.fastdesc
3332 it = self.fastdesc
3334 else:
3333 else:
3335 it = self.fastasc
3334 it = self.fastasc
3336 if it is None:
3335 if it is None:
3337 # we need to consume all and try again
3336 # we need to consume all and try again
3338 for x in self._consumegen():
3337 for x in self._consumegen():
3339 pass
3338 pass
3340 return self.first()
3339 return self.first()
3341 if self:
3340 if self:
3342 return it().next()
3341 return it().next()
3343 return None
3342 return None
3344
3343
3345 def __repr__(self):
3344 def __repr__(self):
3346 d = {False: '-', True: '+'}[self._ascending]
3345 d = {False: '-', True: '+'}[self._ascending]
3347 return '<%s%s>' % (type(self).__name__, d)
3346 return '<%s%s>' % (type(self).__name__, d)
3348
3347
3349 class spanset(abstractsmartset):
3348 class spanset(abstractsmartset):
3350 """Duck type for baseset class which represents a range of revisions and
3349 """Duck type for baseset class which represents a range of revisions and
3351 can work lazily and without having all the range in memory
3350 can work lazily and without having all the range in memory
3352
3351
3353 Note that spanset(x, y) behave almost like xrange(x, y) except for two
3352 Note that spanset(x, y) behave almost like xrange(x, y) except for two
3354 notable points:
3353 notable points:
3355 - when x < y it will be automatically descending,
3354 - when x < y it will be automatically descending,
3356 - revision filtered with this repoview will be skipped.
3355 - revision filtered with this repoview will be skipped.
3357
3356
3358 """
3357 """
3359 def __init__(self, repo, start=0, end=None):
3358 def __init__(self, repo, start=0, end=None):
3360 """
3359 """
3361 start: first revision included the set
3360 start: first revision included the set
3362 (default to 0)
3361 (default to 0)
3363 end: first revision excluded (last+1)
3362 end: first revision excluded (last+1)
3364 (default to len(repo)
3363 (default to len(repo)
3365
3364
3366 Spanset will be descending if `end` < `start`.
3365 Spanset will be descending if `end` < `start`.
3367 """
3366 """
3368 if end is None:
3367 if end is None:
3369 end = len(repo)
3368 end = len(repo)
3370 self._ascending = start <= end
3369 self._ascending = start <= end
3371 if not self._ascending:
3370 if not self._ascending:
3372 start, end = end + 1, start +1
3371 start, end = end + 1, start +1
3373 self._start = start
3372 self._start = start
3374 self._end = end
3373 self._end = end
3375 self._hiddenrevs = repo.changelog.filteredrevs
3374 self._hiddenrevs = repo.changelog.filteredrevs
3376
3375
3377 def sort(self, reverse=False):
3376 def sort(self, reverse=False):
3378 self._ascending = not reverse
3377 self._ascending = not reverse
3379
3378
3380 def reverse(self):
3379 def reverse(self):
3381 self._ascending = not self._ascending
3380 self._ascending = not self._ascending
3382
3381
3383 def _iterfilter(self, iterrange):
3382 def _iterfilter(self, iterrange):
3384 s = self._hiddenrevs
3383 s = self._hiddenrevs
3385 for r in iterrange:
3384 for r in iterrange:
3386 if r not in s:
3385 if r not in s:
3387 yield r
3386 yield r
3388
3387
3389 def __iter__(self):
3388 def __iter__(self):
3390 if self._ascending:
3389 if self._ascending:
3391 return self.fastasc()
3390 return self.fastasc()
3392 else:
3391 else:
3393 return self.fastdesc()
3392 return self.fastdesc()
3394
3393
3395 def fastasc(self):
3394 def fastasc(self):
3396 iterrange = xrange(self._start, self._end)
3395 iterrange = xrange(self._start, self._end)
3397 if self._hiddenrevs:
3396 if self._hiddenrevs:
3398 return self._iterfilter(iterrange)
3397 return self._iterfilter(iterrange)
3399 return iter(iterrange)
3398 return iter(iterrange)
3400
3399
3401 def fastdesc(self):
3400 def fastdesc(self):
3402 iterrange = xrange(self._end - 1, self._start - 1, -1)
3401 iterrange = xrange(self._end - 1, self._start - 1, -1)
3403 if self._hiddenrevs:
3402 if self._hiddenrevs:
3404 return self._iterfilter(iterrange)
3403 return self._iterfilter(iterrange)
3405 return iter(iterrange)
3404 return iter(iterrange)
3406
3405
3407 def __contains__(self, rev):
3406 def __contains__(self, rev):
3408 hidden = self._hiddenrevs
3407 hidden = self._hiddenrevs
3409 return ((self._start <= rev < self._end)
3408 return ((self._start <= rev < self._end)
3410 and not (hidden and rev in hidden))
3409 and not (hidden and rev in hidden))
3411
3410
3412 def __nonzero__(self):
3411 def __nonzero__(self):
3413 for r in self:
3412 for r in self:
3414 return True
3413 return True
3415 return False
3414 return False
3416
3415
3417 def __len__(self):
3416 def __len__(self):
3418 if not self._hiddenrevs:
3417 if not self._hiddenrevs:
3419 return abs(self._end - self._start)
3418 return abs(self._end - self._start)
3420 else:
3419 else:
3421 count = 0
3420 count = 0
3422 start = self._start
3421 start = self._start
3423 end = self._end
3422 end = self._end
3424 for rev in self._hiddenrevs:
3423 for rev in self._hiddenrevs:
3425 if (end < rev <= start) or (start <= rev < end):
3424 if (end < rev <= start) or (start <= rev < end):
3426 count += 1
3425 count += 1
3427 return abs(self._end - self._start) - count
3426 return abs(self._end - self._start) - count
3428
3427
3429 def isascending(self):
3428 def isascending(self):
3430 return self._ascending
3429 return self._ascending
3431
3430
3432 def isdescending(self):
3431 def isdescending(self):
3433 return not self._ascending
3432 return not self._ascending
3434
3433
3435 def first(self):
3434 def first(self):
3436 if self._ascending:
3435 if self._ascending:
3437 it = self.fastasc
3436 it = self.fastasc
3438 else:
3437 else:
3439 it = self.fastdesc
3438 it = self.fastdesc
3440 for x in it():
3439 for x in it():
3441 return x
3440 return x
3442 return None
3441 return None
3443
3442
3444 def last(self):
3443 def last(self):
3445 if self._ascending:
3444 if self._ascending:
3446 it = self.fastdesc
3445 it = self.fastdesc
3447 else:
3446 else:
3448 it = self.fastasc
3447 it = self.fastasc
3449 for x in it():
3448 for x in it():
3450 return x
3449 return x
3451 return None
3450 return None
3452
3451
3453 def __repr__(self):
3452 def __repr__(self):
3454 d = {False: '-', True: '+'}[self._ascending]
3453 d = {False: '-', True: '+'}[self._ascending]
3455 return '<%s%s %d:%d>' % (type(self).__name__, d,
3454 return '<%s%s %d:%d>' % (type(self).__name__, d,
3456 self._start, self._end - 1)
3455 self._start, self._end - 1)
3457
3456
3458 class fullreposet(spanset):
3457 class fullreposet(spanset):
3459 """a set containing all revisions in the repo
3458 """a set containing all revisions in the repo
3460
3459
3461 This class exists to host special optimization and magic to handle virtual
3460 This class exists to host special optimization and magic to handle virtual
3462 revisions such as "null".
3461 revisions such as "null".
3463 """
3462 """
3464
3463
3465 def __init__(self, repo):
3464 def __init__(self, repo):
3466 super(fullreposet, self).__init__(repo)
3465 super(fullreposet, self).__init__(repo)
3467
3466
3468 def __contains__(self, rev):
3467 def __contains__(self, rev):
3469 # assumes the given rev is valid
3468 # assumes the given rev is valid
3470 hidden = self._hiddenrevs
3469 hidden = self._hiddenrevs
3471 return not (hidden and rev in hidden)
3470 return not (hidden and rev in hidden)
3472
3471
3473 def __and__(self, other):
3472 def __and__(self, other):
3474 """As self contains the whole repo, all of the other set should also be
3473 """As self contains the whole repo, all of the other set should also be
3475 in self. Therefore `self & other = other`.
3474 in self. Therefore `self & other = other`.
3476
3475
3477 This boldly assumes the other contains valid revs only.
3476 This boldly assumes the other contains valid revs only.
3478 """
3477 """
3479 # other not a smartset, make is so
3478 # other not a smartset, make is so
3480 if not util.safehasattr(other, 'isascending'):
3479 if not util.safehasattr(other, 'isascending'):
3481 # filter out hidden revision
3480 # filter out hidden revision
3482 # (this boldly assumes all smartset are pure)
3481 # (this boldly assumes all smartset are pure)
3483 #
3482 #
3484 # `other` was used with "&", let's assume this is a set like
3483 # `other` was used with "&", let's assume this is a set like
3485 # object.
3484 # object.
3486 other = baseset(other - self._hiddenrevs)
3485 other = baseset(other - self._hiddenrevs)
3487
3486
3488 other.sort(reverse=self.isdescending())
3487 other.sort(reverse=self.isdescending())
3489 return other
3488 return other
3490
3489
3491 def prettyformatset(revs):
3490 def prettyformatset(revs):
3492 lines = []
3491 lines = []
3493 rs = repr(revs)
3492 rs = repr(revs)
3494 p = 0
3493 p = 0
3495 while p < len(rs):
3494 while p < len(rs):
3496 q = rs.find('<', p + 1)
3495 q = rs.find('<', p + 1)
3497 if q < 0:
3496 if q < 0:
3498 q = len(rs)
3497 q = len(rs)
3499 l = rs.count('<', 0, p) - rs.count('>', 0, p)
3498 l = rs.count('<', 0, p) - rs.count('>', 0, p)
3500 assert l >= 0
3499 assert l >= 0
3501 lines.append((l, rs[p:q].rstrip()))
3500 lines.append((l, rs[p:q].rstrip()))
3502 p = q
3501 p = q
3503 return '\n'.join(' ' * l + s for l, s in lines)
3502 return '\n'.join(' ' * l + s for l, s in lines)
3504
3503
3505 # tell hggettext to extract docstrings from these functions:
3504 # tell hggettext to extract docstrings from these functions:
3506 i18nfunctions = symbols.values()
3505 i18nfunctions = symbols.values()
General Comments 0
You need to be logged in to leave comments. Login now