hgkw/pretxnkw.py
author Christian Ebert <blacktrash@gmx.net>
Thu, 14 Dec 2006 12:55:52 +0100
branchdecodefilter
changeset 8 180d8484503a
parent 6 3ee39807daa5
child 9 437724c81b8f
permissions -rw-r--r--
FIX: range of status selection; copyfile using absolute path
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
6
3ee39807daa5 Add pretxnkw module for commits
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
     1
# $Hg: pretxnkw.py,v$
3ee39807daa5 Add pretxnkw module for commits
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
     2
3ee39807daa5 Add pretxnkw module for commits
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
     3
from mercurial.i18n import gettext as _
3ee39807daa5 Add pretxnkw module for commits
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
     4
from mercurial.demandload import demandload
3ee39807daa5 Add pretxnkw module for commits
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
     5
demandload(globals(), 'hgkw:kwutil mercurial:util os.path re')
3ee39807daa5 Add pretxnkw module for commits
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
     6
3ee39807daa5 Add pretxnkw module for commits
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
     7
kwencodefilter = 'hgkwencode'
3ee39807daa5 Add pretxnkw module for commits
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
     8
3ee39807daa5 Add pretxnkw module for commits
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
     9
def pretxnkw(ui=None, repo=None, hooktype='', **args):
3ee39807daa5 Add pretxnkw module for commits
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
    10
    '''Important: returns False on success, True on failure.'''
3ee39807daa5 Add pretxnkw module for commits
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
    11
3ee39807daa5 Add pretxnkw module for commits
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
    12
    node = args['node'][0:12]
3ee39807daa5 Add pretxnkw module for commits
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
    13
3ee39807daa5 Add pretxnkw module for commits
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
    14
    if not ui or not repo or not node or hooktype != 'pretxncommit':
3ee39807daa5 Add pretxnkw module for commits
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
    15
        # bail out with error
3ee39807daa5 Add pretxnkw module for commits
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
    16
        return True
3ee39807daa5 Add pretxnkw module for commits
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
    17
8
180d8484503a FIX: range of status selection; copyfile using absolute path
Christian Ebert <blacktrash@gmx.net>
parents: 6
diff changeset
    18
    modified, added = repo.status()[:2]
6
3ee39807daa5 Add pretxnkw module for commits
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
    19
    candidates = modified + added
3ee39807daa5 Add pretxnkw module for commits
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
    20
3ee39807daa5 Add pretxnkw module for commits
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
    21
    files = []
3ee39807daa5 Add pretxnkw module for commits
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
    22
    for pat, cmd in repo.ui.configitems('encode'):
3ee39807daa5 Add pretxnkw module for commits
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
    23
        if cmd.endswith(kwencodefilter):
3ee39807daa5 Add pretxnkw module for commits
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
    24
            mf = util.matcher(repo.root, '', [pat], [], [])[1]
3ee39807daa5 Add pretxnkw module for commits
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
    25
            for candidate in candidates:
3ee39807daa5 Add pretxnkw module for commits
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
    26
                if mf(candidate):
3ee39807daa5 Add pretxnkw module for commits
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
    27
                    files.append(candidate)
3ee39807daa5 Add pretxnkw module for commits
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
    28
3ee39807daa5 Add pretxnkw module for commits
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
    29
    if not files: # nothing to do
3ee39807daa5 Add pretxnkw module for commits
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
    30
        return False
3ee39807daa5 Add pretxnkw module for commits
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
    31
3ee39807daa5 Add pretxnkw module for commits
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
    32
    re_kw = kwutil.rekw()
3ee39807daa5 Add pretxnkw module for commits
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
    33
    kword = kwutil.mkkw(repo, tip=True, node=node)
3ee39807daa5 Add pretxnkw module for commits
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
    34
3ee39807daa5 Add pretxnkw module for commits
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
    35
    re_kwcheck = re.compile(r'[$]Hg: (.*?),v.*?\$')
3ee39807daa5 Add pretxnkw module for commits
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
    36
3ee39807daa5 Add pretxnkw module for commits
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
    37
    for filename in files:
3ee39807daa5 Add pretxnkw module for commits
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
    38
3ee39807daa5 Add pretxnkw module for commits
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
    39
        data = repo.wopener(filename, 'rb').read()
3ee39807daa5 Add pretxnkw module for commits
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
    40
        bn = os.path.basename(filename)
3ee39807daa5 Add pretxnkw module for commits
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
    41
3ee39807daa5 Add pretxnkw module for commits
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
    42
        # check for keywords with incorrect basename
3ee39807daa5 Add pretxnkw module for commits
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
    43
        # eg. if you forgot to update basename manually after "hg mv"
3ee39807daa5 Add pretxnkw module for commits
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
    44
        failures = [m for m in map(str, re_kwcheck.findall(data)) if m != bn]
3ee39807daa5 Add pretxnkw module for commits
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
    45
        if failures:
3ee39807daa5 Add pretxnkw module for commits
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
    46
            failures = ['%sHg: %s,v$' % ('$', nobn) for nobn in failures]
3ee39807daa5 Add pretxnkw module for commits
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
    47
            ui.warn(_('%d incorrect basenames in file %s:\n'
3ee39807daa5 Add pretxnkw module for commits
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
    48
                '%s\nplease correct to %sHg: %s,v$\n'
3ee39807daa5 Add pretxnkw module for commits
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
    49
                % (len(failures), filename, ', '.join(failures), '$', bn)))
3ee39807daa5 Add pretxnkw module for commits
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
    50
            return True
3ee39807daa5 Add pretxnkw module for commits
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
    51
3ee39807daa5 Add pretxnkw module for commits
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
    52
        # substitute <Dollar>(Hg|Id): <basename>,v.*<Dollar>
3ee39807daa5 Add pretxnkw module for commits
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
    53
        data, kwct = re_kw.subn(kword, data)
3ee39807daa5 Add pretxnkw module for commits
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
    54
3ee39807daa5 Add pretxnkw module for commits
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
    55
        if kwct:
3ee39807daa5 Add pretxnkw module for commits
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
    56
            # backup file and write with expanded keyword
3ee39807daa5 Add pretxnkw module for commits
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
    57
            ui.note(_('expanding keywords in %s\n' % filename))
8
180d8484503a FIX: range of status selection; copyfile using absolute path
Christian Ebert <blacktrash@gmx.net>
parents: 6
diff changeset
    58
            absname = os.path.join(repo.root, filename)
180d8484503a FIX: range of status selection; copyfile using absolute path
Christian Ebert <blacktrash@gmx.net>
parents: 6
diff changeset
    59
            util.copyfile(absname, absname+'~')
6
3ee39807daa5 Add pretxnkw module for commits
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
    60
            repo.wopener(filename, 'wb').write(data)
3ee39807daa5 Add pretxnkw module for commits
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
    61
3ee39807daa5 Add pretxnkw module for commits
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
    62
    return False