[PATCH 2 of 3] keyword: make main class and hg command accessible

Christian Ebert blacktrash at gmx.net
Thu Feb 14 04:04:52 CST 2008


# HG changeset patch
# User Christian Ebert <blacktrash at gmx.net>
# Date 1202983462 -3600
# Node ID dcc5992e621e4fc80bafa21959a70bb2641465e6
# Parent  0f4f01055af56c38cd4e40e627006d2a5390dd0d
keyword: make main class and hg command accessible

Switch from global vars to top level dictionary.

Goal: make it easier for external tools (like tortoisehg)
      to hook into keyword extension.

diff --git a/hgext/keyword.py b/hgext/keyword.py
--- a/hgext/keyword.py
+++ b/hgext/keyword.py
@@ -101,7 +101,8 @@
     return time.strftime('%Y/%m/%d %H:%M:%S', time.gmtime(date[0]))
 
 
-_kwtemplater = _cmd = None
+# make keyword tools accessible
+kwx = { 'templater': None, 'hgcmd': None }
  
 # store originals of monkeypatches
 _patchfile_init = patch.patchfile.__init__
@@ -113,33 +114,34 @@
     rejects or conflicts due to expanded keywords in working dir.'''
     _patchfile_init(self, ui, fname, missing=missing)
     # shrink keywords read from working dir
-    self.lines = _kwtemplater.shrinklines(self.fname, self.lines)
+    kwt = kwx['templater']
+    self.lines = kwt.shrinklines(self.fname, self.lines)
 
 def _kw_diff(repo, node1=None, node2=None, files=None, match=util.always,
              fp=None, changes=None, opts=None):
     # only expand if comparing against working dir
     if node2 is not None:
-        _kwtemplater.matcher = util.never
+        kwx['templater'].matcher = util.never
     if node1 is not None and node1 != repo.changectx().node():
-        _kwtemplater.restrict = True
+        kwx['templater'].restrict = True
     _patch_diff(repo, node1=node1, node2=node2, files=files, match=match,
                 fp=fp, changes=changes, opts=opts)
 
 def _kwweb_changeset(web, req, tmpl):
     '''Wraps webcommands.changeset turning off keyword expansion.'''
-    _kwtemplater.matcher = util.never
+    kwx['templater'].matcher = util.never
     return web.changeset(tmpl, web.changectx(req))
 
 def _kwweb_filediff(web, req, tmpl):
     '''Wraps webcommands.filediff turning off keyword expansion.'''
-    _kwtemplater.matcher = util.never
+    kwx['templater'].matcher = util.never
     return web.filediff(tmpl, web.filectx(req))
 
 def _kwdispatch_parse(ui, args):
     '''Monkeypatch dispatch._parse to obtain running hg command.'''
-    global _cmd
-    _cmd, func, args, options, cmdoptions = _dispatch_parse(ui, args)
-    return _cmd, func, args, options, cmdoptions
+    cmd, func, args, options, cmdoptions = _dispatch_parse(ui, args)
+    kwx['hgcmd'] = cmd
+    return cmd, func, args, options, cmdoptions
 
 # dispatch._parse is run before reposetup, so wrap it here
 dispatch._parse = _kwdispatch_parse
@@ -164,7 +166,7 @@
         self.ui = ui
         self.repo = repo
         self.matcher = util.matcher(repo.root, inc=inc, exc=exc)[1]
-        self.restrict = _cmd in restricted.split()
+        self.restrict = kwx['hgcmd'] in restricted.split()
 
         kwmaps = self.ui.configitems('keywordmaps')
         if kwmaps: # override default templates
@@ -269,30 +271,31 @@
     '''
     def __init__(self, opener, path):
         super(kwfilelog, self).__init__(opener, path)
+        self.kwt = kwx['templater']
         self.path = path
 
     def read(self, node):
         '''Expands keywords when reading filelog.'''
         data = super(kwfilelog, self).read(node)
