changeset 6579:0159b7a36184

walk: pass match object to cmdutil.walk - introduce cmdutil.match - change args to cmdutil.walk - create match objects for walk calls
author Matt Mackall <mpm@selenic.com>
date Mon, 12 May 2008 11:37:07 -0500
parents f242d3684f83
children da2a20d2ba3d
files mercurial/cmdutil.py mercurial/commands.py tests/test-revert.out
diffstat 3 files changed, 49 insertions(+), 38 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,8 @@
 from node import hex, nullid, nullrev, short
 from i18n import _
 import os, sys, bisect, stat
-import mdiff, bdiff, util, templater, templatefilters, patch, errno, match
+import mdiff, bdiff, util, templater, templatefilters, patch, errno
+import match as _match
 
 revrangesep = ':'
 
@@ -223,24 +224,24 @@
                               pathname),
                 mode)
 
-def matchpats(repo, pats=[], opts={}, globbed=False, default='relpath'):
+def match(repo, pats=[], opts={}, globbed=False, default='relpath'):
     if not globbed and default == 'relpath':
         pats = util.expand_glob(pats or [])
-    m = match.match(repo.root, repo.getcwd(), pats, opts.get('include'),
-                    opts.get('exclude'), default)
+    m = _match.match(repo.root, repo.getcwd(), pats,
+                    opts.get('include'), opts.get('exclude'), default)
     def badfn(f, msg):
         repo.ui.warn("%s: %s\n" % (m.rel(f), msg))
         return False
     m.bad = badfn
+    return m
+
+def matchpats(repo, pats=[], opts={}, globbed=False, default='relpath'):
+    m = match(repo, pats, opts, globbed, default)
     return m.files(), m, m.anypats()
 
-def walk(repo, pats=[], opts={}, node=None, badmatch=None, globbed=False,
-         default='relpath'):
-    dummy, m, dummy = matchpats(repo, pats, opts, globbed, default)
-    if badmatch:
-        m.bad = badmatch
-    for src, fn in repo.walk(node, m):
-        yield src, fn, m.rel(fn), m.exact(fn)
+def walk(repo, match, node=None):
+    for src, fn in repo.walk(node, match):
+        yield src, fn, match.rel(fn), match.exact(fn)
 
 def findrenames(repo, added=None, removed=None, threshold=0.5):
     '''find renamed files -- yields (before, after, score) tuples'''
@@ -277,7 +278,8 @@
         similarity = float(opts.get('similarity') or 0)
     add, remove = [], []
     mapping = {}
-    for src, abs, rel, exact in walk(repo, pats, opts):
+    m = match(repo, pats, opts)
+    for src, abs, rel, exact in walk(repo, m):
         target = repo.wjoin(abs)
         if src == 'f' and abs not in repo.dirstate:
             add.append(abs)
@@ -316,7 +318,8 @@
 
     def walkpat(pat):
         srcs = []
-        for tag, abs, rel, exact in walk(repo, [pat], opts, globbed=True):
+        m = match(repo, [pat], opts, globbed=True)
+        for tag, abs, rel, exact in walk(repo, m):
             state = repo.dirstate[abs]
             if state in '?r':
                 if exact and state == '?':
--- a/mercurial/commands.py	Mon May 12 11:37:07 2008 -0500
+++ b/mercurial/commands.py	Mon May 12 11:37:07 2008 -0500
@@ -31,8 +31,9 @@
     rejected = None
     exacts = {}
     names = []
-    for src, abs, rel, exact in cmdutil.walk(repo, pats, opts,
-                                             badmatch=lambda x,y: True):
+    m = cmdutil.match(repo, pats, opts)
+    m.bad = lambda x,y: True
+    for src, abs, rel, exact in cmdutil.walk(repo, m):
         if exact:
             if ui.verbose:
                 ui.status(_('adding %s\n') % rel)
@@ -108,8 +109,8 @@
 
     ctx = repo.changectx(opts['rev'])
 
-    for src, abs, rel, exact in cmdutil.walk(repo, pats, opts,
-                                             node=ctx.node()):
+    m = cmdutil.match(repo, pats, opts)
+    for src, abs, rel, exact in cmdutil.walk(repo, m, ctx.node()):
         fctx = ctx.filectx(abs)
         if not opts['text'] and util.binary(fctx.data()):
             ui.write(_("%s: binary file\n") % ((pats and rel) or abs))
@@ -160,7 +161,7 @@
     dest = cmdutil.make_filename(repo, dest, node)
     if os.path.realpath(dest) == repo.root:
         raise util.Abort(_('repository root cannot be destination'))
