changeset 6576:69f3e9ac7c56

walk: introduce match objects
author Matt Mackall <mpm@selenic.com>
date Mon, 12 May 2008 11:37:07 -0500
parents e08e0367ba15
children 569761919450
files mercurial/cmdutil.py mercurial/localrepo.py mercurial/match.py
diffstat 3 files changed, 45 insertions(+), 11 deletions(-) [+]
line wrap: on
line diff
--- a/mercurial/cmdutil.py	Mon May 12 11:37:07 2008 -0500
+++ b/mercurial/cmdutil.py	Mon May 12 11:37:07 2008 -0500
@@ -8,7 +8,7 @@
 from node import hex, nullid, nullrev, short
 from i18n import _
 import os, sys, bisect, stat
-import mdiff, bdiff, util, templater, templatefilters, patch, errno
+import mdiff, bdiff, util, templater, templatefilters, patch, errno, match
 
 revrangesep = ':'
 
@@ -224,21 +224,18 @@
                 mode)
 
 def matchpats(repo, pats=[], opts={}, globbed=False, default='relpath'):
-    pats = pats or []
     if not globbed and default == 'relpath':
         pats = util.expand_glob(pats or [])
-    return util.matcher(repo.root, repo.getcwd(), pats, opts.get('include'),
-                        opts.get('exclude'), None, default)
+    m = match.match(repo.root, repo.getcwd(), pats, opts.get('include'),
+                    opts.get('exclude'), default)
+    return m.files(), m, m.anypats()
 
 def walk(repo, pats=[], opts={}, node=None, badmatch=None, globbed=False,
          default='relpath'):
-    files, matchfn, anypats = matchpats(repo, pats, opts, globbed=globbed,
-                                        default=default)
-    exact = dict.fromkeys(files)
-    cwd = repo.getcwd()
-    for src, fn in repo.walk(node=node, files=files, match=matchfn,
+    dummy, m, dummy = matchpats(repo, pats, opts, globbed, default)
+    for src, fn in repo.walk(node=node, files=m.files(), match=m,
                              badmatch=badmatch):
-        yield src, fn, repo.pathto(fn, cwd), fn in exact
+        yield src, fn, m.rel(fn), m.exact(fn)
 
 def findrenames(repo, added=None, removed=None, threshold=0.5):
     '''find renamed files -- yields (before, after, score) tuples'''
--- a/mercurial/localrepo.py	Mon May 12 11:37:07 2008 -0500
+++ b/mercurial/localrepo.py	Mon May 12 11:37:07 2008 -0500
@@ -786,7 +786,7 @@
                 update_dirstate = True
 
                 if (not force and p2 != nullid and
-                    (files or match != util.always)):
+                    (match.files() or match.anypats())):
                     raise util.Abort(_('cannot partially commit a merge '
                                        '(do not specify files or patterns)'))
             else:
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mercurial/match.py	Mon May 12 11:37:07 2008 -0500
@@ -0,0 +1,37 @@
+import util
+
+class match(object):
+    def __init__(self, root, cwd, patterns, include, exclude, default):
+        self._patterns = patterns
+        self._root = root
+        self._cwd = cwd
+        self._include = include
+        self._exclude = exclude
+        f, mf, ap = util.matcher(self._root, self._cwd, self._patterns,
+                                 self._include, self._exclude, self.src(),
+                                 default)
+        self._files = f
+        self._fmap = dict.fromkeys(f)
+        self._matchfn = mf
+        self._anypats = ap
+    def src(self):
+        return None
+    def __call__(self, fn):
+        return self._matchfn(fn)
+    def __iter__(self):
+        for f in self._files:
+            yield f
+    def bad(self, f, msg):
+        return True
+    def dir(self, f):
+        pass
+    def missing(self, f):
+        pass
+    def exact(self, f):
+        return f in self._fmap
+    def rel(self, f):
+        return util.pathto(self._root, self._cwd, f)
+    def files(self):
+        return self._files
+    def anypats(self):
+        return self._anypats