##// END OF EJS Templates
match: introduce basic fileset support
Matt Mackall -
r14675:cfc89398 default
parent child Browse files
Show More
@@ -6,9 +6,24 b''
6 6 # GNU General Public License version 2 or any later version.
7 7
8 8 import re
9 import scmutil, util
9 import scmutil, util, fileset
10 10 from i18n import _
11 11
12 def _expandsets(pats, ctx):
13 '''convert set: patterns into a list of files in the given context'''
14 fset = set()
15 other = []
16
17 for kind, expr in pats:
18 if kind == 'set':
19 if not ctx:
20 raise util.Abort("fileset expression with no context")
21 s = fileset.getfileset(ctx, expr)
22 fset.update(s)
23 continue
24 other.append((kind, expr))
25 return fset, other
26
12 27 class match(object):
13 28 def __init__(self, root, cwd, patterns, include=[], exclude=[],
14 29 default='glob', exact=False, auditor=None, ctx=None):
@@ -30,9 +45,11 b' class match(object):'
30 45 'relglob:<glob>' - an unrooted glob (*.c matches C files in all dirs)
31 46 'relpath:<path>' - a path relative to cwd
32 47 'relre:<regexp>' - a regexp that needn't match the start of a name
48 'set:<fileset>' - a fileset expression
33 49 '<something>' - a pattern of the specified default type
34 50 """
35 51
52 self._ctx = None
36 53 self._root = root
37 54 self._cwd = cwd
38 55 self._files = []
@@ -41,10 +58,10 b' class match(object):'
41 58
42 59 if include:
43 60 pats = _normalize(include, 'glob', root, cwd, auditor)
44 self.includepat, im = _buildmatch(pats, '(?:/|$)')
61 self.includepat, im = _buildmatch(ctx, pats, '(?:/|$)')
45 62 if exclude:
46 63 pats = _normalize(exclude, 'glob', root, cwd, auditor)
47 self.excludepat, em = _buildmatch(pats, '(?:/|$)')
64 self.excludepat, em = _buildmatch(ctx, pats, '(?:/|$)')
48 65 if exact:
49 66 self._files = patterns
50 67 pm = self.exact
@@ -52,7 +69,7 b' class match(object):'
52 69 pats = _normalize(patterns, default, root, cwd, auditor)
53 70 self._files = _roots(pats)
54 71 self._anypats = self._anypats or _anypats(pats)
55 self.patternspat, pm = _buildmatch(pats, '$')
72 self.patternspat, pm = _buildmatch(ctx, pats, '$')
56 73
57 74 if patterns or exact:
58 75 if include:
@@ -163,7 +180,7 b' def _patsplit(pat, default):'
163 180 if ':' in pat:
164 181 kind, val = pat.split(':', 1)
165 182 if kind in ('re', 'glob', 'path', 'relglob', 'relpath', 'relre',
166 'listfile', 'listfile0'):
183 'listfile', 'listfile0', 'set'):
167 184 return kind, val
168 185 return default, pat
169 186
@@ -241,7 +258,17 b' def _regex(kind, name, tail):'
241 258 return '.*' + name
242 259 return _globre(name) + tail
243 260
244 def _buildmatch(pats, tail):
261 def _buildmatch(ctx, pats, tail):
262 fset, pats = _expandsets(pats, ctx)
263 if not pats:
264 return "", fset.__contains__
265
266 pat, mf = _buildregexmatch(pats, tail)
267 if fset:
268 return pat, lambda f: f in fset or mf(f)
269 return pat, mf
270
271 def _buildregexmatch(pats, tail):
245 272 """build a matching function from a set of patterns"""
246 273 try:
247 274 pat = '(?:%s)' % '|'.join([_regex(k, p, tail) for (k, p) in pats])
General Comments 0
You need to be logged in to leave comments. Login now