hgkw/keyword.py
branch0.9.2compat
changeset 322 b1ac738404f8
parent 312 b92767fb8fb5
child 329 27f2e2126447
--- a/hgkw/keyword.py	Mon Dec 17 16:43:26 2007 +0100
+++ b/hgkw/keyword.py	Fri Jan 04 17:16:13 2008 +0100
@@ -281,49 +281,48 @@
     Subclass of filelog to hook into its read, add, cmp methods.
     Keywords are "stored" unexpanded, and processed on reading.
     '''
-    def __init__(self, opener, path, kwtemplater):
+    def __init__(self, opener, path):
         super(kwfilelog, self).__init__(opener, path)
-        self.kwtemplater = kwtemplater
-        self.kwtemplater.path = path
+        _kwtemplater.path = path
 
     def kwctread(self, node, expand):
         '''Reads expanding and counting keywords
         (only called from kwtemplater.overwrite).'''
         data = super(kwfilelog, self).read(node)
-        return self.kwtemplater.process(node, data, expand)
+        return _kwtemplater.process(node, data, expand)
 
     def read(self, node):
         '''Expands keywords when reading filelog.'''
         data = super(kwfilelog, self).read(node)
-        return self.kwtemplater.expand(node, data)
+        return _kwtemplater.expand(node, data)
 
     def add(self, text, meta, tr, link, p1=None, p2=None):
         '''Removes keyword substitutions when adding to filelog.'''
-        text = self.kwtemplater.shrink(text)
+        text = _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)
+        text = _kwtemplater.shrink(text)
         if self.renamed(node):
             t2 = super(kwfilelog, self).read(node)
             return t2 != text
         return revlog.revlog.cmp(self, node, text)
 
-def _iskwfile(f, kwtemplater, link):
-    return not link(f) and kwtemplater.matcher(f)
+def _iskwfile(f, link):
+    return not link(f) and _kwtemplater.matcher(f)
 
-def _status(ui, repo, kwtemplater, *pats, **opts):
+def _status(ui, repo, *pats, **opts):
     '''Bails out if [keyword] configuration is not active.
     Returns status of working directory.'''
-    if kwtemplater:
+    if _kwtemplater:
         files, match, anypats = cmdutil.matchpats(repo, pats, opts)
         return repo.status(files=files, match=match, list_clean=True)
     if ui.configitems('keyword'):
         raise util.Abort(_('[keyword] patterns cannot match'))
     raise util.Abort(_('no [keyword] patterns configured'))
 
-def _overwrite(ui, repo, kwtemplater, node=None, expand=True, files=None):
+def _overwrite(ui, repo, node=None, expand=True, files=None):
     '''Overwrites selected files expanding/shrinking keywords.'''
     ctx = repo.changectx(node)
     mf = ctx.manifest()
@@ -332,12 +331,12 @@
         files = [f for f in ctx.files() if mf.has_key(f)]
     else:
         notify = ui.note  # kwexpand/kwshrink
-    candidates = [f for f in files if _iskwfile(f, kwtemplater, mf.linkf)]
+    candidates = [f for f in files if _iskwfile(f, mf.linkf)]
     if candidates:
         overwritten = []
         candidates.sort()
         action = expand and 'expanding' or 'shrinking'
-        kwtemplater.node = node or ctx.node()
+        _kwtemplater.node = node or ctx.node()
         for f in candidates:
             fp = repo.file(f, kwmatch=True)
             data, kwfound = fp.kwctread(mf[f], expand)
@@ -349,8 +348,7 @@
 
 def _kwfwrite(ui, repo, expand, *pats, **opts):
     '''Selects files and passes them to _overwrite.'''
-    global _kwtemplater
-    status = _status(ui, repo, _kwtemplater, *pats, **opts)
+    status = _status(ui, repo, *pats, **opts)
     modified, added, removed, deleted, unknown, ignored, clean = status
     if modified or added or removed or deleted:
         raise util.Abort(_('outstanding uncommitted changes in given files'))
@@ -358,7 +356,7 @@
     try:
         wlock = repo.wlock()
         lock = repo.lock()
-        _overwrite(ui, repo, _kwtemplater, expand=expand, files=clean)
+        _overwrite(ui, repo, expand=expand, files=clean)
     finally:
         del wlock, lock
 
@@ -390,11 +388,11 @@
     ui.note(_('creating temporary repo at %s\n') % tmpdir)
     repo = localrepo.localrepository(ui, path=tmpdir, create=True)
     ui.setconfig('keyword', fn, '')
-    if args or opts['rcfile']:
+    if args or opts.get('rcfile'):
         kwstatus = 'custom'
-    if opts['rcfile']:
-        ui.readconfig(opts['rcfile'])
-    if opts['default']:
+    if opts.get('rcfile'):
+        ui.readconfig(opts.get('rcfile'))
+    if opts.get('default'):
         kwstatus = 'default'
         kwmaps = kwtemplater.templates
         if ui.configitems('keywordmaps'):
@@ -408,7 +406,7 @@
         fp.writelines(rcmaps)
         fp.close()
         ui.readconfig(repo.join('hgrc'))
-    if not opts['default']:
+    if not opts.get('default'):
         kwmaps = dict(ui.configitems('keywordmaps')) or kwtemplater.templates
     reposetup(ui, repo)
     for k, v in ui.configitems('extensions'):
@@ -461,10 +459,9 @@
     keyword expansion.
     That is, files matched by [keyword] config patterns but not symlinks.
     '''
