hgkw/keyword.py
author Christian Ebert <blacktrash@gmx.net>
Thu, 21 Dec 2006 16:00:45 +0100
branchmodular
changeset 46 67e9fb23a32b
parent 44 dc6e7d0e607f
permissions -rw-r--r--
Make keyword.py depend on resurrected kwutil.py This makes pretxnkw perhaps a bit slower, but is more readable.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
46
67e9fb23a32b Make keyword.py depend on resurrected kwutil.py
Christian Ebert <blacktrash@gmx.net>
parents: 44
diff changeset
     1
from mercurial.i18n import _
67e9fb23a32b Make keyword.py depend on resurrected kwutil.py
Christian Ebert <blacktrash@gmx.net>
parents: 44
diff changeset
     2
from mercurial import filelog, revlog, context, util
67e9fb23a32b Make keyword.py depend on resurrected kwutil.py
Christian Ebert <blacktrash@gmx.net>
parents: 44
diff changeset
     3
import re
42
ba000e29ecf3 Implement near CVS compability with more than one keyword
Christian Ebert <blacktrash@gmx.net>
parents: 41
diff changeset
     4
33
ebb39c6a1476 Add original keyword extension by Thomas Arendsen Hain
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
     5
def reposetup(ui, repo):
46
67e9fb23a32b Make keyword.py depend on resurrected kwutil.py
Christian Ebert <blacktrash@gmx.net>
parents: 44
diff changeset
     6
    try:
67e9fb23a32b Make keyword.py depend on resurrected kwutil.py
Christian Ebert <blacktrash@gmx.net>
parents: 44
diff changeset
     7
        from hgkw import kwutil
67e9fb23a32b Make keyword.py depend on resurrected kwutil.py
Christian Ebert <blacktrash@gmx.net>
parents: 44
diff changeset
     8
    except ImportError, e:
67e9fb23a32b Make keyword.py depend on resurrected kwutil.py
Christian Ebert <blacktrash@gmx.net>
parents: 44
diff changeset
     9
        raise util.Abort(_('%s\nkeyword extension needs package hgkw\n') % e)
67e9fb23a32b Make keyword.py depend on resurrected kwutil.py
Christian Ebert <blacktrash@gmx.net>
parents: 44
diff changeset
    10
33
ebb39c6a1476 Add original keyword extension by Thomas Arendsen Hain
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
    11
    if not repo.local():
ebb39c6a1476 Add original keyword extension by Thomas Arendsen Hain
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
    12
        return
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 kwrepo(repo.__class__):
ebb39c6a1476 Add original keyword extension by Thomas Arendsen Hain
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
    15
        def file(self, f):
ebb39c6a1476 Add original keyword extension by Thomas Arendsen Hain
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
    16
            if f[0] == '/':
ebb39c6a1476 Add original keyword extension by Thomas Arendsen Hain
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
    17
                f = f[1:]
ebb39c6a1476 Add original keyword extension by Thomas Arendsen Hain
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
    18
            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
    19
ebb39c6a1476 Add original keyword extension by Thomas Arendsen Hain
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
    20
    class kwfilelog(filelog.filelog):
ebb39c6a1476 Add original keyword extension by Thomas Arendsen Hain
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
    21
        def __init__(self, opener, path, repo,
ebb39c6a1476 Add original keyword extension by Thomas Arendsen Hain
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
    22
                     defversion=revlog.REVLOG_DEFAULT_VERSION):
ebb39c6a1476 Add original keyword extension by Thomas Arendsen Hain
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
    23
            super(kwfilelog, self).__init__(opener, path, defversion)
ebb39c6a1476 Add original keyword extension by Thomas Arendsen Hain
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
    24
            self._repo = repo
ebb39c6a1476 Add original keyword extension by Thomas Arendsen Hain
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
    25
            self._path = path
36
b3ace8cc5a33 Change config opt "expand" to accept filename patterns
Christian Ebert <blacktrash@gmx.net>
parents: 34
diff changeset
    26
33
ebb39c6a1476 Add original keyword extension by Thomas Arendsen Hain
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
    27
        def read(self, node):
42
ba000e29ecf3 Implement near CVS compability with more than one keyword
Christian Ebert <blacktrash@gmx.net>
parents: 41
diff changeset
    28
            data = super(kwfilelog, self).read(node)
44
dc6e7d0e607f Exclude tag cmd from pretxcommit hook; exclude .hg files
Christian Ebert <blacktrash@gmx.net>
parents: 42
diff changeset
    29
            if util.binary(data):
dc6e7d0e607f Exclude tag cmd from pretxcommit hook; exclude .hg files
Christian Ebert <blacktrash@gmx.net>
parents: 42
diff changeset
    30
                return data
dc6e7d0e607f Exclude tag cmd from pretxcommit hook; exclude .hg files
Christian Ebert <blacktrash@gmx.net>
parents: 42
diff changeset
    31
