hgkw/keyword.py
author Christian Ebert <blacktrash@gmx.net>
Thu, 28 Dec 2006 18:41:54 +0100
branchsolo-extension
changeset 51 1ecb6ec5d16b
parent 50 fe891f87a16c
child 52 7783eb22de30
permissions -rw-r--r--
Reintroduce simpler kwfilelog.add(); simplify kwfilelog.read() Without add() strange things happen to changectx().
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
48
59fedb6b41da add header and start documentation
Christian Ebert <blacktrash@gmx.net>
parents: 47
diff changeset
     1
# keyword.py - keyword expansion for mercurial
59fedb6b41da add header and start documentation
Christian Ebert <blacktrash@gmx.net>
parents: 47
diff changeset
     2
#
59fedb6b41da add header and start documentation
Christian Ebert <blacktrash@gmx.net>
parents: 47
diff changeset
     3
# Copyright 2006 Christian Ebert <blacktrash@gmx.net>
59fedb6b41da add header and start documentation
Christian Ebert <blacktrash@gmx.net>
parents: 47
diff changeset
     4
#
59fedb6b41da add header and start documentation
Christian Ebert <blacktrash@gmx.net>
parents: 47
diff changeset
     5
# This software may be used and distributed according to the terms
59fedb6b41da add header and start documentation
Christian Ebert <blacktrash@gmx.net>
parents: 47
diff changeset
     6
# of the GNU General Public License, incorporated herein by reference.
59fedb6b41da add header and start documentation
Christian Ebert <blacktrash@gmx.net>
parents: 47
diff changeset
     7
59fedb6b41da add header and start documentation
Christian Ebert <blacktrash@gmx.net>
parents: 47
diff changeset
     8