-    global _kwtemplater
-    status = _status(ui, repo, _kwtemplater, *pats, **opts)
+    status = _status(ui, repo, *pats, **opts)
     modified, added, removed, deleted, unknown, ignored, clean = status
-    if opts['untracked']:
+    if opts.get('untracked'):
         files = modified + added + unknown + clean
     else:
         files = modified + added + clean
@@ -473,16 +470,11 @@
     kwfiles = [f for f in files if _kwtemplater.matcher(f)
                and not os.path.islink(repo.wjoin(f))]
     cwd = pats and repo.getcwd() or ''
-    allf = opts['all']
-    ignore = opts['ignore']
-    if ignore:
-        kwfstats = ()
-    else:
-        kwfstats = (('K', kwfiles),)
-    if allf or ignore:
+    kwfstats = not opts.get('ignore') and (('K', kwfiles),) or ()
+    if opts.get('all') or opts.get('ignore'):
         kwfstats += (('I', [f for f in files if f not in kwfiles]),)
     for char, filenames in kwfstats:
-        format = (allf or ui.verbose) and '%s %%s\n' % char or '%s\n'
+        format = (opts.get('all') or ui.verbose) and '%s %%s\n' % char or '%s\n'
         for f in filenames:
             ui.write(format % _pathto(repo, f, cwd))
 
@@ -507,18 +499,19 @@
     This is done for local repos only, and only if there are
     files configured at all for keyword substitution.'''
 
-    nokwcommands = ('add', 'addremove', 'bundle', 'clone', 'copy', 'export',
-                    'grep', 'identify', 'incoming', 'init', 'outgoing', 'push',
-                    'remove', 'rename', 'rollback', 'convert')
-
-    def _getcmd():
-        '''Simplified argument parsing as we are only interested in command.'''
+    def kwbailout():
+        '''Obtains command via simplified cmdline parsing,
+        returns True if keyword expansion not needed.'''
+        nokwcommands = ('add', 'addremove', 'bundle', 'clone', 'copy',
+                        'export', 'grep', 'identify', 'incoming', 'init',
+                        'outgoing', 'push', 'remove', 'rename', 'rollback',
+                        'convert')
         args = fancyopts(sys.argv[1:], commands.globalopts, {})
         if args:
             aliases, i = findcmd(ui, args[0], commands.table)
-            return aliases[0]
+            return aliases[0] in nokwcommands
 
-    if not repo.local() or _getcmd() in nokwcommands:
+    if not repo.local() or kwbailout():
         return
 
     inc, exc = [], ['.hgtags']
@@ -538,7 +531,7 @@
             if f[0] == '/':
                 f = f[1:]
             if kwmatch or _kwtemplater.matcher(f):
-                return kwfilelog(self.sopener, f, _kwtemplater)
+                return kwfilelog(self.sopener, f)
             return filelog.filelog(self.sopener, f)
 
         def _commit(self, files, text, user, date, match, force, lock, wlock,
@@ -598,7 +591,7 @@
                 for name, cmd in commithooks:
                     ui.setconfig('hooks', name, cmd)
                 if node is not None:
-                    _overwrite(ui, self, _kwtemplater, node=node)
+                    _overwrite(ui, self, node=node)
                     repo.hook('commit', node=node, parent1=_p1, parent2=_p2)
                 return node
             finally: