[PATCH 02 of 19 STABLE] match: add 'convert()' to match for encapsulation of its internal information

FUJIWARA Katsunori foozy at lares.dti.ne.jp
Mon Feb 27 10:46:27 UTC 2012


# HG changeset patch
# User FUJIWARA Katsunori <foozy at lares.dti.ne.jp>
# Date 1330335216 -32400
# Branch stable
# Node ID 5cb66b4539fc04204657121f0f09bfd4335227bb
# Parent  177c261dc6e60be5d14007d678a8ceca8e7385e9
match: add 'convert()' to match for encapsulation of its internal information

this patch adds 'convert()' to match for creating new match object
which bases on itself, without accessing/modifying internal
information of it from outside.

diff -r 177c261dc6e6 -r 5cb66b4539fc mercurial/match.py
--- a/mercurial/match.py	Mon Feb 27 18:33:36 2012 +0900
+++ b/mercurial/match.py	Mon Feb 27 18:33:36 2012 +0900
@@ -5,6 +5,7 @@
 # This software may be used and distributed according to the terms of the
 # GNU General Public License version 2 or any later version.
 
+import copy
 import re
 import scmutil, util, fileset
 from i18n import _
@@ -119,6 +120,61 @@
     def anypats(self):
         return self._anypats
 
+    def convert(self, filefn=None, matchfn=None):
+        '''create new matcher which bases on itself.
+
+        new matcher has:
+
+        - files returned from 'filefn'
+        - specified 'matchfn' to examine filename matching
+
+        'filefn' is invoked with 'files' of based one at creation.
+        if 'filefn' is not specified, original files are used.
+
+        'machfn' should take two parameters:
+          1. matchfn of based matcher ('original matchfn')
+          2. filename to be examined
+
+        if 'matchfn' is not specified to 'convert()', 'original
+        matchfn' is used to examine filename matching.
+
+        >>> m1 = match('root', '', ['a.txt', 'sub/b.txt'])
+        >>> prefix = 'sub/'
+        >>> def filefn(files):
+        ...     for f in files:
+        ...         if f.startswith(prefix):
+        ...             yield f[len(prefix):]
+        >>> def matchfn(orgmatchfn, f):
+        ...     return orgmatchfn('%s%s' % (prefix, f))
+        >>> m2 = m1.convert(filefn, matchfn)
+        >>> 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
+        >>> m2.rel('b.txt')
+        'b.txt'
+
+        '''
+        m = copy.copy(self)
+        m._files = []
+        if filefn:
+            for f in filefn(self._files):
+                m._files.append(f)
+        else:
+            m._files.extend(self._files)
+        m._fmap = set(m._files)
+        if matchfn:
+            m.matchfn = lambda f: matchfn(self.matchfn, f)
+        return m
+
 class exact(match):
     def __init__(self, root, cwd, files):
         match.__init__(self, root, cwd, files, exact = True)



More information about the Mercurial-devel mailing list