diff mercurial/match.py @ 12165:b7fbf24c8a93

match: add narrowmatcher class This class can be used to adapt an existing match object to a new match object that only cares about paths within a certain subdirectory.
author Martin Geisler <mg@lazybytes.net>
date Fri, 03 Sep 2010 12:58:51 +0200
parents 505f64bb58af
children 69e43c0515f2
line wrap: on
line diff
--- a/mercurial/match.py	Fri Sep 03 12:58:51 2010 +0200
+++ b/mercurial/match.py	Fri Sep 03 12:58:51 2010 +0200
@@ -110,6 +110,37 @@
     def __init__(self, root, cwd):
         match.__init__(self, root, cwd, [])
 
+class narrowmatcher(match):
+    """Adapt a matcher to work on a subdirectory only.
+
+    The paths are remapped to remove/insert the path as needed:
+
+    >>> m1 = match('root', '', ['a.txt', 'sub/b.txt'])
+    >>> m2 = narrowmatcher('sub', m1)
+    >>> bool(m2('a.txt'))
+    False
+    >>> bool(m2('b.txt'))
+    True
+    >>> bool(m2.matchfn('a.txt'))
+    False
+    >>> bool(m2.matchfn('b.txt'))
+    True
+    >>> m2.files()
+    ['b.txt']
+    >>> m2.exact('b.txt')
+    True
+    """
+
+    def __init__(self, path, matcher):
+        self._path = path
+        self._matcher = matcher
+
+        self._files = [f[len(path) + 1:] for f in matcher._files
+                       if f.startswith(path + "/")]
+        self._anypats = matcher._anypats
+        self.matchfn = lambda fn: matcher.matchfn(self._path + "/" + fn)
+        self._fmap = set(self._files)
+
 def patkind(pat):
     return _patsplit(pat, None)[0]