hgkw/pretxnkw.py
author Christian Ebert <blacktrash@gmx.net>
Wed, 20 Dec 2006 10:37:06 +0100
branchextension
changeset 40 38ee735886d2
parent 39 14038784f986
child 41 e1c539e1282b
permissions -rw-r--r--
Rename "text" to "data" always as set before call to util.binary()
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
37
3dc31476c148 Standalone version for keyword.py
Christian Ebert <blacktrash@gmx.net>
parents: 32
diff changeset
     1
from mercurial.i18n import _
3dc31476c148 Standalone version for keyword.py
Christian Ebert <blacktrash@gmx.net>
parents: 32
diff changeset
     2
from mercurial import cmdutil, commands, util
3dc31476c148 Standalone version for keyword.py
Christian Ebert <blacktrash@gmx.net>
parents: 32
diff changeset
     3
import os.path, re, sys
7
e5f131217f87 Add pretxnkw module for updatehook branch
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
     4
20
6dc2b4268920 Simplify for $Hg$ expansion scheme
Christian Ebert <blacktrash@gmx.net>
parents: 7
diff changeset
     5
def pretxnkw(ui, repo, hooktype, **args):
32
b70b38b15fa4 Update function descriptions
Christian Ebert <blacktrash@gmx.net>
parents: 27
diff changeset
     6
    '''Collects candidates for keyword expansion on commit
39
14038784f986 Reflect new scheme in function docstring
Christian Ebert <blacktrash@gmx.net>
parents: 37
diff changeset
     7
    and expands keywords in working dir.
14038784f986 Reflect new scheme in function docstring
Christian Ebert <blacktrash@gmx.net>
parents: 37
diff changeset
     8
    NOTE: for use in combination with hgext.keyword!'''
7
e5f131217f87 Add pretxnkw module for updatehook branch
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
     9
20
6dc2b4268920 Simplify for $Hg$ expansion scheme
Christian Ebert <blacktrash@gmx.net>
parents: 7
diff changeset
    10
    if hooktype != 'pretxncommit':
7
e5f131217f87 Add pretxnkw module for updatehook branch
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
    11
        # bail out with error
e5f131217f87 Add pretxnkw module for updatehook branch
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
    12
        return True
e5f131217f87 Add pretxnkw module for updatehook branch
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
    13
26
bda0dec1aaf1 Reparse cmdline to avoid expansion in uncommitted files
Christian Ebert <blacktrash@gmx.net>
parents: 20
diff changeset
    14
    # reparse args, opts again as pretxncommit hook is silent about them
bda0dec1aaf1 Reparse cmdline to avoid expansion in uncommitted files
Christian Ebert <blacktrash@gmx.net>
parents: 20
diff changeset
    15
    sysargs, globalopts, cmdopts = commands.parse(ui, sys.argv[1:])[2:]
bda0dec1aaf1 Reparse cmdline to avoid expansion in uncommitted files
Christian Ebert <blacktrash@gmx.net>
parents: 20
diff changeset
    16
    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
    17
bda0dec1aaf1 Reparse cmdline to avoid expansion in uncommitted files
Christian Ebert <blacktrash@gmx.net>
parents: 20
diff changeset
    18
    # 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
    19
    modified, added = repo.status(files=files, match=match)[:2]
7
e5f131217f87 Add pretxnkw module for updatehook branch
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
    20
    candidates = modified + added
e5f131217f87 Add pretxnkw module for updatehook branch
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
    21
37
3dc31476c148 Standalone version for keyword.py
Christian Ebert <blacktrash@gmx.net>
parents: 32
diff changeset
    22
    if not candidates:
3dc31476c148 Standalone version for keyword.py
Christian Ebert <blacktrash@gmx.net>
parents: 32
diff changeset
    23
        return False
3dc31476c148 Standalone version for keyword.py
Christian Ebert <blacktrash@gmx.net>
parents: 32
diff changeset
    24
3dc31476c148 Standalone version for keyword.py
Christian Ebert <blacktrash@gmx.net>
parents: 32
diff changeset
    25
    # 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
    26
    files = []
3dc31476c148 Standalone version for keyword.py
Christian Ebert <blacktrash@gmx.net>
parents: 32
diff changeset
    27
    # python2.4: files = set()
3dc31476c148 Standalone version for keyword.py
Christian Ebert <blacktrash@gmx.net>
parents: 32
diff changeset
    28
    for pat, opt in repo.ui.configitems('keyword'):
3dc31476c148 Standalone version for keyword.py
Christian Ebert <blacktrash@gmx.net>
parents: 32
diff changeset
    29
        if opt == 'expand':
