##// END OF EJS Templates
fileset: add support for file status predicates...
Matt Mackall -
r14677:2a758ffc default
parent child Browse files
Show More
@@ -1,177 +1,234
1 1 # fileset.py - file set queries for mercurial
2 2 #
3 3 # Copyright 2010 Matt Mackall <mpm@selenic.com>
4 4 #
5 5 # This software may be used and distributed according to the terms of the
6 6 # GNU General Public License version 2 or any later version.
7 7
8 8 import parser, error, util
9 9 from i18n import _
10 10
11 11 elements = {
12 12 "(": (20, ("group", 1, ")"), ("func", 1, ")")),
13 13 "-": (5, ("negate", 19), ("minus", 5)),
14 14 "not": (10, ("not", 10)),
15 15 "!": (10, ("not", 10)),
16 16 "and": (5, None, ("and", 5)),
17 17 "&": (5, None, ("and", 5)),
18 18 "or": (4, None, ("or", 4)),
19 19 "|": (4, None, ("or", 4)),
20 20 "+": (4, None, ("or", 4)),
21 21 ",": (2, None, ("list", 2)),
22 22 ")": (0, None, None),
23 23 "symbol": (0, ("symbol",), None),
24 24 "string": (0, ("string",), None),
25 25 "end": (0, None, None),
26 26 }
27 27
28 28 keywords = set(['and', 'or', 'not'])
29 29
30 30 globchars = ".*{}[]?/\\"
31 31
32 32 def tokenize(program):
33 33 pos, l = 0, len(program)
34 34 while pos < l:
35 35 c = program[pos]
36 36 if c.isspace(): # skip inter-token whitespace
37 37 pass
38 38 elif c in "(),-|&+!": # handle simple operators
39 39 yield (c, None, pos)
40 40 elif (c in '"\'' or c == 'r' and
41 41 program[pos:pos + 2] in ("r'", 'r"')): # handle quoted strings
42 42 if c == 'r':
43 43 pos += 1
44 44 c = program[pos]
45 45 decode = lambda x: x
46 46 else:
47 47 decode = lambda x: x.decode('string-escape')
48 48 pos += 1
49 49 s = pos
50 50 while pos < l: # find closing quote
51 51 d = program[pos]
52 52 if d == '\\': # skip over escaped characters
53 53 pos += 2
54 54 continue
55 55 if d == c:
56 56 yield ('string', decode(program[s:pos]), s)
57 57 break
58 58 pos += 1
59 59 else:
60 60 raise error.ParseError(_("unterminated string"), s)
61 61 elif c.isalnum() or c in globchars or ord(c) > 127:
62 62 # gather up a symbol/keyword
63 63 s = pos
64 64 pos += 1
65 65 while pos < l: # find end of symbol
66 66 d = program[pos]
67 67 if not (d.isalnum() or d in globchars or ord(d) > 127):
68 68 break
69 69 pos += 1
70 70 sym = program[s:pos]
71 71 if sym in keywords: # operator keywords
72 72 yield (sym, None, s)
73 73 else:
74 74 yield ('symbol', sym, s)
75 75 pos -= 1
76 76 else:
77 77 raise error.ParseError(_("syntax error"), pos)
78 78 pos += 1
79 79 yield ('end', None, pos)
80 80
81 81 parse = parser.parser(tokenize, elements).parse
82 82
83 83 def getstring(x, err):
84 84 if x and (x[0] == 'string' or x[0] == 'symbol'):
85 85 return x[1]
86 86 raise error.ParseError(err)
87 87
88 88 def getset(mctx, x):
89 89 if not x:
90 90 raise error.ParseError(_("missing argument"))
91 91 return methods[x[0]](mctx, *x[1:])
92 92
93 93 def stringset(mctx, x):
94 94 m = mctx.matcher([x])
95 95 return [f for f in mctx.subset if m(f)]
96 96
97 97 def andset(mctx, x, y):
98 98 return getset(mctx.narrow(getset(mctx, x)), y)
99 99
100 100 def orset(mctx, x, y):
101 101 # needs optimizing
102 102 xl = getset(mctx, x)
103 103 yl = getset(mctx, y)
104 104 return xl + [f for f in yl if f not in xl]
105 105
106 106 def notset(mctx, x):
107 107 s = set(getset(mctx, x))
108 108 return [r for r in mctx.subset if r not in s]
109 109
110 110 def listset(mctx, a, b):
111 111 raise error.ParseError(_("can't use a list in this context"))
112 112
113 def modified(mctx, x):
114 getargs(x, 0, 0, _("modified takes no arguments"))
115 s = mctx.status()[0]
116 return [f for f in mctx.subset if f in s]
117
118 def added(mctx, x):
119 getargs(x, 0, 0, _("added takes no arguments"))
120 s = mctx.status()[1]
121 return [f for f in mctx.subset if f in s]
122
123 def removed(mctx, x):
124 getargs(x, 0, 0, _("removed takes no arguments"))
125 s = mctx.status()[2]
126 return [f for f in mctx.subset if f in s]
127
128 def deleted(mctx, x):
129 getargs(x, 0, 0, _("deleted takes no arguments"))
130 s = mctx.status()[3]
131 return [f for f in mctx.subset if f in s]
132
133 def unknown(mctx, x):
134 getargs(x, 0, 0, _("unknown takes no arguments"))
135 s = mctx.status()[4]
136 return [f for f in mctx.subset if f in s]
137
138 def ignored(mctx, x):
139 getargs(x, 0, 0, _("ignored takes no arguments"))
140 s = mctx.status()[5]
141 return [f for f in mctx.subset if f in s]
142
143 def clean(mctx, x):
144 getargs(x, 0, 0, _("clean takes no arguments"))
145 s = mctx.status()[6]
146 return [f for f in mctx.subset if f in s]
147
113 148 def func(mctx, a, b):
114 149 if a[0] == 'symbol' and a[1] in symbols:
115 150 return symbols[a[1]](mctx, b)
116 151 raise error.ParseError(_("not a function: %s") % a[1])
117 152
118 153 def getlist(x):
119 154 if not x:
120 155 return []
121 156 if x[0] == 'list':
122 157 return getlist(x[1]) + [x[2]]
123 158 return [x]
124 159
125 160 def getargs(x, min, max, err):
126 161 l = getlist(x)
127 162 if len(l) < min or len(l) > max:
128 163 raise error.ParseError(err)
129 164 return l
130 165
131 166 def binary(mctx, x):
132 167 getargs(x, 0, 0, _("binary takes no arguments"))
133 168 return [f for f in mctx.subset if util.binary(mctx.ctx[f].data())]
134 169
135 170 def exec_(mctx, x):
136 171 getargs(x, 0, 0, _("exec takes no arguments"))
137 172 return [f for f in mctx.subset if mctx.ctx.flags(f) == 'x']
138 173
139 174 def symlink(mctx, x):
140 175 getargs(x, 0, 0, _("symlink takes no arguments"))
141 176 return [f for f in mctx.subset if mctx.ctx.flags(f) == 'l']
142 177
143 178 symbols = {
179 'added': added,
144 180 'binary': binary,
181 'clean': clean,
182 'deleted': deleted,
145 183 'exec': exec_,
184 'ignored': ignored,
185 'modified': modified,
186 'removed': removed,
146 187 'symlink': symlink,
188 'unknown': unknown,
147 189 }
148 190
149 191 methods = {
150 192 'string': stringset,
151 193 'symbol': stringset,
152 194 'and': andset,
153 195 'or': orset,
154 196 'list': listset,
155 197 'group': getset,
156 198 'not': notset,
157 199 'func': func,
158 200 }
159 201
160 202 class matchctx(object):
161 def __init__(self, ctx, subset=None):
203 def __init__(self, ctx, subset=None, status=None):
162 204 self.ctx = ctx
163 205 self.subset = subset
206 self._status = status
207 if status is None:
208 # desperately wants optimizing
209 r = self.ctx._repo
210 self._status = r.status(self.ctx.p1(), self.ctx,
211 unknown=True, ignored=True, clean=True)
164 212 if subset is None:
165 self.subset = ctx.walk(self.matcher([])) # optimize this later
213 self.subset = []
214 for c in self._status:
215 self.subset.extend(c)
216 def status(self):
217 if not self._status:
218 r = self.ctx._repo
219 # also wants optimizing
220 self._status = r.status(self.ctx.p1(), self.ctx,
221 unknown=True, ignored=True, clean=True)
222 return self._status
166 223 def matcher(self, patterns):
167 224 return self.ctx.match(patterns)
168 225 def filter(self, files):
169 226 return [f for f in files if f in self.subset]
170 227 def narrow(self, files):
171 return matchctx(self.ctx, self.filter(files))
228 return matchctx(self.ctx, self.filter(files), self._status)
172 229
173 230 def getfileset(ctx, expr):
174 231 tree, pos = parse(expr)
175 232 if (pos != len(expr)):
176 233 raise error.ParseError("invalid token", pos)
177 234 return getset(matchctx(ctx), tree)
General Comments 0
You need to be logged in to leave comments. Login now