hgkw/pretxnkw.py
author Christian Ebert <blacktrash@gmx.net>
Thu, 21 Dec 2006 11:02:21 +0100
branchextension
changeset 45 5acf520f2115
parent 44 dc6e7d0e607f
child 46 67e9fb23a32b
permissions -rw-r--r--
Exclude import command too
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
42
ba000e29ecf3 Implement near CVS compability with more than one keyword
Christian Ebert <blacktrash@gmx.net>
parents: 41
diff changeset
     5
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
     6
20
6dc2b4268920 Simplify for $Hg$ expansion scheme
Christian Ebert <blacktrash@gmx.net>
parents: 7
diff changeset
     7
def pretxnkw(ui, repo, hooktype, **args):
32
b70b38b15fa4 Update function descriptions
Christian Ebert <blacktrash@gmx.net>
parents: 27
diff changeset
     8
    '''Collects candidates for keyword expansion on commit
39
14038784f986 Reflect new scheme in function docstring
Christian Ebert <blacktrash@gmx.net>
parents: 37
diff changeset
     9
    and expands keywords in working dir.
14038784f986 Reflect new scheme in function docstring
Christian Ebert <blacktrash@gmx.net>
parents: 37
diff changeset
    10
    NOTE: for use in combination with hgext.keyword!'''
7
e5f131217f87 Add pretxnkw module for updatehook branch
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
    11
20
6dc2b4268920 Simplify for $Hg$ expansion scheme
Christian Ebert <blacktrash@gmx.net>
parents: 7
diff changeset
    12
    if hooktype != 'pretxncommit':
7
e5f131217f87 Add pretxnkw module for updatehook branch
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
    13
        # bail out with error
e5f131217f87 Add pretxnkw module for updatehook branch
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
    14
        return True
e5f131217f87 Add pretxnkw module for updatehook branch
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
    15
26
bda0dec1aaf1 Reparse cmdline to avoid expansion in uncommitted files
Christian Ebert <blacktrash@gmx.net>
parents: 20
diff changeset
    16
    # 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
    17
    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
    18
    # exclude tag and import
5acf520f2115 Exclude import command too
Christian Ebert <blacktrash@gmx.net>
parents: 44
diff changeset
    19
    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
    20
        return False
26
bda0dec1aaf1 Reparse cmdline to avoid expansion in uncommitted files
Christian Ebert <blacktrash@gmx.net>
parents: 20
diff changeset
    21
    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
    22
bda0dec1aaf1 Reparse cmdline to avoid expansion in uncommitted files
Christian Ebert <blacktrash@gmx.net>
parents: 20
diff changeset
    23
    # 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
    24
    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
    25
    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
    26
37
3dc31476c148 Standalone version for keyword.py
Christian Ebert <blacktrash@gmx.net>
parents: 32
diff changeset
    27
    if not candidates:
3dc31476c148 Standalone version for keyword.py
Christian Ebert <blacktrash@gmx.net>
parents: 32
diff changeset
    28
        return False
3dc31476c148 Standalone version for keyword.py
Christian Ebert <blacktrash@gmx.net>
parents: 32
diff changeset
    29
3dc31476c148 Standalone version for keyword.py
Christian Ebert <blacktrash@gmx.net>
parents: 32
diff changeset
    30
    # 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
    31
    files = []
3dc31476c148 Standalone version for keyword.py
Christian Ebert <blacktrash@gmx.net>
parents: 32
diff changeset
    32
    # python2.4: files = set()
3dc31476c148 Standalone version for keyword.py
Christian Ebert <blacktrash@gmx.net>
parents: 32
diff changeset
    33
    for pat, opt in repo.ui.configitems('keyword'):
3dc31476c148 Standalone version for keyword.py
Christian Ebert <blacktrash@gmx.net>
parents: 32
diff changeset
    34
        if opt == 'expand':
3dc31476c148 Standalone version for keyword.py
Christian Ebert <blacktrash@gmx.net>
parents: 32
diff changeset
    35
            mf = util.matcher(repo.root, '', [pat], [], [])[1]
3dc31476c148 Standalone version for keyword.py
Christian Ebert <blacktrash@gmx.net>
parents: 32
diff changeset
    36
            for candidate in candidates:
3dc31476c148 Standalone version for keyword.py
Christian Ebert <blacktrash@gmx.net>
parents: 32
diff changeset
    37
                if mf(candidate) and candidate not in files:
3dc31476c148 Standalone version for keyword.py
Christian Ebert <blacktrash@gmx.net>
parents: 32
diff changeset
    38
                    files.append(candidate)
3dc31476c148 Standalone version for keyword.py
Christian Ebert <blacktrash@gmx.net>
parents: 32
diff changeset
    39
                # python2.4:
3dc31476c148 Standalone version for keyword.py
Christian Ebert <blacktrash@gmx.net>
parents: 32
diff changeset
    40
                # if mf(candidate): files.add(candidate)
3dc31476c148 Standalone version for keyword.py
Christian Ebert <blacktrash@gmx.net>
parents: 32
diff changeset
    41
