9 import util |
9 import util |
10 from i18n import _ |
10 from i18n import _ |
11 |
11 |
12 class match(object): |
12 class match(object): |
13 def __init__(self, root, cwd, patterns, include=[], exclude=[], |
13 def __init__(self, root, cwd, patterns, include=[], exclude=[], |
14 default='glob', exact=False): |
14 default='glob', exact=False, auditor=None): |
15 """build an object to match a set of file patterns |
15 """build an object to match a set of file patterns |
16 |
16 |
17 arguments: |
17 arguments: |
18 root - the canonical root of the tree you're matching against |
18 root - the canonical root of the tree you're matching against |
19 cwd - the current working directory, if relevant |
19 cwd - the current working directory, if relevant |
37 self._cwd = cwd |
37 self._cwd = cwd |
38 self._files = [] |
38 self._files = [] |
39 self._anypats = bool(include or exclude) |
39 self._anypats = bool(include or exclude) |
40 |
40 |
41 if include: |
41 if include: |
42 im = _buildmatch(_normalize(include, 'glob', root, cwd), '(?:/|$)') |
42 im = _buildmatch(_normalize(include, 'glob', root, cwd, auditor), |
|
43 '(?:/|$)') |
43 if exclude: |
44 if exclude: |
44 em = _buildmatch(_normalize(exclude, 'glob', root, cwd), '(?:/|$)') |
45 em = _buildmatch(_normalize(exclude, 'glob', root, cwd, auditor), |
|
46 '(?:/|$)') |
45 if exact: |
47 if exact: |
46 self._files = patterns |
48 self._files = patterns |
47 pm = self.exact |
49 pm = self.exact |
48 elif patterns: |
50 elif patterns: |
49 pats = _normalize(patterns, default, root, cwd) |
51 pats = _normalize(patterns, default, root, cwd, auditor) |
50 self._files = _roots(pats) |
52 self._files = _roots(pats) |
51 self._anypats = self._anypats or _anypats(pats) |
53 self._anypats = self._anypats or _anypats(pats) |
52 pm = _buildmatch(pats, '$') |
54 pm = _buildmatch(pats, '$') |
53 |
55 |
54 if patterns or exact: |
56 if patterns or exact: |
216 re.compile('(?:%s)' % _regex(k, p, tail)) |
218 re.compile('(?:%s)' % _regex(k, p, tail)) |
217 except re.error: |
219 except re.error: |
218 raise util.Abort(_("invalid pattern (%s): %s") % (k, p)) |
220 raise util.Abort(_("invalid pattern (%s): %s") % (k, p)) |
219 raise util.Abort(_("invalid pattern")) |
221 raise util.Abort(_("invalid pattern")) |
220 |
222 |
221 def _normalize(names, default, root, cwd): |
223 def _normalize(names, default, root, cwd, auditor): |
222 pats = [] |
224 pats = [] |
223 for kind, name in [_patsplit(p, default) for p in names]: |
225 for kind, name in [_patsplit(p, default) for p in names]: |
224 if kind in ('glob', 'relpath'): |
226 if kind in ('glob', 'relpath'): |
225 name = util.canonpath(root, cwd, name) |
227 name = util.canonpath(root, cwd, name, auditor) |
226 elif kind in ('relglob', 'path'): |
228 elif kind in ('relglob', 'path'): |
227 name = util.normpath(name) |
229 name = util.normpath(name) |
228 |
230 |
229 pats.append((kind, name)) |
231 pats.append((kind, name)) |
230 return pats |
232 return pats |