mercurial/match.py
author Matt Mackall <mpm@selenic.com>
Sun, 24 May 2009 02:56:14 -0500
changeset 8571 9f12e1a27a1b
parent 8570 7fe2012b3bd0
child 8572 dd46948a07fa
permissions -rw-r--r--
match: refactor matchfn generation
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
8231
5d4d88a4f5e6 match: add copyright and license header
Martin Geisler <mg@lazybytes.net>
parents: 8152
diff changeset
     1
# match.py - file name matching
5d4d88a4f5e6 match: add copyright and license header
Martin Geisler <mg@lazybytes.net>
parents: 8152
diff changeset
     2
#
5d4d88a4f5e6 match: add copyright and license header
Martin Geisler <mg@lazybytes.net>
parents: 8152
diff changeset
     3
#  Copyright 2008, 2009 Matt Mackall <mpm@selenic.com> and others
5d4d88a4f5e6 match: add copyright and license header
Martin Geisler <mg@lazybytes.net>
parents: 8152
diff changeset
     4
#
5d4d88a4f5e6 match: add copyright and license header
Martin Geisler <mg@lazybytes.net>
parents: 8152
diff changeset
     5
# This software may be used and distributed according to the terms of the
5d4d88a4f5e6 match: add copyright and license header
Martin Geisler <mg@lazybytes.net>
parents: 8152
diff changeset
     6
# GNU General Public License version 2, incorporated herein by reference.
5d4d88a4f5e6 match: add copyright and license header
Martin Geisler <mg@lazybytes.net>
parents: 8152
diff changeset
     7
8570
7fe2012b3bd0 match: move util match functions over
Matt Mackall <mpm@selenic.com>
parents: 8568
diff changeset
     8
import util, re
6576
69f3e9ac7c56 walk: introduce match objects
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
     9
6604
98b6e6f0e49b match: cleanup match classes a bit
Matt Mackall <mpm@selenic.com>
parents: 6596
diff changeset
    10
class _match(object):
98b6e6f0e49b match: cleanup match classes a bit
Matt Mackall <mpm@selenic.com>
parents: 6596
diff changeset
    11
    def __init__(self, root, cwd, files, mf, ap):
6576
69f3e9ac7c56 walk: introduce match objects
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    12
        self._root = root
69f3e9ac7c56 walk: introduce match objects
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    13
        self._cwd = cwd
6604
98b6e6f0e49b match: cleanup match classes a bit
Matt Mackall <mpm@selenic.com>
parents: 6596
diff changeset
    14
        self._files = files
8152
08e1baf924ca replace set-like dictionaries with real sets
Martin Geisler <mg@lazybytes.net>
parents: 6834
diff changeset
    15
        self._fmap = set(files)
6834
cbdfd08eabc9 dirstate.walk: speed up calling match function
Matt Mackall <mpm@selenic.com>
parents: 6604
diff changeset
    16
        self.matchfn = mf
6576
69f3e9ac7c56 walk: introduce match objects
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    17
        self._anypats = ap
69f3e9ac7c56 walk: introduce match objects
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    18
    def __call__(self, fn):
6834
cbdfd08eabc9 dirstate.walk: speed up calling match function
Matt Mackall <mpm@selenic.com>
parents: 6604
diff changeset
    19
        return self.matchfn(fn)
6576
69f3e9ac7c56 walk: introduce match objects
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    20
    def __iter__(self):
69f3e9ac7c56 walk: introduce match objects
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    21
        for f in self._files:
69f3e9ac7c56 walk: introduce match objects
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    22
            yield f
69f3e9ac7c56 walk: introduce match objects
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    23
    def bad(self, f, msg):
69f3e9ac7c56 walk: introduce match objects
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    24
        return True
69f3e9ac7c56 walk: introduce match objects
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    25
    def dir(self, f):
69f3e9ac7c56 walk: introduce match objects
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    26
        pass
69f3e9ac7c56 walk: introduce match objects
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    27
    def missing(self, f):
69f3e9ac7c56 walk: introduce match objects
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    28
        pass
69f3e9ac7c56 walk: introduce match objects
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    29
    def exact(self, f):
69f3e9ac7c56 walk: introduce match objects
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    30
        return f in self._fmap
69f3e9ac7c56 walk: introduce match objects
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    31
    def rel(self, f):
69f3e9ac7c56 walk: introduce match objects
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    32
        return util.pathto(self._root, self._cwd, f)
69f3e9ac7c56 walk: introduce match objects
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    33
    def files(self):
69f3e9ac7c56 walk: introduce match objects
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    34
        return self._files
69f3e9ac7c56 walk: introduce match objects
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    35
    def anypats(self):
69f3e9ac7c56 walk: introduce match objects
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    36
        return self._anypats
6596
7fe4610cf920 match: add always, never, and exact methods
Matt Mackall <mpm@selenic.com>
parents: 6578
diff changeset
    37
6604
98b6e6f0e49b match: cleanup match classes a bit
Matt Mackall <mpm@selenic.com>
parents: 6596
diff changeset
    38
class always(_match):
98b6e6f0e49b match: cleanup match classes a bit
Matt Mackall <mpm@selenic.com>
parents: 6596
diff changeset
    39
    def __init__(self, root, cwd):
98b6e6f0e49b match: cleanup match classes a bit
Matt Mackall <mpm@selenic.com>
parents: 6596
diff changeset
    40
        _match.__init__(self, root, cwd, [], lambda f: True, False)