3dc31476c148 Standalone version for keyword.py
Christian Ebert <blacktrash@gmx.net>
parents: 32
diff changeset
    30
            mf = util.matcher(repo.root, '', [pat], [], [])[1]
3dc31476c148 Standalone version for keyword.py
Christian Ebert <blacktrash@gmx.net>
parents: 32
diff changeset
    31
            for candidate in candidates:
3dc31476c148 Standalone version for keyword.py
Christian Ebert <blacktrash@gmx.net>
parents: 32
diff changeset
    32
                if mf(candidate) and candidate not in files:
3dc31476c148 Standalone version for keyword.py
Christian Ebert <blacktrash@gmx.net>
parents: 32
diff changeset
    33
                    files.append(candidate)
3dc31476c148 Standalone version for keyword.py
Christian Ebert <blacktrash@gmx.net>
parents: 32
diff changeset
    34
                # python2.4:
3dc31476c148 Standalone version for keyword.py
Christian Ebert <blacktrash@gmx.net>
parents: 32
diff changeset
    35
                # if mf(candidate): files.add(candidate)
3dc31476c148 Standalone version for keyword.py
Christian Ebert <blacktrash@gmx.net>
parents: 32
diff changeset
    36
3dc31476c148 Standalone version for keyword.py
Christian Ebert <blacktrash@gmx.net>
parents: 32
diff changeset
    37
    if not files: # nothing to do
3dc31476c148 Standalone version for keyword.py
Christian Ebert <blacktrash@gmx.net>
parents: 32
diff changeset
    38
        return False
3dc31476c148 Standalone version for keyword.py
Christian Ebert <blacktrash@gmx.net>
parents: 32
diff changeset
    39
3dc31476c148 Standalone version for keyword.py
Christian Ebert <blacktrash@gmx.net>
parents: 32
diff changeset
    40
    cid = args['node'][:12]
3dc31476c148 Standalone version for keyword.py
Christian Ebert <blacktrash@gmx.net>
parents: 32
diff changeset
    41
    user, date = repo.changelog.read(repo.changelog.tip())[1:3]
3dc31476c148 Standalone version for keyword.py
Christian Ebert <blacktrash@gmx.net>
parents: 32
diff changeset
    42
    user = util.shortuser(user)
3dc31476c148 Standalone version for keyword.py
Christian Ebert <blacktrash@gmx.net>
parents: 32
diff changeset
    43
    date = util.datestr(date=date, format=util.defaultdateformats[0])
3dc31476c148 Standalone version for keyword.py
Christian Ebert <blacktrash@gmx.net>
parents: 32
diff changeset
    44
                                               # %Y-%m-%d %H:%M:%S
3dc31476c148 Standalone version for keyword.py
Christian Ebert <blacktrash@gmx.net>
parents: 32
diff changeset
    45
3dc31476c148 Standalone version for keyword.py
Christian Ebert <blacktrash@gmx.net>
parents: 32
diff changeset
    46
    re_kw = re.compile(r'\$Hg[^$]*?\$')
3dc31476c148 Standalone version for keyword.py
Christian Ebert <blacktrash@gmx.net>
parents: 32
diff changeset
    47
3dc31476c148 Standalone version for keyword.py
Christian Ebert <blacktrash@gmx.net>
parents: 32
diff changeset
    48
    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
    49
        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
    50
        if not util.binary(data):
37
3dc31476c148 Standalone version for keyword.py
Christian Ebert <blacktrash@gmx.net>
parents: 32
diff changeset
    51
            kw = '$Hg: %s,v %s %s %s $' % (
3dc31476c148 Standalone version for keyword.py
Christian Ebert <blacktrash@gmx.net>
parents: 32
diff changeset
    52
                    os.path.basename(f), cid, date, user)
40
38ee735886d2 Rename "text" to "data" always as set before call to util.binary()
Christian Ebert <blacktrash@gmx.net>
parents: 39
diff changeset
    53
            data, kwct = re_kw.subn(kw, data)
37
3dc31476c148 Standalone version for keyword.py
Christian Ebert <blacktrash@gmx.net>
parents: 32
diff changeset
    54
            if kwct:
3dc31476c148 Standalone version for keyword.py
Christian Ebert <blacktrash@gmx.net>
parents: 32
diff changeset
    55
                ui.note(_('expanding keywords in %s\n' % f))
3dc31476c148 Standalone version for keyword.py
Christian Ebert <blacktrash@gmx.net>
parents: 32
diff changeset
    56
                # 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
    57
                repo.wfile(f, 'w').write(data)