-        return _kwtemplater.expand(self.path, node, data)
+        return self.kwt.expand(self.path, node, data)
 
     def add(self, text, meta, tr, link, p1=None, p2=None):
         '''Removes keyword substitutions when adding to filelog.'''
-        text = _kwtemplater.shrink(self.path, text)
+        text = self.kwt.shrink(self.path, 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 = _kwtemplater.shrink(self.path, text)
+        text = self.kwt.shrink(self.path, text)
         if self.renamed(node):
             t2 = super(kwfilelog, self).read(node)
             return t2 != text
         return revlog.revlog.cmp(self, node, text)
 
-def _status(ui, repo, *pats, **opts):
+def _status(ui, repo, kwt, *pats, **opts):
     '''Bails out if [keyword] configuration is not active.
     Returns status of working directory.'''
-    if _kwtemplater:
+    if kwt:
         files, match, anypats = cmdutil.matchpats(repo, pats, opts)
         return repo.status(files=files, match=match, list_clean=True)
     if ui.configitems('keyword'):
@@ -301,7 +304,8 @@
 
 def _kwfwrite(ui, repo, expand, *pats, **opts):
     '''Selects files and passes them to kwtemplater.overwrite.'''
-    status = _status(ui, repo, *pats, **opts)
+    kwt = kwx['templater']
+    status = _status(ui, repo, kwt, *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'))
@@ -309,7 +313,7 @@
     try:
         wlock = repo.wlock()
         lock = repo.lock()
-        _kwtemplater.overwrite(expand=expand, files=clean)
+        kwt.overwrite(expand=expand, files=clean)
     finally:
         del wlock, lock
 
@@ -411,7 +415,8 @@
     keyword expansion.
     That is, files matched by [keyword] config patterns but not symlinks.
     '''
-    status = _status(ui, repo, *pats, **opts)
+    kwt = kwx['templater']
+    status = _status(ui, repo, kwt, *pats, **opts)
     modified, added, removed, deleted, unknown, ignored, clean = status
     files = modified + added + clean
     if opts.get('untracked'):
@@ -419,7 +424,7 @@
     files.sort()
     wctx = repo.workingctx()
     islink = lambda p: 'l' in wctx.fileflags(p)
-    kwfiles = [f for f in files if _kwtemplater.iskwfile(f, islink)]
+    kwfiles = [f for f in files if kwt.iskwfile(f, islink)]
     cwd = pats and repo.getcwd() or ''
     kwfstats = not opts.get('ignore') and (('K', kwfiles),) or ()
     if opts.get('all') or opts.get('ignore'):
@@ -450,10 +455,8 @@
     This is done for local repos only, and only if there are
     files configured at all for keyword substitution.'''
 
-    global _kwtemplater
-
     try:
-        if (not repo.local() or _cmd in nokwcommands.split() 
+        if (not repo.local() or kwx['hgcmd'] in nokwcommands.split() 
             or '.hg' in util.splitpath(repo.root)
             or repo._url.startswith('bundle:')):
             return
@@ -469,7 +472,7 @@
     if not inc:
         return
 
-    _kwtemplater = kwtemplater(ui, repo, inc, exc)
+    kwx['templater'] = kwt = kwtemplater(ui, repo, inc, exc)
 
     class kwrepo(repo.__class__):
         def file(self, f):
@@ -479,7 +482,7 @@
 
         def wread(self, filename):
             data = super(kwrepo, self).wread(filename)
-            return _kwtemplater.wread(filename, data)
+            return kwt.wread(filename, data)
 
         def commit(self, files=None, text='', user=None, date=None,
                    match=util.always, force=False, force_editor=False,
@@ -518,7 +521,7 @@
                 for name, cmd in commithooks.iteritems():
                     ui.setconfig('hooks', name, cmd)
                 if node is not None:
-                    _kwtemplater.overwrite(node=node)
+                    kwt.overwrite(node=node)
                     repo.hook('commit', node=node, parent1=_p1, parent2=_p2)
                 return node
             finally:


More information about the Mercurial-devel mailing list