hgkw/keyword.py
branchkwmap-templates
changeset 144 19b3d1de3db0
parent 143 3485b0ef99c4
child 145 1c2cefa97b96
--- a/hgkw/keyword.py	Wed Feb 14 02:50:06 2007 +0100
+++ b/hgkw/keyword.py	Wed Feb 14 03:03:16 2007 +0100
@@ -161,10 +161,42 @@
         if files:
             self.repo.dirstate.update(files, 'n')
 
+class kwfilelog(filelog.filelog):
+    '''
+    Superclass over filelog to customize its read, add, cmp methods.
+    Keywords are "stored" unexpanded, and expanded on reading.
+    '''
+    def __init__(self, opener, path, kwtemplater,
+                 defversion=revlog.REVLOG_DEFAULT_VERSION):
+        super(kwfilelog, self).__init__(opener, path, defversion)
+        self.path = path
+        self.kwtemplater = kwtemplater
+
+    def read(self, node):
+        '''Substitutes keywords when reading filelog.'''
+        data = super(kwfilelog, self).read(node)
+        return self.kwtemplater.expand(self.path, node, self, data)
+
+    def add(self, text, meta, tr, link, p1=None, p2=None):
+        '''Removes keyword substitutions when adding to filelog.'''
+        text = self.kwtemplater.shrink(text)
+        return super(kwfilelog, self).add(text,
+                        meta, tr, link, p1=p1, p2=p2)
+
+    def cmp(self, node, text):
+        '''Removes keyword substitutions for comparison.'''
+        text = self.kwtemplater.shrink(text)
+        return super(kwfilelog, self).cmp(node, text)
+
 
 def reposetup(ui, repo):
-    '''Sets up repo, and filelog especially, as kwrepo and kwfilelog
-    for keyword substitution. This is done for local repos only.'''
+    '''Sets up repo as kwrepo for keyword substitution.
+    Overrides file method to return kwfilelog instead of filelog
+    if file matches user configuration.
+    Wraps commit to overwrite configured files with updated
+    keyword substitutions.
+    This is done for local repos only, and only if there are
+    files configured at all for keyword substitution.'''
 
     if not repo.local():
         return
@@ -184,7 +216,12 @@
         def file(self, f):
             if f[0] == '/':
                 f = f[1:]
-            return filelog.filelog(self.sopener, f, self, self.revlogversion)
+            # only use kwfilelog when needed
+            if self.kwfmatcher(f):
+                kwt = kwtemplater(self.ui, self)
+                return kwfilelog(self.sopener, f, kwt, self.revlogversion)
+            else:
+                return filelog.filelog(self.sopener, f, self.revlogversion)
 
         def commit(self, files=None, text="", user=None, date=None,
                 match=util.always, force=False, lock=None, wlock=None,
@@ -219,41 +256,4 @@
                     wlock.release()
             return node
 
-    class kwfilelog(filelog.filelog):
-        '''
-        Superclass over filelog to customize it's read, add, cmp methods.
-        Keywords are "stored" unexpanded, and expanded on reading.
-        '''
-        def __init__(self, opener, path, repo=None,
-                     defversion=revlog.REVLOG_DEFAULT_VERSION):
-            super(kwfilelog, self).__init__(opener, path, defversion)
-            self._repo = repo
-            self._path = path
-            # only init kwtemplater if needed
-            if hasattr(repo, 'kwfmatcher') and repo.kwfmatcher(path):
-                self.kwt = kwtemplater(repo.ui, repo)
-            else:
-                self.kwt = None
-
-        def read(self, node):
-            '''Substitutes keywords when reading filelog.'''
-            data = super(kwfilelog, self).read(node)
-            if self.kwt:
-                data = self.kwt.expand(self._path, node, self, data)
-            return data
-
-        def add(self, text, meta, tr, link, p1=None, p2=None):
-            '''Removes keyword substitutions when adding to filelog.'''
-            if self.kwt:
-                text = self.kwt.shrink(text)
-            return super(kwfilelog, self).add(text,
-                            meta, tr, link, p1=p1, p2=p2)
-
-        def cmp(self, node, text):
-            '''Removes keyword substitutions for comparison.'''
-            if self.kwt:
-                text = self.kwt.shrink(text)
-            return super(kwfilelog, self).cmp(node, text)
-
-    filelog.filelog = kwfilelog
     repo.__class__ = kwrepo