mercurial/match.py
author Matt Mackall <mpm@selenic.com>
Mon, 12 May 2008 11:37:08 -0500
changeset 6596 7fe4610cf920
parent 6578 f242d3684f83
child 6604 98b6e6f0e49b
permissions -rw-r--r--
match: add always, never, and exact methods
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
6576
69f3e9ac7c56 walk: introduce match objects
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
     1
import util
69f3e9ac7c56 walk: introduce match objects
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
     2
69f3e9ac7c56 walk: introduce match objects
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
     3
class match(object):
69f3e9ac7c56 walk: introduce match objects
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
     4
    def __init__(self, root, cwd, patterns, include, exclude, default):
69f3e9ac7c56 walk: introduce match objects
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
     5
        self._patterns = patterns
69f3e9ac7c56 walk: introduce match objects
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
     6
        self._root = root
69f3e9ac7c56 walk: introduce match objects
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
     7
        self._cwd = cwd
69f3e9ac7c56 walk: introduce match objects
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
     8
        self._include = include
69f3e9ac7c56 walk: introduce match objects
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
     9
        self._exclude = exclude
6578
f242d3684f83 walk: begin refactoring badmatch handling
Matt Mackall <mpm@selenic.com>
parents: 6576
diff changeset
    10
        f, mf, ap = util.matcher(root, cwd, patterns, include, exclude,
f242d3684f83 walk: begin refactoring badmatch handling
Matt Mackall <mpm@selenic.com>
parents: 6576
diff changeset
    11
                                 self.src(), default)
6576
69f3e9ac7c56 walk: introduce match objects
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    12
        self._files = f
69f3e9ac7c56 walk: introduce match objects
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    13
        self._fmap = dict.fromkeys(f)
69f3e9ac7c56 walk: introduce match objects
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    14
        self._matchfn = mf
69f3e9ac7c56 walk: introduce match objects
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    15
        self._anypats = ap
69f3e9ac7c56 walk: introduce match objects
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    16
    def src(self):
69f3e9ac7c56 walk: introduce match objects
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    17
        return None
69f3e9ac7c56 walk: introduce match objects
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    18
    def __call__(self, fn):
69f3e9ac7c56 walk: introduce match objects
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    19
        return self._matchfn(fn)
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
7fe4610cf920 match: add always, never, and exact methods
Matt Mackall <mpm@selenic.com>
parents: 6578
diff changeset
    38
def always(root, cwd):
7fe4610cf920 match: add always, never, and exact methods
Matt Mackall <mpm@selenic.com>
parents: 6578
diff changeset
    39
    return match(root, cwd, [], None, None, 'relpath')
7fe4610cf920 match: add always, never, and exact methods
Matt Mackall <mpm@selenic.com>
parents: 6578
diff changeset
    40
7fe4610cf920 match: add always, never, and exact methods
Matt Mackall <mpm@selenic.com>
parents: 6578
diff changeset
    41
def never(root, cwd):
7fe4610cf920 match: add always, never, and exact methods
Matt Mackall <mpm@selenic.com>
parents: 6578
diff changeset
    42
    m = match(root, cwd, [], None, None, 'relpath')
7fe4610cf920 match: add always, never, and exact methods
Matt Mackall <mpm@selenic.com>
parents: 6578
diff changeset
    43
    m._matchfn = lambda f: False
7fe4610cf920 match: add always, never, and exact methods
Matt Mackall <mpm@selenic.com>
parents: 6578
diff changeset
    44
    return m
7fe4610cf920 match: add always, never, and exact methods
Matt Mackall <mpm@selenic.com>
parents: 6578
diff changeset
    45
7fe4610cf920 match: add always, never, and exact methods
Matt Mackall <mpm@selenic.com>
parents: 6578
diff changeset
    46
def exact(root, cwd, files):
7fe4610cf920 match: add always, never, and exact methods
Matt Mackall <mpm@selenic.com>
parents: 6578
diff changeset
    47
    m = always(root, cwd)
7fe4610cf920 match: add always, never, and exact methods
Matt Mackall <mpm@selenic.com>
parents: 6578
diff changeset
    48
    m._files = files
7fe4610cf920 match: add always, never, and exact methods
Matt Mackall <mpm@selenic.com>
parents: 6578
diff changeset
    49
    m._fmap = dict.fromkeys(files)
7fe4610cf920 match: add always, never, and exact methods
Matt Mackall <mpm@selenic.com>
parents: 6578
diff changeset
    50
    m._matchfn = m._fmap.has_key
7fe4610cf920 match: add always, never, and exact methods
Matt Mackall <mpm@selenic.com>
parents: 6578
diff changeset
    51
    return m