'''keyword expansion hack against the grain of a DSCM
59fedb6b41da add header and start documentation
Christian Ebert <blacktrash@gmx.net>
parents: 47
diff changeset
     9
59fedb6b41da add header and start documentation
Christian Ebert <blacktrash@gmx.net>
parents: 47
diff changeset
    10
This extension lets you expand RCS/CVS-like keywords in a Mercurial
59fedb6b41da add header and start documentation
Christian Ebert <blacktrash@gmx.net>
parents: 47
diff changeset
    11
repository.
59fedb6b41da add header and start documentation
Christian Ebert <blacktrash@gmx.net>
parents: 47
diff changeset
    12
59fedb6b41da add header and start documentation
Christian Ebert <blacktrash@gmx.net>
parents: 47
diff changeset
    13
There are many good reasons why this is not needed in a distributed
59fedb6b41da add header and start documentation
Christian Ebert <blacktrash@gmx.net>
parents: 47
diff changeset
    14
SCM, still it may be useful in very small projects based on single
59fedb6b41da add header and start documentation
Christian Ebert <blacktrash@gmx.net>
parents: 47
diff changeset
    15
files (like LaTeX packages), that are mostly addressed to an audience
59fedb6b41da add header and start documentation
Christian Ebert <blacktrash@gmx.net>
parents: 47
diff changeset
    16
not running version control.
59fedb6b41da add header and start documentation
Christian Ebert <blacktrash@gmx.net>
parents: 47
diff changeset
    17
59fedb6b41da add header and start documentation
Christian Ebert <blacktrash@gmx.net>
parents: 47
diff changeset
    18
The extension consists actually in 2 parts:
59fedb6b41da add header and start documentation
Christian Ebert <blacktrash@gmx.net>
parents: 47
diff changeset
    19
59fedb6b41da add header and start documentation
Christian Ebert <blacktrash@gmx.net>
parents: 47
diff changeset
    20
    1. extension code (reposetup) that is triggered on checkout and
59fedb6b41da add header and start documentation
Christian Ebert <blacktrash@gmx.net>
parents: 47
diff changeset
    21
       logging of changes.
59fedb6b41da add header and start documentation
Christian Ebert <blacktrash@gmx.net>
parents: 47
diff changeset
    22
    2. a pretxncommit hook (hgrc (5)) that expands keywords immediately
59fedb6b41da add header and start documentation
Christian Ebert <blacktrash@gmx.net>
parents: 47
diff changeset
    23
       at commit time in the working directory.
59fedb6b41da add header and start documentation
Christian Ebert <blacktrash@gmx.net>
parents: 47
diff changeset
    24
59fedb6b41da add header and start documentation
Christian Ebert <blacktrash@gmx.net>
parents: 47
diff changeset
    25
Simple setup in hgrc:
59fedb6b41da add header and start documentation
Christian Ebert <blacktrash@gmx.net>
parents: 47
diff changeset
    26
59fedb6b41da add header and start documentation
Christian Ebert <blacktrash@gmx.net>
parents: 47
diff changeset
    27
    # enable extension
59fedb6b41da add header and start documentation
Christian Ebert <blacktrash@gmx.net>
parents: 47
diff changeset
    28
    hgext.keyword =
59fedb6b41da add header and start documentation
Christian Ebert <blacktrash@gmx.net>
parents: 47
diff changeset
    29
    
59fedb6b41da add header and start documentation
Christian Ebert <blacktrash@gmx.net>
parents: 47
diff changeset
    30
    # filename patterns for expansion are configured in this section
59fedb6b41da add header and start documentation
Christian Ebert <blacktrash@gmx.net>
parents: 47
diff changeset
    31
    [keyword]
59fedb6b41da add header and start documentation
Christian Ebert <blacktrash@gmx.net>
parents: 47
diff changeset
    32
    *.sty = expand
59fedb6b41da add header and start documentation
Christian Ebert <blacktrash@gmx.net>
parents: 47
diff changeset
    33
    ...
59fedb6b41da add header and start documentation
Christian Ebert <blacktrash@gmx.net>
parents: 47
diff changeset
    34
59fedb6b41da add header and start documentation
Christian Ebert <blacktrash@gmx.net>
parents: 47
diff changeset
    35
    # set up pretxncommit hook
59fedb6b41da add header and start documentation
Christian Ebert <blacktrash@gmx.net>
parents: 47
diff changeset
    36
    [hooks]
59fedb6b41da add header and start documentation
Christian Ebert <blacktrash@gmx.net>
parents: 47
diff changeset
    37
    pretxncommit =
59fedb6b41da add header and start documentation
Christian Ebert <blacktrash@gmx.net>
parents: 47
diff changeset
    38
    pretxncommit.keyword = python:hgext.keyword.pretxnkw
59fedb6b41da add header and start documentation
Christian Ebert <blacktrash@gmx.net>
parents: 47
diff changeset
    39
'''
59fedb6b41da add header and start documentation
Christian Ebert <blacktrash@gmx.net>
parents: 47
diff changeset
    40
47
0617e7d497f6 Branch standalone extension, including pretxncommit hook function
Christian Ebert <blacktrash@gmx.net>
parents: 44
diff changeset
    41
from mercurial.i18n import _
49
5acfe7dc3086 Override filelog's size(), cmp() as well; remove add()
Christian Ebert <blacktrash@gmx.net>
parents: 48
diff changeset
    42
from mercurial import cmdutil, commands, context, filelog, revlog, util
47
0617e7d497f6 Branch standalone extension, including pretxncommit hook function
Christian Ebert <blacktrash@gmx.net>
parents: 44
diff changeset
    43
import os.path, re, sys
0617e7d497f6 Branch standalone extension, including pretxncommit hook function
Christian Ebert <blacktrash@gmx.net>
parents: 44
diff changeset
    44
0617e7d497f6 Branch standalone extension, including pretxncommit hook function
Christian Ebert <blacktrash@gmx.net>
parents: 44
diff changeset
    45
# supported keywords for use in regexes
0617e7d497f6 Branch standalone extension, including pretxncommit hook function
Christian Ebert <blacktrash@gmx.net>
parents: 44
diff changeset
    46
hgkeywords = 'Id|Header|Author|Date|Revision|RCSFile|Source'
0617e7d497f6 Branch standalone extension, including pretxncommit hook function
Christian Ebert <blacktrash@gmx.net>
parents: 44
diff changeset
    47
0617e7d497f6 Branch standalone extension, including pretxncommit hook function
Christian Ebert <blacktrash@gmx.net>
parents: 44
diff changeset
    48
