hgkw/keyword.py
author Christian Ebert <blacktrash@gmx.net>
Tue, 19 Dec 2006 09:56:41 +0100
branchextension
changeset 34 3a79665dcbc3
parent 33 ebb39c6a1476
child 36 b3ace8cc5a33
permissions -rw-r--r--
Make keyword do my hgkeyword; use basename again add() needs only str.replace(). TODO: Make even more compatible with cvs' $Id$? (date UTC, slashes) Check config before reading. And!: Use config file patterns, in cooperation with pretxnkw. This will expand keywords alread on commit!
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
33
ebb39c6a1476 Add original keyword extension by Thomas Arendsen Hain
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
     1
from mercurial import hg, filelog, revlog, context, util
34
3a79665dcbc3 Make keyword do my hgkeyword; use basename again
Christian Ebert <blacktrash@gmx.net>
parents: 33
diff changeset
     2
import os.path, re
33
ebb39c6a1476 Add original keyword extension by Thomas Arendsen Hain
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
     3
ebb39c6a1476 Add original keyword extension by Thomas Arendsen Hain
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
     4
def reposetup(ui, repo):
ebb39c6a1476 Add original keyword extension by Thomas Arendsen Hain
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
     5
    if not repo.local():
ebb39c6a1476 Add original keyword extension by Thomas Arendsen Hain
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
     6
        return
ebb39c6a1476 Add original keyword extension by Thomas Arendsen Hain
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
     7
ebb39c6a1476 Add original keyword extension by Thomas Arendsen Hain
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
     8
    class kwrepo(repo.__class__):
ebb39c6a1476 Add original keyword extension by Thomas Arendsen Hain
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
     9
        def file(self, f):
ebb39c6a1476 Add original keyword extension by Thomas Arendsen Hain
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
    10
            if f[0] == '/':
ebb39c6a1476 Add original keyword extension by Thomas Arendsen Hain
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
    11
                f = f[1:]
ebb39c6a1476 Add original keyword extension by Thomas Arendsen Hain
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
    12
            return filelog.filelog(self.sopener, f, self, self.revlogversion)
ebb39c6a1476 Add original keyword extension by Thomas Arendsen Hain
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
    13
ebb39c6a1476 Add original keyword extension by Thomas Arendsen Hain
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
    14
    class kwfilelog(filelog.filelog):
ebb39c6a1476 Add original keyword extension by Thomas Arendsen Hain
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
    15
        def __init__(self, opener, path, repo,
ebb39c6a1476 Add original keyword extension by Thomas Arendsen Hain
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
    16
                     defversion=revlog.REVLOG_DEFAULT_VERSION):
ebb39c6a1476 Add original keyword extension by Thomas Arendsen Hain
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
    17
            super(kwfilelog, self).__init__(opener, path, defversion)
ebb39c6a1476 Add original keyword extension by Thomas Arendsen Hain
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
    18
            self._repo = repo
ebb39c6a1476 Add original keyword extension by Thomas Arendsen Hain
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
    19
            self._path = path
ebb39c6a1476 Add original keyword extension by Thomas Arendsen Hain
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
    20
        def read(self, node):
ebb39c6a1476 Add original keyword extension by Thomas Arendsen Hain
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
    21
            data = super(kwfilelog, self).read(node)
ebb39c6a1476 Add original keyword extension by Thomas Arendsen Hain
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
    22
            if (not util.binary(data) and
ebb39c6a1476 Add original keyword extension by Thomas Arendsen Hain
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
    23
                self._repo.ui.config("keywords", "expand", True)):
ebb39c6a1476 Add original keyword extension by Thomas Arendsen Hain
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
    24
                c = context.filectx(self._repo, self._path, fileid=node,
ebb39c6a1476 Add original keyword extension by Thomas Arendsen Hain
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
    25
                                     filelog=self)
34
3a79665dcbc3 Make keyword do my hgkeyword; use basename again
Christian Ebert <blacktrash@gmx.net>
parents: 33
diff changeset
    26
                hgkw = '$Hg: %s,v %s %s %s $' % (
3a79665dcbc3 Make keyword do my hgkeyword; use basename again
Christian Ebert <blacktrash@gmx.net>
parents: 33
diff changeset
    27
                        os.path.basename(c.path()),
3a79665dcbc3 Make keyword do my hgkeyword; use basename again
Christian Ebert <blacktrash@gmx.net>
parents: 33
diff changeset
    28
                        c.changectx(),
3a79665dcbc3 Make keyword do my hgkeyword; use basename again
Christian Ebert <blacktrash@gmx.net>
parents: 33
diff changeset
    29
                        util.datestr(date=c.date(),
3a79665dcbc3 Make keyword do my hgkeyword; use basename again
Christian Ebert <blacktrash@gmx.net>
parents: 33
diff changeset
    30
                            format=util.defaultdateformats[0]),
3a79665dcbc3 Make keyword do my hgkeyword; use basename again
Christian Ebert <blacktrash@gmx.net>
parents: 33
diff changeset
    31
                        util.shortuser(c.user()))
3a79665dcbc3 Make keyword do my hgkeyword; use basename again
Christian Ebert <blacktrash@gmx.net>
parents: 33
diff changeset
    32
                data = data.replace('$Hg$', hgkw)
33
ebb39c6a1476 Add original keyword extension by Thomas Arendsen Hain
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
    33
            return data
ebb39c6a1476 Add original keyword extension by Thomas Arendsen Hain
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
    34
        def add(self, text, meta, tr, link, p1=None, p2=None):
ebb39c6a1476 Add original keyword extension by Thomas Arendsen Hain
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
    35
            if (not util.binary(text) and
ebb39c6a1476 Add original keyword extension by Thomas Arendsen Hain
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
    36
                   self._repo.ui.config("keywords", "remove", True)):
34
3a79665dcbc3 Make keyword do my hgkeyword; use basename again
Christian Ebert <blacktrash@gmx.net>
parents: 33
diff changeset
    37
                text = re.sub(r'\$Hg[^$]*?\$', '$Hg$', text)
33
ebb39c6a1476 Add original keyword extension by Thomas Arendsen Hain
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
    38
            return super(kwfilelog, self).add(text, meta, tr, link, p1, p2)
ebb39c6a1476 Add original keyword extension by Thomas Arendsen Hain
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
    39
ebb39c6a1476 Add original keyword extension by Thomas Arendsen Hain
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
    40
    filelog.filelog = kwfilelog
ebb39c6a1476 Add original keyword extension by Thomas Arendsen Hain
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
    41
    repo.__class__ = kwrepo