hgkw/keyword.py
changeset 207 affc18a621f5
parent 206 8d16b70359da
child 208 5afdcec8a01f
--- a/hgkw/keyword.py	Fri Jul 20 15:20:16 2007 +0200
+++ b/hgkw/keyword.py	Sat Jul 21 09:41:13 2007 +0200
@@ -98,20 +98,6 @@
     '''Returns hgdate in cvs-like UTC format.'''
     return time.strftime('%Y/%m/%d %H:%M:%S', time.gmtime(date[0]))
 
-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 = [], ['.hg*']
-    for pat, opt in ui.configitems('keyword'):
-        if opt != 'ignore':
-            inc.append(pat)
-        else:
-            exc.append(pat)
-    if not inc:
-        return None
-    return util.matcher(repo.root, inc=inc, exc=exc)[1]
-
 class kwtemplater(object):
     '''
     Sets up keyword templates, corresponding keyword regex, and
@@ -127,12 +113,12 @@
         'Header': '{root}/{file},v {node|short} {date|utcdate} {author|user}',
     }
 
-    def __init__(self, ui, repo, path='', node=None, expand=True):
+    def __init__(self, ui, repo, expand, path='', node=None):
         self.ui = ui
         self.repo = repo
+        self.t = expand or None
         self.path = path
         self.node = node
-        self.t = expand or None
 
         templates = dict(ui.configitems('keywordmaps'))
         if templates:
@@ -192,7 +178,7 @@
             return text
         return self.re_kw.sub(r'$\1$', text)
 
-    def overwrite(self, candidates, man, commit=True):
+    def overwrite(self, candidates, man, commit):
         '''Overwrites files in working directory if keywords are detected.
         Keywords are expanded if keyword templater is initialized,
         otherwise their substitution is removed.'''
@@ -201,9 +187,9 @@
         notify = (self.ui.note, self.ui.debug)[commit]
         files = []
         for f in candidates:
-            fp = self.repo.file(f, kwcnt=True, kwexp=expand)
-            data, cnt = fp.read(man[f])
-            if cnt:
+            fp = self.repo.file(f, kwexp=expand, kwcnt=True)
+            data, kwfound = fp.read(man[f])
+            if kwfound:
                 notify(_('overwriting %s %s keywords\n') % (f, action))
                 try:
                     self.repo.wwrite(f, data, man.flags(f))
@@ -247,7 +233,21 @@
             return t2 != text
         return super(kwfilelog, self).cmp(node, text)
 
-def _overwrite(ui, repo, files=None, expand=True):
+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 = [], ['.hg*']
+    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 _overwrite(ui, repo, files, expand):
     '''Expands/shrinks keywords in working directory.'''
     wlock = None
     try:
@@ -256,7 +256,7 @@
         ctx = repo.changectx()
         if not ctx:
             raise hg.RepoError(_('no revision checked out'))
-        kwfmatcher = keywordmatcher(ui, repo)
+        kwfmatcher = _keywordmatcher(ui, repo)
         if kwfmatcher is None:
             ui.warn(_('no files configured for keyword expansion\n'))
             return
@@ -269,8 +269,9 @@
         if not files:
             ui.warn(_('files not configured for expansion or untracked\n'))
             return
-        kwt = kwtemplater(ui, repo, node=ctx.node(), expand=expand)
-        kwt.overwrite(files, m, commit=False)
+        commit = False
+        kwt = kwtemplater(ui, repo, expand, node=ctx.node())
+        kwt.overwrite(files, m, commit)
         wlock = None
     finally:
         del wlock
@@ -284,14 +285,16 @@
                changing keyword expansion configuration
     or if you experience problems with "hg import"
     '''
-    _overwrite(ui, repo, files=args, expand=False)
+    expand = False
+    _overwrite(ui, repo, args, expand)
 
 def expand(ui, repo, *args):
     '''expand keywords in working directory
 
     run after (re)enabling keyword expansion
     '''
-    _overwrite(ui, repo, files=args)
+    expand = True
+    _overwrite(ui, repo, args, expand)
 
 def demo(ui, repo, *args, **opts):
     '''print [keywordmaps] configuration and an expansion example
@@ -369,16 +372,16 @@
     if not repo.local() or _getcmd() in nokwcommands:
         return
 
-    kwfmatcher = keywordmatcher(ui, repo)
+    kwfmatcher = _keywordmatcher(ui, repo)
     if kwfmatcher is None:
         return
 
     class kwrepo(repo.__class__):
-        def file(self, f, kwcnt=False, kwexp=True):
+        def file(self, f, kwexp=True, kwcnt=False):
             if f[0] == '/':
                 f = f[1:]
             if kwfmatcher(f):
-                kwt = kwtemplater(ui, self, path=f, expand=kwexp)
+                kwt = kwtemplater(ui, self, kwexp, path=f)
                 return kwfilelog(self.sopener, f, kwt, kwcnt)
             return filelog.filelog(self.sopener, f)
 
@@ -404,8 +407,9 @@
                                   and not os.path.islink(self.wjoin(f))]
                     if candidates:
                         m = self.manifest.read(cl[0])
-                        kwt = kwtemplater(ui, self, node=node)
-                        kwt.overwrite(candidates, m)
+                        expand = commit = True
+                        kwt = kwtemplater(ui, self, expand, node=node)
+                        kwt.overwrite(candidates, m, commit)
                 wlock = None
                 return node
             finally: