hgkw/keyword.py
author Christian Ebert <blacktrash@gmx.net>
Wed, 20 Dec 2006 17:41:46 +0100
branchextension
changeset 43 fe5d9b9e78fc
parent 42 ba000e29ecf3
child 44 dc6e7d0e607f
permissions -rw-r--r--
Added tag cvs_scheme for changeset ba000e29ecf3
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
42
ba000e29ecf3 Implement near CVS compability with more than one keyword
Christian Ebert <blacktrash@gmx.net>
parents: 41
diff changeset
     4
hgkeywords = 'Id|Header|Author|Date|Revision|RCSFile|Source'
ba000e29ecf3 Implement near CVS compability with more than one keyword
Christian Ebert <blacktrash@gmx.net>
parents: 41
diff changeset
     5
33
ebb39c6a1476 Add original keyword extension by Thomas Arendsen Hain
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
     6
def reposetup(ui, repo):
ebb39c6a1476 Add original keyword extension by Thomas Arendsen Hain
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
     7
    if not repo.local():
ebb39c6a1476 Add original keyword extension by Thomas Arendsen Hain
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
     8
        return
ebb39c6a1476 Add original keyword extension by Thomas Arendsen Hain
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
     9
ebb39c6a1476 Add original keyword extension by Thomas Arendsen Hain
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
    10
    class kwrepo(repo.__class__):
ebb39c6a1476 Add original keyword extension by Thomas Arendsen Hain
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
    11
        def file(self, f):
ebb39c6a1476 Add original keyword extension by Thomas Arendsen Hain
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
    12
            if f[0] == '/':
ebb39c6a1476 Add original keyword extension by Thomas Arendsen Hain
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
    13
                f = f[1:]
ebb39c6a1476 Add original keyword extension by Thomas Arendsen Hain
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
    14
            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
    15
ebb39c6a1476 Add original keyword extension by Thomas Arendsen Hain
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
    16
    class kwfilelog(filelog.filelog):
ebb39c6a1476 Add original keyword extension by Thomas Arendsen Hain
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
    17
        def __init__(self, opener, path, repo,
ebb39c6a1476 Add original keyword extension by Thomas Arendsen Hain
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
    18
                     defversion=revlog.REVLOG_DEFAULT_VERSION):
ebb39c6a1476 Add original keyword extension by Thomas Arendsen Hain
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
    19
            super(kwfilelog, self).__init__(opener, path, defversion)
ebb39c6a1476 Add original keyword extension by Thomas Arendsen Hain
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
    20
            self._repo = repo
ebb39c6a1476 Add original keyword extension by Thomas Arendsen Hain
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
    21
            self._path = path
36
b3ace8cc5a33 Change config opt "expand" to accept filename patterns
Christian Ebert <blacktrash@gmx.net>
parents: 34
diff changeset
    22
33
ebb39c6a1476 Add original keyword extension by Thomas Arendsen Hain
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
    23
        def read(self, node):
42
ba000e29ecf3 Implement near CVS compability with more than one keyword
Christian Ebert <blacktrash@gmx.net>
parents: 41
diff changeset
    24
            data = super(kwfilelog, self).read(node)
ba000e29ecf3 Implement near CVS compability with more than one keyword
Christian Ebert <blacktrash@gmx.net>
parents: 41
diff changeset
    25
            if not util.binary(data):
33
ebb39c6a1476 Add original keyword extension by Thomas Arendsen Hain
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
    26
                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
    27
                                     filelog=self)
42
ba000e29ecf3 Implement near CVS compability with more than one keyword
Christian Ebert <blacktrash@gmx.net>
parents: 41
diff changeset
    28
                f = c.path()
36
b3ace8cc5a33 Change config opt "expand" to accept filename patterns
Christian Ebert <blacktrash@gmx.net>
parents: 34
diff changeset
    29
                for pat, opt in self._repo.ui.configitems('keyword'):
b3ace8cc5a33 Change config opt "expand" to accept filename patterns
Christian Ebert <blacktrash@gmx.net>
parents: 34
diff changeset
    30
                    if opt == 'expand':
b3ace8cc5a33 Change config opt "expand" to accept filename patterns
Christian Ebert <blacktrash@gmx.net>
parents: 34
diff changeset
    31
                        mf = util.matcher(self._repo.root,
b3ace8cc5a33 Change config opt "expand" to accept filename patterns
Christian Ebert <blacktrash@gmx.net>
parents: 34
diff changeset
    32
                                '', [pat], [], [])[1]
42
ba000e29ecf3 Implement near CVS compability with more than one keyword
Christian Ebert <blacktrash@gmx.net>
parents: 41
diff changeset
    33
                        if mf(f):
ba000e29ecf3 Implement near CVS compability with more than one keyword
Christian Ebert <blacktrash@gmx.net>
parents: 41
diff changeset
    34
                            re_kw = re.compile(r'\$(%s)\$' % hgkeywords)
