hgkw/keyword.py
changeset 261 28e64bebc298
parent 260 af9ddafeeb96
child 262 397a5957cc48
--- a/hgkw/keyword.py	Sat Oct 06 16:56:14 2007 +0200
+++ b/hgkw/keyword.py	Sat Oct 06 20:13:18 2007 +0200
@@ -300,46 +300,31 @@
             return t2 != text
         return revlog.revlog.cmp(self, node, text)
 
-def _keywordmatcher(ui, repo):
-    '''Collects include/exclude filename patterns for expansion
-    candidates of current configuration. Returns filename matching
-    function if include patterns exist, None otherwise.'''
-    inc, exc = [], ['.hgtags']
-    for pat, opt in ui.configitems('keyword'):
-        if opt != 'ignore':
-            inc.append(pat)
-        else:
-            exc.append(pat)
-    if inc:
-        return util.matcher(repo.root, inc=inc, exc=exc)[1]
-    return None
-
-def _weedcandidates(man, kwfmatcher, candidates):
+def _weedcandidates(ui, man, candidates):
     '''Weeds out files that do not match keyword file matcher,
     are not tracked, or are links.'''
     files = man.keys()
     if candidates:
-        return [f for f in candidates if kwfmatcher(f)
+        return [f for f in candidates if ui.kwfmatcher(f)
                 and f in files and not man.linkf(f)]
-    return [f for f in files if kwfmatcher(f) and not man.linkf(f)]
+    return [f for f in files if ui.kwfmatcher(f) and not man.linkf(f)]
 
 def _overwrite(ui, repo, files, expand):
     '''Expands/shrinks keywords in working directory.'''
+    if not hasattr(ui, 'kwfmatcher'):
+        ui.warn(_('no files configured for keyword expansion\n'))
+        return
     wlock = lock = None
     try:
         wlock = repo.wlock()
         lock = repo.lock()
         bail_if_changed(repo)
         ctx = repo.changectx()
-        kwfmatcher = _keywordmatcher(ui, repo)
-        if kwfmatcher is None:
-            ui.warn(_('no files configured for keyword expansion\n'))
-            return
         man = ctx.manifest()
         if files:
             cwd = repo.getcwd()
             files = [util.canonpath(repo.root, cwd, f) for f in files]
-        files = _weedcandidates(man, kwfmatcher, files)
+        files = _weedcandidates(ui, man, files)
         if files:
             kwt = kwtemplater(ui, repo, expand, node=ctx.node())
             # 3rd argument sets commit to False
@@ -371,13 +356,12 @@
     Crosscheck which files in working directory
     are matched by [keyword] config patterns.
     '''
-    kwfmatcher = _keywordmatcher(ui, repo)
-    if kwfmatcher is not None:
+    if hasattr(ui, 'kwfmatcher'):
         ctx = repo.workingctx()
-        man = ctx.manifest()
+        man = ctx.manifest().copy()
         for f in ctx.unknown():
             del man[f]
-        files = _weedcandidates(man, kwfmatcher, None)
+        files = _weedcandidates(ui, man, None)
         files.sort()
         ui.write('\n'.join(files) + '\n')
 
@@ -475,15 +459,22 @@
     if not repo.local() or _parse(ui, sys.argv[1:])[0] in nokwcommands:
         return
 
-    kwfmatcher = _keywordmatcher(ui, repo)
-    if kwfmatcher is None:
+    inc, exc = [], ['.hgtags']
+    for pat, opt in ui.configitems('keyword'):
+        if opt != 'ignore':
+            inc.append(pat)
+        else:
+            exc.append(pat)
+    if not inc:
         return
 
+    ui.kwfmatcher = util.matcher(repo.root, inc=inc, exc=exc)[1]
+
     class kwrepo(repo.__class__):
         def file(self, f, kwexp=True, kwcnt=False, kwmatch=False):
             if f[0] == '/':
                 f = f[1:]
-            if kwmatch or kwfmatcher(f):
+            if kwmatch or ui.kwfmatcher(f):
                 kwt = kwtemplater(ui, self, kwexp, path=f)
                 return kwfilelog(self.sopener, f, kwt, kwcnt)
             return filelog.filelog(self.sopener, f)
@@ -524,7 +515,7 @@
                 if node is not None:
                     cl = self.changelog.read(node)
                     mn = self.manifest.read(cl[0])
-                    candidates = _weedcandidates(mn, kwfmatcher, cl[3])
+                    candidates = _weedcandidates(ui, mn, cl[3])
                     if candidates:
                         # 3rd argument sets expansion to True
                         kwt = kwtemplater(ui, self, True, node=node)