98b6e6f0e49b match: cleanup match classes a bit
Matt Mackall <mpm@selenic.com>
parents: 6596
diff changeset
    41
98b6e6f0e49b match: cleanup match classes a bit
Matt Mackall <mpm@selenic.com>
parents: 6596
diff changeset
    42
class never(_match):
98b6e6f0e49b match: cleanup match classes a bit
Matt Mackall <mpm@selenic.com>
parents: 6596
diff changeset
    43
    def __init__(self, root, cwd):
98b6e6f0e49b match: cleanup match classes a bit
Matt Mackall <mpm@selenic.com>
parents: 6596
diff changeset
    44
        _match.__init__(self, root, cwd, [], lambda f: False, False)
6596
7fe4610cf920 match: add always, never, and exact methods
Matt Mackall <mpm@selenic.com>
parents: 6578
diff changeset
    45
6604
98b6e6f0e49b match: cleanup match classes a bit
Matt Mackall <mpm@selenic.com>
parents: 6596
diff changeset
    46
class exact(_match):
98b6e6f0e49b match: cleanup match classes a bit
Matt Mackall <mpm@selenic.com>
parents: 6596
diff changeset
    47
    def __init__(self, root, cwd, files):
8522
39fd67552297 match: use self.exact instead of lambda
Simon Heimberg <simohe@besonet.ch>
parents: 8231
diff changeset
    48
        _match.__init__(self, root, cwd, files, self.exact, False)
6596
7fe4610cf920 match: add always, never, and exact methods
Matt Mackall <mpm@selenic.com>
parents: 6578
diff changeset
    49
6604
98b6e6f0e49b match: cleanup match classes a bit
Matt Mackall <mpm@selenic.com>
parents: 6596
diff changeset
    50
class match(_match):
8567
fea40a677d43 match: add some default args
Matt Mackall <mpm@selenic.com>
parents: 8566
diff changeset
    51
    def __init__(self, root, cwd, patterns, include=[], exclude=[],
fea40a677d43 match: add some default args
Matt Mackall <mpm@selenic.com>
parents: 8566
diff changeset
    52
                 default='glob'):
8570
7fe2012b3bd0 match: move util match functions over
Matt Mackall <mpm@selenic.com>
parents: 8568
diff changeset
    53
        f, mf, ap = _matcher(root, cwd, patterns, include, exclude, default)
6604
98b6e6f0e49b match: cleanup match classes a bit
Matt Mackall <mpm@selenic.com>
parents: 6596
diff changeset
    54
        _match.__init__(self, root, cwd, f, mf, ap)
8568
4fa1618bf495 match: refactor patkind
Matt Mackall <mpm@selenic.com>
parents: 8567
diff changeset
    55
4fa1618bf495 match: refactor patkind
Matt Mackall <mpm@selenic.com>
parents: 8567
diff changeset
    56
def patkind(pat):
8570
7fe2012b3bd0 match: move util match functions over
Matt Mackall <mpm@selenic.com>
parents: 8568
diff changeset
    57
    return _patsplit(pat, None)[0]
7fe2012b3bd0 match: move util match functions over
Matt Mackall <mpm@selenic.com>
parents: 8568
diff changeset
    58
7fe2012b3bd0 match: move util match functions over
Matt Mackall <mpm@selenic.com>
parents: 8568
diff changeset
    59
def _patsplit(pat, default):
7fe2012b3bd0 match: move util match functions over
Matt Mackall <mpm@selenic.com>
parents: 8568
diff changeset
    60
    """Split a string into an optional pattern kind prefix and the
7fe2012b3bd0 match: move util match functions over
Matt Mackall <mpm@selenic.com>
parents: 8568
diff changeset
    61
    actual pattern."""
7fe2012b3bd0 match: move util match functions over
Matt Mackall <mpm@selenic.com>
parents: 8568
diff changeset
    62
    for prefix in 're', 'glob', 'path', 'relglob', 'relpath', 'relre':
7fe2012b3bd0 match: move util match functions over
Matt Mackall <mpm@selenic.com>
parents: 8568
diff changeset
    63
        if pat.startswith(prefix + ':'): return pat.split(':', 1)
7fe2012b3bd0 match: move util match functions over
Matt Mackall <mpm@selenic.com>
parents: 8568
diff changeset
    64
    return default, pat
7fe2012b3bd0 match: move util match functions over
Matt Mackall <mpm@selenic.com>
parents: 8568
diff changeset
    65
7fe2012b3bd0 match: move util match functions over
Matt Mackall <mpm@selenic.com>
parents: 8568
diff changeset
    66
_globchars = set('[{*?')
7fe2012b3bd0 match: move util match functions over
Matt Mackall <mpm@selenic.com>
parents: 8568
diff changeset
    67
7fe2012b3bd0 match: move util match functions over
Matt Mackall <mpm@selenic.com>
parents: 8568
diff changeset
    68
def _globre(pat, head='^', tail='$'):
7fe2012b3bd0 match: move util match functions over
Matt Mackall <mpm@selenic.com>
parents: 8568
diff changeset
    69
    "convert a glob pattern into a regexp"
7fe2012b3bd0 match: move util match functions over
Matt Mackall <mpm@selenic.com>
parents: 8568
diff changeset
    70
    i, n = 0, len(pat)
7fe2012b3bd0 match: move util match functions over
Matt Mackall <mpm@selenic.com>
parents: 8568
diff changeset
    71
    res = ''
7fe2012b3bd0 match: move util match functions over
Matt Mackall <mpm@selenic.com>
parents: 8568
diff changeset
    72
    group = 0
