hgkw/keyword.py
author Christian Ebert <blacktrash@gmx.net>
Thu, 21 Dec 2006 00:48:56 +0100
branchextension
changeset 44 dc6e7d0e607f
parent 42 ba000e29ecf3
child 46 67e9fb23a32b
child 47 0617e7d497f6
permissions -rw-r--r--
Exclude tag cmd from pretxcommit hook; exclude .hg files Return unchanged data in superclass read() after every if clause. Perhaps a bit more readable.

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

hgkeywords = 'Id|Header|Author|Date|Revision|RCSFile|Source'

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 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):
                        re_kw = re.compile(r'\$(%s)\$' % hgkeywords)

                        def kwexpand(matchobj):
                            RCSFile = os.path.basename(f)+',v'
                            Source = os.path.join(self._repo.root, f)+',v'
                            Revision = c.changectx()
                            Date = util.datestr(date=c.date())
                            Author = c.user()
                            revdateauth = '%s %s %s' % (
                                    Revision,
                                    util.datestr(date=c.date(),
                                        format=util.defaultdateformats[0]),
                                    util.shortuser(Author))
                            Header = '%s %s' % (Source, revdateauth)
                            Id = '%s %s' % (RCSFile, revdateauth)
                            return '$%s: %s $' % (
                                    matchobj.group(1), eval(matchobj.group(1)))

                        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): [^$]+? \$' % 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