-    dummy, matchfn, dummy = cmdutil.matchpats(repo, [], opts)
+    matchfn = cmdutil.match(repo, [], opts)
     kind = opts.get('type') or 'files'
     prefix = opts['prefix']
     if dest == '-':
@@ -487,8 +488,8 @@
     """
     ctx = repo.changectx(opts['rev'])
     err = 1
-    for src, abs, rel, exact in cmdutil.walk(repo, (file1,) + pats, opts,
-                                             ctx.node()):
+    m = cmdutil.match(repo, (file1,) + pats, opts)
+    for src, abs, rel, exact in cmdutil.walk(repo, m, ctx.node()):
         fp = cmdutil.make_file(repo, opts['output'], ctx.node(), pathname=abs)
         data = ctx.filectx(abs).data()
         if opts.get('decode'):
@@ -913,18 +914,19 @@
     """dump rename information"""
 
     ctx = repo.changectx(opts.get('rev', 'tip'))
-    for src, abs, rel, exact in cmdutil.walk(repo, (file1,) + pats, opts,
-                                             ctx.node()):
+    m = cmdutil.match(repo, (file1,) + pats, opts)
+    for src, abs, rel, exact in cmdutil.walk(repo, m, ctx.node()):
         fctx = ctx.filectx(abs)
-        m = fctx.filelog().renamed(fctx.filenode())
-        if m:
-            ui.write(_("%s renamed from %s:%s\n") % (rel, m[0], hex(m[1])))
+        o = fctx.filelog().renamed(fctx.filenode())
+        if o:
+            ui.write(_("%s renamed from %s:%s\n") % (rel, o[0], hex(o[1])))
         else:
             ui.write(_("%s not renamed\n") % rel)
 
 def debugwalk(ui, repo, *pats, **opts):
     """show how files match on given patterns"""
-    items = list(cmdutil.walk(repo, pats, opts))
+    m = cmdutil.match(repo, pats, opts)
+    items = list(cmdutil.walk(repo, m))
     if not items:
         return
     fmt = '%%s  %%-%ds  %%-%ds  %%s' % (
@@ -1695,9 +1697,9 @@
         node = None
 
     ret = 1
-    for src, abs, rel, exact in cmdutil.walk(repo, pats, opts, node=node,
-                                             badmatch=lambda x,y: True,
-                                             default='relglob'):
+    m = cmdutil.match(repo, pats, opts, default='relglob')
+    m.bad = lambda x,y: True
+    for src, abs, rel, exact in cmdutil.walk(repo, m, node):
         if src == 'b':
             continue
         if not node and abs not in repo.dirstate:
@@ -2185,7 +2187,8 @@
     modified, added, removed, deleted, unknown = mardu
 
     remove, forget = [], []
-    for src, abs, rel, exact in cmdutil.walk(repo, pats, opts):
+    m = cmdutil.match(repo, pats, opts)
+    for src, abs, rel, exact in cmdutil.walk(repo, m):
 
         reason = None
         if abs in removed or abs in unknown:
@@ -2339,15 +2342,19 @@
     try:
         # walk dirstate.
         files = []
-        for src, abs, rel, exact in cmdutil.walk(repo, pats, opts,
-                                                 badmatch=mf.has_key):
+
+        m = cmdutil.match(repo, pats, opts)
+        def bad(f, msg):
+            if f not in mf:
+                repo.ui.warn("%s: %s\n" % (m.rel(f), msg))
+            return False
+        m.bad = bad
+        for src, abs, rel, exact in cmdutil.walk(repo, m):
             names[abs] = (rel, exact)
-            if src != 'b':
-                files.append(abs)
 
         # walk target manifest.
 
-        def badmatch(path):
+        def badfn(path, msg):
             if path in names:
                 return True
             path_ = path + '/'
@@ -2356,8 +2363,9 @@
                     return True
             return False
 
-        for src, abs, rel, exact in cmdutil.walk(repo, pats, opts, node=node,
-                                                 badmatch=badmatch):
+        m = cmdutil.match(repo, pats, opts)
+        m.bad = badfn
+        for src, abs, rel, exact in cmdutil.walk(repo, m, node=node):
             if abs in names or src == 'b':
                 continue
             names[abs] = (rel, exact)
--- a/tests/test-revert.out	Mon May 12 11:37:07 2008 -0500
+++ b/tests/test-revert.out	Mon May 12 11:37:07 2008 -0500
@@ -37,7 +37,7 @@
 %% should say file not managed
 file not managed: q
 %% should say file not found
-notfound: No such file in rev 095eacd0c0d7
+notfound: No such file or directory
 A z
 ? e.orig
 %% should add a, remove d, forget z