7fe2012b3bd0 match: move util match functions over
Matt Mackall <mpm@selenic.com>
parents: 8568
diff changeset
    73
    def peek(): return i < n and pat[i]
7fe2012b3bd0 match: move util match functions over
Matt Mackall <mpm@selenic.com>
parents: 8568
diff changeset
    74
    while i < n:
7fe2012b3bd0 match: move util match functions over
Matt Mackall <mpm@selenic.com>
parents: 8568
diff changeset
    75
        c = pat[i]
7fe2012b3bd0 match: move util match functions over
Matt Mackall <mpm@selenic.com>
parents: 8568
diff changeset
    76
        i = i+1
7fe2012b3bd0 match: move util match functions over
Matt Mackall <mpm@selenic.com>
parents: 8568
diff changeset
    77
        if c == '*':
7fe2012b3bd0 match: move util match functions over
Matt Mackall <mpm@selenic.com>
parents: 8568
diff changeset
    78
            if peek() == '*':
7fe2012b3bd0 match: move util match functions over
Matt Mackall <mpm@selenic.com>
parents: 8568
diff changeset
    79
                i += 1
7fe2012b3bd0 match: move util match functions over
Matt Mackall <mpm@selenic.com>
parents: 8568
diff changeset
    80
                res += '.*'
7fe2012b3bd0 match: move util match functions over
Matt Mackall <mpm@selenic.com>
parents: 8568
diff changeset
    81
            else:
7fe2012b3bd0 match: move util match functions over
Matt Mackall <mpm@selenic.com>
parents: 8568
diff changeset
    82
                res += '[^/]*'
7fe2012b3bd0 match: move util match functions over
Matt Mackall <mpm@selenic.com>
parents: 8568
diff changeset
    83
        elif c == '?':
7fe2012b3bd0 match: move util match functions over
Matt Mackall <mpm@selenic.com>
parents: 8568
diff changeset
    84
            res += '.'
7fe2012b3bd0 match: move util match functions over
Matt Mackall <mpm@selenic.com>
parents: 8568
diff changeset
    85
        elif c == '[':
7fe2012b3bd0 match: move util match functions over
Matt Mackall <mpm@selenic.com>
parents: 8568
diff changeset
    86
            j = i
7fe2012b3bd0 match: move util match functions over
Matt Mackall <mpm@selenic.com>
parents: 8568
diff changeset
    87
            if j < n and pat[j] in '!]':
7fe2012b3bd0 match: move util match functions over
Matt Mackall <mpm@selenic.com>
parents: 8568
diff changeset
    88
                j += 1
7fe2012b3bd0 match: move util match functions over
Matt Mackall <mpm@selenic.com>
parents: 8568
diff changeset
    89
            while j < n and pat[j] != ']':
7fe2012b3bd0 match: move util match functions over
Matt Mackall <mpm@selenic.com>
parents: 8568
diff changeset
    90
                j += 1
7fe2012b3bd0 match: move util match functions over
Matt Mackall <mpm@selenic.com>
parents: 8568
diff changeset
    91
            if j >= n:
7fe2012b3bd0 match: move util match functions over
Matt Mackall <mpm@selenic.com>
parents: 8568
diff changeset
    92
                res += '\\['
7fe2012b3bd0 match: move util match functions over
Matt Mackall <mpm@selenic.com>
parents: 8568
diff changeset
    93
            else:
7fe2012b3bd0 match: move util match functions over
Matt Mackall <mpm@selenic.com>
parents: 8568
diff changeset
    94
                stuff = pat[i:j].replace('\\','\\\\')
7fe2012b3bd0 match: move util match functions over
Matt Mackall <mpm@selenic.com>
parents: 8568
diff changeset
    95
                i = j + 1
7fe2012b3bd0 match: move util match functions over
Matt Mackall <mpm@selenic.com>
parents: 8568
diff changeset
    96
                if stuff[0] == '!':
7fe2012b3bd0 match: move util match functions over
Matt Mackall <mpm@selenic.com>
parents: 8568
diff changeset
    97
                    stuff = '^' + stuff[1:]
7fe2012b3bd0 match: move util match functions over
Matt Mackall <mpm@selenic.com>
parents: 8568
diff changeset
    98
                elif stuff[0] == '^':
7fe2012b3bd0 match: move util match functions over
Matt Mackall <mpm@selenic.com>
parents: 8568
diff changeset
    99
                    stuff = '\\' + stuff
7fe2012b3bd0 match: move util match functions over
Matt Mackall <mpm@selenic.com>
parents: 8568
diff changeset
   100
                res = '%s[%s]' % (res, stuff)
7fe2012b3bd0 match: move util match functions over
Matt Mackall <mpm@selenic.com>
parents: 8568
diff changeset
   101
        elif c == '{':
7fe2012b3bd0 match: move util match functions over
Matt Mackall <mpm@selenic.com>
parents: 8568
diff changeset
   102
            group += 1
7fe2012b3bd0 match: move util match functions over
Matt Mackall <mpm@selenic.com>
parents: 8568
diff changeset
   103
            res += '(?:'
7fe2012b3bd0 match: move util match functions over
Matt Mackall <mpm@selenic.com>
parents: 8568
diff changeset
   104
        elif c == '}' and group:
7fe2012b3bd0 match: move util match functions over
Matt Mackall <mpm@selenic.com>
parents: 8568
diff changeset
   105
            res += ')'