def kwexpand(matchobj, repo, Revision, f, date, Author):
0617e7d497f6 Branch standalone extension, including pretxncommit hook function
Christian Ebert <blacktrash@gmx.net>
parents: 44
diff changeset
    49
    '''Called by kwfilelog.read and pretxnkw.
0617e7d497f6 Branch standalone extension, including pretxncommit hook function
Christian Ebert <blacktrash@gmx.net>
parents: 44
diff changeset
    50
    Sets supported keywords as local variables and evaluates them to
0617e7d497f6 Branch standalone extension, including pretxncommit hook function
Christian Ebert <blacktrash@gmx.net>
parents: 44
diff changeset
    51
    their expansion if matchobj is equal to string representation.'''
33
ebb39c6a1476 Add original keyword extension by Thomas Arendsen Hain
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
    52
47
0617e7d497f6 Branch standalone extension, including pretxncommit hook function
Christian Ebert <blacktrash@gmx.net>
parents: 44
diff changeset
    53
    RCSFile = os.path.basename(f)+',v'
0617e7d497f6 Branch standalone extension, including pretxncommit hook function
Christian Ebert <blacktrash@gmx.net>
parents: 44
diff changeset
    54
    Source = os.path.join(repo.root, f)+',v'
0617e7d497f6 Branch standalone extension, including pretxncommit hook function
Christian Ebert <blacktrash@gmx.net>
parents: 44
diff changeset
    55
    Date = util.datestr(date=date)
0617e7d497f6 Branch standalone extension, including pretxncommit hook function
Christian Ebert <blacktrash@gmx.net>
parents: 44
diff changeset
    56
    revdateauth = '%s %s %s' % (Revision,       # %Y-%m-%d %H:%M:%S
0617e7d497f6 Branch standalone extension, including pretxncommit hook function
Christian Ebert <blacktrash@gmx.net>
parents: 44
diff changeset
    57
            util.datestr(date=date, format=util.defaultdateformats[0]),
0617e7d497f6 Branch standalone extension, including pretxncommit hook function
Christian Ebert <blacktrash@gmx.net>
parents: 44
diff changeset
    58
            util.shortuser(Author))
0617e7d497f6 Branch standalone extension, including pretxncommit hook function
Christian Ebert <blacktrash@gmx.net>
parents: 44
diff changeset
    59
    Header = '%s %s' % (Source, revdateauth)
0617e7d497f6 Branch standalone extension, including pretxncommit hook function
Christian Ebert <blacktrash@gmx.net>
parents: 44
diff changeset
    60
    Id = '%s %s' % (RCSFile, revdateauth)
0617e7d497f6 Branch standalone extension, including pretxncommit hook function
Christian Ebert <blacktrash@gmx.net>
parents: 44
diff changeset
    61
0617e7d497f6 Branch standalone extension, including pretxncommit hook function
Christian Ebert <blacktrash@gmx.net>
parents: 44
diff changeset
    62
    return '$%s: %s $' % (matchobj.group(1), eval(matchobj.group(1)))
0617e7d497f6 Branch standalone extension, including pretxncommit hook function
Christian Ebert <blacktrash@gmx.net>
parents: 44
diff changeset
    63
42
ba000e29ecf3 Implement near CVS compability with more than one keyword
Christian Ebert <blacktrash@gmx.net>
parents: 41
diff changeset
    64
33
ebb39c6a1476 Add original keyword extension by Thomas Arendsen Hain
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
    65
def reposetup(ui, repo):
ebb39c6a1476 Add original keyword extension by Thomas Arendsen Hain
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
    66
    if not repo.local():
ebb39c6a1476 Add original keyword extension by Thomas Arendsen Hain
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
    67
        return
ebb39c6a1476 Add original keyword extension by Thomas Arendsen Hain
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
    68
ebb39c6a1476 Add original keyword extension by Thomas Arendsen Hain
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
    69
    class kwrepo(repo.__class__):
ebb39c6a1476 Add original keyword extension by Thomas Arendsen Hain
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
    70
        def file(self, f):
ebb39c6a1476 Add original keyword extension by Thomas Arendsen Hain
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
    71
            if f[0] == '/':
ebb39c6a1476 Add original keyword extension by Thomas Arendsen Hain
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
    72
                f = f[1:]
