hgkw/pretxnkw.py
author Christian Ebert <blacktrash@gmx.net>
Tue, 19 Dec 2006 15:57:03 +0100
branchextension
changeset 37 3dc31476c148
parent 32 b70b38b15fa4
child 39 14038784f986
permissions -rw-r--r--
Standalone version for keyword.py Check "expand" patterns in the [keyword] config section. Removed keyword in file itself to simplify patterns. Revert (again) to basename.
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
b70b38b15fa4 Update function descriptions
Christian Ebert <blacktrash@gmx.net>
parents: 27
diff changeset
     7
    and passes them to kwexpander.'''
7
e5f131217f87 Add pretxnkw module for updatehook branch
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
     8
20
6dc2b4268920 Simplify for $Hg$ expansion scheme
Christian Ebert <blacktrash@gmx.net>
parents: 7
diff changeset
     9
    if hooktype != 'pretxncommit':
7
e5f131217f87 Add pretxnkw module for updatehook branch
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
    10
        # bail out with error
e5f131217f87 Add pretxnkw module for updatehook branch
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
    11
        return True
e5f131217f87 Add pretxnkw module for updatehook branch
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
    12
26
bda0dec1aaf1 Reparse cmdline to avoid expansion in uncommitted files
Christian Ebert <blacktrash@gmx.net>
parents: 20
diff changeset
    13
    # 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
    14
    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
    15
    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
    16
bda0dec1aaf1 Reparse cmdline to avoid expansion in uncommitted files
Christian Ebert <blacktrash@gmx.net>
parents: 20
diff changeset
    17
    # 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
    18
    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
    19
    candidates = modified + added
e5f131217f87 Add pretxnkw module for updatehook branch
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
    20
37
3dc31476c148 Standalone version for keyword.py
Christian Ebert <blacktrash@gmx.net>
parents: 32
diff changeset
    21
    if not candidates:
3dc31476c148 Standalone version for keyword.py
Christian Ebert <blacktrash@gmx.net>
parents: 32
diff changeset
    22
        return False
3dc31476c148 Standalone version for keyword.py
Christian Ebert <blacktrash@gmx.net>
parents: 32
diff changeset
    23
3dc31476c148 Standalone version for keyword.py
Christian Ebert <blacktrash@gmx.net>
parents: 32
diff changeset
    24
    # 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
    25
    files = []
3dc31476c148 Standalone version for keyword.py
Christian Ebert <blacktrash@gmx.net>
parents: 32
diff changeset
    26
    # python2.4: files = set()
3dc31476c148 Standalone version for keyword.py
Christian Ebert <blacktrash@gmx.net>
parents: 32
diff changeset
    27
    for pat, opt in repo.ui.configitems('keyword'):
3dc31476c148 Standalone version for keyword.py
Christian Ebert <blacktrash@gmx.net>
parents: 32
diff changeset
    28
        if opt == 'expand':
3dc31476c148 Standalone version for keyword.py
Christian Ebert <blacktrash@gmx.net>
parents: 32
diff changeset
    29
            mf = util.matcher(repo.root, '', [pat], [], [])[1]
3dc31476c148 Standalone version for keyword.py
Christian Ebert <blacktrash@gmx.net>
parents: 32
diff changeset
    30
            for candidate in candidates:
3dc31476c148 Standalone version for keyword.py
Christian Ebert <blacktrash@gmx.net>
parents: 32
diff changeset
    31
                if mf(candidate) and candidate not in files:
3dc31476c148 Standalone version for keyword.py
Christian Ebert <blacktrash@gmx.net>
parents: 32
diff changeset
    32
                    files.append(candidate)
3dc31476c148 Standalone version for keyword.py
Christian Ebert <blacktrash@gmx.net>
parents: 32
diff changeset
    33
                # python2.4:
3dc31476c148 Standalone version for keyword.py
Christian Ebert <blacktrash@gmx.net>
parents: 32
diff changeset
    34
                # if mf(candidate): files.add(candidate)
3dc31476c148 Standalone version for keyword.py
Christian Ebert <blacktrash@gmx.net>
parents: 32
diff changeset
    35
3dc31476c148 Standalone version for keyword.py
Christian Ebert <blacktrash@gmx.net>
parents: 32
diff changeset
    36
    if not files: # nothing to do
3dc31476c148 Standalone version for keyword.py
Christian Ebert <blacktrash@gmx.net>
parents: 32
diff changeset
    37
        return False
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
    cid = args['node'][:12]
3dc31476c148 Standalone version for keyword.py
Christian Ebert <blacktrash@gmx.net>
parents: 32
diff changeset
    40
    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
    41
    user = util.shortuser(user)
3dc31476c148 Standalone version for keyword.py
Christian Ebert <blacktrash@gmx.net>
parents: 32
diff changeset
    42
    date = util.datestr(date=date, format=util.defaultdateformats[0])
3dc31476c148 Standalone version for keyword.py
Christian Ebert <blacktrash@gmx.net>
parents: 32
diff changeset
    43
                                               # %Y-%m-%d %H:%M:%S
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
    re_kw = re.compile(r'\$Hg[^$]*?\$')
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:
3dc31476c148 Standalone version for keyword.py
Christian Ebert <blacktrash@gmx.net>
parents: 32
diff changeset
    48
        text = repo.wfile(f).read()
3dc31476c148 Standalone version for keyword.py
Christian Ebert <blacktrash@gmx.net>
parents: 32
diff changeset
    49
        if not util.binary(text):
3dc31476c148 Standalone version for keyword.py
Christian Ebert <blacktrash@gmx.net>
parents: 32
diff changeset
    50
            kw = '$Hg: %s,v %s %s %s $' % (
3dc31476c148 Standalone version for keyword.py
Christian Ebert <blacktrash@gmx.net>
parents: 32
diff changeset
    51
                    os.path.basename(f), cid, date, user)
3dc31476c148 Standalone version for keyword.py
Christian Ebert <blacktrash@gmx.net>
parents: 32
diff changeset
    52
            text, kwct = re_kw.subn(kw, text)
3dc31476c148 Standalone version for keyword.py
Christian Ebert <blacktrash@gmx.net>
parents: 32
diff changeset
    53
            if kwct:
3dc31476c148 Standalone version for keyword.py
Christian Ebert <blacktrash@gmx.net>
parents: 32
diff changeset
    54
                ui.note(_('expanding keywords in %s\n' % f))
3dc31476c148 Standalone version for keyword.py
Christian Ebert <blacktrash@gmx.net>
parents: 32
diff changeset
    55
                # backup file?
3dc31476c148 Standalone version for keyword.py
Christian Ebert <blacktrash@gmx.net>
parents: 32
diff changeset
    56
                repo.wfile(f, 'w').write(text)