[PATCH] match: support reading pattern lists from files

Steve Borho steve at borho.org
Thu Dec 23 15:13:03 CST 2010


# HG changeset patch
# User Steve Borho <steve at borho.org>
# Date 1293138744 21600
# Node ID 6fef56ff2f032f9ab436bcaf73b31471cef308b8
# Parent  e3b87fb34d002b55ca9bc469b3fcbc7585091112
match: support reading pattern lists from files

diff -r e3b87fb34d00 -r 6fef56ff2f03 mercurial/help/patterns.txt
--- a/mercurial/help/patterns.txt	Wed Dec 22 13:16:03 2010 -0600
+++ b/mercurial/help/patterns.txt	Thu Dec 23 15:12:24 2010 -0600
@@ -20,6 +20,11 @@
 To use a Perl/Python regular expression, start a name with ``re:``.
 Regexp pattern matching is anchored at the root of the repository.
 
+To read name patterns from a file, use ``file:`` or ``file0:``.  The
+latter expects null delimited patterns while the former expects line
+feeds.  Each string read from the file is itself treated as a file
+pattern.
+
 Plain examples::
 
   path:foo/bar   a name bar in a directory named foo in the root
@@ -39,3 +44,8 @@
 Regexp examples::
 
   re:.*\.c$      any name ending in ".c", anywhere in the repository
+
+File examples::
+
+  file:list.txt  read list from list.txt with one file pattern per line
+  file0:list.txt read list from list.txt with null byte delimiters
diff -r e3b87fb34d00 -r 6fef56ff2f03 mercurial/match.py
--- a/mercurial/match.py	Wed Dec 22 13:16:03 2010 -0600
+++ b/mercurial/match.py	Thu Dec 23 15:12:24 2010 -0600
@@ -161,7 +161,8 @@
     actual pattern."""
     if ':' in pat:
         kind, val = pat.split(':', 1)
-        if kind in ('re', 'glob', 'path', 'relglob', 'relpath', 'relre'):
+        if kind in ('re', 'glob', 'path', 'relglob', 'relpath',
+                    'relre', 'file', 'file0'):
             return kind, val
     return default, pat
 
@@ -268,10 +269,19 @@
     for kind, name in [_patsplit(p, default) for p in names]:
         if kind in ('glob', 'relpath'):
             name = util.canonpath(root, cwd, name, auditor)
+            pats.append((kind, name))
         elif kind in ('relglob', 'path'):
             name = util.normpath(name)
+            pats.append((kind, name))
+        elif kind in ('file', 'file0'):
+            delimiter = kind == 'file0' and '\0' or '\n'
+            try:
+                files = open(name, 'r').read().split(delimiter)
+                files = [f for f in files if f]
+            except EnvironmentError:
+                raise util.Abort(_("unable to read file list (%s)") % name)
+            pats += _normalize(files, default, root, cwd, auditor)
 
-        pats.append((kind, name))
     return pats
 
 def _roots(patterns):


More information about the Mercurial-devel mailing list