ebb39c6a1476 Add original keyword extension by Thomas Arendsen Hain
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
    73
            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
    74
ebb39c6a1476 Add original keyword extension by Thomas Arendsen Hain
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
    75
    class kwfilelog(filelog.filelog):
ebb39c6a1476 Add original keyword extension by Thomas Arendsen Hain
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
    76
        def __init__(self, opener, path, repo,
ebb39c6a1476 Add original keyword extension by Thomas Arendsen Hain
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
    77
                     defversion=revlog.REVLOG_DEFAULT_VERSION):
ebb39c6a1476 Add original keyword extension by Thomas Arendsen Hain
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
    78
            super(kwfilelog, self).__init__(opener, path, defversion)
ebb39c6a1476 Add original keyword extension by Thomas Arendsen Hain
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
    79
            self._repo = repo
ebb39c6a1476 Add original keyword extension by Thomas Arendsen Hain
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
    80
            self._path = path
36
b3ace8cc5a33 Change config opt "expand" to accept filename patterns
Christian Ebert <blacktrash@gmx.net>
parents: 34
diff changeset
    81
33
ebb39c6a1476 Add original keyword extension by Thomas Arendsen Hain
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
    82
        def read(self, node):
42
ba000e29ecf3 Implement near CVS compability with more than one keyword
Christian Ebert <blacktrash@gmx.net>
parents: 41
diff changeset
    83
            data = super(kwfilelog, self).read(node)
51
1ecb6ec5d16b Reintroduce simpler kwfilelog.add(); simplify kwfilelog.read()
Christian Ebert <blacktrash@gmx.net>
parents: 50
diff changeset
    84
            if not self._path.startswith('.hg') and not util.binary(data):
44
dc6e7d0e607f Exclude tag cmd from pretxcommit hook; exclude .hg files
Christian Ebert <blacktrash@gmx.net>
parents: 42
diff changeset
    85
51
1ecb6ec5d16b Reintroduce simpler kwfilelog.add(); simplify kwfilelog.read()
Christian Ebert <blacktrash@gmx.net>
parents: 50
diff changeset
    86
                c = context.filectx(self._repo, self._path, fileid=node,
1ecb6ec5d16b Reintroduce simpler kwfilelog.add(); simplify kwfilelog.read()
Christian Ebert <blacktrash@gmx.net>
parents: 50
diff changeset
    87
                                     filelog=self)
1ecb6ec5d16b Reintroduce simpler kwfilelog.add(); simplify kwfilelog.read()
Christian Ebert <blacktrash@gmx.net>
parents: 50
diff changeset
    88
                for pat, opt in self._repo.ui.configitems('keyword'):
1ecb6ec5d16b Reintroduce simpler kwfilelog.add(); simplify kwfilelog.read()
Christian Ebert <blacktrash@gmx.net>
parents: 50
diff changeset
    89
                    if opt == 'expand':
1ecb6ec5d16b Reintroduce simpler kwfilelog.add(); simplify kwfilelog.read()
Christian Ebert <blacktrash@gmx.net>
parents: 50
diff changeset
    90
                        mf = util.matcher(self._repo.root,
1ecb6ec5d16b Reintroduce simpler kwfilelog.add(); simplify kwfilelog.read()
Christian Ebert <blacktrash@gmx.net>
parents: 50
diff changeset
    91
                                '', [pat], [], [])[1]
1ecb6ec5d16b Reintroduce simpler kwfilelog.add(); simplify kwfilelog.read()
Christian Ebert <blacktrash@gmx.net>
parents: 50
diff changeset
    92
                        if mf(self._path):
44
dc6e7d0e607f Exclude tag cmd from pretxcommit hook; exclude .hg files
Christian Ebert <blacktrash@gmx.net>
parents: 42
diff changeset
    93
51
1ecb6ec5d16b Reintroduce simpler kwfilelog.add(); simplify kwfilelog.read()
Christian Ebert <blacktrash@gmx.net>
parents: 50
diff changeset
    94
                            def kwexpander(matchobj):
