hgkw/pretxnkw.py
author Christian Ebert <blacktrash@gmx.net>
Thu, 14 Dec 2006 16:24:30 +0100
branchdecodefilter
changeset 11 56a61a5c696d
parent 10 d8cd07b798f2
child 13 d6c0f391a662
permissions -rw-r--r--
Switch to complete filename in first keyword field This will hopefully allow to iterate over the stream in hgkwdecode, and lookup the relatively latest revision of corresponding file.
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
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
9
437724c81b8f Use wfile instead of wopener; read and write string not binary
Christian Ebert <blacktrash@gmx.net>
parents: 8
diff changeset
    39
        data = repo.wfile(filename).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))
56a61a5c696d Switch to complete filename in first keyword field
Christian Ebert <blacktrash@gmx.net>
parents: 10
diff changeset
    44
                if m != filename]
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'
11
56a61a5c696d Switch to complete filename in first keyword field
Christian Ebert <blacktrash@gmx.net>
parents: 10
diff changeset
    49
                % (len(invalids), filename, ', '.join(invalids), '$', filename)
56a61a5c696d Switch to complete filename in first keyword field
Christian Ebert <blacktrash@gmx.net>
parents: 10
diff changeset
    50
                ))
6
3ee39807daa5 Add pretxnkw module for commits
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
    51
            return True
3ee39807daa5 Add pretxnkw module for commits
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
    52
3ee39807daa5 Add pretxnkw module for commits
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
    53
        # substitute <Dollar>(Hg|Id): <basename>,v.*<Dollar>
3ee39807daa5 Add pretxnkw module for commits
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
    54
        data, kwct = re_kw.subn(kword, data)
3ee39807daa5 Add pretxnkw module for commits
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
    55
3ee39807daa5 Add pretxnkw module for commits
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
    56
        if kwct:
3ee39807daa5 Add pretxnkw module for commits
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
    57
            # backup file and write with expanded keyword
3ee39807daa5 Add pretxnkw module for commits
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
    58
            ui.note(_('expanding keywords in %s\n' % filename))
10
d8cd07b798f2 Use localrepository's wjoin() to obtain absolute path
Christian Ebert <blacktrash@gmx.net>
parents: 9
diff changeset
    59
            absname = repo.wjoin(filename)
8
180d8484503a FIX: range of status selection; copyfile using absolute path
Christian Ebert <blacktrash@gmx.net>
parents: 6
diff changeset
    60
            util.copyfile(absname, absname+'~')
9
437724c81b8f Use wfile instead of wopener; read and write string not binary
Christian Ebert <blacktrash@gmx.net>
parents: 8
diff changeset
    61
            repo.wfile(filename, 'w').write(data)
6
3ee39807daa5 Add pretxnkw module for commits
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
    62
3ee39807daa5 Add pretxnkw module for commits
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
    63
    return False