hgkw/keyword.py
author Christian Ebert <blacktrash@gmx.net>
Fri, 12 Jan 2007 01:20:51 +0100
branchself_initializing_hook
changeset 89 16f1a5ed70ce
parent 88 ffec227fced3
child 90 2e930f842242
permissions -rw-r--r--
Functions instead of variables in kwexpand() Should be slightly faster as filectx is asked only on demand.

# keyword.py - keyword expansion for mercurial
# $Id$

'''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$ and their $keyword: substition $ are:
    Revision: changeset id
    Author:   full username
    Date:     %a %b %d %H:%M:%S %Y %z $
    RCSFile:  basename,v
    Source:   /path/to/basename,v
    Id:       basename,v csetid %Y-%m-%d %H:%M:%S %z shortname
    Header:   /path/to/basename,v csetid %Y-%m-%d %H:%M:%S %z shortname

Simple setup in hgrc:

    # enable extension
    hgext.keyword =
    # or, if script not in hgext folder:
    # hgext.keyword = /full/path/to/script
    
    # filename patterns for expansion are configured in this section
    [keyword]
    **.py = expand
    ...
'''

from mercurial import context, 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)
    def Revision():
        return c.changectx()
    def Author():
        return c.user()
    def Date():
        return util.datestr(date=c.date())
    def RCSFile():
        return os.path.basename(path)+',v'
    def Source():
        return repo.wjoin(path)+',v'
    def revdateauth():
        return '%s %s %s' % (c.changectx(),
            util.datestr(date=c.date(), format=util.defaultdateformats[0]),
            util.shortuser(c.user()))
    def Header():
        return '%s %s' % (Source(), revdateauth())
    def Id():
        return '%s %s' % (RCSFile(), revdateauth())
    return '$%s: %s $' % (matchobj.group(1), eval('%s()' % matchobj.group(1)))

def kwfmatches(ui, repo, files):
    '''Selects candidates for keyword substitution
    configured in keyword section in hgrc.'''
    files = [f for f in files if not f.startswith('.hg')]
    if not files:
        return []
    candidates = []
    kwfmatchers = [util.matcher(repo.root, '', [pat], [], [])[1]
            for pat, opt in ui.configitems('keyword') if opt == 'expand']
    for f in files:
        for mf in kwfmatchers:
            if mf(f):
                candidates.append(f)
                break
    return candidates


def reposetup(ui, repo):
    from mercurial import filelog, revlog

    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):
        '''
        Superclass over filelog to customize it's read, add, cmp methods.
        Keywords are "stored" unexpanded, and expanded on reading.
        '''
        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 iskwcandidate(self, data):
            '''Decides whether to act on keywords.'''
            return (kwfmatches(ui, self._repo, [self._path])
                    and not util.binary(data))

        def read(self, node):
            '''Substitutes keywords when reading filelog.'''
            data = super(kwfilelog, self).read(node)
            if self.iskwcandidate(data):
                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):
            '''Removes keyword substitutions when adding to filelog.'''
            if self.iskwcandidate(text):
                text = re_kw.sub(r'$\1$', text)
            return super(kwfilelog, self).add(text,
                    meta, tr, link, p1=None, p2=None)

        def cmp(self, node, text):
            '''Removes keyword substitutions for comparison.'''
            if self.iskwcandidate(text):
                text = re_kw.sub(r'$\1$', text)
            return super(kwfilelog, self).cmp(node, text)

    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


def pretxnkw(ui, repo, hooktype, **args):
    '''pretxncommit hook that collects candidates for keyword expansion
    on commit and expands keywords in working dir.'''
    from mercurial.i18n import gettext as _
    # above line for backwards compatibility; can be changed to
    #   from mercurial.i18n import _
    # some day
    from mercurial import cmdutil, commands

    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]

    for f in kwfmatches(ui, repo, modified+added):
        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.debug(_('overwriting %s expanding keywords\n' % f))
                repo.wfile(f, 'w').write(data)