1ecb6ec5d16b Reintroduce simpler kwfilelog.add(); simplify kwfilelog.read()
Christian Ebert <blacktrash@gmx.net>
parents: 50
diff changeset
    95
                                return kwexpand(matchobj,
1ecb6ec5d16b Reintroduce simpler kwfilelog.add(); simplify kwfilelog.read()
Christian Ebert <blacktrash@gmx.net>
parents: 50
diff changeset
    96
                                        self._repo, c.changectx(), self._path,
1ecb6ec5d16b Reintroduce simpler kwfilelog.add(); simplify kwfilelog.read()
Christian Ebert <blacktrash@gmx.net>
parents: 50
diff changeset
    97
                                        c.date(), c.user())
42
ba000e29ecf3 Implement near CVS compability with more than one keyword
Christian Ebert <blacktrash@gmx.net>
parents: 41
diff changeset
    98
51
1ecb6ec5d16b Reintroduce simpler kwfilelog.add(); simplify kwfilelog.read()
Christian Ebert <blacktrash@gmx.net>
parents: 50
diff changeset
    99
                            re_kw = re.compile(r'\$(%s)\$' % hgkeywords)
1ecb6ec5d16b Reintroduce simpler kwfilelog.add(); simplify kwfilelog.read()
Christian Ebert <blacktrash@gmx.net>
parents: 50
diff changeset
   100
                            return re_kw.sub(kwexpander, data)
1ecb6ec5d16b Reintroduce simpler kwfilelog.add(); simplify kwfilelog.read()
Christian Ebert <blacktrash@gmx.net>
parents: 50
diff changeset
   101
            return data
42
ba000e29ecf3 Implement near CVS compability with more than one keyword
Christian Ebert <blacktrash@gmx.net>
parents: 41
diff changeset
   102
51
1ecb6ec5d16b Reintroduce simpler kwfilelog.add(); simplify kwfilelog.read()
Christian Ebert <blacktrash@gmx.net>
parents: 50
diff changeset
   103
        def add(self, text, meta, tr, link, p1=None, p2=None):
1ecb6ec5d16b Reintroduce simpler kwfilelog.add(); simplify kwfilelog.read()
Christian Ebert <blacktrash@gmx.net>
parents: 50
diff changeset
   104
            if not util.binary(text):
1ecb6ec5d16b Reintroduce simpler kwfilelog.add(); simplify kwfilelog.read()
Christian Ebert <blacktrash@gmx.net>
parents: 50
diff changeset
   105
                re_kw = re.compile(r'\$(%s): [^$]+? \$' % hgkeywords)
1ecb6ec5d16b Reintroduce simpler kwfilelog.add(); simplify kwfilelog.read()
Christian Ebert <blacktrash@gmx.net>
parents: 50
diff changeset
   106
                text = re_kw.sub(r'$\1$', text)
1ecb6ec5d16b Reintroduce simpler kwfilelog.add(); simplify kwfilelog.read()
Christian Ebert <blacktrash@gmx.net>
parents: 50
diff changeset
   107
            return super(kwfilelog, self).add(text, meta, tr, link, p1, p2)
36
b3ace8cc5a33 Change config opt "expand" to accept filename patterns
Christian Ebert <blacktrash@gmx.net>
parents: 34
diff changeset
   108
49
5acfe7dc3086 Override filelog's size(), cmp() as well; remove add()
Christian Ebert <blacktrash@gmx.net>
parents: 48
diff changeset
   109
        def size(self, rev):
5acfe7dc3086 Override filelog's size(), cmp() as well; remove add()
Christian Ebert <blacktrash@gmx.net>
parents: 48
diff changeset
   110
            '''Overrides filelog's size() to use kwfilelog.read().'''
5acfe7dc3086 Override filelog's size(), cmp() as well; remove add()
Christian Ebert <blacktrash@gmx.net>
parents: 48
diff changeset
   111
            node = self.node(rev)
5acfe7dc3086 Override filelog's size(), cmp() as well; remove add()
Christian Ebert <blacktrash@gmx.net>
parents: 48
diff changeset
   112
            if self.renamed(node):
5acfe7dc3086 Override filelog's size(), cmp() as well; remove add()
Christian Ebert <blacktrash@gmx.net>
parents: 48
diff changeset
   113
                return len(self.read(node))
