hgkw/keyword.py
changeset 354 8e3364294d0c
parent 353 159bf80a4301
child 355 b3caec747375
--- a/hgkw/keyword.py	Tue Jan 29 17:32:10 2008 +0100
+++ b/hgkw/keyword.py	Tue Jan 29 17:32:15 2008 +0100
@@ -92,8 +92,6 @@
     '''Returns hgdate in cvs-like UTC format.'''
     return time.strftime('%Y/%m/%d %H:%M:%S', time.gmtime(date[0]))
 
-_kwtemplater = None
-
 class kwtemplater(object):
     '''
     Sets up keyword templates, corresponding keyword regex, and
@@ -173,28 +171,17 @@
             return data
         return self.re_kw.sub(r'$\1$', data)
 
-
 # store original patch.patchfile.__init__
 _patchfile_init = patch.patchfile.__init__
 
-def _kwpatchfile_init(self, ui, fname, missing=False):
-    '''Monkeypatch/wrap patch.patchfile.__init__ to avoid
-    rejects or conflicts due to expanded keywords in working dir.'''
-    _patchfile_init(self, ui, fname, missing=missing)
 
-    if _kwtemplater.matcher(self.fname):
-        # shrink keywords read from working dir
-        kwshrunk = _kwtemplater.shrink(''.join(self.lines))
-        self.lines = kwshrunk.splitlines(True)
-
-
-def _iskwfile(f, link):
-    return not link(f) and _kwtemplater.matcher(f)
+def _iskwfile(f, link, kwt):
+    return not link(f) and kwt.matcher(f)
 
 def _status(ui, repo, *pats, **opts):
     '''Bails out if [keyword] configuration is not active.
     Returns status of working directory.'''
-    if _kwtemplater:
+    if hasattr(repo, '_kwt'):
         files, match, anypats = cmdutil.matchpats(repo, pats, opts)
         return repo.status(files=files, match=match, list_clean=True)
     if ui.configitems('keyword'):
@@ -212,7 +199,7 @@
     else:
         # kwexpand/kwshrink
         notify = ui.note
-    candidates = [f for f in files if _iskwfile(f, mf.linkf)]
+    candidates = [f for f in files if _iskwfile(f, mf.linkf, repo._kwt)]
     if candidates:
         candidates.sort()
         action = expand and 'expanding' or 'shrinking'
@@ -341,7 +328,7 @@
     if opts.get('untracked'):
         files += unknown
     files.sort()
-    kwfiles = [f for f in files if _iskwfile(f, repo._link)]
+    kwfiles = [f for f in files if _iskwfile(f, repo._link, repo._kwt)]
     cwd = pats and repo.getcwd() or ''
     kwfstats = not opts.get('ignore') and (('K', kwfiles),) or ()
     if opts.get('all') or opts.get('ignore'):
@@ -376,30 +363,27 @@
     if not inc:
         return
 
-    global _kwtemplater
-    _kwtemplater = kwtemplater(ui, repo, inc, exc)
-
     class kwrepo(repo.__class__):
         def _wreadkwct(self, filename, expand, ctx, node):
             '''Reads filename and returns tuple of data with keywords
             expanded/shrunk and count of keywords (for _overwrite).'''
             data = super(kwrepo, self).wread(filename)
-            return _kwtemplater.process(filename, data, expand, ctx, node)
+            return self._kwt.process(filename, data, expand, ctx, node)
 
         def wread(self, filename):
             data = super(kwrepo, self).wread(filename)
-            if _kwtemplater.matcher(filename):
-                return _kwtemplater.shrink(data)
+            if self._kwt.matcher(filename):
+                return self._kwt.shrink(data)
             return data
 
         def wwrite(self, filename, data, flags, overwrite=False):
-            if not overwrite and _kwtemplater.matcher(filename):
-                data = _kwtemplater.expand(filename, data)
+            if not overwrite and self._kwt.matcher(filename):
+                data = self._kwt.expand(filename, data)
             super(kwrepo, self).wwrite(filename, data, flags)
 
         def wwritedata(self, filename, data):
-            if _kwtemplater.matcher(filename):
-                data = _kwtemplater.expand(filename, data)
+            if self._kwt.matcher(filename):
+                data = self._kwt.expand(filename, data)
             return super(kwrepo, self).wwritedata(filename, data)
 
         def commit(self, files=None, text='', user=None, date=None,
@@ -444,8 +428,20 @@
             finally:
                 del wlock, lock
 
+    kwt = kwrepo._kwt = kwtemplater(ui, repo, inc, exc)
+
+    def kwpatchfile_init(self, ui, fname, missing=False):
+        '''Monkeypatch/wrap patch.patchfile.__init__ to avoid
+        rejects or conflicts due to expanded keywords in working dir.'''
+        _patchfile_init(self, ui, fname, missing=missing)
+
+        if kwt.matcher(self.fname):
+            # shrink keywords read from working dir
+            kwshrunk = kwt.shrink(''.join(self.lines))
+            self.lines = kwshrunk.splitlines(True)
+
     repo.__class__ = kwrepo
-    patch.patchfile.__init__ = _kwpatchfile_init
+    patch.patchfile.__init__ = kwpatchfile_init
 
 
 cmdtable = {