3dc31476c148 Standalone version for keyword.py
Christian Ebert <blacktrash@gmx.net>
parents: 32
diff changeset
    42
    if not files: # nothing to do
3dc31476c148 Standalone version for keyword.py
Christian Ebert <blacktrash@gmx.net>
parents: 32
diff changeset
    43
        return False
3dc31476c148 Standalone version for keyword.py
Christian Ebert <blacktrash@gmx.net>
parents: 32
diff changeset
    44
3dc31476c148 Standalone version for keyword.py
Christian Ebert <blacktrash@gmx.net>
parents: 32
diff changeset
    45
    user, date = repo.changelog.read(repo.changelog.tip())[1:3]
42
ba000e29ecf3 Implement near CVS compability with more than one keyword
Christian Ebert <blacktrash@gmx.net>
parents: 41
diff changeset
    46
    strdate = util.datestr(date=date)
ba000e29ecf3 Implement near CVS compability with more than one keyword
Christian Ebert <blacktrash@gmx.net>
parents: 41
diff changeset
    47
    shortuser = util.shortuser(user)
ba000e29ecf3 Implement near CVS compability with more than one keyword
Christian Ebert <blacktrash@gmx.net>
parents: 41
diff changeset
    48
    shortdate = util.datestr(date=date, format=util.defaultdateformats[0])
37
3dc31476c148 Standalone version for keyword.py
Christian Ebert <blacktrash@gmx.net>
parents: 32
diff changeset
    49
                                               # %Y-%m-%d %H:%M:%S
3dc31476c148 Standalone version for keyword.py
Christian Ebert <blacktrash@gmx.net>
parents: 32
diff changeset
    50
42
ba000e29ecf3 Implement near CVS compability with more than one keyword
Christian Ebert <blacktrash@gmx.net>
parents: 41
diff changeset
    51
    re_kw = re.compile(r'\$(%s)(: [^$]+? )?\$' % hgkeywords)
37
3dc31476c148 Standalone version for keyword.py
Christian Ebert <blacktrash@gmx.net>
parents: 32
diff changeset
    52
3dc31476c148 Standalone version for keyword.py
Christian Ebert <blacktrash@gmx.net>
parents: 32
diff changeset
    53
    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
    54
        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
    55
        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
    56
ba000e29ecf3 Implement near CVS compability with more than one keyword
Christian Ebert <blacktrash@gmx.net>
parents: 41
diff changeset
    57
            def kwexpand(matchobj):
ba000e29ecf3 Implement near CVS compability with more than one keyword
Christian Ebert <blacktrash@gmx.net>
parents: 41
diff changeset
    58
                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
    59
                Source = os.path.join(repo.root, f)+',v'
ba000e29ecf3 Implement near CVS compability with more than one keyword
Christian Ebert <blacktrash@gmx.net>
parents: 41
diff changeset
    60
                Revision = args['node'][:12]
ba000e29ecf3 Implement near CVS compability with more than one keyword
Christian Ebert <blacktrash@gmx.net>
parents: 41
diff changeset
    61
                Date = strdate
ba000e29ecf3 Implement near CVS compability with more than one keyword
Christian Ebert <blacktrash@gmx.net>
parents: 41
diff changeset
    62
                Author = user
ba000e29ecf3 Implement near CVS compability with more than one keyword
Christian Ebert <blacktrash@gmx.net>
parents: 41
diff changeset
    63
                revdateauth = '%s %s %s' % (Revision, shortdate, shortuser)
ba000e29ecf3 Implement near CVS compability with more than one keyword
Christian Ebert <blacktrash@gmx.net>
parents: 41
diff changeset
    64
                Header = '%s %s' % (Source, revdateauth)
ba000e29ecf3 Implement near CVS compability with more than one keyword
Christian Ebert <blacktrash@gmx.net>
parents: 41
diff changeset
    65
                Id = '%s %s' % (RCSFile, revdateauth)
ba000e29ecf3 Implement near CVS compability with more than one keyword
Christian Ebert <blacktrash@gmx.net>
parents: 41
diff changeset
    66
                return '$%s: %s $' % (
ba000e29ecf3 Implement near CVS compability with more than one keyword
Christian Ebert <blacktrash@gmx.net>
parents: 41
diff changeset
    67
                        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
    68
ba000e29ecf3 Implement near CVS compability with more than one keyword
Christian Ebert <blacktrash@gmx.net>
parents: 41
diff changeset
    69
            data, kwct = re_kw.subn(kwexpand, data)
37
3dc31476c148 Standalone version for keyword.py
Christian Ebert <blacktrash@gmx.net>
parents: 32
diff changeset
    70
            if kwct:
3dc31476c148 Standalone version for keyword.py
Christian Ebert <blacktrash@gmx.net>
parents: 32
diff changeset
    71
                ui.note(_('expanding keywords in %s\n' % f))
3dc31476c148 Standalone version for keyword.py
Christian Ebert <blacktrash@gmx.net>
parents: 32
diff changeset
    72
                # 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
    73
                repo.wfile(f, 'w').write(data)