5acfe7dc3086 Override filelog's size(), cmp() as well; remove add()
Christian Ebert <blacktrash@gmx.net>
parents: 48
diff changeset
   114
            return revlog.size(self, rev)
5acfe7dc3086 Override filelog's size(), cmp() as well; remove add()
Christian Ebert <blacktrash@gmx.net>
parents: 48
diff changeset
   115
5acfe7dc3086 Override filelog's size(), cmp() as well; remove add()
Christian Ebert <blacktrash@gmx.net>
parents: 48
diff changeset
   116
        def cmp(self, node, text):
5acfe7dc3086 Override filelog's size(), cmp() as well; remove add()
Christian Ebert <blacktrash@gmx.net>
parents: 48
diff changeset
   117
            '''Overrides filelog's cmp() to use kwfilelog.read().'''
5acfe7dc3086 Override filelog's size(), cmp() as well; remove add()
Christian Ebert <blacktrash@gmx.net>
parents: 48
diff changeset
   118
            if self.renamed(node):
5acfe7dc3086 Override filelog's size(), cmp() as well; remove add()
Christian Ebert <blacktrash@gmx.net>
parents: 48
diff changeset
   119
                t2 = self.read(node)
5acfe7dc3086 Override filelog's size(), cmp() as well; remove add()
Christian Ebert <blacktrash@gmx.net>
parents: 48
diff changeset
   120
                return t2 != text
33
ebb39c6a1476 Add original keyword extension by Thomas Arendsen Hain
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   121
ebb39c6a1476 Add original keyword extension by Thomas Arendsen Hain
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   122
    filelog.filelog = kwfilelog
ebb39c6a1476 Add original keyword extension by Thomas Arendsen Hain
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   123
    repo.__class__ = kwrepo
47
0617e7d497f6 Branch standalone extension, including pretxncommit hook function
Christian Ebert <blacktrash@gmx.net>
parents: 44
diff changeset
   124
0617e7d497f6 Branch standalone extension, including pretxncommit hook function
Christian Ebert <blacktrash@gmx.net>
parents: 44
diff changeset
   125
0617e7d497f6 Branch standalone extension, including pretxncommit hook function
Christian Ebert <blacktrash@gmx.net>
parents: 44
diff changeset
   126
def pretxnkw(ui, repo, hooktype, **args):
0617e7d497f6 Branch standalone extension, including pretxncommit hook function
Christian Ebert <blacktrash@gmx.net>
parents: 44
diff changeset
   127
    '''pretxncommit hook that collects candidates for keyword expansion
0617e7d497f6 Branch standalone extension, including pretxncommit hook function
Christian Ebert <blacktrash@gmx.net>
parents: 44
diff changeset
   128
    on commit and expands keywords in working dir.'''
0617e7d497f6 Branch standalone extension, including pretxncommit hook function
Christian Ebert <blacktrash@gmx.net>
parents: 44
diff changeset
   129
0617e7d497f6 Branch standalone extension, including pretxncommit hook function
Christian Ebert <blacktrash@gmx.net>
parents: 44
diff changeset
   130
    if hooktype != 'pretxncommit': # bail out with error
0617e7d497f6 Branch standalone extension, including pretxncommit hook function
Christian Ebert <blacktrash@gmx.net>
parents: 44
diff changeset
   131
        return True
0617e7d497f6 Branch standalone extension, including pretxncommit hook function
Christian Ebert <blacktrash@gmx.net>
parents: 44
diff changeset
   132
0617e7d497f6 Branch standalone extension, including pretxncommit hook function
Christian Ebert <blacktrash@gmx.net>
parents: 44
diff changeset
   133
    # reparse args, opts again as pretxncommit hook is silent about them
0617e7d497f6 Branch standalone extension, including pretxncommit hook function
Christian Ebert <blacktrash@gmx.net>
parents: 44
diff changeset
   134
    cmd, sysargs, globalopts, cmdopts = commands.parse(ui, sys.argv[1:])[1:]
0617e7d497f6 Branch standalone extension, including pretxncommit hook function
Christian Ebert <blacktrash@gmx.net>
parents: 44
diff changeset
   135
    # exclude tag and import
