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