hgkw/keyword.py
author Christian Ebert <blacktrash@gmx.net>
Sun, 14 Jan 2007 14:00:45 +0100
branchself_initializing_hook
changeset 96 682e684a15e9
parent 94 d34486cc99b0
child 98 121da9c0a325
permissions -rw-r--r--
Expansion entirely in kwfilectx.expand()

# 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:   short 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 shortname
    Header:   /path/to/basename,v csetid %Y/%m/%d %H:%M:%S 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, time

re_kw = re.compile(
        r'\$(Id|Header|Author|Date|Revision|RCSFile|Source)[^$]*?\$')

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 utcdate(date):
    '''Returns hgdate in cvs-like UTC format.'''
    return time.strftime('%Y/%m/%d %H:%M:%S', time.gmtime(date[0]))


class kwfilectx(context.filectx):
    '''
    Provides keyword expansion functions based on file context.
    '''
    def __init__(self, repo, path, changeid=None, fileid=None, filelog=None):
        context.filectx.__init__(self, repo, path, changeid, fileid, filelog)
    def Revision(self):
        return str(self.changectx())
    def Author(self):
        return util.shortuser(self.user())
    def Date(self):
        return utcdate(self.date())
    def RCSFile(self):
        return os.path.basename(self._path)+',v'
    def Source(self):
        return self._repo.wjoin(self._path)+',v'
    def Header(self):
        return ' '.join(
                [self.Source(), self.Revision(), self.Date(), self.Author()])
    def Id(self):
        return ' '.join(
                [self.RCSFile(), self.Revision(), self.Date(), self.Author()])
    def expand(self, mobj):
        '''Called from kwexpand, evaluates keyword.'''
        kw = mobj.group(1)
        return '$%s: %s $' % (kw, eval('self.%s()' % kw))


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):
                kwfctx = kwfilectx(self._repo, self._path,
                            fileid=node, filelog=self)
                return re_kw.sub(kwfctx.expand, 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=p1, p2=p2)

        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):
            kwfctx = kwfilectx(repo, f, changeid=args['node'])
            data, kwct = re_kw.subn(kwfctx.expand, data)
            if kwct:
                ui.debug(_('overwriting %s expanding keywords\n' % f))
                repo.wfile(f, 'w').write(data)