match: fold _matcher into match.__init__
authorMatt Mackall <mpm@selenic.com>
Sun, 24 May 2009 02:56:14 -0500
changeset 8581 101d305c1d0b
parent 8580 f648c096f6d0
child 8582 a4c199e12b5a
match: fold _matcher into match.__init__
mercurial/match.py
--- a/mercurial/match.py	Sun May 24 02:56:14 2009 -0500
+++ b/mercurial/match.py	Sun May 24 02:56:14 2009 -0500
@@ -50,8 +50,63 @@
 class match(_match):
     def __init__(self, root, cwd, patterns, include=[], exclude=[],
                  default='glob'):
-        f, mf, ap = _matcher(root, cwd, patterns, include, exclude, default)
-        _match.__init__(self, root, cwd, f, mf, ap)
+        """build an object to match a set of file patterns
+
+        arguments:
+        root - the canonical root of the tree you're matching against
+        cwd - the current working directory, if relevant
+        patterns - patterns to find
+        include - patterns to include
+        exclude - patterns to exclude
+        default - if a pattern in names has no explicit type, assume this one
+
+        a pattern is one of:
+        'glob:<glob>' - a glob relative to cwd
+        're:<regexp>' - a regular expression
+        'path:<path>' - a path relative to canonroot
+        'relglob:<glob>' - an unrooted glob (*.c matches C files in all dirs)
+        'relpath:<path>' - a path relative to cwd
+        'relre:<regexp>' - a regexp that doesn't have to match the start of a name
+        '<something>' - one of the cases above, selected by the dflt_pat argument
+        """
+
+        roots = []
+        anypats = bool(include or exclude)
+
+        if patterns:
+            pats = _normalize(patterns, default, root, cwd)
+            roots = _roots(pats)
+            anypats = anypats or _anypats(pats)
+            pm = _buildmatch(pats, '$')
+        if include:
+            im = _buildmatch(_normalize(include, 'glob', root, cwd), '(?:/|$)')
+        if exclude:
+            em = _buildmatch(_normalize(exclude, 'glob', root, cwd), '(?:/|$)')
+
+        if patterns:
+            if include:
+                if exclude:
+                    m = lambda f: im(f) and not em(f) and pm(f)
+                else:
+                    m = lambda f: im(f) and pm(f)
+            else:
+                if exclude:
+                    m = lambda f: not em(f) and pm(f)
+                else:
+                    m = pm
+        else:
+            if include:
+                if exclude:
+                    m = lambda f: im(f) and not em(f)
+                else:
+                    m = im
+            else:
+                if exclude:
+                    m = lambda f: not em(f)
+                else:
+                    m = lambda f: True
+
+        _match.__init__(self, root, cwd, roots, m, anypats)
 
 def patkind(pat):
     return _patsplit(pat, None)[0]
@@ -194,70 +249,3 @@
     for kind, name in patterns:
         if kind in ('glob', 're', 'relglob', 'relre'):
             return True
-
-def _matcher(root, cwd='', names=[], inc=[], exc=[], dflt_pat='glob'):
-    """build a function to match a set of file patterns
-
-    arguments:
-    root - the canonical root of the tree you're matching against
-    cwd - the current working directory, if relevant
-    names - patterns to find
-    inc - patterns to include
-    exc - patterns to exclude
-    dflt_pat - if a pattern in names has no explicit type, assume this one
-
-    a pattern is one of:
-    'glob:<glob>' - a glob relative to cwd
-    're:<regexp>' - a regular expression
-    'path:<path>' - a path relative to canonroot
-    'relglob:<glob>' - an unrooted glob (*.c matches C files in all dirs)
-    'relpath:<path>' - a path relative to cwd
-    'relre:<regexp>' - a regexp that doesn't have to match the start of a name
-    '<something>' - one of the cases above, selected by the dflt_pat argument
-
-    returns:
-    a 3-tuple containing
-    - list of roots (places where one should start a recursive walk of the fs);
-      this often matches the explicit non-pattern names passed in, but also
-      includes the initial part of glob: patterns that has no glob characters
-    - a bool match(filename) function
-    - a bool indicating if any patterns were passed in
-    """
-
-    roots = []
-    anypats = bool(inc or exc)
-
-    if names:
-        pats = _normalize(names, dflt_pat, root, cwd)
-        roots = _roots(pats)
-        anypats = anypats or _anypats(pats)
-        patmatch = _buildmatch(pats, '$')
-    if inc:
-        incmatch = _buildmatch(_normalize(inc, 'glob', root, cwd), '(?:/|$)')
-    if exc:
-        excmatch = _buildmatch(_normalize(exc, 'glob', root, cwd), '(?:/|$)')
-
-    if names:
-        if inc:
-            if exc:
-                m = lambda f: incmatch(f) and not excmatch(f) and patmatch(f)
-            else:
-                m = lambda f: incmatch(f) and patmatch(f)
-        else:
-            if exc:
-                m = lambda f: not excmatch(f) and patmatch(f)
-            else:
-                m = patmatch
-    else:
-        if inc:
-            if exc:
-                m = lambda f: incmatch(f) and not excmatch(f)
-            else:
-                m = incmatch
-        else:
-            if exc:
-                m = lambda f: not excmatch(f)
-            else:
-                m = lambda f: True
-
-    return (roots, m, anypats)