dc6e7d0e607f Exclude tag cmd from pretxcommit hook; exclude .hg files
Christian Ebert <blacktrash@gmx.net>
parents: 42
diff changeset
    32
            c = context.filectx(self._repo, self._path, fileid=node,
dc6e7d0e607f Exclude tag cmd from pretxcommit hook; exclude .hg files
Christian Ebert <blacktrash@gmx.net>
parents: 42
diff changeset
    33
                                 filelog=self)
dc6e7d0e607f Exclude tag cmd from pretxcommit hook; exclude .hg files
Christian Ebert <blacktrash@gmx.net>
parents: 42
diff changeset
    34
            f = c.path()
dc6e7d0e607f Exclude tag cmd from pretxcommit hook; exclude .hg files
Christian Ebert <blacktrash@gmx.net>
parents: 42
diff changeset
    35
            if f.startswith('.hg'):
dc6e7d0e607f Exclude tag cmd from pretxcommit hook; exclude .hg files
Christian Ebert <blacktrash@gmx.net>
parents: 42
diff changeset
    36
                return data
dc6e7d0e607f Exclude tag cmd from pretxcommit hook; exclude .hg files
Christian Ebert <blacktrash@gmx.net>
parents: 42
diff changeset
    37
dc6e7d0e607f Exclude tag cmd from pretxcommit hook; exclude .hg files
Christian Ebert <blacktrash@gmx.net>
parents: 42
diff changeset
    38
            for pat, opt in self._repo.ui.configitems('keyword'):
dc6e7d0e607f Exclude tag cmd from pretxcommit hook; exclude .hg files
Christian Ebert <blacktrash@gmx.net>
parents: 42
diff changeset
    39
                if opt == 'expand':
dc6e7d0e607f Exclude tag cmd from pretxcommit hook; exclude .hg files
Christian Ebert <blacktrash@gmx.net>
parents: 42
diff changeset
    40
                    mf = util.matcher(self._repo.root,
dc6e7d0e607f Exclude tag cmd from pretxcommit hook; exclude .hg files
Christian Ebert <blacktrash@gmx.net>
parents: 42
diff changeset
    41
                            '', [pat], [], [])[1]
dc6e7d0e607f Exclude tag cmd from pretxcommit hook; exclude .hg files
Christian Ebert <blacktrash@gmx.net>
parents: 42
diff changeset
    42
                    if mf(f):
42
ba000e29ecf3 Implement near CVS compability with more than one keyword
Christian Ebert <blacktrash@gmx.net>
parents: 41
diff changeset
    43
44
dc6e7d0e607f Exclude tag cmd from pretxcommit hook; exclude .hg files
Christian Ebert <blacktrash@gmx.net>
parents: 42
diff changeset
    44
                        def kwexpand(matchobj):
46
67e9fb23a32b Make keyword.py depend on resurrected kwutil.py
Christian Ebert <blacktrash@gmx.net>
parents: 44
diff changeset
    45
                            return kwutil.kwexpand(matchobj,
67e9fb23a32b Make keyword.py depend on resurrected kwutil.py
Christian Ebert <blacktrash@gmx.net>
parents: 44
diff changeset
    46
                                    self._repo, c.changectx(), f,
67e9fb23a32b Make keyword.py depend on resurrected kwutil.py
Christian Ebert <blacktrash@gmx.net>
parents: 44
diff changeset
    47
                                    c.date(), c.user())
42
ba000e29ecf3 Implement near CVS compability with more than one keyword
Christian Ebert <blacktrash@gmx.net>
parents: 41
diff changeset
    48
46
67e9fb23a32b Make keyword.py depend on resurrected kwutil.py
Christian Ebert <blacktrash@gmx.net>
parents: 44
diff changeset
    49
                        re_kw = re.compile(r'\$(%s)\$' % kwutil.hgkeywords)
44
dc6e7d0e607f Exclude tag cmd from pretxcommit hook; exclude .hg files
Christian Ebert <blacktrash@gmx.net>
parents: 42
diff changeset
    50
                        return re_kw.sub(kwexpand, data)
33
ebb39c6a1476 Add original keyword extension by Thomas Arendsen Hain
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
    51
            return data
36
b3ace8cc5a33 Change config opt "expand" to accept filename patterns
Christian Ebert <blacktrash@gmx.net>
parents: 34
diff changeset
    52
33
ebb39c6a1476 Add original keyword extension by Thomas Arendsen Hain
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
    53
        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
    54
            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
    55
                   self._repo.ui.config('keyword', 'remove', True)):
46
67e9fb23a32b Make keyword.py depend on resurrected kwutil.py
Christian Ebert <blacktrash@gmx.net>
parents: 44
diff changeset
    56
                re_kw = re.compile(r'\$(%s): [^$]+? \$' % kwutil.hgkeywords)
42
ba000e29ecf3 Implement near CVS compability with more than one keyword
Christian Ebert <blacktrash@gmx.net>
parents: 41
diff changeset
    57
                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
    58
            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
    59
ebb39c6a1476 Add original keyword extension by Thomas Arendsen Hain
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
    60
    filelog.filelog = kwfilelog
ebb39c6a1476 Add original keyword extension by Thomas Arendsen Hain
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
    61
    repo.__class__ = kwrepo