ba000e29ecf3 Implement near CVS compability with more than one keyword
Christian Ebert <blacktrash@gmx.net>
parents: 41
diff changeset
    35
ba000e29ecf3 Implement near CVS compability with more than one keyword
Christian Ebert <blacktrash@gmx.net>
parents: 41
diff changeset
    36
                            def kwexpand(matchobj):
ba000e29ecf3 Implement near CVS compability with more than one keyword
Christian Ebert <blacktrash@gmx.net>
parents: 41
diff changeset
    37
                                RCSFile = os.path.basename(f)+',v'
ba000e29ecf3 Implement near CVS compability with more than one keyword
Christian Ebert <blacktrash@gmx.net>
parents: 41
diff changeset
    38
                                Source = os.path.join(self._repo.root, f)+',v'
ba000e29ecf3 Implement near CVS compability with more than one keyword
Christian Ebert <blacktrash@gmx.net>
parents: 41
diff changeset
    39
                                Revision = c.changectx()
ba000e29ecf3 Implement near CVS compability with more than one keyword
Christian Ebert <blacktrash@gmx.net>
parents: 41
diff changeset
    40
                                Date = util.datestr(date=c.date())
ba000e29ecf3 Implement near CVS compability with more than one keyword
Christian Ebert <blacktrash@gmx.net>
parents: 41
diff changeset
    41
                                Author = c.user()
ba000e29ecf3 Implement near CVS compability with more than one keyword
Christian Ebert <blacktrash@gmx.net>
parents: 41
diff changeset
    42
                                revdateauth = '%s %s %s' % (
ba000e29ecf3 Implement near CVS compability with more than one keyword
Christian Ebert <blacktrash@gmx.net>
parents: 41
diff changeset
    43
                                        Revision,
ba000e29ecf3 Implement near CVS compability with more than one keyword
Christian Ebert <blacktrash@gmx.net>
parents: 41
diff changeset
    44
                                        util.datestr(date=c.date(),
ba000e29ecf3 Implement near CVS compability with more than one keyword
Christian Ebert <blacktrash@gmx.net>
parents: 41
diff changeset
    45
                                            format=util.defaultdateformats[0]),
ba000e29ecf3 Implement near CVS compability with more than one keyword
Christian Ebert <blacktrash@gmx.net>
parents: 41
diff changeset
    46
                                        util.shortuser(Author))
ba000e29ecf3 Implement near CVS compability with more than one keyword
Christian Ebert <blacktrash@gmx.net>
parents: 41
diff changeset
    47
                                Header = '%s %s' % (Source, revdateauth)
ba000e29ecf3 Implement near CVS compability with more than one keyword
Christian Ebert <blacktrash@gmx.net>
parents: 41
diff changeset
    48
                                Id = '%s %s' % (RCSFile, revdateauth)
ba000e29ecf3 Implement near CVS compability with more than one keyword
Christian Ebert <blacktrash@gmx.net>
parents: 41
diff changeset
    49
                                return '$%s: %s $' % (
ba000e29ecf3 Implement near CVS compability with more than one keyword
Christian Ebert <blacktrash@gmx.net>
parents: 41
diff changeset
    50
                                        matchobj.group(1), eval(matchobj.group(1)))
ba000e29ecf3 Implement near CVS compability with more than one keyword
Christian Ebert <blacktrash@gmx.net>
parents: 41
diff changeset
    51
ba000e29ecf3 Implement near CVS compability with more than one keyword
Christian Ebert <blacktrash@gmx.net>
parents: 41
diff changeset
    52
                            return re_kw.sub(kwexpand, data)
33
ebb39c6a1476 Add original keyword extension by Thomas Arendsen Hain
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
    53
            return data
36
b3ace8cc5a33 Change config opt "expand" to accept filename patterns
Christian Ebert <blacktrash@gmx.net>
parents: 34
diff changeset
    54
33
ebb39c6a1476 Add original keyword extension by Thomas Arendsen Hain
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
    55
        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
    56
            if (not util.binary(text) and
36
b3ace8cc5a33 Change config opt "expand" to accept filename patterns
Christian Ebert <blacktrash@gmx.net>
parents: 34
diff changeset
    57
                   self._repo.ui.config('keyword', 'remove', True)):
42
ba000e29ecf3 Implement near CVS compability with more than one keyword
Christian Ebert <blacktrash@gmx.net>
parents: 41
diff changeset
    58
                re_kw = re.compile(r'\$(%s): [^$]+? \$' % hgkeywords)
ba000e29ecf3 Implement near CVS compability with more than one keyword
Christian Ebert <blacktrash@gmx.net>
parents: 41
diff changeset
    59
                text = re_kw.sub(r'$\1$', text)
33
ebb39c6a1476 Add original keyword extension by Thomas Arendsen Hain
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
    60
            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
    61
ebb39c6a1476 Add original keyword extension by Thomas Arendsen Hain
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
    62
    filelog.filelog = kwfilelog
ebb39c6a1476 Add original keyword extension by Thomas Arendsen Hain
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
    63
    repo.__class__ = kwrepo