7fe2012b3bd0 match: move util match functions over
Matt Mackall <mpm@selenic.com>
parents: 8568
diff changeset
   106
            group -= 1
7fe2012b3bd0 match: move util match functions over
Matt Mackall <mpm@selenic.com>
parents: 8568
diff changeset
   107
        elif c == ',' and group:
7fe2012b3bd0 match: move util match functions over
Matt Mackall <mpm@selenic.com>
parents: 8568
diff changeset
   108
            res += '|'
7fe2012b3bd0 match: move util match functions over
Matt Mackall <mpm@selenic.com>
parents: 8568
diff changeset
   109
        elif c == '\\':
7fe2012b3bd0 match: move util match functions over
Matt Mackall <mpm@selenic.com>
parents: 8568
diff changeset
   110
            p = peek()
7fe2012b3bd0 match: move util match functions over
Matt Mackall <mpm@selenic.com>
parents: 8568
diff changeset
   111
            if p:
7fe2012b3bd0 match: move util match functions over
Matt Mackall <mpm@selenic.com>
parents: 8568
diff changeset
   112
                i += 1
7fe2012b3bd0 match: move util match functions over
Matt Mackall <mpm@selenic.com>
parents: 8568
diff changeset
   113
                res += re.escape(p)
7fe2012b3bd0 match: move util match functions over
Matt Mackall <mpm@selenic.com>
parents: 8568
diff changeset
   114
            else:
7fe2012b3bd0 match: move util match functions over
Matt Mackall <mpm@selenic.com>
parents: 8568
diff changeset
   115
                res += re.escape(c)
7fe2012b3bd0 match: move util match functions over
Matt Mackall <mpm@selenic.com>
parents: 8568
diff changeset
   116
        else:
7fe2012b3bd0 match: move util match functions over
Matt Mackall <mpm@selenic.com>
parents: 8568
diff changeset
   117
            res += re.escape(c)
7fe2012b3bd0 match: move util match functions over
Matt Mackall <mpm@selenic.com>
parents: 8568
diff changeset
   118
    return head + res + tail
7fe2012b3bd0 match: move util match functions over
Matt Mackall <mpm@selenic.com>
parents: 8568
diff changeset
   119
7fe2012b3bd0 match: move util match functions over
Matt Mackall <mpm@selenic.com>
parents: 8568
diff changeset
   120
def _matcher(canonroot, cwd='', names=[], inc=[], exc=[], dflt_pat='glob'):
7fe2012b3bd0 match: move util match functions over
Matt Mackall <mpm@selenic.com>
parents: 8568
diff changeset
   121
    """build a function to match a set of file patterns
7fe2012b3bd0 match: move util match functions over
Matt Mackall <mpm@selenic.com>
parents: 8568
diff changeset
   122
7fe2012b3bd0 match: move util match functions over
Matt Mackall <mpm@selenic.com>
parents: 8568
diff changeset
   123
    arguments:
7fe2012b3bd0 match: move util match functions over
Matt Mackall <mpm@selenic.com>
parents: 8568
diff changeset
   124
    canonroot - the canonical root of the tree you're matching against
7fe2012b3bd0 match: move util match functions over
Matt Mackall <mpm@selenic.com>
parents: 8568
diff changeset
   125
    cwd - the current working directory, if relevant
7fe2012b3bd0 match: move util match functions over
Matt Mackall <mpm@selenic.com>
parents: 8568
diff changeset
   126
    names - patterns to find
7fe2012b3bd0 match: move util match functions over
Matt Mackall <mpm@selenic.com>
parents: 8568
diff changeset
   127
    inc - patterns to include
7fe2012b3bd0 match: move util match functions over
Matt Mackall <mpm@selenic.com>
parents: 8568
diff changeset
   128
    exc - patterns to exclude
7fe2012b3bd0 match: move util match functions over
Matt Mackall <mpm@selenic.com>
parents: 8568
diff changeset
   129
    dflt_pat - if a pattern in names has no explicit type, assume this one
7fe2012b3bd0 match: move util match functions over
Matt Mackall <mpm@selenic.com>
parents: 8568
diff changeset
   130
7fe2012b3bd0 match: move util match functions over
Matt Mackall <mpm@selenic.com>
parents: 8568
diff changeset
   131
    a pattern is one of:
7fe2012b3bd0 match: move util match functions over
Matt Mackall <mpm@selenic.com>
parents: 8568
diff changeset
   132
    'glob:<glob>' - a glob relative to cwd
7fe2012b3bd0 match: move util match functions over
Matt Mackall <mpm@selenic.com>
parents: 8568
diff changeset
   133
    're:<regexp>' - a regular expression
7fe2012b3bd0 match: move util match functions over
Matt Mackall <mpm@selenic.com>
parents: 8568
diff changeset
   134
    'path:<path>' - a path relative to canonroot
7fe2012b3bd0 match: move util match functions over
Matt Mackall <mpm@selenic.com>
parents: 8568
diff changeset
   135
    'relglob:<glob>' - an unrooted glob (*.c matches C files in all dirs)
7fe2012b3bd0 match: move util match functions over
Matt Mackall <mpm@selenic.com>
parents: 8568
diff changeset
   136
    'relpath:<path>' - a path relative to cwd
7fe2012b3bd0 match: move util match functions over
Matt Mackall <mpm@selenic.com>
parents: 8568
diff changeset
   137
    'relre:<regexp>' - a regexp that doesn't have to match the start of a name
7fe2012b3bd0 match: move util match functions over
Matt Mackall <mpm@selenic.com>
parents: 8568
diff changeset
   138
    '<something>' - one of the cases above, selected by the dflt_pat argument
7fe2012b3bd0 match: move util match functions over
Matt Mackall <mpm@selenic.com>
parents: 8568
diff changeset
   139
7fe2012b3bd0 match: move util match functions over
Matt Mackall <mpm@selenic.com>
parents: 8568
diff changeset
   140
    returns:
7fe2012b3bd0 match: move util match functions over
Matt Mackall <mpm@selenic.com>
parents: 8568
diff changeset
   141
    a 3-tuple containing
7fe2012b3bd0 match: move util match functions over
Matt Mackall <mpm@selenic.com>
parents: 8568
diff changeset
   142
    - list of roots (places where one should start a recursive walk of the fs);
7fe2012b3bd0 match: move util match functions over
Matt Mackall <mpm@selenic.com>
parents: 8568
diff changeset
   143
      this often matches the explicit non-pattern names passed in, but also
7fe2012b3bd0 match: move util match functions over
Matt Mackall <mpm@selenic.com>
parents: 8568
diff changeset
   144
      includes the initial part of glob: patterns that has no glob characters
7fe2012b3bd0 match: move util match functions over
Matt Mackall <mpm@selenic.com>
parents: 8568
diff changeset
   145
    - a bool match(filename) function
7fe2012b3bd0 match: move util match functions over
Matt Mackall <mpm@selenic.com>
parents: 8568
diff changeset
   146
    - a bool indicating if any patterns were passed in
7fe2012b3bd0 match: move util match functions over
Matt Mackall <mpm@selenic.com>
parents: 8568
diff changeset
   147
    """
