Implement near CVS compability with more than one keyword
Keywords are set as local vars in kwexpand() and then eval'd.
TODO: (sticky) tag?
--- a/hgkw/keyword.py Wed Dec 20 14:54:38 2006 +0100
+++ b/hgkw/keyword.py Wed Dec 20 17:41:19 2006 +0100
@@ -1,6 +1,8 @@
from mercurial import hg, filelog, revlog, context, util
import os.path, re
+hgkeywords = 'Id|Header|Author|Date|Revision|RCSFile|Source'
+
def reposetup(ui, repo):
if not repo.local():
return
@@ -19,34 +21,42 @@
self._path = path
def read(self, node):
- def gethgkw():
+ data = super(kwfilelog, self).read(node)
+ if not util.binary(data):
c = context.filectx(self._repo, self._path, fileid=node,
filelog=self)
- filename = c.path()
+ f = c.path()
for pat, opt in self._repo.ui.configitems('keyword'):
if opt == 'expand':
mf = util.matcher(self._repo.root,
'', [pat], [], [])[1]
- if mf(filename):
- return '$Hg: %s,v %s %s %s $' % (
- os.path.basename(filename),
- c.changectx(),
- util.datestr(date=c.date(),
- format=util.defaultdateformats[0]),
- util.shortuser(c.user())
- )
- data = super(kwfilelog, self).read(node)
- if not util.binary(data):
- hgkw = gethgkw()
- if hgkw:
- data = data.replace('$Hg$', hgkw)
+ if mf(f):
+ re_kw = re.compile(r'\$(%s)\$' % hgkeywords)
+
+ def kwexpand(matchobj):
+ RCSFile = os.path.basename(f)+',v'
+ Source = os.path.join(self._repo.root, f)+',v'
+ Revision = c.changectx()
+ Date = util.datestr(date=c.date())
+ Author = c.user()
+ revdateauth = '%s %s %s' % (
+ Revision,
+ util.datestr(date=c.date(),
+ format=util.defaultdateformats[0]),
+ util.shortuser(Author))
+ Header = '%s %s' % (Source, revdateauth)
+ Id = '%s %s' % (RCSFile, revdateauth)
+ return '$%s: %s $' % (
+ matchobj.group(1), eval(matchobj.group(1)))
+
+ return re_kw.sub(kwexpand, data)
return data
def add(self, text, meta, tr, link, p1=None, p2=None):
if (not util.binary(text) and
self._repo.ui.config('keyword', 'remove', True)):
- re_kw = re.compile(r'\$Hg: [^$]+?,v [a-z0-9]{12} [^$]+? \$')
- text = re_kw.sub('$Hg$', text)
+ re_kw = re.compile(r'\$(%s): [^$]+? \$' % hgkeywords)
+ text = re_kw.sub(r'$\1$', text)
return super(kwfilelog, self).add(text, meta, tr, link, p1, p2)
filelog.filelog = kwfilelog
--- a/hgkw/pretxnkw.py Wed Dec 20 14:54:38 2006 +0100
+++ b/hgkw/pretxnkw.py Wed Dec 20 17:41:19 2006 +0100
@@ -2,6 +2,8 @@
from mercurial import cmdutil, commands, util
import os.path, re, sys
+hgkeywords = 'Id|Header|Author|Date|Revision|RCSFile|Source'
+
def pretxnkw(ui, repo, hooktype, **args):
'''Collects candidates for keyword expansion on commit
and expands keywords in working dir.
@@ -37,20 +39,31 @@
if not files: # nothing to do
return False
- cid = args['node'][:12]
user, date = repo.changelog.read(repo.changelog.tip())[1:3]
- user = util.shortuser(user)
- date = util.datestr(date=date, format=util.defaultdateformats[0])
+ strdate = util.datestr(date=date)
+ shortuser = util.shortuser(user)
+ shortdate = util.datestr(date=date, format=util.defaultdateformats[0])
# %Y-%m-%d %H:%M:%S
- re_kw = re.compile(r'\$Hg(: [^$]+?,v [a-z0-9]{12} [^$]+? )?\$')
+ re_kw = re.compile(r'\$(%s)(: [^$]+? )?\$' % hgkeywords)
for f in files:
data = repo.wfile(f).read()
if not util.binary(data):
- kw = '$Hg: %s,v %s %s %s $' % (
- os.path.basename(f), cid, date, user)
- data, kwct = re_kw.subn(kw, data)
+
+ def kwexpand(matchobj):
+ RCSFile = os.path.basename(f)+',v'
+ Source = os.path.join(repo.root, f)+',v'
+ Revision = args['node'][:12]
+ Date = strdate
+ Author = user
+ revdateauth = '%s %s %s' % (Revision, shortdate, shortuser)
+ Header = '%s %s' % (Source, revdateauth)
+ Id = '%s %s' % (RCSFile, revdateauth)
+ return '$%s: %s $' % (
+ matchobj.group(1), eval(matchobj.group(1)))
+
+ data, kwct = re_kw.subn(kwexpand, data)
if kwct:
ui.note(_('expanding keywords in %s\n' % f))
# backup file?