mercurial/match.py
author Matt Mackall <mpm@selenic.com>
Sun, 24 May 2009 02:56:14 -0500
changeset 8566 744d6322b05b
parent 8522 39fd67552297
child 8567 fea40a677d43
permissions -rw-r--r--
match: change all users of util.matcher to match.match
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
6576
69f3e9ac7c56 walk: introduce match objects
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
     8
import util
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):
98b6e6f0e49b match: cleanup match classes a bit
Matt Mackall <mpm@selenic.com>
parents: 6596
diff changeset
    51
    def __init__(self, root, cwd, patterns, include, exclude, default):
98b6e6f0e49b match: cleanup match classes a bit
Matt Mackall <mpm@selenic.com>
parents: 6596
diff changeset
    52
        f, mf, ap = util.matcher(root, cwd, patterns, include, exclude,
8566
744d6322b05b match: change all users of util.matcher to match.match
Matt Mackall <mpm@selenic.com>
parents: 8522
diff changeset
    53
                                 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)