7fe2012b3bd0 match: move util match functions over
Matt Mackall <mpm@selenic.com>
parents: 8568
diff changeset
   148
7fe2012b3bd0 match: move util match functions over
Matt Mackall <mpm@selenic.com>
parents: 8568
diff changeset
   149
    # a common case: no patterns at all
7fe2012b3bd0 match: move util match functions over
Matt Mackall <mpm@selenic.com>
parents: 8568
diff changeset
   150
    if not names and not inc and not exc:
8571
9f12e1a27a1b match: refactor matchfn generation
Matt Mackall <mpm@selenic.com>
parents: 8570
diff changeset
   151
        return [], lambda f: True, False
8568
4fa1618bf495 match: refactor patkind
Matt Mackall <mpm@selenic.com>
parents: 8567
diff changeset
   152
8570
7fe2012b3bd0 match: move util match functions over
Matt Mackall <mpm@selenic.com>
parents: 8568
diff changeset
   153
    def contains_glob(name):
7fe2012b3bd0 match: move util match functions over
Matt Mackall <mpm@selenic.com>
parents: 8568
diff changeset
   154
        for c in name:
7fe2012b3bd0 match: move util match functions over
Matt Mackall <mpm@selenic.com>
parents: 8568
diff changeset
   155
            if c in _globchars: return True
7fe2012b3bd0 match: move util match functions over
Matt Mackall <mpm@selenic.com>
parents: 8568
diff changeset
   156
        return False
7fe2012b3bd0 match: move util match functions over
Matt Mackall <mpm@selenic.com>
parents: 8568
diff changeset
   157
7fe2012b3bd0 match: move util match functions over
Matt Mackall <mpm@selenic.com>
parents: 8568
diff changeset
   158
    def regex(kind, name, tail):
7fe2012b3bd0 match: move util match functions over
Matt Mackall <mpm@selenic.com>
parents: 8568
diff changeset
   159
        '''convert a pattern into a regular expression'''
7fe2012b3bd0 match: move util match functions over
Matt Mackall <mpm@selenic.com>
parents: 8568
diff changeset
   160
        if not name:
7fe2012b3bd0 match: move util match functions over
Matt Mackall <mpm@selenic.com>
parents: 8568
diff changeset
   161
            return ''
7fe2012b3bd0 match: move util match functions over
Matt Mackall <mpm@selenic.com>
parents: 8568
diff changeset
   162
        if kind == 're':
7fe2012b3bd0 match: move util match functions over
Matt Mackall <mpm@selenic.com>
parents: 8568
diff changeset
   163
            return name
7fe2012b3bd0 match: move util match functions over
Matt Mackall <mpm@selenic.com>
parents: 8568
diff changeset
   164
        elif kind == 'path':
7fe2012b3bd0 match: move util match functions over
Matt Mackall <mpm@selenic.com>
parents: 8568
diff changeset
   165
            return '^' + re.escape(name) + '(?:/|$)'
7fe2012b3bd0 match: move util match functions over
Matt Mackall <mpm@selenic.com>
parents: 8568
diff changeset
   166
        elif kind == 'relglob':
7fe2012b3bd0 match: move util match functions over
Matt Mackall <mpm@selenic.com>
parents: 8568
diff changeset
   167
            return _globre(name, '(?:|.*/)', tail)
7fe2012b3bd0 match: move util match functions over
Matt Mackall <mpm@selenic.com>
parents: 8568
diff changeset
   168
        elif kind == 'relpath':
7fe2012b3bd0 match: move util match functions over
Matt Mackall <mpm@selenic.com>
parents: 8568
diff changeset
   169
            return re.escape(name) + '(?:/|$)'
7fe2012b3bd0 match: move util match functions over
Matt Mackall <mpm@selenic.com>
parents: 8568
diff changeset
   170
        elif kind == 'relre':
