hgkw/pretxnkw.py
author Christian Ebert <blacktrash@gmx.net>
Thu, 21 Dec 2006 16:00:45 +0100
branchmodular
changeset 46 67e9fb23a32b
parent 45 5acf520f2115
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: 45
diff changeset
     1
from hgkw import kwutil
37
3dc31476c148 Standalone version for keyword.py
Christian Ebert <blacktrash@gmx.net>
parents: 32
diff changeset
     2
from mercurial.i18n import _
3dc31476c148 Standalone version for keyword.py
Christian Ebert <blacktrash@gmx.net>
parents: 32
diff changeset
     3
from mercurial import cmdutil, commands, util
46
67e9fb23a32b Make keyword.py depend on resurrected kwutil.py
Christian Ebert <blacktrash@gmx.net>
parents: 45
diff changeset
     4
import re, sys
42
ba000e29ecf3 Implement near CVS compability with more than one keyword
Christian Ebert <blacktrash@gmx.net>
parents: 41
diff changeset
     5
20
6dc2b4268920 Simplify for $Hg$ expansion scheme
Christian Ebert <blacktrash@gmx.net>
parents: 7
diff changeset
     6
def pretxnkw(ui, repo, hooktype, **args):
32
b70b38b15fa4 Update function descriptions
Christian Ebert <blacktrash@gmx.net>
parents: 27
diff changeset
     7
    '''Collects candidates for keyword expansion on commit
39
14038784f986 Reflect new scheme in function docstring
Christian Ebert <blacktrash@gmx.net>
parents: 37
diff changeset
     8
    and expands keywords in working dir.
14038784f986 Reflect new scheme in function docstring
Christian Ebert <blacktrash@gmx.net>
parents: 37
diff changeset
     9
    NOTE: for use in combination with hgext.keyword!'''
7
e5f131217f87 Add pretxnkw module for updatehook branch
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
    10
20
6dc2b4268920 Simplify for $Hg$ expansion scheme
Christian Ebert <blacktrash@gmx.net>
parents: 7
diff changeset
    11
    if hooktype != 'pretxncommit':
7
e5f131217f87 Add pretxnkw module for updatehook branch
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
    12
        # bail out with error
e5f131217f87 Add pretxnkw module for updatehook branch
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
    13
        return True
e5f131217f87 Add pretxnkw module for updatehook branch
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
    14
26
bda0dec1aaf1 Reparse cmdline to avoid expansion in uncommitted files
Christian Ebert <blacktrash@gmx.net>
parents: 20
diff changeset
    15
    # reparse args, opts again as pretxncommit hook is silent about them
44
dc6e7d0e607f Exclude tag cmd from pretxcommit hook; exclude .hg files
Christian Ebert <blacktrash@gmx.net>
parents: 42
diff changeset
    16
    cmd, sysargs, globalopts, cmdopts = commands.parse(ui, sys.argv[1:])[1:]
45
5acf520f2115 Exclude import command too
Christian Ebert <blacktrash@gmx.net>
parents: 44
diff changeset
    17
    # exclude tag and import
5acf520f2115 Exclude import command too
Christian Ebert <blacktrash@gmx.net>
parents: 44
diff changeset
    18
    if repr(cmd).split()[1] in ('tag', 'import_'):
44
dc6e7d0e607f Exclude tag cmd from pretxcommit hook; exclude .hg files
Christian Ebert <blacktrash@gmx.net>
parents: 42
diff changeset
    19
        return False
26
bda0dec1aaf1 Reparse cmdline to avoid expansion in uncommitted files
Christian Ebert <blacktrash@gmx.net>
parents: 20
diff changeset
    20
    files, match, anypats = cmdutil.matchpats(repo, sysargs, cmdopts)
bda0dec1aaf1 Reparse cmdline to avoid expansion in uncommitted files
Christian Ebert <blacktrash@gmx.net>
parents: 20
diff changeset
    21
bda0dec1aaf1 Reparse cmdline to avoid expansion in uncommitted files
Christian Ebert <blacktrash@gmx.net>
parents: 20
diff changeset
    22
    # validity checks should have been done already
bda0dec1aaf1 Reparse cmdline to avoid expansion in uncommitted files
Christian Ebert <blacktrash@gmx.net>
parents: 20
diff changeset
    23
    modified, added = repo.status(files=files, match=match)[:2]
44
dc6e7d0e607f Exclude tag cmd from pretxcommit hook; exclude .hg files
Christian Ebert <blacktrash@gmx.net>
parents: 42
diff changeset
    24
    candidates = [f for f in modified + added if not f.startswith('.hg')]
7
e5f131217f87 Add pretxnkw module for updatehook branch
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
    25
37
3dc31476c148 Standalone version for keyword.py
Christian Ebert <blacktrash@gmx.net>
parents: 32
diff changeset
    26
    if not candidates:
3dc31476c148 Standalone version for keyword.py
Christian Ebert <blacktrash@gmx.net>
parents: 32
diff changeset
    27
        return False
3dc31476c148 Standalone version for keyword.py
Christian Ebert <blacktrash@gmx.net>
parents: 32
diff changeset
    28
3dc31476c148 Standalone version for keyword.py
Christian Ebert <blacktrash@gmx.net>
parents: 32
diff changeset
    29
    # only check files that have hgkwencode assigned as encode filter
3dc31476c148 Standalone version for keyword.py
Christian Ebert <blacktrash@gmx.net>
parents: 32
diff changeset
    30
    files = []
