hgkw/pretxnkw.py
author Christian Ebert <blacktrash@gmx.net>
Thu, 14 Dec 2006 16:27:30 +0100
branchdecodefilter
changeset 13 d6c0f391a662
parent 11 56a61a5c696d
permissions -rw-r--r--
Short "f" for "filename" iteration variable
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
11
56a61a5c696d Switch to complete filename in first keyword field
Christian Ebert <blacktrash@gmx.net>
parents: 10
diff changeset
     1
# $Hg: hgkw/pretxnkw.py,v$
6
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
11
56a61a5c696d Switch to complete filename in first keyword field
Christian Ebert <blacktrash@gmx.net>
parents: 10
diff changeset
     5
demandload(globals(), 'hgkw:kwutil mercurial:util re')
6
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
13
d6c0f391a662 Short "f" for "filename" iteration variable
Christian Ebert <blacktrash@gmx.net>
parents: 11
diff changeset
    37
    for f in files:
6
3ee39807daa5 Add pretxnkw module for commits
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
    38
13
d6c0f391a662 Short "f" for "filename" iteration variable
Christian Ebert <blacktrash@gmx.net>
parents: 11
diff changeset
    39
        data = repo.wfile(f).read()
6
3ee39807daa5 Add pretxnkw module for commits
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
    40
11
56a61a5c696d Switch to complete filename in first keyword field
Christian Ebert <blacktrash@gmx.net>
parents: 10
diff changeset
    41
        # check for keywords with incorrect filename
56a61a5c696d Switch to complete filename in first keyword field
Christian Ebert <blacktrash@gmx.net>
parents: 10
diff changeset
    42
        # eg. if you forgot to update filename manually after "hg mv"
56a61a5c696d Switch to complete filename in first keyword field
Christian Ebert <blacktrash@gmx.net>
parents: 10
diff changeset
    43
        invalids = [m for m in map(str, re_kwcheck.findall(data))
13
d6c0f391a662 Short "f" for "filename" iteration variable
Christian Ebert <blacktrash@gmx.net>
parents: 11
diff changeset
    44
                if m != f]
11
56a61a5c696d Switch to complete filename in first keyword field
Christian Ebert <blacktrash@gmx.net>
parents: 10
diff changeset
    45
        if invalids:
56a61a5c696d Switch to complete filename in first keyword field
Christian Ebert <blacktrash@gmx.net>
parents: 10
diff changeset
    46
            invalids = ['%sHg: %s,v$' % ('$', i) for i in invalids]
56a61a5c696d Switch to complete filename in first keyword field
Christian Ebert <blacktrash@gmx.net>
parents: 10
diff changeset
    47
            ui.warn(_('%d invalid keyword filenames in file %s:\n'
6
3ee39807daa5 Add pretxnkw module for commits
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
    48
                '%s\nplease correct to %sHg: %s,v$\n'
13
d6c0f391a662 Short "f" for "filename" iteration variable
Christian Ebert <blacktrash@gmx.net>
parents: 11
diff changeset
    49
                % (len(invalids), f, ', '.join(invalids), '$', f)))
6
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
13
d6c0f391a662 Short "f" for "filename" iteration variable
Christian Ebert <blacktrash@gmx.net>
parents: 11
diff changeset
    57
            ui.note(_('expanding keywords in %s\n' % f))
d6c0f391a662 Short "f" for "filename" iteration variable
Christian Ebert <blacktrash@gmx.net>
parents: 11
diff changeset
    58
            absname = repo.wjoin(f)
8
180d8484503a FIX: range of status selection; copyfile using absolute path
Christian Ebert <blacktrash@gmx.net>
parents: 6
diff changeset
    59
            util.copyfile(absname, absname+'~')
13
d6c0f391a662 Short "f" for "filename" iteration variable
Christian Ebert <blacktrash@gmx.net>
parents: 11
diff changeset
    60
            repo.wfile(f, 'w').write(data)
6
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