hgkw/keyword.py
changeset 377 ce5eb725d0c6
parent 368 82e2b3807073
child 378 0683aaf458d8
--- a/hgkw/keyword.py	Wed Feb 06 00:24:49 2008 +0100
+++ b/hgkw/keyword.py	Wed Feb 06 11:32:34 2008 +0100
@@ -132,8 +132,6 @@
         and calls given substitution function.'''
         if node is None:
             # kwrepo.wwrite except when overwriting on commit
-            if self.ctx is None:
-                self.ctx = self.repo.changectx()
             try:
                 fnode = self.ctx.filenode(path)
                 fl = self.repo.file(path)
@@ -154,11 +152,13 @@
 
         return subfunc(kwsub, data)
 
-    def expand(self, path, data, node):
+    def expand(self, path, data, ctx):
         '''Returns data with keywords expanded.'''
         if util.binary(data):
             return data
-        return self.substitute(path, data, node, self.re_kw.sub)
+        if self.ctx is None:
+            self.ctx = ctx or self.repo.changectx()
+        return self.substitute(path, data, None, self.re_kw.sub)
 
     def process(self, path, data, expand, ctx, node):
         '''Returns a tuple: data, count.
@@ -246,18 +246,16 @@
     %p   root-relative path name of file being printed
     '''
     ctx = repo.changectx(opts['rev'])
-    try:
-        repo._kwt.ctx = ctx
-        kw = True
-    except AttributeError:
-        kw = False
     err = 1
     for src, abs, rel, exact in cmdutil.walk(repo, (file1,) + pats, opts,
                                              ctx.node()):
         fp = cmdutil.make_file(repo, opts['output'], ctx.node(), pathname=abs)
         data = ctx.filectx(abs).data()
-        if kw and repo._kwt.matcher(abs):
-            data = repo._kwt.expand(abs, data, None)
+        try:
+            if repo._kwt.matcher(abs):
+                data = repo._kwt.expand(abs, data, ctx)
+        except AttributeError:
+            pass
         fp.write(data)
         err = 0
     return err
@@ -494,7 +492,7 @@
         path = fctx.path()
         text = fctx.data()
         if kwt.matcher(path):
-            text = kwt.expand(path, text, fctx.node())
+            text = kwt.expand(path, text, web.changectx(req))
         mt = mimetypes.guess_type(path)[0]
         if mt is None or util.binary(text):
             mt = mt or 'application/octet-stream'