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