Implement archive mode by configuration kwmap-templates
authorChristian Ebert <blacktrash@gmx.net>
Sat, 24 Feb 2007 11:10:31 +0100
branchkwmap-templates
changeset 149 0c8b7e5c25a6
parent 148 c0baa881d90a
child 150 ba5cb61db7a0
Implement archive mode by configuration Note: For some reason commands.parse(ui, sys.argv[1:]) breaks "hg diff -r".
hgkw/keyword.py
--- a/hgkw/keyword.py	Wed Feb 21 02:27:15 2007 +0100
+++ b/hgkw/keyword.py	Sat Feb 24 11:10:31 2007 +0100
@@ -44,8 +44,10 @@
 Mercurial internally. The mechanism can be regarded as a convenience
 for the current user and may be turned off anytime.
 
-Substitution takes place on every commit and update of the working
-repository.
+The exansion works in 2 modes:
+    1) working mode: substitution takes place on every commit and
+       update of the working repository.
+    2) archive mode: substitution is only triggered by "hg archive".
 
 Caveat: "hg import" might fail if the patches were exported from a
 repo with a different/no keyword setup, whereas "hg unbundle" is
@@ -60,6 +62,10 @@
      # files matching patterns with value 'ignore' are ignored
      **.py =          ## expand keywords in all python files
      x* = ignore      ## but ignore files matching "x*"
+     ** = archive     ## keywords in all textfiles are expanded
+                      ## when creating a distribution
+     y* = noarchive   ## keywords in files matching "y*" are not expanded
+                      ## on archive creation
      ...
      [keywordmaps]
      # custom hg template maps _replace_ the CVS-like default ones
@@ -83,14 +89,14 @@
 try:
     from mercurial.demandload import * # stable
     from mercurial.i18n import gettext as _
-    demandload(globals(), 'mercurial:cmdutil,templater,util')
-    demandload(globals(), 'mercurial:context,filelog,revlog')
-    demandload(globals(), 're time')
+    demandload(globals(), 'mercurial:commands,fancyopts,templater,util')
+    demandload(globals(), 'mercurial:cmdutil,context,filelog,revlog')
+    demandload(globals(), 're sys time')
 except ImportError:                    # demandimport
     from mercurial.i18n import _
-    from mercurial import cmdutil, templater, util
-    from mercurial import context, filelog, revlog
-    import re, time
+    from mercurial import commands, fancyopts, templater, util
+    from mercurial import cmdutil, context, filelog, revlog
+    import re, sys, time
 
 deftemplates = {
         'Revision': '{node|short}',
@@ -106,6 +112,18 @@
     '''Returns hgdate in cvs-like UTC format.'''
     return time.strftime('%Y/%m/%d %H:%M:%S', time.gmtime(date[0]))
 
+def getcmd(ui):
+    '''Returns current hg command.'''
+    # commands.parse(ui, sys.argv[1:])[0] breaks "hg diff -r"
+    try:
+        args = fancyopts.fancyopts(sys.argv[1:], commands.globalopts, {})
+    except fancyopts.getopt.GetoptError, inst:
+        raise commands.ParseError(None, inst)
+    if args:
+        cmd = args[0]
+        aliases, i = commands.findcmd(ui, cmd)
+        return aliases[0]
+
 class kwtemplater(object):
     '''
     Sets up keyword templates, corresponding keyword regex, and
@@ -210,12 +228,20 @@
     if not repo.local():
         return
 
-    inc, exc = [], ['.hg*']
+    archivemode = (getcmd(repo.ui) == 'archive')
+
+    inc, exc, archive, noarchive = [], ['.hg*'], [], ['.hg*']
     for pat, opt in repo.ui.configitems('keyword'):
-        if opt != 'ignore':
+        if opt == 'archive':
+            archive.append(pat)
+        elif opt == 'noarchive':
+            noarchive.append(pat)
+        elif opt == 'ignore':
+            exc.append(pat)
+        else:
             inc.append(pat)
-        else:
-            exc.append(pat)
+    if archivemode:
+        inc, exc = archive, noarchive
     if not inc:
         return