7fe2012b3bd0 match: move util match functions over
Matt Mackall <mpm@selenic.com>
parents: 8568
diff changeset
   171
            if name.startswith('^'):
7fe2012b3bd0 match: move util match functions over
Matt Mackall <mpm@selenic.com>
parents: 8568
diff changeset
   172
                return name
7fe2012b3bd0 match: move util match functions over
Matt Mackall <mpm@selenic.com>
parents: 8568
diff changeset
   173
            return '.*' + name
7fe2012b3bd0 match: move util match functions over
Matt Mackall <mpm@selenic.com>
parents: 8568
diff changeset
   174
        return _globre(name, '', tail)
7fe2012b3bd0 match: move util match functions over
Matt Mackall <mpm@selenic.com>
parents: 8568
diff changeset
   175
7fe2012b3bd0 match: move util match functions over
Matt Mackall <mpm@selenic.com>
parents: 8568
diff changeset
   176
    def matchfn(pats, tail):
7fe2012b3bd0 match: move util match functions over
Matt Mackall <mpm@selenic.com>
parents: 8568
diff changeset
   177
        """build a matching function from a set of patterns"""
7fe2012b3bd0 match: move util match functions over
Matt Mackall <mpm@selenic.com>
parents: 8568
diff changeset
   178
        if not pats:
7fe2012b3bd0 match: move util match functions over
Matt Mackall <mpm@selenic.com>
parents: 8568
diff changeset
   179
            return
7fe2012b3bd0 match: move util match functions over
Matt Mackall <mpm@selenic.com>
parents: 8568
diff changeset
   180
        try:
7fe2012b3bd0 match: move util match functions over
Matt Mackall <mpm@selenic.com>
parents: 8568
diff changeset
   181
            pat = '(?:%s)' % '|'.join([regex(k, p, tail) for (k, p) in pats])
7fe2012b3bd0 match: move util match functions over
Matt Mackall <mpm@selenic.com>
parents: 8568
diff changeset
   182
            if len(pat) > 20000:
7fe2012b3bd0 match: move util match functions over
Matt Mackall <mpm@selenic.com>
parents: 8568
diff changeset
   183
                raise OverflowError()
7fe2012b3bd0 match: move util match functions over
Matt Mackall <mpm@selenic.com>
parents: 8568
diff changeset
   184
            return re.compile(pat).match
7fe2012b3bd0 match: move util match functions over
Matt Mackall <mpm@selenic.com>
parents: 8568
diff changeset
   185
        except OverflowError:
7fe2012b3bd0 match: move util match functions over
Matt Mackall <mpm@selenic.com>
parents: 8568
diff changeset
   186
            # We're using a Python with a tiny regex engine and we
7fe2012b3bd0 match: move util match functions over
Matt Mackall <mpm@selenic.com>
parents: 8568
diff changeset
   187
            # made it explode, so we'll divide the pattern list in two
7fe2012b3bd0 match: move util match functions over
Matt Mackall <mpm@selenic.com>
parents: 8568
diff changeset
   188
            # until it works
7fe2012b3bd0 match: move util match functions over
Matt Mackall <mpm@selenic.com>
parents: 8568
diff changeset
   189
            l = len(pats)
7fe2012b3bd0 match: move util match functions over
Matt Mackall <mpm@selenic.com>
parents: 8568
diff changeset
   190
            if l < 2:
7fe2012b3bd0 match: move util match functions over
Matt Mackall <mpm@selenic.com>
parents: 8568
diff changeset
   191
                raise
