hgkw/keyword.py
author Christian Ebert <blacktrash@gmx.net>
Tue, 19 Dec 2006 09:39:03 +0100
branchextension
changeset 33 ebb39c6a1476
child 34 3a79665dcbc3
permissions -rw-r--r--
Add original keyword extension by Thomas Arendsen Hain

from mercurial import hg, filelog, revlog, context, util
import 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):
            data = super(kwfilelog, self).read(node)
            if (not util.binary(data) and
                self._repo.ui.config("keywords", "expand", True)):
                c = context.filectx(self._repo, self._path, fileid=node,
                                     filelog=self)
                def author(): return "$Author %s $" % c.user()
                def date(): return "$Date %s $" % util.datestr(c.date())

                data = re.sub(r'\$Author\$', author(), data)
                data = re.sub(r'\$Date\$', date(), data)
            return data
        def add(self, text, meta, tr, link, p1=None, p2=None):
            if (not util.binary(text) and
                   self._repo.ui.config("keywords", "remove", True)):
                text = re.sub(r'\$Author[^\$]*\$', '$Author$', text)
                text = re.sub(r'\$Date[^\$]*\$', '$Date$', text)
            return super(kwfilelog, self).add(text, meta, tr, link, p1, p2)

    filelog.filelog = kwfilelog
    repo.__class__ = kwrepo