##// END OF EJS Templates
match: drop dir callback...
Siddharth Agarwal -
r19141:aed8ec10 default
parent child Browse files
Show More
@@ -1,358 +1,356 b''
1 # match.py - filename matching
1 # match.py - filename matching
2 #
2 #
3 # Copyright 2008, 2009 Matt Mackall <mpm@selenic.com> and others
3 # Copyright 2008, 2009 Matt Mackall <mpm@selenic.com> and others
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 scmutil, util, fileset
9 import scmutil, util, fileset
10 from i18n import _
10 from i18n import _
11
11
12 def _rematcher(pat):
12 def _rematcher(pat):
13 m = util.compilere(pat)
13 m = util.compilere(pat)
14 try:
14 try:
15 # slightly faster, provided by facebook's re2 bindings
15 # slightly faster, provided by facebook's re2 bindings
16 return m.test_match
16 return m.test_match
17 except AttributeError:
17 except AttributeError:
18 return m.match
18 return m.match
19
19
20 def _expandsets(pats, ctx):
20 def _expandsets(pats, ctx):
21 '''convert set: patterns into a list of files in the given context'''
21 '''convert set: patterns into a list of files in the given context'''
22 fset = set()
22 fset = set()
23 other = []
23 other = []
24
24
25 for kind, expr in pats:
25 for kind, expr in pats:
26 if kind == 'set':
26 if kind == 'set':
27 if not ctx:
27 if not ctx:
28 raise util.Abort("fileset expression with no context")
28 raise util.Abort("fileset expression with no context")
29 s = fileset.getfileset(ctx, expr)
29 s = fileset.getfileset(ctx, expr)
30 fset.update(s)
30 fset.update(s)
31 continue
31 continue
32 other.append((kind, expr))
32 other.append((kind, expr))
33 return fset, other
33 return fset, other
34
34
35 class match(object):
35 class match(object):
36 def __init__(self, root, cwd, patterns, include=[], exclude=[],
36 def __init__(self, root, cwd, patterns, include=[], exclude=[],
37 default='glob', exact=False, auditor=None, ctx=None):
37 default='glob', exact=False, auditor=None, ctx=None):
38 """build an object to match a set of file patterns
38 """build an object to match a set of file patterns
39
39
40 arguments:
40 arguments:
41 root - the canonical root of the tree you're matching against
41 root - the canonical root of the tree you're matching against
42 cwd - the current working directory, if relevant
42 cwd - the current working directory, if relevant
43 patterns - patterns to find
43 patterns - patterns to find
44 include - patterns to include
44 include - patterns to include
45 exclude - patterns to exclude
45 exclude - patterns to exclude
46 default - if a pattern in names has no explicit type, assume this one
46 default - if a pattern in names has no explicit type, assume this one
47 exact - patterns are actually literals
47 exact - patterns are actually literals
48
48
49 a pattern is one of:
49 a pattern is one of:
50 'glob:<glob>' - a glob relative to cwd
50 'glob:<glob>' - a glob relative to cwd
51 're:<regexp>' - a regular expression
51 're:<regexp>' - a regular expression
52 'path:<path>' - a path relative to repository root
52 'path:<path>' - a path relative to repository root
53 'relglob:<glob>' - an unrooted glob (*.c matches C files in all dirs)
53 'relglob:<glob>' - an unrooted glob (*.c matches C files in all dirs)
54 'relpath:<path>' - a path relative to cwd
54 'relpath:<path>' - a path relative to cwd
55 'relre:<regexp>' - a regexp that needn't match the start of a name
55 'relre:<regexp>' - a regexp that needn't match the start of a name
56 'set:<fileset>' - a fileset expression
56 'set:<fileset>' - a fileset expression
57 '<something>' - a pattern of the specified default type
57 '<something>' - a pattern of the specified default type
58 """
58 """
59
59
60 self._root = root
60 self._root = root
61 self._cwd = cwd
61 self._cwd = cwd
62 self._files = []
62 self._files = []
63 self._anypats = bool(include or exclude)
63 self._anypats = bool(include or exclude)
64 self._ctx = ctx
64 self._ctx = ctx
65 self._always = False
65 self._always = False
66
66
67 if include:
67 if include:
68 pats = _normalize(include, 'glob', root, cwd, auditor)
68 pats = _normalize(include, 'glob', root, cwd, auditor)
69 self.includepat, im = _buildmatch(ctx, pats, '(?:/|$)')
69 self.includepat, im = _buildmatch(ctx, pats, '(?:/|$)')
70 if exclude:
70 if exclude:
71 pats = _normalize(exclude, 'glob', root, cwd, auditor)
71 pats = _normalize(exclude, 'glob', root, cwd, auditor)
72 self.excludepat, em = _buildmatch(ctx, pats, '(?:/|$)')
72 self.excludepat, em = _buildmatch(ctx, pats, '(?:/|$)')
73 if exact:
73 if exact:
74 if isinstance(patterns, list):
74 if isinstance(patterns, list):
75 self._files = patterns
75 self._files = patterns
76 else:
76 else:
77 self._files = list(patterns)
77 self._files = list(patterns)
78 pm = self.exact
78 pm = self.exact
79 elif patterns:
79 elif patterns:
80 pats = _normalize(patterns, default, root, cwd, auditor)
80 pats = _normalize(patterns, default, root, cwd, auditor)
81 self._files = _roots(pats)
81 self._files = _roots(pats)
82 self._anypats = self._anypats or _anypats(pats)
82 self._anypats = self._anypats or _anypats(pats)
83 self.patternspat, pm = _buildmatch(ctx, pats, '$')
83 self.patternspat, pm = _buildmatch(ctx, pats, '$')
84
84
85 if patterns or exact:
85 if patterns or exact:
86 if include:
86 if include:
87 if exclude:
87 if exclude:
88 m = lambda f: im(f) and not em(f) and pm(f)
88 m = lambda f: im(f) and not em(f) and pm(f)
89 else:
89 else:
90 m = lambda f: im(f) and pm(f)
90 m = lambda f: im(f) and pm(f)
91 else:
91 else:
92 if exclude:
92 if exclude:
93 m = lambda f: not em(f) and pm(f)
93 m = lambda f: not em(f) and pm(f)
94 else:
94 else:
95 m = pm
95 m = pm
96 else:
96 else:
97 if include:
97 if include:
98 if exclude:
98 if exclude:
99 m = lambda f: im(f) and not em(f)
99 m = lambda f: im(f) and not em(f)
100 else:
100 else:
101 m = im
101 m = im
102 else:
102 else:
103 if exclude:
103 if exclude:
104 m = lambda f: not em(f)
104 m = lambda f: not em(f)
105 else:
105 else:
106 m = lambda f: True
106 m = lambda f: True
107 self._always = True
107 self._always = True
108
108
109 self.matchfn = m
109 self.matchfn = m
110 self._fmap = set(self._files)
110 self._fmap = set(self._files)
111
111
112 def __call__(self, fn):
112 def __call__(self, fn):
113 return self.matchfn(fn)
113 return self.matchfn(fn)
114 def __iter__(self):
114 def __iter__(self):
115 for f in self._files:
115 for f in self._files:
116 yield f
116 yield f
117 def bad(self, f, msg):
117 def bad(self, f, msg):
118 '''callback for each explicit file that can't be
118 '''callback for each explicit file that can't be
119 found/accessed, with an error message
119 found/accessed, with an error message
120 '''
120 '''
121 pass
121 pass
122 def dir(self, f):
122 def explicitdir(self, f):
123 pass
123 pass
124 def explicitdir(self, f):
125 self.dir(f)
126 def traversedir(self, f):
124 def traversedir(self, f):
127 self.dir(f)
125 pass
128 def missing(self, f):
126 def missing(self, f):
129 pass
127 pass
130 def exact(self, f):
128 def exact(self, f):
131 return f in self._fmap
129 return f in self._fmap
132 def rel(self, f):
130 def rel(self, f):
133 return util.pathto(self._root, self._cwd, f)
131 return util.pathto(self._root, self._cwd, f)
134 def files(self):
132 def files(self):
135 return self._files
133 return self._files
136 def anypats(self):
134 def anypats(self):
137 return self._anypats
135 return self._anypats
138 def always(self):
136 def always(self):
139 return self._always
137 return self._always
140
138
141 class exact(match):
139 class exact(match):
142 def __init__(self, root, cwd, files):
140 def __init__(self, root, cwd, files):
143 match.__init__(self, root, cwd, files, exact = True)
141 match.__init__(self, root, cwd, files, exact = True)
144
142
145 class always(match):
143 class always(match):
146 def __init__(self, root, cwd):
144 def __init__(self, root, cwd):
147 match.__init__(self, root, cwd, [])
145 match.__init__(self, root, cwd, [])
148 self._always = True
146 self._always = True
149
147
150 class narrowmatcher(match):
148 class narrowmatcher(match):
151 """Adapt a matcher to work on a subdirectory only.
149 """Adapt a matcher to work on a subdirectory only.
152
150
153 The paths are remapped to remove/insert the path as needed:
151 The paths are remapped to remove/insert the path as needed:
154
152
155 >>> m1 = match('root', '', ['a.txt', 'sub/b.txt'])
153 >>> m1 = match('root', '', ['a.txt', 'sub/b.txt'])
156 >>> m2 = narrowmatcher('sub', m1)
154 >>> m2 = narrowmatcher('sub', m1)
157 >>> bool(m2('a.txt'))
155 >>> bool(m2('a.txt'))
158 False
156 False
159 >>> bool(m2('b.txt'))
157 >>> bool(m2('b.txt'))
160 True
158 True
161 >>> bool(m2.matchfn('a.txt'))
159 >>> bool(m2.matchfn('a.txt'))
162 False
160 False
163 >>> bool(m2.matchfn('b.txt'))
161 >>> bool(m2.matchfn('b.txt'))
164 True
162 True
165 >>> m2.files()
163 >>> m2.files()
166 ['b.txt']
164 ['b.txt']
167 >>> m2.exact('b.txt')
165 >>> m2.exact('b.txt')
168 True
166 True
169 >>> m2.rel('b.txt')
167 >>> m2.rel('b.txt')
170 'b.txt'
168 'b.txt'
171 >>> def bad(f, msg):
169 >>> def bad(f, msg):
172 ... print "%s: %s" % (f, msg)
170 ... print "%s: %s" % (f, msg)
173 >>> m1.bad = bad
171 >>> m1.bad = bad
174 >>> m2.bad('x.txt', 'No such file')
172 >>> m2.bad('x.txt', 'No such file')
175 sub/x.txt: No such file
173 sub/x.txt: No such file
176 """
174 """
177
175
178 def __init__(self, path, matcher):
176 def __init__(self, path, matcher):
179 self._root = matcher._root
177 self._root = matcher._root
180 self._cwd = matcher._cwd
178 self._cwd = matcher._cwd
181 self._path = path
179 self._path = path
182 self._matcher = matcher
180 self._matcher = matcher
183 self._always = matcher._always
181 self._always = matcher._always
184
182
185 self._files = [f[len(path) + 1:] for f in matcher._files
183 self._files = [f[len(path) + 1:] for f in matcher._files
186 if f.startswith(path + "/")]
184 if f.startswith(path + "/")]
187 self._anypats = matcher._anypats
185 self._anypats = matcher._anypats
188 self.matchfn = lambda fn: matcher.matchfn(self._path + "/" + fn)
186 self.matchfn = lambda fn: matcher.matchfn(self._path + "/" + fn)
189 self._fmap = set(self._files)
187 self._fmap = set(self._files)
190
188
191 def bad(self, f, msg):
189 def bad(self, f, msg):
192 self._matcher.bad(self._path + "/" + f, msg)
190 self._matcher.bad(self._path + "/" + f, msg)
193
191
194 def patkind(pat):
192 def patkind(pat):
195 return _patsplit(pat, None)[0]
193 return _patsplit(pat, None)[0]
196
194
197 def _patsplit(pat, default):
195 def _patsplit(pat, default):
198 """Split a string into an optional pattern kind prefix and the
196 """Split a string into an optional pattern kind prefix and the
199 actual pattern."""
197 actual pattern."""
200 if ':' in pat:
198 if ':' in pat:
201 kind, val = pat.split(':', 1)
199 kind, val = pat.split(':', 1)
202 if kind in ('re', 'glob', 'path', 'relglob', 'relpath', 'relre',
200 if kind in ('re', 'glob', 'path', 'relglob', 'relpath', 'relre',
203 'listfile', 'listfile0', 'set'):
201 'listfile', 'listfile0', 'set'):
204 return kind, val
202 return kind, val
205 return default, pat
203 return default, pat
206
204
207 def _globre(pat):
205 def _globre(pat):
208 "convert a glob pattern into a regexp"
206 "convert a glob pattern into a regexp"
209 i, n = 0, len(pat)
207 i, n = 0, len(pat)
210 res = ''
208 res = ''
211 group = 0
209 group = 0
212 escape = re.escape
210 escape = re.escape
213 def peek():
211 def peek():
214 return i < n and pat[i]
212 return i < n and pat[i]
215 while i < n:
213 while i < n:
216 c = pat[i]
214 c = pat[i]
217 i += 1
215 i += 1
218 if c not in '*?[{},\\':
216 if c not in '*?[{},\\':
219 res += escape(c)
217 res += escape(c)
220 elif c == '*':
218 elif c == '*':
221 if peek() == '*':
219 if peek() == '*':
222 i += 1
220 i += 1
223 res += '.*'
221 res += '.*'
224 else:
222 else:
225 res += '[^/]*'
223 res += '[^/]*'
226 elif c == '?':
224 elif c == '?':
227 res += '.'
225 res += '.'
228 elif c == '[':
226 elif c == '[':
229 j = i
227 j = i
230 if j < n and pat[j] in '!]':
228 if j < n and pat[j] in '!]':
231 j += 1
229 j += 1
232 while j < n and pat[j] != ']':
230 while j < n and pat[j] != ']':
233 j += 1
231 j += 1
234 if j >= n:
232 if j >= n:
235 res += '\\['
233 res += '\\['
236 else:
234 else:
237 stuff = pat[i:j].replace('\\','\\\\')
235 stuff = pat[i:j].replace('\\','\\\\')
238 i = j + 1
236 i = j + 1
239 if stuff[0] == '!':
237 if stuff[0] == '!':
240 stuff = '^' + stuff[1:]
238 stuff = '^' + stuff[1:]
241 elif stuff[0] == '^':
239 elif stuff[0] == '^':
242 stuff = '\\' + stuff
240 stuff = '\\' + stuff
243 res = '%s[%s]' % (res, stuff)
241 res = '%s[%s]' % (res, stuff)
244 elif c == '{':
242 elif c == '{':
245 group += 1
243 group += 1
246 res += '(?:'
244 res += '(?:'
247 elif c == '}' and group:
245 elif c == '}' and group:
248 res += ')'
246 res += ')'
249 group -= 1
247 group -= 1
250 elif c == ',' and group:
248 elif c == ',' and group:
251 res += '|'
249 res += '|'
252 elif c == '\\':
250 elif c == '\\':
253 p = peek()
251 p = peek()
254 if p:
252 if p:
255 i += 1
253 i += 1
256 res += escape(p)
254 res += escape(p)
257 else:
255 else:
258 res += escape(c)
256 res += escape(c)
259 else:
257 else:
260 res += escape(c)
258 res += escape(c)
261 return res
259 return res
262
260
263 def _regex(kind, name, tail):
261 def _regex(kind, name, tail):
264 '''convert a pattern into a regular expression'''
262 '''convert a pattern into a regular expression'''
265 if not name:
263 if not name:
266 return ''
264 return ''
267 if kind == 're':
265 if kind == 're':
268 return name
266 return name
269 elif kind == 'path':
267 elif kind == 'path':
270 return '^' + re.escape(name) + '(?:/|$)'
268 return '^' + re.escape(name) + '(?:/|$)'
271 elif kind == 'relglob':
269 elif kind == 'relglob':
272 return '(?:|.*/)' + _globre(name) + tail
270 return '(?:|.*/)' + _globre(name) + tail
273 elif kind == 'relpath':
271 elif kind == 'relpath':
274 return re.escape(name) + '(?:/|$)'
272 return re.escape(name) + '(?:/|$)'
275 elif kind == 'relre':
273 elif kind == 'relre':
276 if name.startswith('^'):
274 if name.startswith('^'):
277 return name
275 return name
278 return '.*' + name
276 return '.*' + name
279 return _globre(name) + tail
277 return _globre(name) + tail
280
278
281 def _buildmatch(ctx, pats, tail):
279 def _buildmatch(ctx, pats, tail):
282 fset, pats = _expandsets(pats, ctx)
280 fset, pats = _expandsets(pats, ctx)
283 if not pats:
281 if not pats:
284 return "", fset.__contains__
282 return "", fset.__contains__
285
283
286 pat, mf = _buildregexmatch(pats, tail)
284 pat, mf = _buildregexmatch(pats, tail)
287 if fset:
285 if fset:
288 return pat, lambda f: f in fset or mf(f)
286 return pat, lambda f: f in fset or mf(f)
289 return pat, mf
287 return pat, mf
290
288
291 def _buildregexmatch(pats, tail):
289 def _buildregexmatch(pats, tail):
292 """build a matching function from a set of patterns"""
290 """build a matching function from a set of patterns"""
293 try:
291 try:
294 pat = '(?:%s)' % '|'.join([_regex(k, p, tail) for (k, p) in pats])
292 pat = '(?:%s)' % '|'.join([_regex(k, p, tail) for (k, p) in pats])
295 if len(pat) > 20000:
293 if len(pat) > 20000:
296 raise OverflowError
294 raise OverflowError
297 return pat, _rematcher(pat)
295 return pat, _rematcher(pat)
298 except OverflowError:
296 except OverflowError:
299 # We're using a Python with a tiny regex engine and we
297 # We're using a Python with a tiny regex engine and we
300 # made it explode, so we'll divide the pattern list in two
298 # made it explode, so we'll divide the pattern list in two
301 # until it works
299 # until it works
302 l = len(pats)
300 l = len(pats)
303 if l < 2:
301 if l < 2:
304 raise
302 raise
305 pata, a = _buildregexmatch(pats[:l//2], tail)
303 pata, a = _buildregexmatch(pats[:l//2], tail)
306 patb, b = _buildregexmatch(pats[l//2:], tail)
304 patb, b = _buildregexmatch(pats[l//2:], tail)
307 return pat, lambda s: a(s) or b(s)
305 return pat, lambda s: a(s) or b(s)
308 except re.error:
306 except re.error:
309 for k, p in pats:
307 for k, p in pats:
310 try:
308 try:
311 _rematcher('(?:%s)' % _regex(k, p, tail))
309 _rematcher('(?:%s)' % _regex(k, p, tail))
312 except re.error:
310 except re.error:
313 raise util.Abort(_("invalid pattern (%s): %s") % (k, p))
311 raise util.Abort(_("invalid pattern (%s): %s") % (k, p))
314 raise util.Abort(_("invalid pattern"))
312 raise util.Abort(_("invalid pattern"))
315
313
316 def _normalize(names, default, root, cwd, auditor):
314 def _normalize(names, default, root, cwd, auditor):
317 pats = []
315 pats = []
318 for kind, name in [_patsplit(p, default) for p in names]:
316 for kind, name in [_patsplit(p, default) for p in names]:
319 if kind in ('glob', 'relpath'):
317 if kind in ('glob', 'relpath'):
320 name = scmutil.canonpath(root, cwd, name, auditor)
318 name = scmutil.canonpath(root, cwd, name, auditor)
321 elif kind in ('relglob', 'path'):
319 elif kind in ('relglob', 'path'):
322 name = util.normpath(name)
320 name = util.normpath(name)
323 elif kind in ('listfile', 'listfile0'):
321 elif kind in ('listfile', 'listfile0'):
324 try:
322 try:
325 files = util.readfile(name)
323 files = util.readfile(name)
326 if kind == 'listfile0':
324 if kind == 'listfile0':
327 files = files.split('\0')
325 files = files.split('\0')
328 else:
326 else:
329 files = files.splitlines()
327 files = files.splitlines()
330 files = [f for f in files if f]
328 files = [f for f in files if f]
331 except EnvironmentError:
329 except EnvironmentError:
332 raise util.Abort(_("unable to read file list (%s)") % name)
330 raise util.Abort(_("unable to read file list (%s)") % name)
333 pats += _normalize(files, default, root, cwd, auditor)
331 pats += _normalize(files, default, root, cwd, auditor)
334 continue
332 continue
335
333
336 pats.append((kind, name))
334 pats.append((kind, name))
337 return pats
335 return pats
338
336
339 def _roots(patterns):
337 def _roots(patterns):
340 r = []
338 r = []
341 for kind, name in patterns:
339 for kind, name in patterns:
342 if kind == 'glob': # find the non-glob prefix
340 if kind == 'glob': # find the non-glob prefix
343 root = []
341 root = []
344 for p in name.split('/'):
342 for p in name.split('/'):
345 if '[' in p or '{' in p or '*' in p or '?' in p:
343 if '[' in p or '{' in p or '*' in p or '?' in p:
346 break
344 break
347 root.append(p)
345 root.append(p)
348 r.append('/'.join(root) or '.')
346 r.append('/'.join(root) or '.')
349 elif kind in ('relpath', 'path'):
347 elif kind in ('relpath', 'path'):
350 r.append(name or '.')
348 r.append(name or '.')
351 else: # relglob, re, relre
349 else: # relglob, re, relre
352 r.append('.')
350 r.append('.')
353 return r
351 return r
354
352
355 def _anypats(patterns):
353 def _anypats(patterns):
356 for kind, name in patterns:
354 for kind, name in patterns:
357 if kind in ('glob', 're', 'relglob', 'relre', 'set'):
355 if kind in ('glob', 're', 'relglob', 'relre', 'set'):
358 return True
356 return True
General Comments 0
You need to be logged in to leave comments. Login now