0617e7d497f6 Branch standalone extension, including pretxncommit hook function
Christian Ebert <blacktrash@gmx.net>
parents: 44
diff changeset
   136
    if repr(cmd).split()[1] in ('tag', 'import_'):
0617e7d497f6 Branch standalone extension, including pretxncommit hook function
Christian Ebert <blacktrash@gmx.net>
parents: 44
diff changeset
   137
        return False
0617e7d497f6 Branch standalone extension, including pretxncommit hook function
Christian Ebert <blacktrash@gmx.net>
parents: 44
diff changeset
   138
0617e7d497f6 Branch standalone extension, including pretxncommit hook function
Christian Ebert <blacktrash@gmx.net>
parents: 44
diff changeset
   139
    files, match, anypats = cmdutil.matchpats(repo, sysargs, cmdopts)
0617e7d497f6 Branch standalone extension, including pretxncommit hook function
Christian Ebert <blacktrash@gmx.net>
parents: 44
diff changeset
   140
    # validity checks should have been done already
0617e7d497f6 Branch standalone extension, including pretxncommit hook function
Christian Ebert <blacktrash@gmx.net>
parents: 44
diff changeset
   141
    modified, added = repo.status(files=files, match=match)[:2]
0617e7d497f6 Branch standalone extension, including pretxncommit hook function
Christian Ebert <blacktrash@gmx.net>
parents: 44
diff changeset
   142
    candidates = [f for f in modified + added if not f.startswith('.hg')]
0617e7d497f6 Branch standalone extension, including pretxncommit hook function
Christian Ebert <blacktrash@gmx.net>
parents: 44
diff changeset
   143
0617e7d497f6 Branch standalone extension, including pretxncommit hook function
Christian Ebert <blacktrash@gmx.net>
parents: 44
diff changeset
   144
    if not candidates:
0617e7d497f6 Branch standalone extension, including pretxncommit hook function
Christian Ebert <blacktrash@gmx.net>
parents: 44
diff changeset
   145
        return False
0617e7d497f6 Branch standalone extension, including pretxncommit hook function
Christian Ebert <blacktrash@gmx.net>
parents: 44
diff changeset
   146
0617e7d497f6 Branch standalone extension, including pretxncommit hook function
Christian Ebert <blacktrash@gmx.net>
parents: 44
diff changeset
   147
    # only check files that are configured in keyword section
0617e7d497f6 Branch standalone extension, including pretxncommit hook function
Christian Ebert <blacktrash@gmx.net>
parents: 44
diff changeset
   148
    files = []
0617e7d497f6 Branch standalone extension, including pretxncommit hook function
Christian Ebert <blacktrash@gmx.net>
parents: 44
diff changeset
   149
    # python2.4: files = set()
0617e7d497f6 Branch standalone extension, including pretxncommit hook function
Christian Ebert <blacktrash@gmx.net>
parents: 44
diff changeset
   150
    for pat, opt in repo.ui.configitems('keyword'):
0617e7d497f6 Branch standalone extension, including pretxncommit hook function
Christian Ebert <blacktrash@gmx.net>
parents: 44
diff changeset
   151
        if opt == 'expand':
0617e7d497f6 Branch standalone extension, including pretxncommit hook function
Christian Ebert <blacktrash@gmx.net>
parents: 44
diff changeset
   152
            mf = util.matcher(repo.root, '', [pat], [], [])[1]
0617e7d497f6 Branch standalone extension, including pretxncommit hook function
Christian Ebert <blacktrash@gmx.net>
parents: 44
diff changeset
   153
            for candidate in candidates:
0617e7d497f6 Branch standalone extension, including pretxncommit hook function
Christian Ebert <blacktrash@gmx.net>
parents: 44
diff changeset
   154
                if mf(candidate) and candidate not in files:
0617e7d497f6 Branch standalone extension, including pretxncommit hook function
Christian Ebert <blacktrash@gmx.net>
parents: 44
diff changeset
   155
                    files.append(candidate)
0617e7d497f6 Branch standalone extension, including pretxncommit hook function
Christian Ebert <blacktrash@gmx.net>
parents: 44
diff changeset
   156
                # python2.4:
0617e7d497f6 Branch standalone extension, including pretxncommit hook function
Christian Ebert <blacktrash@gmx.net>
parents: 44
diff changeset
   157
                # if mf(candidate): files.add(candidate)
