##// END OF EJS Templates
match: make it more clear what _roots do and that it ends up in match()._files
Mads Kiilerich -
r21079:b02ab648 default
parent child Browse files
Show More
@@ -1,358 +1,367 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 util, pathutil
9 import util, pathutil
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 = ctx.getfileset(expr)
29 s = ctx.getfileset(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 = [] # exact files and roots of patterns
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 # If this is set, it will be called when an explicitly listed directory is
122 # If this is set, it will be called when an explicitly listed directory is
123 # visited.
123 # visited.
124 explicitdir = None
124 explicitdir = None
125 # If this is set, it will be called when a directory discovered by recursive
125 # If this is set, it will be called when a directory discovered by recursive
126 # traversal is visited.
126 # traversal is visited.
127 traversedir = None
127 traversedir = None
128 def missing(self, f):
128 def missing(self, f):
129 pass
129 pass
130 def exact(self, f):
130 def exact(self, f):
131 return f in self._fmap
131 return f in self._fmap
132 def rel(self, f):
132 def rel(self, f):
133 return util.pathto(self._root, self._cwd, f)
133 return util.pathto(self._root, self._cwd, f)
134 def files(self):
134 def files(self):
135 return self._files
135 return self._files
136 def anypats(self):
136 def anypats(self):
137 return self._anypats
137 return self._anypats
138 def always(self):
138 def always(self):
139 return self._always
139 return self._always
140
140
141 class exact(match):
141 class exact(match):
142 def __init__(self, root, cwd, files):
142 def __init__(self, root, cwd, files):
143 match.__init__(self, root, cwd, files, exact=True)
143 match.__init__(self, root, cwd, files, exact=True)
144
144
145 class always(match):
145 class always(match):
146 def __init__(self, root, cwd):
146 def __init__(self, root, cwd):
147 match.__init__(self, root, cwd, [])
147 match.__init__(self, root, cwd, [])
148 self._always = True
148 self._always = True
149
149
150 class narrowmatcher(match):
150 class narrowmatcher(match):
151 """Adapt a matcher to work on a subdirectory only.
151 """Adapt a matcher to work on a subdirectory only.
152
152
153 The paths are remapped to remove/insert the path as needed:
153 The paths are remapped to remove/insert the path as needed:
154
154
155 >>> m1 = match('root', '', ['a.txt', 'sub/b.txt'])
155 >>> m1 = match('root', '', ['a.txt', 'sub/b.txt'])
156 >>> m2 = narrowmatcher('sub', m1)
156 >>> m2 = narrowmatcher('sub', m1)
157 >>> bool(m2('a.txt'))
157 >>> bool(m2('a.txt'))
158 False
158 False
159 >>> bool(m2('b.txt'))
159 >>> bool(m2('b.txt'))
160 True
160 True
161 >>> bool(m2.matchfn('a.txt'))
161 >>> bool(m2.matchfn('a.txt'))
162 False
162 False
163 >>> bool(m2.matchfn('b.txt'))
163 >>> bool(m2.matchfn('b.txt'))
164 True
164 True
165 >>> m2.files()
165 >>> m2.files()
166 ['b.txt']
166 ['b.txt']
167 >>> m2.exact('b.txt')
167 >>> m2.exact('b.txt')
168 True
168 True
169 >>> m2.rel('b.txt')
169 >>> m2.rel('b.txt')
170 'b.txt'
170 'b.txt'
171 >>> def bad(f, msg):
171 >>> def bad(f, msg):
172 ... print "%s: %s" % (f, msg)
172 ... print "%s: %s" % (f, msg)
173 >>> m1.bad = bad
173 >>> m1.bad = bad
174 >>> m2.bad('x.txt', 'No such file')
174 >>> m2.bad('x.txt', 'No such file')
175 sub/x.txt: No such file
175 sub/x.txt: No such file
176 """
176 """
177
177
178 def __init__(self, path, matcher):
178 def __init__(self, path, matcher):
179 self._root = matcher._root
179 self._root = matcher._root
180 self._cwd = matcher._cwd
180 self._cwd = matcher._cwd
181 self._path = path
181 self._path = path
182 self._matcher = matcher
182 self._matcher = matcher
183 self._always = matcher._always
183 self._always = matcher._always
184
184
185 self._files = [f[len(path) + 1:] for f in matcher._files
185 self._files = [f[len(path) + 1:] for f in matcher._files
186 if f.startswith(path + "/")]
186 if f.startswith(path + "/")]
187 self._anypats = matcher._anypats
187 self._anypats = matcher._anypats
188 self.matchfn = lambda fn: matcher.matchfn(self._path + "/" + fn)
188 self.matchfn = lambda fn: matcher.matchfn(self._path + "/" + fn)
189 self._fmap = set(self._files)
189 self._fmap = set(self._files)
190
190
191 def bad(self, f, msg):
191 def bad(self, f, msg):
192 self._matcher.bad(self._path + "/" + f, msg)
192 self._matcher.bad(self._path + "/" + f, msg)
193
193
194 def patkind(pat):
194 def patkind(pat):
195 return _patsplit(pat, None)[0]
195 return _patsplit(pat, None)[0]
196
196
197 def _patsplit(pat, default):
197 def _patsplit(pat, default):
198 """Split a string into an optional pattern kind prefix and the
198 """Split a string into an optional pattern kind prefix and the
199 actual pattern."""
199 actual pattern."""
200 if ':' in pat:
200 if ':' in pat:
201 kind, val = pat.split(':', 1)
201 kind, val = pat.split(':', 1)
202 if kind in ('re', 'glob', 'path', 'relglob', 'relpath', 'relre',
202 if kind in ('re', 'glob', 'path', 'relglob', 'relpath', 'relre',
203 'listfile', 'listfile0', 'set'):
203 'listfile', 'listfile0', 'set'):
204 return kind, val
204 return kind, val
205 return default, pat
205 return default, pat
206
206
207 def _globre(pat):
207 def _globre(pat):
208 "convert a glob pattern into a regexp"
208 "convert a glob pattern into a regexp"
209 i, n = 0, len(pat)
209 i, n = 0, len(pat)
210 res = ''
210 res = ''
211 group = 0
211 group = 0
212 escape = re.escape
212 escape = re.escape
213 def peek():
213 def peek():
214 return i < n and pat[i]
214 return i < n and pat[i]
215 while i < n:
215 while i < n:
216 c = pat[i]
216 c = pat[i]
217 i += 1
217 i += 1
218 if c not in '*?[{},\\':
218 if c not in '*?[{},\\':
219 res += escape(c)
219 res += escape(c)
220 elif c == '*':
220 elif c == '*':
221 if peek() == '*':
221 if peek() == '*':
222 i += 1
222 i += 1
223 res += '.*'
223 res += '.*'
224 else:
224 else:
225 res += '[^/]*'
225 res += '[^/]*'
226 elif c == '?':
226 elif c == '?':
227 res += '.'
227 res += '.'
228 elif c == '[':
228 elif c == '[':
229 j = i
229 j = i
230 if j < n and pat[j] in '!]':
230 if j < n and pat[j] in '!]':
231 j += 1
231 j += 1
232 while j < n and pat[j] != ']':
232 while j < n and pat[j] != ']':
233 j += 1
233 j += 1
234 if j >= n:
234 if j >= n:
235 res += '\\['
235 res += '\\['
236 else:
236 else:
237 stuff = pat[i:j].replace('\\','\\\\')
237 stuff = pat[i:j].replace('\\','\\\\')
238 i = j + 1
238 i = j + 1
239 if stuff[0] == '!':
239 if stuff[0] == '!':
240 stuff = '^' + stuff[1:]
240 stuff = '^' + stuff[1:]
241 elif stuff[0] == '^':
241 elif stuff[0] == '^':
242 stuff = '\\' + stuff
242 stuff = '\\' + stuff
243 res = '%s[%s]' % (res, stuff)
243 res = '%s[%s]' % (res, stuff)
244 elif c == '{':
244 elif c == '{':
245 group += 1
245 group += 1
246 res += '(?:'
246 res += '(?:'
247 elif c == '}' and group:
247 elif c == '}' and group:
248 res += ')'
248 res += ')'
249 group -= 1
249 group -= 1
250 elif c == ',' and group:
250 elif c == ',' and group:
251 res += '|'
251 res += '|'
252 elif c == '\\':
252 elif c == '\\':
253 p = peek()
253 p = peek()
254 if p:
254 if p:
255 i += 1
255 i += 1
256 res += escape(p)
256 res += escape(p)
257 else:
257 else:
258 res += escape(c)
258 res += escape(c)
259 else:
259 else:
260 res += escape(c)
260 res += escape(c)
261 return res
261 return res
262
262
263 def _regex(kind, name, tail):
263 def _regex(kind, name, tail):
264 '''convert a pattern into a regular expression'''
264 '''convert a pattern into a regular expression'''
265 if not name:
265 if not name:
266 return ''
266 return ''
267 if kind == 're':
267 if kind == 're':
268 return name
268 return name
269 elif kind == 'path':
269 elif kind == 'path':
270 return '^' + re.escape(name) + '(?:/|$)'
270 return '^' + re.escape(name) + '(?:/|$)'
271 elif kind == 'relglob':
271 elif kind == 'relglob':
272 return '(?:|.*/)' + _globre(name) + tail
272 return '(?:|.*/)' + _globre(name) + tail
273 elif kind == 'relpath':
273 elif kind == 'relpath':
274 return re.escape(name) + '(?:/|$)'
274 return re.escape(name) + '(?:/|$)'
275 elif kind == 'relre':
275 elif kind == 'relre':
276 if name.startswith('^'):
276 if name.startswith('^'):
277 return name
277 return name
278 return '.*' + name
278 return '.*' + name
279 return _globre(name) + tail
279 return _globre(name) + tail
280
280
281 def _buildmatch(ctx, pats, tail):
281 def _buildmatch(ctx, pats, tail):
282 fset, pats = _expandsets(pats, ctx)
282 fset, pats = _expandsets(pats, ctx)
283 if not pats:
283 if not pats:
284 return "", fset.__contains__
284 return "", fset.__contains__
285
285
286 pat, mf = _buildregexmatch(pats, tail)
286 pat, mf = _buildregexmatch(pats, tail)
287 if fset:
287 if fset:
288 return pat, lambda f: f in fset or mf(f)
288 return pat, lambda f: f in fset or mf(f)
289 return pat, mf
289 return pat, mf
290
290
291 def _buildregexmatch(pats, tail):
291 def _buildregexmatch(pats, tail):
292 """build a matching function from a set of patterns"""
292 """build a matching function from a set of patterns"""
293 try:
293 try:
294 pat = '(?:%s)' % '|'.join([_regex(k, p, tail) for (k, p) in pats])
294 pat = '(?:%s)' % '|'.join([_regex(k, p, tail) for (k, p) in pats])
295 if len(pat) > 20000:
295 if len(pat) > 20000:
296 raise OverflowError
296 raise OverflowError
297 return pat, _rematcher(pat)
297 return pat, _rematcher(pat)
298 except OverflowError:
298 except OverflowError:
299 # We're using a Python with a tiny regex engine and we
299 # 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
300 # made it explode, so we'll divide the pattern list in two
301 # until it works
301 # until it works
302 l = len(pats)
302 l = len(pats)
303 if l < 2:
303 if l < 2:
304 raise
304 raise
305 pata, a = _buildregexmatch(pats[:l//2], tail)
305 pata, a = _buildregexmatch(pats[:l//2], tail)
306 patb, b = _buildregexmatch(pats[l//2:], tail)
306 patb, b = _buildregexmatch(pats[l//2:], tail)
307 return pat, lambda s: a(s) or b(s)
307 return pat, lambda s: a(s) or b(s)
308 except re.error:
308 except re.error:
309 for k, p in pats:
309 for k, p in pats:
310 try:
310 try:
311 _rematcher('(?:%s)' % _regex(k, p, tail))
311 _rematcher('(?:%s)' % _regex(k, p, tail))
312 except re.error:
312 except re.error:
313 raise util.Abort(_("invalid pattern (%s): %s") % (k, p))
313 raise util.Abort(_("invalid pattern (%s): %s") % (k, p))
314 raise util.Abort(_("invalid pattern"))
314 raise util.Abort(_("invalid pattern"))
315
315
316 def _normalize(names, default, root, cwd, auditor):
316 def _normalize(names, default, root, cwd, auditor):
317 pats = []
317 pats = []
318 for kind, name in [_patsplit(p, default) for p in names]:
318 for kind, name in [_patsplit(p, default) for p in names]:
319 if kind in ('glob', 'relpath'):
319 if kind in ('glob', 'relpath'):
320 name = pathutil.canonpath(root, cwd, name, auditor)
320 name = pathutil.canonpath(root, cwd, name, auditor)
321 elif kind in ('relglob', 'path'):
321 elif kind in ('relglob', 'path'):
322 name = util.normpath(name)
322 name = util.normpath(name)
323 elif kind in ('listfile', 'listfile0'):
323 elif kind in ('listfile', 'listfile0'):
324 try:
324 try:
325 files = util.readfile(name)
325 files = util.readfile(name)
326 if kind == 'listfile0':
326 if kind == 'listfile0':
327 files = files.split('\0')
327 files = files.split('\0')
328 else:
328 else:
329 files = files.splitlines()
329 files = files.splitlines()
330 files = [f for f in files if f]
330 files = [f for f in files if f]
331 except EnvironmentError:
331 except EnvironmentError:
332 raise util.Abort(_("unable to read file list (%s)") % name)
332 raise util.Abort(_("unable to read file list (%s)") % name)
333 pats += _normalize(files, default, root, cwd, auditor)
333 pats += _normalize(files, default, root, cwd, auditor)
334 continue
334 continue
335
335
336 pats.append((kind, name))
336 pats.append((kind, name))
337 return pats
337 return pats
338
338
339 def _roots(patterns):
339 def _roots(patterns):
340 '''return roots and exact explicitly listed files from patterns
341
342 >>> _roots([('glob', 'g/*'), ('glob', 'g'), ('glob', 'g*')])
343 ['g', 'g', '.']
344 >>> _roots([('relpath', 'r'), ('path', 'p/p'), ('path', '')])
345 ['r', 'p/p', '.']
346 >>> _roots([('relglob', 'rg*'), ('re', 're/'), ('relre', 'rr')])
347 ['.', '.', '.']
348 '''
340 r = []
349 r = []
341 for kind, name in patterns:
350 for kind, name in patterns:
342 if kind == 'glob': # find the non-glob prefix
351 if kind == 'glob': # find the non-glob prefix
343 root = []
352 root = []
344 for p in name.split('/'):
353 for p in name.split('/'):
345 if '[' in p or '{' in p or '*' in p or '?' in p:
354 if '[' in p or '{' in p or '*' in p or '?' in p:
346 break
355 break
347 root.append(p)
356 root.append(p)
348 r.append('/'.join(root) or '.')
357 r.append('/'.join(root) or '.')
349 elif kind in ('relpath', 'path'):
358 elif kind in ('relpath', 'path'):
350 r.append(name or '.')
359 r.append(name or '.')
351 else: # relglob, re, relre
360 else: # relglob, re, relre
352 r.append('.')
361 r.append('.')
353 return r
362 return r
354
363
355 def _anypats(patterns):
364 def _anypats(patterns):
356 for kind, name in patterns:
365 for kind, name in patterns:
357 if kind in ('glob', 're', 'relglob', 'relre', 'set'):
366 if kind in ('glob', 're', 'relglob', 'relre', 'set'):
358 return True
367 return True
General Comments 0
You need to be logged in to leave comments. Login now