[PATCH] add keyword extension

Christian Ebert blacktrash at gmx.net
Fri Dec 29 09:47:38 CST 2006


# HG changeset patch
# User Christian Ebert <blacktrash at gmx.net>
# Date 1167410680 -3600
# Node ID 63a9ded46bd368dfea5ced0c40d23b4999af821e
# Parent  04d919cdf263e3d89642b8d73f7536f3979e559a
add keyword extension

This is a proposal for keyword substitution in Mercurial,
shamelessly exploiting code from the KeywordPlan wiki.

Feel free to improve/ignore.

diff -r 04d919cdf263 -r 63a9ded46bd3 hgext/keyword.py
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/hgext/keyword.py	Fri Dec 29 17:44:40 2006 +0100
@@ -0,0 +1,171 @@
+# keyword.py - keyword expansion for mercurial
+#
+# Copyright 2006 Christian Ebert <blacktrash at gmx.net>
+#
+# This software may be used and distributed according to the terms
+# of the GNU General Public License, incorporated herein by reference.
+
+'''keyword expansion hack against the grain of a DSCM
+
+This extension lets you expand RCS/CVS-like keywords in a Mercurial
+repository.
+
+There are many good reasons why this is not needed in a distributed
+SCM, still it may be useful in very small projects based on single
+files (like LaTeX packages), that are mostly addressed to an audience
+not running a version control system.
+
+Supported keywords are (changeset 000000000000):
+    $Revision: 000000000000 $
+    $Author: Your Name <address at example.com> $
+    $Date: %a %b %d %H:%M:%S %Y %z $
+    $RCSFile: basename,v $
+    $Source: /path/to/basename,v $
+    $Id: basename,v 000000000000 %Y-%m-%d %H:%M:%S %z shortname $
+    $Header: /path/to/basename,v 000000000000 %Y-%m-%d %H:%M:%S %z shortname $
+
+The extension, according to its hackish nature, is a hybrid and consists
+actually in 2 parts:
+
+    1. pure extension code (reposetup) that is triggered on checkout and
+       logging of changes.
+    2. a pretxncommit hook (hgrc (5)) that expands keywords immediately
+       at commit time in the working directory.
+
+Simple setup in hgrc:
+
+    # enable extension
+    hgext.keyword =
+    
+    # filename patterns for expansion are configured in this section
+    [keyword]
+    *.sty = expand
+    ...
+
+    # set up pretxncommit hook
+    [hooks]
+    pretxncommit =
+    pretxncommit.keyword = python:hgext.keyword.pretxnkw
+'''
+
+from mercurial.i18n import _
+from mercurial import cmdutil, commands, context, filelog, revlog, util
+import os.path, re, sys
+
+
+re_kw = re.compile(
+        r'\$(Id|Header|Author|Date|Revision|RCSFile|Source)[^$]*?\$')
+
+
+def kwexpand(matchobj, repo, path, changeid=None, fileid=None, filelog=None):
+    '''Called by kwfilelog.read and pretxnkw.
+    Sets supported keywords as local variables and evaluates them to
+    their expansion if matchobj is equal to string representation.'''
+
+    c = context.filectx(repo, path,
+            changeid=changeid, fileid=fileid, filelog=filelog)
+    date = c.date()
+
+    Revision = c.changectx()
+    Author = c.user()
+    RCSFile = os.path.basename(path)+',v'
+    Source = repo.wjoin(path)+',v'
+    Date = util.datestr(date=date)
+    revdateauth = '%s %s %s' % (Revision,
+            util.datestr(date=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)))
+
+
+def reposetup(ui, repo):
+    if not repo.local():
+        return
+
+    class kwrepo(repo.__class__):
+        def file(self, f):
+            if f[0] == '/':
+                f = f[1:]
+            return filelog.filelog(self.sopener, f, self, self.revlogversion)
+
+    class kwfilelog(filelog.filelog):
+        def __init__(self, opener, path, repo,
+                     defversion=revlog.REVLOG_DEFAULT_VERSION):
+            super(kwfilelog, self).__init__(opener, path, defversion)
+            self._repo = repo
+            self._path = path
+
+        def read(self, node):
+            data = super(kwfilelog, self).read(node)
+            if not self._path.startswith('.hg') and not util.binary(data):
+                for pat, opt in self._repo.ui.configitems('keyword'):
+                    if opt == 'expand':
+                        mf = util.matcher(self._repo.root,
+                                '', [pat], [], [])[1]
+                        if mf(self._path):
+                            return re_kw.sub(lambda m:
+                                    kwexpand(m, self._repo, self._path,
+                                        fileid=node, filelog=self),
+                                    data)
+            return data
+
+        def add(self, text, meta, tr, link, p1=None, p2=None):
+            if not util.binary(text):
+                text = re_kw.sub(r'$\1$', text)
+            return super(kwfilelog, self).add(text, meta, tr, link, p1, p2)
+
+        def size(self, rev):
+            '''Overrides filelog's size() to use kwfilelog.read().'''
+            node = revlog.node(self, rev)
+            if super(kwfilelog, self).renamed(node):
+                return len(self.read(node))
+            return revlog.size(self, rev)
+
+        def cmp(self, node, text):
+            '''Overrides filelog's cmp() to use kwfilelog.read().'''
+            if super(kwfilelog, self).renamed(node):
+                t2 = self.read(node)
+                return t2 != text
+
+    filelog.filelog = kwfilelog
+    repo.__class__ = kwrepo
+
+
+def pretxnkw(ui, repo, hooktype, **args):
+    '''pretxncommit hook that collects candidates for keyword expansion
+    on commit and expands keywords in working dir.'''
+
+    if hooktype != 'pretxncommit':
+        return True
+
+    cmd, sysargs, globalopts, cmdopts = commands.parse(ui, sys.argv[1:])[1:]
+    if repr(cmd).split()[1] in ('tag', 'import_'):
+        return False
+
+    files, match, anypats = cmdutil.matchpats(repo, sysargs, cmdopts)
+    modified, added = repo.status(files=files, match=match)[:2]
+    candidates = [f for f in modified + added if not f.startswith('.hg')]
+    if not candidates:
+        return False
+
+    files = []
+    for pat, opt in repo.ui.configitems('keyword'):
+        if opt == 'expand':
+            mf = util.matcher(repo.root, '', [pat], [], [])[1]
+            for candidate in candidates:
+                if mf(candidate) and candidate not in files:
+                    files.append(candidate)
+    if not files:
+        return False
+
+    for f in files:
+        data = repo.wfile(f).read()
+        if not util.binary(data):
+            data, kwct = re_kw.subn(lambda m:
+                    kwexpand(m, repo, f, changeid=args['node']),
+                    data)
+            if kwct:
+                ui.note(_('expanding keywords in %s\n' % f))
+                repo.wfile(f, 'w').write(data)


More information about the Mercurial-devel mailing list