hgkw/pretxnkw.py
author Christian Ebert <blacktrash@gmx.net>
Wed, 20 Dec 2006 17:41:19 +0100
branchextension
changeset 42 ba000e29ecf3
parent 41 e1c539e1282b
child 44 dc6e7d0e607f
permissions -rw-r--r--
Implement near CVS compability with more than one keyword Keywords are set as local vars in kwexpand() and then eval'd. TODO: (sticky) tag?
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
bda0dec1aaf1 Reparse cmdline to avoid expansion in uncommitted files
Christian Ebert <blacktrash@gmx.net>
parents: 20
diff changeset
    17
    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
    18
    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
    19
bda0dec1aaf1 Reparse cmdline to avoid expansion in uncommitted files
Christian Ebert <blacktrash@gmx.net>
parents: 20
diff changeset
    20
    # 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
    21
    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
    22
    candidates = modified + added
e5f131217f87 Add pretxnkw module for updatehook branch
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
    23
37
3dc31476c148 Standalone version for keyword.py
Christian Ebert <blacktrash@gmx.net>
parents: 32
diff changeset
    24
    if not candidates:
3dc31476c148 Standalone version for keyword.py
Christian Ebert <blacktrash@gmx.net>
parents: 32
diff changeset
    25
        return False
3dc31476c148 Standalone version for keyword.py
Christian Ebert <blacktrash@gmx.net>
parents: 32
diff changeset
    26
3dc31476c148 Standalone version for keyword.py
Christian Ebert <blacktrash@gmx.net>
parents: 32
diff changeset
    27
    # 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
    28
    files = []
3dc31476c148 Standalone version for keyword.py
Christian Ebert <blacktrash@gmx.net>
parents: 32
diff changeset
    29
    # python2.4: files = set()
3dc31476c148 Standalone version for keyword.py
Christian Ebert <blacktrash@gmx.net>
parents: 32
diff changeset
    30
    for pat, opt in repo.ui.configitems('keyword'):
3dc31476c148 Standalone version for keyword.py
Christian Ebert <blacktrash@gmx.net>
parents: 32
diff changeset
    31
        if opt == 'expand':
3dc31476c148 Standalone version for keyword.py
Christian Ebert <blacktrash@gmx.net>
parents: 32
diff changeset
    32
            mf = util.matcher(repo.root, '', [pat], [], [])[1]
3dc31476c148 Standalone version for keyword.py
Christian Ebert <blacktrash@gmx.net>
parents: 32
diff changeset
    33
            for candidate in candidates:
3dc31476c148 Standalone version for keyword.py
Christian Ebert <blacktrash@gmx.net>
parents: 32
diff changeset
    34
                if mf(candidate) and candidate not in files:
3dc31476c148 Standalone version for keyword.py
Christian Ebert <blacktrash@gmx.net>
parents: 32
diff changeset
    35
                    files.append(candidate)
3dc31476c148 Standalone version for keyword.py
Christian Ebert <blacktrash@gmx.net>
parents: 32
diff changeset
    36
                # python2.4:
3dc31476c148 Standalone version for keyword.py
Christian Ebert <blacktrash@gmx.net>
parents: 32
diff changeset
    37
                # if mf(candidate): files.add(candidate)
3dc31476c148 Standalone version for keyword.py
Christian Ebert <blacktrash@gmx.net>
parents: 32
diff changeset
    38
3dc31476c148 Standalone version for keyword.py
Christian Ebert <blacktrash@gmx.net>
parents: 32
diff changeset
    39
    if not files: # nothing to do
3dc31476c148 Standalone version for keyword.py
Christian Ebert <blacktrash@gmx.net>
parents: 32
diff changeset
    40
        return False
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
    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
    43
    strdate = util.datestr(date=date)
ba000e29ecf3 Implement near CVS compability with more than one keyword
Christian Ebert <blacktrash@gmx.net>
parents: 41
diff changeset
    44
    shortuser = util.shortuser(user)
ba000e29ecf3 Implement near CVS compability with more than one keyword
Christian Ebert <blacktrash@gmx.net>
parents: 41
diff changeset
    45
    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
    46
                                               # %Y-%m-%d %H:%M:%S
3dc31476c148 Standalone version for keyword.py
Christian Ebert <blacktrash@gmx.net>
parents: 32
diff changeset
    47
42
ba000e29ecf3 Implement near CVS compability with more than one keyword
Christian Ebert <blacktrash@gmx.net>
parents: 41
diff changeset
    48
    re_kw = re.compile(r'\$(%s)(: [^$]+? )?\$' % hgkeywords)
37
3dc31476c148 Standalone version for keyword.py
Christian Ebert <blacktrash@gmx.net>
parents: 32
diff changeset
    49
3dc31476c148 Standalone version for keyword.py
Christian Ebert <blacktrash@gmx.net>
parents: 32
diff changeset
    50
    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
    51
        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
    52
        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
    53
ba000e29ecf3 Implement near CVS compability with more than one keyword
Christian Ebert <blacktrash@gmx.net>
parents: 41
diff changeset
    54
            def kwexpand(matchobj):
ba000e29ecf3 Implement near CVS compability with more than one keyword
Christian Ebert <blacktrash@gmx.net>
parents: 41
diff changeset
    55
                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
    56
                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
    57
                Revision = args['node'][:12]
ba000e29ecf3 Implement near CVS compability with more than one keyword
Christian Ebert <blacktrash@gmx.net>
parents: 41
diff changeset
    58
                Date = strdate
ba000e29ecf3 Implement near CVS compability with more than one keyword
Christian Ebert <blacktrash@gmx.net>
parents: 41
diff changeset
    59
                Author = user
ba000e29ecf3 Implement near CVS compability with more than one keyword
Christian Ebert <blacktrash@gmx.net>
parents: 41
diff changeset
    60
                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
    61
                Header = '%s %s' % (Source, revdateauth)
ba000e29ecf3 Implement near CVS compability with more than one keyword
Christian Ebert <blacktrash@gmx.net>
parents: 41
diff changeset
    62
                Id = '%s %s' % (RCSFile, revdateauth)
ba000e29ecf3 Implement near CVS compability with more than one keyword
Christian Ebert <blacktrash@gmx.net>
parents: 41
diff changeset
    63
                return '$%s: %s $' % (
ba000e29ecf3 Implement near CVS compability with more than one keyword
Christian Ebert <blacktrash@gmx.net>
parents: 41
diff changeset
    64
                        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
    65
ba000e29ecf3 Implement near CVS compability with more than one keyword
Christian Ebert <blacktrash@gmx.net>
parents: 41
diff changeset
    66
            data, kwct = re_kw.subn(kwexpand, data)
37
3dc31476c148 Standalone version for keyword.py
Christian Ebert <blacktrash@gmx.net>
parents: 32
diff changeset
    67
            if kwct:
3dc31476c148 Standalone version for keyword.py
Christian Ebert <blacktrash@gmx.net>
parents: 32
diff changeset
    68
                ui.note(_('expanding keywords in %s\n' % f))
3dc31476c148 Standalone version for keyword.py
Christian Ebert <blacktrash@gmx.net>
parents: 32
diff changeset
    69
                # 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
    70
                repo.wfile(f, 'w').write(data)