[PATCH 4 of 6] match: add subrepomatcher class

Martin Geisler mg at lazybytes.net
Mon Aug 30 17:01:36 CDT 2010


# HG changeset patch
# User Martin Geisler <mg at lazybytes.net>
# Date 1283205545 -7200
# Node ID d84f8da539f995c47437933d8161c6664736dd01
# Parent  46b14be7f61d52329e4543a13aa92316dae40330
match: add subrepomatcher class

diff --git a/mercurial/match.py b/mercurial/match.py
--- a/mercurial/match.py
+++ b/mercurial/match.py
@@ -109,6 +109,37 @@
     def __init__(self, root, cwd):
         match.__init__(self, root, cwd, [])
 
+class subrepomatcher(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 = subrepomatcher('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]
 
diff --git a/tests/test-doctest.py b/tests/test-doctest.py
--- a/tests/test-doctest.py
+++ b/tests/test-doctest.py
@@ -15,6 +15,9 @@
 import mercurial.util
 doctest.testmod(mercurial.util)
 
+import mercurial.match
+doctest.testmod(mercurial.match)
+
 import mercurial.dagparser
 doctest.testmod(mercurial.dagparser, optionflags=doctest.NORMALIZE_WHITESPACE)
 


More information about the Mercurial-devel mailing list