0617e7d497f6 Branch standalone extension, including pretxncommit hook function
Christian Ebert <blacktrash@gmx.net>
parents: 44
diff changeset
   158
0617e7d497f6 Branch standalone extension, including pretxncommit hook function
Christian Ebert <blacktrash@gmx.net>
parents: 44
diff changeset
   159
    if not files:
0617e7d497f6 Branch standalone extension, including pretxncommit hook function
Christian Ebert <blacktrash@gmx.net>
parents: 44
diff changeset
   160
        return False
0617e7d497f6 Branch standalone extension, including pretxncommit hook function
Christian Ebert <blacktrash@gmx.net>
parents: 44
diff changeset
   161
0617e7d497f6 Branch standalone extension, including pretxncommit hook function
Christian Ebert <blacktrash@gmx.net>
parents: 44
diff changeset
   162
    user, date = repo.changelog.read(repo.changelog.tip())[1:3]
0617e7d497f6 Branch standalone extension, including pretxncommit hook function
Christian Ebert <blacktrash@gmx.net>
parents: 44
diff changeset
   163
    # expand both expanded and unexpanded keywords
0617e7d497f6 Branch standalone extension, including pretxncommit hook function
Christian Ebert <blacktrash@gmx.net>
parents: 44
diff changeset
   164
    re_kw = re.compile(r'\$(%s)(: [^$]+? )?\$' % hgkeywords)
0617e7d497f6 Branch standalone extension, including pretxncommit hook function
Christian Ebert <blacktrash@gmx.net>
parents: 44
diff changeset
   165
0617e7d497f6 Branch standalone extension, including pretxncommit hook function
Christian Ebert <blacktrash@gmx.net>
parents: 44
diff changeset
   166
    for f in files:
0617e7d497f6 Branch standalone extension, including pretxncommit hook function
Christian Ebert <blacktrash@gmx.net>
parents: 44
diff changeset
   167
        data = repo.wfile(f).read()
0617e7d497f6 Branch standalone extension, including pretxncommit hook function
Christian Ebert <blacktrash@gmx.net>
parents: 44
diff changeset
   168
        if not util.binary(data):
0617e7d497f6 Branch standalone extension, including pretxncommit hook function
Christian Ebert <blacktrash@gmx.net>
parents: 44
diff changeset
   169
0617e7d497f6 Branch standalone extension, including pretxncommit hook function
Christian Ebert <blacktrash@gmx.net>
parents: 44
diff changeset
   170
            def kwexpander(matchobj):
0617e7d497f6 Branch standalone extension, including pretxncommit hook function
Christian Ebert <blacktrash@gmx.net>
parents: 44
diff changeset
   171
                return kwexpand(matchobj,
0617e7d497f6 Branch standalone extension, including pretxncommit hook function
Christian Ebert <blacktrash@gmx.net>
parents: 44
diff changeset
   172
                        repo, args['node'][:12], f, date, user)
0617e7d497f6 Branch standalone extension, including pretxncommit hook function
Christian Ebert <blacktrash@gmx.net>
parents: 44
diff changeset
   173
0617e7d497f6 Branch standalone extension, including pretxncommit hook function
Christian Ebert <blacktrash@gmx.net>
parents: 44
diff changeset
   174
            data, kwct = re_kw.subn(kwexpander, data)
0617e7d497f6 Branch standalone extension, including pretxncommit hook function
Christian Ebert <blacktrash@gmx.net>
parents: 44
diff changeset
   175
            if kwct:
0617e7d497f6 Branch standalone extension, including pretxncommit hook function
Christian Ebert <blacktrash@gmx.net>
parents: 44
diff changeset
   176
                ui.note(_('expanding keywords in %s\n' % f))
0617e7d497f6 Branch standalone extension, including pretxncommit hook function
Christian Ebert <blacktrash@gmx.net>
parents: 44
diff changeset
   177
                # backup file?
0617e7d497f6 Branch standalone extension, including pretxncommit hook function
Christian Ebert <blacktrash@gmx.net>
parents: 44
diff changeset
   178
                repo.wfile(f, 'w').write(data)