hgkw/keyword.py
branchkwmap-templates
changeset 178 4a27c306c6a2
parent 177 261a6844145e
child 179 8ddd39a79dc7
--- a/hgkw/keyword.py	Sat Jul 14 20:28:45 2007 +0200
+++ b/hgkw/keyword.py	Sun Jul 15 01:19:20 2007 +0200
@@ -44,6 +44,8 @@
 Mercurial internally. The mechanism can be regarded as a convenience
 for the current user and may be turned off anytime.
 
+An additional date template filter {date|utcdate} is provided.
+
 Caveat: "hg import" might fail if the patches were exported from a
 repo with a different/no keyword setup, whereas "hg unbundle" is
 safe.
@@ -52,51 +54,38 @@
 hgrc files.
 
 Example:
-     [keyword]
-     # filename patterns for expansion are configured in this section
-     # files matching patterns with value 'ignore' are ignored
-     **.py =          ## expand keywords in all python files
-     x* = ignore      ## but ignore files matching "x*"
-     ...
-     [keywordmaps]
-     # custom hg template maps _replace_ the CVS-like default ones
-     HGdate = {date|rfc822date}
-     lastlog = {desc} ## same as {desc|firstline} in this context
-     checked in by = {author}
-     ...
+    [extensions]
+    hgext.keyword =
 
-If no [keywordmaps] are configured the extension falls back on the
-following defaults:
+    [keyword]
+    # expand keywords in every python file,
+    # except those matching "x*"
+    **.py =
+    x* = ignore
 
-     Revision: changeset id
-     Author: username
-     Date: %Y/%m/%d %H:%M:%S      ## [UTC]
-     RCSFile: basename,v
-     Source: /path/to/basename,v
-     Id: basename,v csetid %Y/%m/%d %H:%M:%S username
-     Header: /path/to/basename,v csetid %Y/%m/%d %H:%M:%S username
+For [keywordmaps] demonstration run "hg kwdemo".
 '''
 
 from mercurial.i18n import gettext as _
 from mercurial import commands, fancyopts, templater, util
-from mercurial import cmdutil, context, filelog
+from mercurial import cmdutil, context, filelog, localrepo
 # findcmd might be in cmdutil or commands
 # depending on mercurial version
 if hasattr(cmdutil, 'findcmd'):
     findcmd = cmdutil.findcmd
 else:
     findcmd = commands.findcmd
-import os, re, sys, time
+import os, re, shutil, sys, tempfile, time
 
 deftemplates = {
-        'Revision': '{node|short}',
-        'Author': '{author|user}',
-        'Date': '{date|utcdate}',
-        'RCSFile': '{file|basename},v',
-        'Source': '{root}/{file},v',
-        'Id': '{file|basename},v {node|short} {date|utcdate} {author|user}',
-        'Header': '{root}/{file},v {node|short} {date|utcdate} {author|user}',
-        }
+    'Revision': '{node|short}',
+    'Author': '{author|user}',
+    'Date': '{date|utcdate}',
+    'RCSFile': '{file|basename},v',
+    'Source': '{root}/{file},v',
+    'Id': '{file|basename},v {node|short} {date|utcdate} {author|user}',
+    'Header': '{root}/{file},v {node|short} {date|utcdate} {author|user}',
+}
 
 nokwcommands = ('add', 'addremove', 'bundle', 'clone', 'copy', 'export',
                 'incoming', 'outgoing', 'push', 'remove', 'rename', 'rollback')
@@ -105,6 +94,47 @@
     '''Returns hgdate in cvs-like UTC format.'''
     return time.strftime('%Y/%m/%d %H:%M:%S', time.gmtime(date[0]))
 
+def kwdemo(ui, repo, **opts):
+    '''print [keywordmaps] configuration and an expansion example
+    '''
+    log = 'hg keyword config and expansion example'
+    fn = 'demo.txt'
+    ui.setconfig('keyword', fn, '')
+    if opts['default']:
+        kwstatus = 'default'
+        kwmaps = deftemplates
+        if ui.configitems('keywordmaps'):
+            for k, v in kwmaps.items():
+                ui.setconfig('keywordmaps', k, v)
+    else:
+        kwstatus = 'current'
+        kwmaps = dict(ui.configitems('keywordmaps')) or deftemplates
+    tmpdir = tempfile.mkdtemp('', 'kwdemo.')
+    if ui.verbose:
+        ui.status(_('creating temporary repo at %s\n') % tmpdir)
+    _repo = localrepo.localrepository(ui, path=tmpdir, create=True)
+    reposetup(ui, _repo)
+    ui.status(_('config with %s keyword maps:\n') % kwstatus)
+    ui.write('[keyword]\n%s =\n[keywordmaps\n' % fn)
+    for k, v in kwmaps.items():
+        ui.write('%s = %s\n' % (k, v))
+    path = _repo.wjoin(fn)
+    keywords = '$' + '$\n$'.join(kwmaps.keys()) + '$\n'
+    _repo.wfile(fn, 'w').write(keywords)
+    _repo.add([fn])
+    if ui.verbose:
+        ui.status(_('\n%s keywords written to %s:\n') % (kwstatus, path))
+        ui.write(keywords)
+        ui.status(_("\nhg --repository '%s' commit\n") % tmpdir)
+    _repo.commit(text=log)
+    if ui.verbose:
+        ui.status(_('\n%s keywords expanded in %s:\n') % (kwstatus, path))
+    else:
+        ui.status(_('\n%s keywords expanded:\n') % kwstatus)
+    ui.write(_repo.wread(fn))
+    ui.debug(_('\nremoving temporary repo\n'))
+    shutil.rmtree(tmpdir)
+
 def getcmd(ui):
     '''Returns current hg command.'''
     # commands.parse(ui, sys.argv[1:])[0] breaks "hg diff -r"
@@ -286,3 +316,11 @@
                     wlock.release()
 
     repo.__class__ = kwrepo
+
+
+cmdtable = {
+    'kwdemo':
+        (kwdemo,
+         [('d', 'default', None, _('use default keyword maps'))],
+         _('hg kwdemo [-d]')),
+}