hgkw/keyword.py
author Christian Ebert <blacktrash@gmx.net>
Thu, 21 Dec 2006 16:00:45 +0100
branchmodular
changeset 46 67e9fb23a32b
parent 44 dc6e7d0e607f
permissions -rw-r--r--
Make keyword.py depend on resurrected kwutil.py This makes pretxnkw perhaps a bit slower, but is more readable.

from mercurial.i18n import _
from mercurial import filelog, revlog, context, util
import re

def reposetup(ui, repo):
    try:
        from hgkw import kwutil
    except ImportError, e:
        raise util.Abort(_('%s\nkeyword extension needs package hgkw\n') % e)

    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 util.binary(data):
                return data

            c = context.filectx(self._repo, self._path, fileid=node,
                                 filelog=self)
            f = c.path()
            if f.startswith('.hg'):
                return data

            for pat, opt in self._repo.ui.configitems('keyword'):
                if opt == 'expand':
                    mf = util.matcher(self._repo.root,
                            '', [pat], [], [])[1]
                    if mf(f):

                        def kwexpand(matchobj):
                            return kwutil.kwexpand(matchobj,
                                    self._repo, c.changectx(), f,
                                    c.date(), c.user())

                        re_kw = re.compile(r'\$(%s)\$' % kwutil.hgkeywords)
                        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'\$(%s): [^$]+? \$' % kwutil.hgkeywords)
                text = re_kw.sub(r'$\1$', text)
            return super(kwfilelog, self).add(text, meta, tr, link, p1, p2)

    filelog.filelog = kwfilelog
    repo.__class__ = kwrepo