author | Matt Mackall <mpm@selenic.com> |
Mon, 28 Jun 2010 11:07:27 -0500 | |
branch | stable |
changeset 11456 | 88abbb046e66 |
parent 11446 | 0f623839a5cc |
child 11467 | 6b836d5c8c9e |
permissions | -rw-r--r-- |
11275 | 1 |
# revset.py - revision set queries for mercurial |
2 |
# |
|
3 |
# Copyright 2010 Matt Mackall <mpm@selenic.com> |
|
4 |
# |
|
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. |
|
7 |
||
8 |
import re |
|
11301
3d0591a66118
move discovery methods from localrepo into new discovery module
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
11293
diff
changeset
|
9 |
import parser, util, error, discovery |
11275 | 10 |
import match as _match |
11383
de544774ebea
revset: all your error messages are belong to _
Martin Geisler <mg@lazybytes.net>
parents:
11349
diff
changeset
|
11 |
from i18n import _ |
11275 | 12 |
|
13 |
elements = { |
|
14 |
"(": (20, ("group", 1, ")"), ("func", 1, ")")), |
|
15 |
"-": (19, ("negate", 19), ("minus", 19)), |
|
11278
7df88cdf47fd
revset: add support for prefix and suffix versions of : and ::
Matt Mackall <mpm@selenic.com>
parents:
11275
diff
changeset
|
16 |
"::": (17, ("dagrangepre", 17), ("dagrange", 17), |
7df88cdf47fd
revset: add support for prefix and suffix versions of : and ::
Matt Mackall <mpm@selenic.com>
parents:
11275
diff
changeset
|
17 |
("dagrangepost", 17)), |
7df88cdf47fd
revset: add support for prefix and suffix versions of : and ::
Matt Mackall <mpm@selenic.com>
parents:
11275
diff
changeset
|
18 |
"..": (17, ("dagrangepre", 17), ("dagrange", 17), |
7df88cdf47fd
revset: add support for prefix and suffix versions of : and ::
Matt Mackall <mpm@selenic.com>
parents:
11275
diff
changeset
|
19 |
("dagrangepost", 17)), |
7df88cdf47fd
revset: add support for prefix and suffix versions of : and ::
Matt Mackall <mpm@selenic.com>
parents:
11275
diff
changeset
|
20 |
":": (15, ("rangepre", 15), ("range", 15), ("rangepost", 15)), |
11275 | 21 |
"not": (10, ("not", 10)), |
22 |
"!": (10, ("not", 10)), |
|
23 |
"and": (5, None, ("and", 5)), |
|
24 |
"&": (5, None, ("and", 5)), |
|
25 |
"or": (4, None, ("or", 4)), |
|
26 |
"|": (4, None, ("or", 4)), |
|
27 |
"+": (4, None, ("or", 4)), |
|
28 |
",": (2, None, ("list", 2)), |
|
29 |
")": (0, None, None), |
|
30 |
"symbol": (0, ("symbol",), None), |
|
31 |
"string": (0, ("string",), None), |
|
32 |
"end": (0, None, None), |
|
33 |
} |
|
34 |
||
35 |
keywords = set(['and', 'or', 'not']) |
|
36 |
||
37 |
def tokenize(program): |
|
38 |
pos, l = 0, len(program) |
|
39 |
while pos < l: |
|
40 |
c = program[pos] |
|
41 |
if c.isspace(): # skip inter-token whitespace |
|
42 |
pass |
|
11278
7df88cdf47fd
revset: add support for prefix and suffix versions of : and ::
Matt Mackall <mpm@selenic.com>
parents:
11275
diff
changeset
|
43 |
elif c == ':' and program[pos:pos + 2] == '::': # look ahead carefully |
11289
4215ce511134
revset: raise ParseError exceptions
Matt Mackall <mpm@selenic.com>
parents:
11284
diff
changeset
|
44 |
yield ('::', None, pos) |
11278
7df88cdf47fd
revset: add support for prefix and suffix versions of : and ::
Matt Mackall <mpm@selenic.com>
parents:
11275
diff
changeset
|
45 |
pos += 1 # skip ahead |
11275 | 46 |
elif c == '.' and program[pos:pos + 2] == '..': # look ahead carefully |
11289
4215ce511134
revset: raise ParseError exceptions
Matt Mackall <mpm@selenic.com>
parents:
11284
diff
changeset
|
47 |
yield ('..', None, pos) |
11275 | 48 |
pos += 1 # skip ahead |
11278
7df88cdf47fd
revset: add support for prefix and suffix versions of : and ::
Matt Mackall <mpm@selenic.com>
parents:
11275
diff
changeset
|
49 |
elif c in "():,-|&+!": # handle simple operators |
11289
4215ce511134
revset: raise ParseError exceptions
Matt Mackall <mpm@selenic.com>
parents:
11284
diff
changeset
|
50 |
yield (c, None, pos) |
11275 | 51 |
elif c in '"\'': # handle quoted strings |
52 |
pos += 1 |
|
53 |
s = pos |
|
54 |
while pos < l: # find closing quote |
|
55 |
d = program[pos] |
|
56 |
if d == '\\': # skip over escaped characters |
|
57 |
pos += 2 |
|
58 |
continue |
|
59 |
if d == c: |
|
11289
4215ce511134
revset: raise ParseError exceptions
Matt Mackall <mpm@selenic.com>
parents:
11284
diff
changeset
|
60 |
yield ('string', program[s:pos].decode('string-escape'), s) |
11275 | 61 |
break |
62 |
pos += 1 |
|
63 |
else: |
|
11383
de544774ebea
revset: all your error messages are belong to _
Martin Geisler <mg@lazybytes.net>
parents:
11349
diff
changeset
|
64 |
raise error.ParseError(_("unterminated string"), s) |
11404
37cbedbeae96
revset: allow extended characters in symbols
Matt Mackall <mpm@selenic.com>
parents:
11385
diff
changeset
|
65 |
elif c.isalnum() or c in '._' or ord(c) > 127: # gather up a symbol/keyword |
11275 | 66 |
s = pos |
67 |
pos += 1 |
|
68 |
while pos < l: # find end of symbol |
|
69 |
d = program[pos] |
|
11404
37cbedbeae96
revset: allow extended characters in symbols
Matt Mackall <mpm@selenic.com>
parents:
11385
diff
changeset
|
70 |
if not (d.isalnum() or d in "._" or ord(d) > 127): |
11275 | 71 |
break |
72 |
if d == '.' and program[pos - 1] == '.': # special case for .. |
|
73 |
pos -= 1 |
|
74 |
break |
|
75 |
pos += 1 |
|
76 |
sym = program[s:pos] |
|
77 |
if sym in keywords: # operator keywords |
|
11289
4215ce511134
revset: raise ParseError exceptions
Matt Mackall <mpm@selenic.com>
parents:
11284
diff
changeset
|
78 |
yield (sym, None, s) |
11275 | 79 |
else: |
11289
4215ce511134
revset: raise ParseError exceptions
Matt Mackall <mpm@selenic.com>
parents:
11284
diff
changeset
|
80 |
yield ('symbol', sym, s) |
11275 | 81 |
pos -= 1 |
82 |
else: |
|
11383
de544774ebea
revset: all your error messages are belong to _
Martin Geisler <mg@lazybytes.net>
parents:
11349
diff
changeset
|
83 |
raise error.ParseError(_("syntax error"), pos) |
11275 | 84 |
pos += 1 |
11289
4215ce511134
revset: raise ParseError exceptions
Matt Mackall <mpm@selenic.com>
parents:
11284
diff
changeset
|
85 |
yield ('end', None, pos) |
11275 | 86 |
|
87 |
# helpers |
|
88 |
||
89 |
def getstring(x, err): |
|
11406
42408cd43f55
revset: fix up contains/getstring when no args passed
Matt Mackall <mpm@selenic.com>
parents:
11404
diff
changeset
|
90 |
if x and (x[0] == 'string' or x[0] == 'symbol'): |
11275 | 91 |
return x[1] |
11289
4215ce511134
revset: raise ParseError exceptions
Matt Mackall <mpm@selenic.com>
parents:
11284
diff
changeset
|
92 |
raise error.ParseError(err) |
11275 | 93 |
|
94 |
def getlist(x): |
|
95 |
if not x: |
|
96 |
return [] |
|
97 |
if x[0] == 'list': |
|
98 |
return getlist(x[1]) + [x[2]] |
|
99 |
return [x] |
|
100 |
||
11339
744d5b73f776
revset: improve filter argument handling
Matt Mackall <mpm@selenic.com>
parents:
11304
diff
changeset
|
101 |
def getargs(x, min, max, err): |
11275 | 102 |
l = getlist(x) |
11339
744d5b73f776
revset: improve filter argument handling
Matt Mackall <mpm@selenic.com>
parents:
11304
diff
changeset
|
103 |
if len(l) < min or len(l) > max: |
11289
4215ce511134
revset: raise ParseError exceptions
Matt Mackall <mpm@selenic.com>
parents:
11284
diff
changeset
|
104 |
raise error.ParseError(err) |
11275 | 105 |
return l |
106 |
||
107 |
def getset(repo, subset, x): |
|
108 |
if not x: |
|
11383
de544774ebea
revset: all your error messages are belong to _
Martin Geisler <mg@lazybytes.net>
parents:
11349
diff
changeset
|
109 |
raise error.ParseError(_("missing argument")) |
11275 | 110 |
return methods[x[0]](repo, subset, *x[1:]) |
111 |
||
112 |
# operator methods |
|
113 |
||
114 |
def negate(repo, subset, x): |
|
115 |
return getset(repo, subset, |
|
11383
de544774ebea
revset: all your error messages are belong to _
Martin Geisler <mg@lazybytes.net>
parents:
11349
diff
changeset
|
116 |
('string', '-' + getstring(x, _("can't negate that")))) |
11275 | 117 |
|
118 |
def stringset(repo, subset, x): |
|
119 |
x = repo[x].rev() |
|
11282 | 120 |
if x == -1 and len(subset) == len(repo): |
121 |
return [-1] |
|
11275 | 122 |
if x in subset: |
123 |
return [x] |
|
124 |
return [] |
|
125 |
||
126 |
def symbolset(repo, subset, x): |
|
127 |
if x in symbols: |
|
11383
de544774ebea
revset: all your error messages are belong to _
Martin Geisler <mg@lazybytes.net>
parents:
11349
diff
changeset
|
128 |
raise error.ParseError(_("can't use %s here") % x) |
11275 | 129 |
return stringset(repo, subset, x) |
130 |
||
131 |
def rangeset(repo, subset, x, y): |
|
11456
88abbb046e66
revset: deal with empty sets in range endpoints
Matt Mackall <mpm@selenic.com>
parents:
11446
diff
changeset
|
132 |
m = getset(repo, subset, x) |
88abbb046e66
revset: deal with empty sets in range endpoints
Matt Mackall <mpm@selenic.com>
parents:
11446
diff
changeset
|
133 |
if not m: |
88abbb046e66
revset: deal with empty sets in range endpoints
Matt Mackall <mpm@selenic.com>
parents:
11446
diff
changeset
|
134 |
m = getset(repo, range(len(repo)), x) |
88abbb046e66
revset: deal with empty sets in range endpoints
Matt Mackall <mpm@selenic.com>
parents:
11446
diff
changeset
|
135 |
|
88abbb046e66
revset: deal with empty sets in range endpoints
Matt Mackall <mpm@selenic.com>
parents:
11446
diff
changeset
|
136 |
n = getset(repo, subset, y) |
88abbb046e66
revset: deal with empty sets in range endpoints
Matt Mackall <mpm@selenic.com>
parents:
11446
diff
changeset
|
137 |
if not n: |
88abbb046e66
revset: deal with empty sets in range endpoints
Matt Mackall <mpm@selenic.com>
parents:
11446
diff
changeset
|
138 |
n = getset(repo, range(len(repo)), y) |
88abbb046e66
revset: deal with empty sets in range endpoints
Matt Mackall <mpm@selenic.com>
parents:
11446
diff
changeset
|
139 |
|
88abbb046e66
revset: deal with empty sets in range endpoints
Matt Mackall <mpm@selenic.com>
parents:
11446
diff
changeset
|
140 |
if not m or not n: |
88abbb046e66
revset: deal with empty sets in range endpoints
Matt Mackall <mpm@selenic.com>
parents:
11446
diff
changeset
|
141 |
return [] |
88abbb046e66
revset: deal with empty sets in range endpoints
Matt Mackall <mpm@selenic.com>
parents:
11446
diff
changeset
|
142 |
m, n = m[0], n[-1] |
88abbb046e66
revset: deal with empty sets in range endpoints
Matt Mackall <mpm@selenic.com>
parents:
11446
diff
changeset
|
143 |
|
11275 | 144 |
if m < n: |
11456
88abbb046e66
revset: deal with empty sets in range endpoints
Matt Mackall <mpm@selenic.com>
parents:
11446
diff
changeset
|
145 |
r = range(m, n + 1) |
88abbb046e66
revset: deal with empty sets in range endpoints
Matt Mackall <mpm@selenic.com>
parents:
11446
diff
changeset
|
146 |
else: |
88abbb046e66
revset: deal with empty sets in range endpoints
Matt Mackall <mpm@selenic.com>
parents:
11446
diff
changeset
|
147 |
r = range(m, n - 1, -1) |
88abbb046e66
revset: deal with empty sets in range endpoints
Matt Mackall <mpm@selenic.com>
parents:
11446
diff
changeset
|
148 |
s = set(subset) |
88abbb046e66
revset: deal with empty sets in range endpoints
Matt Mackall <mpm@selenic.com>
parents:
11446
diff
changeset
|
149 |
return [x for x in r if x in s] |
11275 | 150 |
|
151 |
def andset(repo, subset, x, y): |
|
152 |
return getset(repo, getset(repo, subset, x), y) |
|
153 |
||
154 |
def orset(repo, subset, x, y): |
|
155 |
s = set(getset(repo, subset, x)) |
|
156 |
s |= set(getset(repo, [r for r in subset if r not in s], y)) |
|
157 |
return [r for r in subset if r in s] |
|
158 |
||
159 |
def notset(repo, subset, x): |
|
160 |
s = set(getset(repo, subset, x)) |
|
161 |
return [r for r in subset if r not in s] |
|
162 |
||
163 |
def listset(repo, subset, a, b): |
|
11383
de544774ebea
revset: all your error messages are belong to _
Martin Geisler <mg@lazybytes.net>
parents:
11349
diff
changeset
|
164 |
raise error.ParseError(_("can't use a list in this context")) |
11275 | 165 |
|
166 |
def func(repo, subset, a, b): |
|
167 |
if a[0] == 'symbol' and a[1] in symbols: |
|
168 |
return symbols[a[1]](repo, subset, b) |
|
11383
de544774ebea
revset: all your error messages are belong to _
Martin Geisler <mg@lazybytes.net>
parents:
11349
diff
changeset
|
169 |
raise error.ParseError(_("not a function: %s") % a[1]) |
11275 | 170 |
|
171 |
# functions |
|
172 |
||
173 |
def p1(repo, subset, x): |
|
174 |
ps = set() |
|
175 |
cl = repo.changelog |
|
176 |
for r in getset(repo, subset, x): |
|
177 |
ps.add(cl.parentrevs(r)[0]) |
|
178 |
return [r for r in subset if r in ps] |
|
179 |
||
180 |
def p2(repo, subset, x): |
|
181 |
ps = set() |
|
182 |
cl = repo.changelog |
|
183 |
for r in getset(repo, subset, x): |
|
184 |
ps.add(cl.parentrevs(r)[1]) |
|
185 |
return [r for r in subset if r in ps] |
|
186 |
||
187 |
def parents(repo, subset, x): |
|
188 |
ps = set() |
|
189 |
cl = repo.changelog |
|
190 |
for r in getset(repo, subset, x): |
|
191 |
ps.update(cl.parentrevs(r)) |
|
192 |
return [r for r in subset if r in ps] |
|
193 |
||
194 |
def maxrev(repo, subset, x): |
|
195 |
s = getset(repo, subset, x) |
|
196 |
if s: |
|
197 |
m = max(s) |
|
198 |
if m in subset: |
|
199 |
return [m] |
|
200 |
return [] |
|
201 |
||
202 |
def limit(repo, subset, x): |
|
11383
de544774ebea
revset: all your error messages are belong to _
Martin Geisler <mg@lazybytes.net>
parents:
11349
diff
changeset
|
203 |
l = getargs(x, 2, 2, _("limit wants two arguments")) |
11275 | 204 |
try: |
11383
de544774ebea
revset: all your error messages are belong to _
Martin Geisler <mg@lazybytes.net>
parents:
11349
diff
changeset
|
205 |
lim = int(getstring(l[1], _("limit wants a number"))) |
11275 | 206 |
except ValueError: |
11383
de544774ebea
revset: all your error messages are belong to _
Martin Geisler <mg@lazybytes.net>
parents:
11349
diff
changeset
|
207 |
raise error.ParseError(_("limit expects a number")) |
11275 | 208 |
return getset(repo, subset, l[0])[:lim] |
209 |
||
210 |
def children(repo, subset, x): |
|
211 |
cs = set() |
|
212 |
cl = repo.changelog |
|
213 |
s = set(getset(repo, subset, x)) |
|
214 |
for r in xrange(0, len(repo)): |
|
215 |
for p in cl.parentrevs(r): |
|
216 |
if p in s: |
|
217 |
cs.add(r) |
|
218 |
return [r for r in subset if r in cs] |
|
219 |
||
220 |
def branch(repo, subset, x): |
|
221 |
s = getset(repo, range(len(repo)), x) |
|
222 |
b = set() |
|
223 |
for r in s: |
|
224 |
b.add(repo[r].branch()) |
|
225 |
s = set(s) |
|
226 |
return [r for r in subset if r in s or repo[r].branch() in b] |
|
227 |
||
228 |
def ancestor(repo, subset, x): |
|
11383
de544774ebea
revset: all your error messages are belong to _
Martin Geisler <mg@lazybytes.net>
parents:
11349
diff
changeset
|
229 |
l = getargs(x, 2, 2, _("ancestor wants two arguments")) |
11275 | 230 |
a = getset(repo, subset, l[0]) |
231 |
b = getset(repo, subset, l[1]) |
|
232 |
if len(a) > 1 or len(b) > 1: |
|
11383
de544774ebea
revset: all your error messages are belong to _
Martin Geisler <mg@lazybytes.net>
parents:
11349
diff
changeset
|
233 |
raise error.ParseError(_("ancestor arguments must be single revisions")) |
11275 | 234 |
return [repo[a[0]].ancestor(repo[b[0]]).rev()] |
235 |
||
236 |
def ancestors(repo, subset, x): |
|
237 |
args = getset(repo, range(len(repo)), x) |
|
11456
88abbb046e66
revset: deal with empty sets in range endpoints
Matt Mackall <mpm@selenic.com>
parents:
11446
diff
changeset
|
238 |
if not args: |
88abbb046e66
revset: deal with empty sets in range endpoints
Matt Mackall <mpm@selenic.com>
parents:
11446
diff
changeset
|
239 |
return [] |
11275 | 240 |
s = set(repo.changelog.ancestors(*args)) | set(args) |
241 |
return [r for r in subset if r in s] |
|
242 |
||
243 |
def descendants(repo, subset, x): |
|
244 |
args = getset(repo, range(len(repo)), x) |
|
11456
88abbb046e66
revset: deal with empty sets in range endpoints
Matt Mackall <mpm@selenic.com>
parents:
11446
diff
changeset
|
245 |
if not args: |
88abbb046e66
revset: deal with empty sets in range endpoints
Matt Mackall <mpm@selenic.com>
parents:
11446
diff
changeset
|
246 |
return [] |
11275 | 247 |
s = set(repo.changelog.descendants(*args)) | set(args) |
248 |
return [r for r in subset if r in s] |
|
249 |
||
250 |
def follow(repo, subset, x): |
|
11383
de544774ebea
revset: all your error messages are belong to _
Martin Geisler <mg@lazybytes.net>
parents:
11349
diff
changeset
|
251 |
getargs(x, 0, 0, _("follow takes no arguments")) |
11275 | 252 |
p = repo['.'].rev() |
253 |
s = set(repo.changelog.ancestors(p)) | set([p]) |
|
254 |
return [r for r in subset if r in s] |
|
255 |
||
256 |
def date(repo, subset, x): |
|
11383
de544774ebea
revset: all your error messages are belong to _
Martin Geisler <mg@lazybytes.net>
parents:
11349
diff
changeset
|
257 |
ds = getstring(x, _("date wants a string")) |
11275 | 258 |
dm = util.matchdate(ds) |
259 |
return [r for r in subset if dm(repo[r].date()[0])] |
|
260 |
||
261 |
def keyword(repo, subset, x): |
|
11383
de544774ebea
revset: all your error messages are belong to _
Martin Geisler <mg@lazybytes.net>
parents:
11349
diff
changeset
|
262 |
kw = getstring(x, _("keyword wants a string")).lower() |
11275 | 263 |
l = [] |
264 |
for r in subset: |
|
265 |
c = repo[r] |
|
266 |
t = " ".join(c.files() + [c.user(), c.description()]) |
|
267 |
if kw in t.lower(): |
|
268 |
l.append(r) |
|
269 |
return l |
|
270 |
||
271 |
def grep(repo, subset, x): |
|
11383
de544774ebea
revset: all your error messages are belong to _
Martin Geisler <mg@lazybytes.net>
parents:
11349
diff
changeset
|
272 |
gr = re.compile(getstring(x, _("grep wants a string"))) |
11275 | 273 |
l = [] |
274 |
for r in subset: |
|
275 |
c = repo[r] |
|
276 |
for e in c.files() + [c.user(), c.description()]: |
|
277 |
if gr.search(e): |
|
278 |
l.append(r) |
|
279 |
continue |
|
280 |
return l |
|
281 |
||
282 |
def author(repo, subset, x): |
|
11383
de544774ebea
revset: all your error messages are belong to _
Martin Geisler <mg@lazybytes.net>
parents:
11349
diff
changeset
|
283 |
n = getstring(x, _("author wants a string")).lower() |
11275 | 284 |
return [r for r in subset if n in repo[r].user().lower()] |
285 |
||
286 |
def hasfile(repo, subset, x): |
|
11383
de544774ebea
revset: all your error messages are belong to _
Martin Geisler <mg@lazybytes.net>
parents:
11349
diff
changeset
|
287 |
pat = getstring(x, _("file wants a pattern")) |
11275 | 288 |
m = _match.match(repo.root, repo.getcwd(), [pat]) |
289 |
s = [] |
|
290 |
for r in subset: |
|
291 |
for f in repo[r].files(): |
|
292 |
if m(f): |
|
293 |
s.append(r) |
|
294 |
continue |
|
295 |
return s |
|
296 |
||
297 |
def contains(repo, subset, x): |
|
11406
42408cd43f55
revset: fix up contains/getstring when no args passed
Matt Mackall <mpm@selenic.com>
parents:
11404
diff
changeset
|
298 |
pat = getstring(x, _("contains wants a pattern")) |
11275 | 299 |
m = _match.match(repo.root, repo.getcwd(), [pat]) |
300 |
s = [] |
|
301 |
if m.files() == [pat]: |
|
302 |
for r in subset: |
|
303 |
if pat in repo[r]: |
|
304 |
s.append(r) |
|
305 |
continue |
|
306 |
else: |
|
307 |
for r in subset: |
|
308 |
for f in repo[r].manifest(): |
|
309 |
if m(f): |
|
310 |
s.append(r) |
|
311 |
continue |
|
312 |
return s |
|
313 |
||
314 |
def checkstatus(repo, subset, pat, field): |
|
315 |
m = _match.match(repo.root, repo.getcwd(), [pat]) |
|
316 |
s = [] |
|
317 |
fast = (m.files() == [pat]) |
|
318 |
for r in subset: |
|
319 |
c = repo[r] |
|
320 |
if fast: |
|
321 |
if pat not in c.files(): |
|
322 |
continue |
|
323 |
else: |
|
324 |
for f in c.files(): |
|
325 |
if m(f): |
|
326 |
break |
|
327 |
else: |
|
328 |
continue |
|
329 |
files = repo.status(c.p1().node(), c.node())[field] |
|
330 |
if fast: |
|
331 |
if pat in files: |
|
332 |
s.append(r) |
|
333 |
continue |
|
334 |
else: |
|
335 |
for f in files: |
|
336 |
if m(f): |
|
337 |
s.append(r) |
|
338 |
continue |
|
339 |
return s |
|
340 |
||
341 |
def modifies(repo, subset, x): |
|
11383
de544774ebea
revset: all your error messages are belong to _
Martin Geisler <mg@lazybytes.net>
parents:
11349
diff
changeset
|
342 |
pat = getstring(x, _("modifies wants a pattern")) |
11275 | 343 |
return checkstatus(repo, subset, pat, 0) |
344 |
||
345 |
def adds(repo, subset, x): |
|
11383
de544774ebea
revset: all your error messages are belong to _
Martin Geisler <mg@lazybytes.net>
parents:
11349
diff
changeset
|
346 |
pat = getstring(x, _("adds wants a pattern")) |
11275 | 347 |
return checkstatus(repo, subset, pat, 1) |
348 |
||
349 |
def removes(repo, subset, x): |
|
11383
de544774ebea
revset: all your error messages are belong to _
Martin Geisler <mg@lazybytes.net>
parents:
11349
diff
changeset
|
350 |
pat = getstring(x, _("removes wants a pattern")) |
11275 | 351 |
return checkstatus(repo, subset, pat, 2) |
352 |
||
353 |
def merge(repo, subset, x): |
|
11383
de544774ebea
revset: all your error messages are belong to _
Martin Geisler <mg@lazybytes.net>
parents:
11349
diff
changeset
|
354 |
getargs(x, 0, 0, _("merge takes no arguments")) |
11275 | 355 |
cl = repo.changelog |
356 |
return [r for r in subset if cl.parentrevs(r)[1] != -1] |
|
357 |
||
358 |
def closed(repo, subset, x): |
|
11383
de544774ebea
revset: all your error messages are belong to _
Martin Geisler <mg@lazybytes.net>
parents:
11349
diff
changeset
|
359 |
getargs(x, 0, 0, _("closed takes no arguments")) |
11349
cf8a9154a362
revset: fix call to ctx.extra() in closed()
Georg Brandl <georg@python.org>
parents:
11339
diff
changeset
|
360 |
return [r for r in subset if repo[r].extra().get('close')] |
11275 | 361 |
|
362 |
def head(repo, subset, x): |
|
11383
de544774ebea
revset: all your error messages are belong to _
Martin Geisler <mg@lazybytes.net>
parents:
11349
diff
changeset
|
363 |
getargs(x, 0, 0, _("head takes no arguments")) |
11275 | 364 |
hs = set() |
365 |
for b, ls in repo.branchmap().iteritems(): |
|
366 |
hs.update(repo[h].rev() for h in ls) |
|
367 |
return [r for r in subset if r in hs] |
|
368 |
||
369 |
def reverse(repo, subset, x): |
|
370 |
l = getset(repo, subset, x) |
|
371 |
l.reverse() |
|
372 |
return l |
|
373 |
||
374 |
def sort(repo, subset, x): |
|
11383
de544774ebea
revset: all your error messages are belong to _
Martin Geisler <mg@lazybytes.net>
parents:
11349
diff
changeset
|
375 |
l = getargs(x, 1, 2, _("sort wants one or two arguments")) |
11275 | 376 |
keys = "rev" |
377 |
if len(l) == 2: |
|
11383
de544774ebea
revset: all your error messages are belong to _
Martin Geisler <mg@lazybytes.net>
parents:
11349
diff
changeset
|
378 |
keys = getstring(l[1], _("sort spec must be a string")) |
11275 | 379 |
|
380 |
s = l[0] |
|
381 |
keys = keys.split() |
|
382 |
l = [] |
|
383 |
def invert(s): |
|
384 |
return "".join(chr(255 - ord(c)) for c in s) |
|
385 |
for r in getset(repo, subset, s): |
|
386 |
c = repo[r] |
|
387 |
e = [] |
|
388 |
for k in keys: |
|
389 |
if k == 'rev': |
|
390 |
e.append(r) |
|
391 |
elif k == '-rev': |
|
392 |
e.append(-r) |
|
393 |
elif k == 'branch': |
|
394 |
e.append(c.branch()) |
|
395 |
elif k == '-branch': |
|
396 |
e.append(invert(c.branch())) |
|
397 |
elif k == 'desc': |
|
398 |
e.append(c.description()) |
|
399 |
elif k == '-desc': |
|
400 |
e.append(invert(c.description())) |
|
401 |
elif k in 'user author': |
|
402 |
e.append(c.user()) |
|
403 |
elif k in '-user -author': |
|
404 |
e.append(invert(c.user())) |
|
405 |
elif k == 'date': |
|
406 |
e.append(c.date()[0]) |
|
407 |
elif k == '-date': |
|
408 |
e.append(-c.date()[0]) |
|
409 |
else: |
|
11383
de544774ebea
revset: all your error messages are belong to _
Martin Geisler <mg@lazybytes.net>
parents:
11349
diff
changeset
|
410 |
raise error.ParseError(_("unknown sort key %r") % k) |
11275 | 411 |
e.append(r) |
412 |
l.append(e) |
|
413 |
l.sort() |
|
414 |
return [e[-1] for e in l] |
|
415 |
||
416 |
def getall(repo, subset, x): |
|
11383
de544774ebea
revset: all your error messages are belong to _
Martin Geisler <mg@lazybytes.net>
parents:
11349
diff
changeset
|
417 |
getargs(x, 0, 0, _("all takes no arguments")) |
11275 | 418 |
return subset |
419 |
||
420 |
def heads(repo, subset, x): |
|
421 |
s = getset(repo, subset, x) |
|
422 |
ps = set(parents(repo, subset, x)) |
|
423 |
return [r for r in s if r not in ps] |
|
424 |
||
425 |
def roots(repo, subset, x): |
|
426 |
s = getset(repo, subset, x) |
|
427 |
cs = set(children(repo, subset, x)) |
|
428 |
return [r for r in s if r not in cs] |
|
429 |
||
430 |
def outgoing(repo, subset, x): |
|
11293
0e5ce2325795
revset: delay import of hg to avoid start-up import loops
Matt Mackall <mpm@selenic.com>
parents:
11289
diff
changeset
|
431 |
import hg # avoid start-up nasties |
11383
de544774ebea
revset: all your error messages are belong to _
Martin Geisler <mg@lazybytes.net>
parents:
11349
diff
changeset
|
432 |
l = getargs(x, 0, 1, _("outgoing wants a repository path")) |
11339
744d5b73f776
revset: improve filter argument handling
Matt Mackall <mpm@selenic.com>
parents:
11304
diff
changeset
|
433 |
dest = l[1:] or '' |
11275 | 434 |
dest = repo.ui.expandpath(dest or 'default-push', dest or 'default') |
435 |
dest, branches = hg.parseurl(dest) |
|
436 |
other = hg.repository(hg.remoteui(repo, {}), dest) |
|
437 |
repo.ui.pushbuffer() |
|
11301
3d0591a66118
move discovery methods from localrepo into new discovery module
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
11293
diff
changeset
|
438 |
o = discovery.findoutgoing(repo, other) |
11275 | 439 |
repo.ui.popbuffer() |
440 |
cl = repo.changelog |
|
441 |
o = set([cl.rev(r) for r in repo.changelog.nodesbetween(o, None)[0]]) |
|
442 |
return [r for r in subset if r in o] |
|
443 |
||
11280
a5eb0bf7e158
revset: add tagged predicate
Matt Mackall <mpm@selenic.com>
parents:
11279
diff
changeset
|
444 |
def tagged(repo, subset, x): |
11383
de544774ebea
revset: all your error messages are belong to _
Martin Geisler <mg@lazybytes.net>
parents:
11349
diff
changeset
|
445 |
getargs(x, 0, 0, _("tagged takes no arguments")) |
11280
a5eb0bf7e158
revset: add tagged predicate
Matt Mackall <mpm@selenic.com>
parents:
11279
diff
changeset
|
446 |
cl = repo.changelog |
a5eb0bf7e158
revset: add tagged predicate
Matt Mackall <mpm@selenic.com>
parents:
11279
diff
changeset
|
447 |
s = set([cl.rev(n) for t, n in repo.tagslist() if t != 'tip']) |
a5eb0bf7e158
revset: add tagged predicate
Matt Mackall <mpm@selenic.com>
parents:
11279
diff
changeset
|
448 |
return [r for r in subset if r in s] |
a5eb0bf7e158
revset: add tagged predicate
Matt Mackall <mpm@selenic.com>
parents:
11279
diff
changeset
|
449 |
|
11275 | 450 |
symbols = { |
11284
0b5c2e82aeb5
revset: sort the predicate list
Matt Mackall <mpm@selenic.com>
parents:
11283
diff
changeset
|
451 |
"adds": adds, |
0b5c2e82aeb5
revset: sort the predicate list
Matt Mackall <mpm@selenic.com>
parents:
11283
diff
changeset
|
452 |
"all": getall, |
11275 | 453 |
"ancestor": ancestor, |
454 |
"ancestors": ancestors, |
|
11284
0b5c2e82aeb5
revset: sort the predicate list
Matt Mackall <mpm@selenic.com>
parents:
11283
diff
changeset
|
455 |
"author": author, |
11275 | 456 |
"branch": branch, |
11284
0b5c2e82aeb5
revset: sort the predicate list
Matt Mackall <mpm@selenic.com>
parents:
11283
diff
changeset
|
457 |
"children": children, |
0b5c2e82aeb5
revset: sort the predicate list
Matt Mackall <mpm@selenic.com>
parents:
11283
diff
changeset
|
458 |
"closed": closed, |
0b5c2e82aeb5
revset: sort the predicate list
Matt Mackall <mpm@selenic.com>
parents:
11283
diff
changeset
|
459 |
"contains": contains, |
0b5c2e82aeb5
revset: sort the predicate list
Matt Mackall <mpm@selenic.com>
parents:
11283
diff
changeset
|
460 |
"date": date, |
0b5c2e82aeb5
revset: sort the predicate list
Matt Mackall <mpm@selenic.com>
parents:
11283
diff
changeset
|
461 |
"descendants": descendants, |
0b5c2e82aeb5
revset: sort the predicate list
Matt Mackall <mpm@selenic.com>
parents:
11283
diff
changeset
|
462 |
"file": hasfile, |
0b5c2e82aeb5
revset: sort the predicate list
Matt Mackall <mpm@selenic.com>
parents:
11283
diff
changeset
|
463 |
"follow": follow, |
0b5c2e82aeb5
revset: sort the predicate list
Matt Mackall <mpm@selenic.com>
parents:
11283
diff
changeset
|
464 |
"grep": grep, |
0b5c2e82aeb5
revset: sort the predicate list
Matt Mackall <mpm@selenic.com>
parents:
11283
diff
changeset
|
465 |
"head": head, |
0b5c2e82aeb5
revset: sort the predicate list
Matt Mackall <mpm@selenic.com>
parents:
11283
diff
changeset
|
466 |
"heads": heads, |
11275 | 467 |
"keyword": keyword, |
11284
0b5c2e82aeb5
revset: sort the predicate list
Matt Mackall <mpm@selenic.com>
parents:
11283
diff
changeset
|
468 |
"limit": limit, |
0b5c2e82aeb5
revset: sort the predicate list
Matt Mackall <mpm@selenic.com>
parents:
11283
diff
changeset
|
469 |
"max": maxrev, |
0b5c2e82aeb5
revset: sort the predicate list
Matt Mackall <mpm@selenic.com>
parents:
11283
diff
changeset
|
470 |
"merge": merge, |
0b5c2e82aeb5
revset: sort the predicate list
Matt Mackall <mpm@selenic.com>
parents:
11283
diff
changeset
|
471 |
"modifies": modifies, |
0b5c2e82aeb5
revset: sort the predicate list
Matt Mackall <mpm@selenic.com>
parents:
11283
diff
changeset
|
472 |
"outgoing": outgoing, |
11275 | 473 |
"p1": p1, |
474 |
"p2": p2, |
|
475 |
"parents": parents, |
|
11284
0b5c2e82aeb5
revset: sort the predicate list
Matt Mackall <mpm@selenic.com>
parents:
11283
diff
changeset
|
476 |
"removes": removes, |
0b5c2e82aeb5
revset: sort the predicate list
Matt Mackall <mpm@selenic.com>
parents:
11283
diff
changeset
|
477 |
"reverse": reverse, |
11275 | 478 |
"roots": roots, |
11284
0b5c2e82aeb5
revset: sort the predicate list
Matt Mackall <mpm@selenic.com>
parents:
11283
diff
changeset
|
479 |
"sort": sort, |
11280
a5eb0bf7e158
revset: add tagged predicate
Matt Mackall <mpm@selenic.com>
parents:
11279
diff
changeset
|
480 |
"tagged": tagged, |
11284
0b5c2e82aeb5
revset: sort the predicate list
Matt Mackall <mpm@selenic.com>
parents:
11283
diff
changeset
|
481 |
"user": author, |
11275 | 482 |
} |
483 |
||
484 |
methods = { |
|
485 |
"negate": negate, |
|
486 |
"range": rangeset, |
|
487 |
"string": stringset, |
|
488 |
"symbol": symbolset, |
|
489 |
"and": andset, |
|
490 |
"or": orset, |
|
491 |
"not": notset, |
|
492 |
"list": listset, |
|
493 |
"func": func, |
|
494 |
} |
|
495 |
||
11279
62ccf4cd6e7f
revset: optimize the parse tree directly
Matt Mackall <mpm@selenic.com>
parents:
11278
diff
changeset
|
496 |
def optimize(x, small): |
62ccf4cd6e7f
revset: optimize the parse tree directly
Matt Mackall <mpm@selenic.com>
parents:
11278
diff
changeset
|
497 |
if x == None: |
62ccf4cd6e7f
revset: optimize the parse tree directly
Matt Mackall <mpm@selenic.com>
parents:
11278
diff
changeset
|
498 |
return 0, x |
62ccf4cd6e7f
revset: optimize the parse tree directly
Matt Mackall <mpm@selenic.com>
parents:
11278
diff
changeset
|
499 |
|
11275 | 500 |
smallbonus = 1 |
501 |
if small: |
|
502 |
smallbonus = .5 |
|
503 |
||
504 |
op = x[0] |
|
11283
a6356b2695a3
revset: fix - handling in the optimizer
Matt Mackall <mpm@selenic.com>
parents:
11282
diff
changeset
|
505 |
if op == 'minus': |
11279
62ccf4cd6e7f
revset: optimize the parse tree directly
Matt Mackall <mpm@selenic.com>
parents:
11278
diff
changeset
|
506 |
return optimize(('and', x[1], ('not', x[2])), small) |
62ccf4cd6e7f
revset: optimize the parse tree directly
Matt Mackall <mpm@selenic.com>
parents:
11278
diff
changeset
|
507 |
elif op == 'dagrange': |
62ccf4cd6e7f
revset: optimize the parse tree directly
Matt Mackall <mpm@selenic.com>
parents:
11278
diff
changeset
|
508 |
return optimize(('and', ('func', ('symbol', 'descendants'), x[1]), |
62ccf4cd6e7f
revset: optimize the parse tree directly
Matt Mackall <mpm@selenic.com>
parents:
11278
diff
changeset
|
509 |
('func', ('symbol', 'ancestors'), x[2])), small) |
62ccf4cd6e7f
revset: optimize the parse tree directly
Matt Mackall <mpm@selenic.com>
parents:
11278
diff
changeset
|
510 |
elif op == 'dagrangepre': |
62ccf4cd6e7f
revset: optimize the parse tree directly
Matt Mackall <mpm@selenic.com>
parents:
11278
diff
changeset
|
511 |
return optimize(('func', ('symbol', 'ancestors'), x[1]), small) |
62ccf4cd6e7f
revset: optimize the parse tree directly
Matt Mackall <mpm@selenic.com>
parents:
11278
diff
changeset
|
512 |
elif op == 'dagrangepost': |
62ccf4cd6e7f
revset: optimize the parse tree directly
Matt Mackall <mpm@selenic.com>
parents:
11278
diff
changeset
|
513 |
return optimize(('func', ('symbol', 'descendants'), x[1]), small) |
62ccf4cd6e7f
revset: optimize the parse tree directly
Matt Mackall <mpm@selenic.com>
parents:
11278
diff
changeset
|
514 |
elif op == 'rangepre': |
62ccf4cd6e7f
revset: optimize the parse tree directly
Matt Mackall <mpm@selenic.com>
parents:
11278
diff
changeset
|
515 |
return optimize(('range', ('string', '0'), x[1]), small) |
62ccf4cd6e7f
revset: optimize the parse tree directly
Matt Mackall <mpm@selenic.com>
parents:
11278
diff
changeset
|
516 |
elif op == 'rangepost': |
62ccf4cd6e7f
revset: optimize the parse tree directly
Matt Mackall <mpm@selenic.com>
parents:
11278
diff
changeset
|
517 |
return optimize(('range', x[1], ('string', 'tip')), small) |
62ccf4cd6e7f
revset: optimize the parse tree directly
Matt Mackall <mpm@selenic.com>
parents:
11278
diff
changeset
|
518 |
elif op in 'string symbol negate': |
62ccf4cd6e7f
revset: optimize the parse tree directly
Matt Mackall <mpm@selenic.com>
parents:
11278
diff
changeset
|
519 |
return smallbonus, x # single revisions are small |
11275 | 520 |
elif op == 'and' or op == 'dagrange': |
11279
62ccf4cd6e7f
revset: optimize the parse tree directly
Matt Mackall <mpm@selenic.com>
parents:
11278
diff
changeset
|
521 |
wa, ta = optimize(x[1], True) |
62ccf4cd6e7f
revset: optimize the parse tree directly
Matt Mackall <mpm@selenic.com>
parents:
11278
diff
changeset
|
522 |
wb, tb = optimize(x[2], True) |
62ccf4cd6e7f
revset: optimize the parse tree directly
Matt Mackall <mpm@selenic.com>
parents:
11278
diff
changeset
|
523 |
w = min(wa, wb) |
62ccf4cd6e7f
revset: optimize the parse tree directly
Matt Mackall <mpm@selenic.com>
parents:
11278
diff
changeset
|
524 |
if wa > wb: |
62ccf4cd6e7f
revset: optimize the parse tree directly
Matt Mackall <mpm@selenic.com>
parents:
11278
diff
changeset
|
525 |
return w, (op, tb, ta) |
62ccf4cd6e7f
revset: optimize the parse tree directly
Matt Mackall <mpm@selenic.com>
parents:
11278
diff
changeset
|
526 |
return w, (op, ta, tb) |
62ccf4cd6e7f
revset: optimize the parse tree directly
Matt Mackall <mpm@selenic.com>
parents:
11278
diff
changeset
|
527 |
elif op == 'or': |
62ccf4cd6e7f
revset: optimize the parse tree directly
Matt Mackall <mpm@selenic.com>
parents:
11278
diff
changeset
|
528 |
wa, ta = optimize(x[1], False) |
62ccf4cd6e7f
revset: optimize the parse tree directly
Matt Mackall <mpm@selenic.com>
parents:
11278
diff
changeset
|
529 |
wb, tb = optimize(x[2], False) |
62ccf4cd6e7f
revset: optimize the parse tree directly
Matt Mackall <mpm@selenic.com>
parents:
11278
diff
changeset
|
530 |
if wb < wa: |
62ccf4cd6e7f
revset: optimize the parse tree directly
Matt Mackall <mpm@selenic.com>
parents:
11278
diff
changeset
|
531 |
wb, wa = wa, wb |
62ccf4cd6e7f
revset: optimize the parse tree directly
Matt Mackall <mpm@selenic.com>
parents:
11278
diff
changeset
|
532 |
return max(wa, wb), (op, ta, tb) |
11275 | 533 |
elif op == 'not': |
11279
62ccf4cd6e7f
revset: optimize the parse tree directly
Matt Mackall <mpm@selenic.com>
parents:
11278
diff
changeset
|
534 |
o = optimize(x[1], not small) |
62ccf4cd6e7f
revset: optimize the parse tree directly
Matt Mackall <mpm@selenic.com>
parents:
11278
diff
changeset
|
535 |
return o[0], (op, o[1]) |
11275 | 536 |
elif op == 'group': |
11279
62ccf4cd6e7f
revset: optimize the parse tree directly
Matt Mackall <mpm@selenic.com>
parents:
11278
diff
changeset
|
537 |
return optimize(x[1], small) |
62ccf4cd6e7f
revset: optimize the parse tree directly
Matt Mackall <mpm@selenic.com>
parents:
11278
diff
changeset
|
538 |
elif op in 'range list': |
62ccf4cd6e7f
revset: optimize the parse tree directly
Matt Mackall <mpm@selenic.com>
parents:
11278
diff
changeset
|
539 |
wa, ta = optimize(x[1], small) |
62ccf4cd6e7f
revset: optimize the parse tree directly
Matt Mackall <mpm@selenic.com>
parents:
11278
diff
changeset
|
540 |
wb, tb = optimize(x[2], small) |
62ccf4cd6e7f
revset: optimize the parse tree directly
Matt Mackall <mpm@selenic.com>
parents:
11278
diff
changeset
|
541 |
return wa + wb, (op, ta, tb) |
11275 | 542 |
elif op == 'func': |
11383
de544774ebea
revset: all your error messages are belong to _
Martin Geisler <mg@lazybytes.net>
parents:
11349
diff
changeset
|
543 |
f = getstring(x[1], _("not a symbol")) |
11279
62ccf4cd6e7f
revset: optimize the parse tree directly
Matt Mackall <mpm@selenic.com>
parents:
11278
diff
changeset
|
544 |
wa, ta = optimize(x[2], small) |
11275 | 545 |
if f in "grep date user author keyword branch file": |
11279
62ccf4cd6e7f
revset: optimize the parse tree directly
Matt Mackall <mpm@selenic.com>
parents:
11278
diff
changeset
|
546 |
w = 10 # slow |
62ccf4cd6e7f
revset: optimize the parse tree directly
Matt Mackall <mpm@selenic.com>
parents:
11278
diff
changeset
|
547 |
elif f in "modifies adds removes outgoing": |
62ccf4cd6e7f
revset: optimize the parse tree directly
Matt Mackall <mpm@selenic.com>
parents:
11278
diff
changeset
|
548 |
w = 30 # slower |
11275 | 549 |
elif f == "contains": |
11279
62ccf4cd6e7f
revset: optimize the parse tree directly
Matt Mackall <mpm@selenic.com>
parents:
11278
diff
changeset
|
550 |
w = 100 # very slow |
11275 | 551 |
elif f == "ancestor": |
11279
62ccf4cd6e7f
revset: optimize the parse tree directly
Matt Mackall <mpm@selenic.com>
parents:
11278
diff
changeset
|
552 |
w = 1 * smallbonus |
11275 | 553 |
elif f == "reverse limit": |
11279
62ccf4cd6e7f
revset: optimize the parse tree directly
Matt Mackall <mpm@selenic.com>
parents:
11278
diff
changeset
|
554 |
w = 0 |
11275 | 555 |
elif f in "sort": |
11279
62ccf4cd6e7f
revset: optimize the parse tree directly
Matt Mackall <mpm@selenic.com>
parents:
11278
diff
changeset
|
556 |
w = 10 # assume most sorts look at changelog |
11275 | 557 |
else: |
11279
62ccf4cd6e7f
revset: optimize the parse tree directly
Matt Mackall <mpm@selenic.com>
parents:
11278
diff
changeset
|
558 |
w = 1 |
62ccf4cd6e7f
revset: optimize the parse tree directly
Matt Mackall <mpm@selenic.com>
parents:
11278
diff
changeset
|
559 |
return w + wa, (op, x[1], ta) |
62ccf4cd6e7f
revset: optimize the parse tree directly
Matt Mackall <mpm@selenic.com>
parents:
11278
diff
changeset
|
560 |
return 1, x |
11275 | 561 |
|
562 |
parse = parser.parser(tokenize, elements).parse |
|
563 |
||
564 |
def match(spec): |
|
11385
e5a2134c083b
revset: nicer exception for empty queries
Matt Mackall <mpm@selenic.com>
parents:
11383
diff
changeset
|
565 |
if not spec: |
e5a2134c083b
revset: nicer exception for empty queries
Matt Mackall <mpm@selenic.com>
parents:
11383
diff
changeset
|
566 |
raise error.ParseError(_("empty query")) |
11275 | 567 |
tree = parse(spec) |
11279
62ccf4cd6e7f
revset: optimize the parse tree directly
Matt Mackall <mpm@selenic.com>
parents:
11278
diff
changeset
|
568 |
weight, tree = optimize(tree, True) |
11275 | 569 |
def mfunc(repo, subset): |
570 |
return getset(repo, subset, tree) |
|
571 |
return mfunc |