hgkw/keyword.py
author Christian Ebert <blacktrash@gmx.net>
Tue, 19 Dec 2006 15:41:45 +0100
branchextension
changeset 36 b3ace8cc5a33
parent 34 3a79665dcbc3
child 41 e1c539e1282b
permissions -rw-r--r--
Change config opt "expand" to accept filename patterns Not nice yet; but passes tests together with pretxnkw. TODO: Find out what filelog.add() does exactly.

from mercurial import hg, filelog, revlog, context, util
import os.path, re

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):
            def gethgkw():
                c = context.filectx(self._repo, self._path, fileid=node,
                                     filelog=self)
                filename = 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)
            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)):
                text = re.sub(r'\$Hg[^$]*?\$', '$Hg$', text)
            return super(kwfilelog, self).add(text, meta, tr, link, p1, p2)

    filelog.filelog = kwfilelog
    repo.__class__ = kwrepo