7fe2012b3bd0 match: move util match functions over
Matt Mackall <mpm@selenic.com>
parents: 8568
diff changeset
   192
            a, b = matchfn(pats[:l//2], tail), matchfn(pats[l//2:], tail)
7fe2012b3bd0 match: move util match functions over
Matt Mackall <mpm@selenic.com>
parents: 8568
diff changeset
   193
            return lambda s: a(s) or b(s)
7fe2012b3bd0 match: move util match functions over
Matt Mackall <mpm@selenic.com>
parents: 8568
diff changeset
   194
        except re.error:
7fe2012b3bd0 match: move util match functions over
Matt Mackall <mpm@selenic.com>
parents: 8568
diff changeset
   195
            for k, p in pats:
7fe2012b3bd0 match: move util match functions over
Matt Mackall <mpm@selenic.com>
parents: 8568
diff changeset
   196
                try:
7fe2012b3bd0 match: move util match functions over
Matt Mackall <mpm@selenic.com>
parents: 8568
diff changeset
   197
                    re.compile('(?:%s)' % regex(k, p, tail))
7fe2012b3bd0 match: move util match functions over
Matt Mackall <mpm@selenic.com>
parents: 8568
diff changeset
   198
                except re.error:
7fe2012b3bd0 match: move util match functions over
Matt Mackall <mpm@selenic.com>
parents: 8568
diff changeset
   199
                    raise util.Abort("invalid pattern (%s): %s" % (k, p))
7fe2012b3bd0 match: move util match functions over
Matt Mackall <mpm@selenic.com>
parents: 8568
diff changeset
   200
            raise util.Abort("invalid pattern")
7fe2012b3bd0 match: move util match functions over
Matt Mackall <mpm@selenic.com>
parents: 8568
diff changeset
   201
7fe2012b3bd0 match: move util match functions over
Matt Mackall <mpm@selenic.com>
parents: 8568
diff changeset
   202
    def globprefix(pat):
7fe2012b3bd0 match: move util match functions over
Matt Mackall <mpm@selenic.com>
parents: 8568
diff changeset
   203
        '''return the non-glob prefix of a path, e.g. foo/* -> foo'''
7fe2012b3bd0 match: move util match functions over
Matt Mackall <mpm@selenic.com>
parents: 8568
diff changeset
   204
        root = []
7fe2012b3bd0 match: move util match functions over
Matt Mackall <mpm@selenic.com>
parents: 8568
diff changeset
   205
        for p in pat.split('/'):
7fe2012b3bd0 match: move util match functions over
Matt Mackall <mpm@selenic.com>
parents: 8568
diff changeset
   206
            if contains_glob(p): break
7fe2012b3bd0 match: move util match functions over
Matt Mackall <mpm@selenic.com>
parents: 8568
diff changeset
   207
            root.append(p)
7fe2012b3bd0 match: move util match functions over
Matt Mackall <mpm@selenic.com>
parents: 8568
diff changeset
   208
        return '/'.join(root) or '.'
7fe2012b3bd0 match: move util match functions over
Matt Mackall <mpm@selenic.com>
parents: 8568
diff changeset
   209
7fe2012b3bd0 match: move util match functions over
Matt Mackall <mpm@selenic.com>
parents: 8568
diff changeset
   210
    def normalizepats(names, default):
7fe2012b3bd0 match: move util match functions over
Matt Mackall <mpm@selenic.com>
parents: 8568
diff changeset
   211
        pats = []
7fe2012b3bd0 match: move util match functions over
Matt Mackall <mpm@selenic.com>
parents: 8568
diff changeset
   212
        roots = []
7fe2012b3bd0 match: move util match functions over
Matt Mackall <mpm@selenic.com>
parents: 8568
diff changeset
   213
        anypats = False
7fe2012b3bd0 match: move util match functions over
Matt Mackall <mpm@selenic.com>
parents: 8568
diff changeset
   214
        for kind, name in [_patsplit(p, default) for p in names]:
7fe2012b3bd0 match: move util match functions over
Matt Mackall <mpm@selenic.com>
parents: 8568
diff changeset
   215
            if kind in ('glob', 'relpath'):
7fe2012b3bd0 match: move util match functions over
Matt Mackall <mpm@selenic.com>
parents: 8568
diff changeset
   216
                name = util.canonpath(canonroot, cwd, name)
7fe2012b3bd0 match: move util match functions over
Matt Mackall <mpm@selenic.com>
parents: 8568
diff changeset
   217
            elif kind in ('relglob', 'path'):
7fe2012b3bd0 match: move util match functions over
Matt Mackall <mpm@selenic.com>
parents: 8568
diff changeset
   218
                name = util.normpath(name)
7fe2012b3bd0 match: move util match functions over
Matt Mackall <mpm@selenic.com>
parents: 8568
diff changeset
   219
7fe2012b3bd0 match: move util match functions over
Matt Mackall <mpm@selenic.com>
parents: 8568
diff changeset
   220
            pats.append((kind, name))
7fe2012b3bd0 match: move util match functions over
Matt Mackall <mpm@selenic.com>
parents: 8568
diff changeset
   221
7fe2012b3bd0 match: move util match functions over
Matt Mackall <mpm@selenic.com>
parents: 8568
diff changeset
   222
            if kind in ('glob', 're', 'relglob', 'relre'):
7fe2012b3bd0 match: move util match functions over
Matt Mackall <mpm@selenic.com>
parents: 8568
diff changeset
   223
                anypats = True
7fe2012b3bd0 match: move util match functions over
Matt Mackall <mpm@selenic.com>
parents: 8568
diff changeset
   224
7fe2012b3bd0 match: move util match functions over
Matt Mackall <mpm@selenic.com>
parents: 8568
diff changeset
   225
            if kind == 'glob':
7fe2012b3bd0 match: move util match functions over
Matt Mackall <mpm@selenic.com>
parents: 8568
diff changeset
   226
                root = globprefix(name)
7fe2012b3bd0 match: move util match functions over
Matt Mackall <mpm@selenic.com>
parents: 8568
diff changeset
   227
                roots.append(root)
7fe2012b3bd0 match: move util match functions over
Matt Mackall <mpm@selenic.com>
parents: 8568
diff changeset
   228
            elif kind in ('relpath', 'path'):
7fe2012b3bd0 match: move util match functions over
Matt Mackall <mpm@selenic.com>
parents: 8568
diff changeset
   229
                roots.append(name or '.')
7fe2012b3bd0 match: move util match functions over
Matt Mackall <mpm@selenic.com>
parents: 8568
diff changeset
   230
            elif kind == 'relglob':
7fe2012b3bd0 match: move util match functions over
Matt Mackall <mpm@selenic.com>
parents: 8568
diff changeset
   231
                roots.append('.')
7fe2012b3bd0 match: move util match functions over
Matt Mackall <mpm@selenic.com>
parents: 8568
diff changeset
   232
        return roots, pats, anypats
7fe2012b3bd0 match: move util match functions over
Matt Mackall <mpm@selenic.com>
parents: 8568
diff changeset
   233
7fe2012b3bd0 match: move util match functions over
Matt Mackall <mpm@selenic.com>
parents: 8568
diff changeset
   234
    roots, pats, anypats = normalizepats(names, dflt_pat)
7fe2012b3bd0 match: move util match functions over
Matt Mackall <mpm@selenic.com>
parents: 8568
diff changeset
   235
8571
9f12e1a27a1b match: refactor matchfn generation
Matt Mackall <mpm@selenic.com>
parents: 8570
diff changeset
   236
    patmatch = matchfn(pats, '$')
8570
7fe2012b3bd0 match: move util match functions over
Matt Mackall <mpm@selenic.com>
parents: 8568
diff changeset
   237
    if inc:
7fe2012b3bd0 match: move util match functions over
Matt Mackall <mpm@selenic.com>
parents: 8568
diff changeset
   238
        dummy, inckinds, dummy = normalizepats(inc, 'glob')
7fe2012b3bd0 match: move util match functions over
Matt Mackall <mpm@selenic.com>
parents: 8568
diff changeset
   239
        incmatch = matchfn(inckinds, '(?:/|$)')
7fe2012b3bd0 match: move util match functions over
Matt Mackall <mpm@selenic.com>
parents: 8568
diff changeset
   240
    if exc:
7fe2012b3bd0 match: move util match functions over
Matt Mackall <mpm@selenic.com>
parents: 8568
diff changeset
   241
        dummy, exckinds, dummy = normalizepats(exc, 'glob')
7fe2012b3bd0 match: move util match functions over
Matt Mackall <mpm@selenic.com>
parents: 8568
diff changeset
   242
        excmatch = matchfn(exckinds, '(?:/|$)')
7fe2012b3bd0 match: move util match functions over
Matt Mackall <mpm@selenic.com>
parents: 8568
diff changeset
   243
8571
9f12e1a27a1b match: refactor matchfn generation
Matt Mackall <mpm@selenic.com>
parents: 8570
diff changeset
   244
    if names:
9f12e1a27a1b match: refactor matchfn generation
Matt Mackall <mpm@selenic.com>
parents: 8570
diff changeset
   245
        if inc:
9f12e1a27a1b match: refactor matchfn generation
Matt Mackall <mpm@selenic.com>
parents: 8570
diff changeset
   246
            if exc:
9f12e1a27a1b match: refactor matchfn generation
Matt Mackall <mpm@selenic.com>
parents: 8570
diff changeset
   247
                m = lambda f: incmatch(f) and not excmatch(f) and patmatch(f)
9f12e1a27a1b match: refactor matchfn generation
Matt Mackall <mpm@selenic.com>
parents: 8570
diff changeset
   248
            else:
9f12e1a27a1b match: refactor matchfn generation
Matt Mackall <mpm@selenic.com>
parents: 8570
diff changeset
   249
                m = lambda f: incmatch(f) and patmatch(f)
9f12e1a27a1b match: refactor matchfn generation
Matt Mackall <mpm@selenic.com>
parents: 8570
diff changeset
   250
        else:
9f12e1a27a1b match: refactor matchfn generation
Matt Mackall <mpm@selenic.com>
parents: 8570
diff changeset
   251
            if exc:
9f12e1a27a1b match: refactor matchfn generation
Matt Mackall <mpm@selenic.com>
parents: 8570
diff changeset
   252
                m = lambda f: not excmatch(f) and patmatch(f)
9f12e1a27a1b match: refactor matchfn generation
Matt Mackall <mpm@selenic.com>
parents: 8570
diff changeset
   253
            else:
9f12e1a27a1b match: refactor matchfn generation
Matt Mackall <mpm@selenic.com>
parents: 8570
diff changeset
   254
                m = patmatch
8570
7fe2012b3bd0 match: move util match functions over
Matt Mackall <mpm@selenic.com>
parents: 8568
diff changeset
   255
    else:
8571
9f12e1a27a1b match: refactor matchfn generation
Matt Mackall <mpm@selenic.com>
parents: 8570
diff changeset
   256
        if inc:
9f12e1a27a1b match: refactor matchfn generation
Matt Mackall <mpm@selenic.com>
parents: 8570
diff changeset
   257
            if exc:
9f12e1a27a1b match: refactor matchfn generation
Matt Mackall <mpm@selenic.com>
parents: 8570
diff changeset
   258
                m = lambda f: incmatch(f) and not excmatch(f)
9f12e1a27a1b match: refactor matchfn generation
Matt Mackall <mpm@selenic.com>
parents: 8570
diff changeset
   259
            else:
9f12e1a27a1b match: refactor matchfn generation
Matt Mackall <mpm@selenic.com>
parents: 8570
diff changeset
   260
                m = incmatch
9f12e1a27a1b match: refactor matchfn generation
Matt Mackall <mpm@selenic.com>
parents: 8570
diff changeset
   261
        else:
9f12e1a27a1b match: refactor matchfn generation
Matt Mackall <mpm@selenic.com>
parents: 8570
diff changeset
   262
            if exc:
9f12e1a27a1b match: refactor matchfn generation
Matt Mackall <mpm@selenic.com>
parents: 8570
diff changeset
   263
                m = lambda f: not excmatch(f)
9f12e1a27a1b match: refactor matchfn generation
Matt Mackall <mpm@selenic.com>
parents: 8570
diff changeset
   264
            else:
9f12e1a27a1b match: refactor matchfn generation
Matt Mackall <mpm@selenic.com>
parents: 8570
diff changeset
   265
                m = lambda f: True
8570
7fe2012b3bd0 match: move util match functions over
Matt Mackall <mpm@selenic.com>
parents: 8568
diff changeset
   266
8571
9f12e1a27a1b match: refactor matchfn generation
Matt Mackall <mpm@selenic.com>
parents: 8570
diff changeset
   267
    return (roots, m, (inc or exc or anypats) and True)