3dc31476c148 Standalone version for keyword.py
Christian Ebert <blacktrash@gmx.net>
parents: 32
diff changeset
    31
    # python2.4: files = set()
3dc31476c148 Standalone version for keyword.py
Christian Ebert <blacktrash@gmx.net>
parents: 32
diff changeset
    32
    for pat, opt in repo.ui.configitems('keyword'):
3dc31476c148 Standalone version for keyword.py
Christian Ebert <blacktrash@gmx.net>
parents: 32
diff changeset
    33
        if opt == 'expand':
3dc31476c148 Standalone version for keyword.py
Christian Ebert <blacktrash@gmx.net>
parents: 32
diff changeset
    34
            mf = util.matcher(repo.root, '', [pat], [], [])[1]
3dc31476c148 Standalone version for keyword.py
Christian Ebert <blacktrash@gmx.net>
parents: 32
diff changeset
    35
            for candidate in candidates:
3dc31476c148 Standalone version for keyword.py
Christian Ebert <blacktrash@gmx.net>
parents: 32
diff changeset
    36
                if mf(candidate) and candidate not in files:
3dc31476c148 Standalone version for keyword.py
Christian Ebert <blacktrash@gmx.net>
parents: 32
diff changeset
    37
                    files.append(candidate)
3dc31476c148 Standalone version for keyword.py
Christian Ebert <blacktrash@gmx.net>
parents: 32
diff changeset
    38
                # python2.4:
3dc31476c148 Standalone version for keyword.py
Christian Ebert <blacktrash@gmx.net>
parents: 32
diff changeset
    39
                # if mf(candidate): files.add(candidate)
3dc31476c148 Standalone version for keyword.py
Christian Ebert <blacktrash@gmx.net>
parents: 32
diff changeset
    40
3dc31476c148 Standalone version for keyword.py
Christian Ebert <blacktrash@gmx.net>
parents: 32
diff changeset
    41
    if not files: # nothing to do
3dc31476c148 Standalone version for keyword.py
Christian Ebert <blacktrash@gmx.net>
parents: 32
diff changeset
    42
        return False
3dc31476c148 Standalone version for keyword.py
Christian Ebert <blacktrash@gmx.net>
parents: 32
diff changeset
    43
3dc31476c148 Standalone version for keyword.py
Christian Ebert <blacktrash@gmx.net>
parents: 32
diff changeset
    44
    user, date = repo.changelog.read(repo.changelog.tip())[1:3]
46
67e9fb23a32b Make keyword.py depend on resurrected kwutil.py
Christian Ebert <blacktrash@gmx.net>
parents: 45
diff changeset
    45
    re_kw = re.compile(r'\$(%s)(: [^$]+? )?\$' % kwutil.hgkeywords)
37
3dc31476c148 Standalone version for keyword.py
Christian Ebert <blacktrash@gmx.net>
parents: 32
diff changeset
    46
3dc31476c148 Standalone version for keyword.py
Christian Ebert <blacktrash@gmx.net>
parents: 32
diff changeset
    47
    for f in files:
40
38ee735886d2 Rename "text" to "data" always as set before call to util.binary()
Christian Ebert <blacktrash@gmx.net>
parents: 39
diff changeset
    48
        data = repo.wfile(f).read()
38ee735886d2 Rename "text" to "data" always as set before call to util.binary()
Christian Ebert <blacktrash@gmx.net>
parents: 39
diff changeset
    49
        if not util.binary(data):
42
ba000e29ecf3 Implement near CVS compability with more than one keyword
Christian Ebert <blacktrash@gmx.net>
parents: 41
diff changeset
    50
ba000e29ecf3 Implement near CVS compability with more than one keyword
Christian Ebert <blacktrash@gmx.net>
parents: 41
diff changeset
    51
            def kwexpand(matchobj):
46
67e9fb23a32b Make keyword.py depend on resurrected kwutil.py
Christian Ebert <blacktrash@gmx.net>
parents: 45
diff changeset
    52
                return kwutil.kwexpand(matchobj,
67e9fb23a32b Make keyword.py depend on resurrected kwutil.py
Christian Ebert <blacktrash@gmx.net>
parents: 45
diff changeset
    53
                        repo, args['node'][:12], f, date, user)
42
ba000e29ecf3 Implement near CVS compability with more than one keyword
Christian Ebert <blacktrash@gmx.net>
parents: 41
diff changeset
    54
ba000e29ecf3 Implement near CVS compability with more than one keyword
Christian Ebert <blacktrash@gmx.net>
parents: 41
diff changeset
    55
            data, kwct = re_kw.subn(kwexpand, data)
37
3dc31476c148 Standalone version for keyword.py
Christian Ebert <blacktrash@gmx.net>
parents: 32
diff changeset
    56
            if kwct:
3dc31476c148 Standalone version for keyword.py
Christian Ebert <blacktrash@gmx.net>
parents: 32
diff changeset
    57
                ui.note(_('expanding keywords in %s\n' % f))
3dc31476c148 Standalone version for keyword.py
Christian Ebert <blacktrash@gmx.net>
parents: 32
diff changeset
    58
                # backup file?
40
38ee735886d2 Rename "text" to "data" always as set before call to util.binary()
Christian Ebert <blacktrash@gmx.net>
parents: 39
diff changeset
    59
                repo.wfile(f, 'w').write(data)