hgkw/keyword.py
branchkwmap-templates
changeset 115 520818841684
parent 109 b2cc6a8d4a18
child 116 265714c59607
--- a/hgkw/keyword.py	Thu Jan 18 00:06:11 2007 +0100
+++ b/hgkw/keyword.py	Sat Jan 20 01:52:17 2007 +0100
@@ -14,14 +14,18 @@
 For in-depth discussion refer to
 <http://www.selenic.com/mercurial/wiki/index.cgi/KeywordPlan>.
 
-You can either use the default cvs-like keywords or provide your
-own in hgrc.
+Keywords are only expanded in local repositories and not logged by
+Mercurial internally. The mechanism can be regarded as a convenience
+for the current user and may be turned off anytime.
 
-It is recommended to enable this extension on a per-repo basis only.
-You can still configure keywordmaps globally.
+Substitution takes place on every commit and update of the working
+repository.
 
-Expansions spanning more than one line are truncated to their first line.
-Incremental expansion (like CVS' $Log$) is not supported.
+Keyword expansion is based on Mercurial's changeset template mappings.
+The extension provides an additional UTC-date filter.
+
+The user has the choice either to create his own keywords and their
+expansions or to use the CVS-like default ones.
 
 Default $keywords$ and their $keyword: substition $ are:
     Revision: changeset id
@@ -32,10 +36,13 @@
     Id:       basename,v csetid %Y/%m/%d %H:%M:%S username
     Header:   /path/to/basename,v csetid %Y/%m/%d %H:%M:%S username
 
+Expansions spanning more than one line are truncated to their first line.
+Incremental expansion (like CVS' $Log$) is not supported.
+
 Simple setup in hgrc:
 
     # enable extension
-    hgext.keyword = /full/path/to/script
+    keyword = /full/path/to/keyword.py
     # or, if script in hgext folder:
     # hgext.keyword =
     
@@ -57,8 +64,9 @@
 # above line for backwards compatibility; can be changed to
 #   from mercurial.i18n import _
 # some day
-from mercurial import cmdutil, templater, util
-from mercurial.node import *
+from mercurial import context, filelog, revlog
+from mercurial import commands, cmdutil, templater, util
+from mercurial.node import bin
 import os.path, re, sys, time
 
 deftemplates = {
@@ -75,14 +83,32 @@
     '''Returns hgdate in cvs-like UTC format.'''
     return time.strftime('%Y/%m/%d %H:%M:%S', time.gmtime(date[0]))
 
+def getmodulename():
+    '''Makes sure pretxncommit-hook can import keyword module
+    regardless of where its located.'''
+    for k, v in sys.modules.iteritems():
+        if v is None:
+            continue
+        if not hasattr(v, '__file__'):
+            continue
+        if v.__file__.startswith(__file__):
+            return k
+    else:
+        sys.path.insert(0, os.path.abspath(os.path.dirname(__file__)))
+        return os.path.splitext(os.path.basename(__file__))[0]
+
 def kwfmatches(ui, repo, files):
     '''Selects candidates for keyword substitution
     configured in keyword section in hgrc.'''
-    inc = [pat for pat, opt in ui.configitems('keyword') if opt != 'ignore']
+    inc, exc = [], []
+    for pat, opt in ui.configitems('keyword'):
+        if opt != 'ignore':
+            inc.append(pat)
+        else:
+            exc.append(pat)
     if not inc:
-        ui.warn(_('keyword: no filename globs for expansion\n'))
+        ui.debug(_('keyword: no filename globs for substitution\n'))
         return []
-    exc = [pat for pat, opt in ui.configitems('keyword') if opt == 'ignore']
     kwfmatcher = util.matcher(repo.root, inc=inc, exc=['.hg*']+exc)[1]
     return [f for f in files if kwfmatcher(f)]
 
@@ -109,18 +135,18 @@
         self.t.use_template(template)
         self.ui.pushbuffer()
         self.t.show(changenode=node, root=self.repo.root, file=path)
-        expansion = templater.firstline(self.ui.popbuffer())
-        return '$%s: %s $' % (kw, expansion)
+        kwsub = templater.firstline(self.ui.popbuffer())
+        return '$%s: %s $' % (kw, kwsub)
 
 
 def reposetup(ui, repo):
     '''Sets up repo, and filelog especially, as kwrepo and kwfilelog
     for keyword substitution.  This is done for local repos only.'''
 
-    from mercurial import context, filelog, revlog
     if not repo.local():
         return
 
+
     class kwrepo(repo.__class__):
         def file(self, f):
             if f[0] == '/':
@@ -172,26 +198,14 @@
 
     filelog.filelog = kwfilelog
     repo.__class__ = kwrepo
-    # make pretxncommit hook import kwmodule regardless of where it's located
-    for k, v in sys.modules.iteritems():
-        if v is None:
-            continue
-        if not hasattr(v, '__file__'):
-            continue
-        if v.__file__.startswith(__file__):
-            mod = k
-            break
-    else:
-        sys.path.insert(0, os.path.abspath(os.path.dirname(__file__)))
-        mod = os.path.splitext(os.path.basename(__file__))[0]
-    ui.setconfig('hooks', 'pretxncommit.keyword', 'python:%s.pretxnkw' % mod)
-    del mod
+    # configure pretxncommit hook
+    ui.setconfig('hooks', 'pretxncommit.keyword',
+            'python:%s.pretxnkw' % getmodulename())
 
 
 def pretxnkw(ui, repo, hooktype, **args):
     '''pretxncommit hook that collects candidates for keyword expansion
     on commit and expands keywords in working dir.'''
-    from mercurial import commands
 
     cmd, sysargs, globalopts, cmdopts = commands.parse(ui, sys.argv[1:])[1:]
     if repr(cmd).split()[1] in ('tag', 'import_'):