hgkw/kwexpander.py
author Christian Ebert <blacktrash@gmx.net>
Sat, 16 Dec 2006 15:22:56 +0100
branchupdatehook
changeset 17 7aebaf13ea3c
parent 5 85d1f5bf7cfc
child 21 536c1797202d
permissions -rw-r--r--
Start universal $Hg$ keyword expansion
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
5
85d1f5bf7cfc updatehook branch: add kwexpander and updatekw modules
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
     1
# $Hg: kwexpander.py,v$
85d1f5bf7cfc updatehook branch: add kwexpander and updatekw modules
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
     2
85d1f5bf7cfc updatehook branch: add kwexpander and updatekw modules
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
     3
from mercurial.i18n import gettext as _
85d1f5bf7cfc updatehook branch: add kwexpander and updatekw modules
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
     4
from mercurial.demandload import demandload
85d1f5bf7cfc updatehook branch: add kwexpander and updatekw modules
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
     5
demandload(globals(), 'mercurial:util')
85d1f5bf7cfc updatehook branch: add kwexpander and updatekw modules
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
     6
demandload(globals(), 'os.path re sys')
85d1f5bf7cfc updatehook branch: add kwexpander and updatekw modules
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
     7
85d1f5bf7cfc updatehook branch: add kwexpander and updatekw modules
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
     8
# name of keyword encode filter:
85d1f5bf7cfc updatehook branch: add kwexpander and updatekw modules
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
     9
kwencodefilter = 'hgkwencode'
85d1f5bf7cfc updatehook branch: add kwexpander and updatekw modules
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
    10
85d1f5bf7cfc updatehook branch: add kwexpander and updatekw modules
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
    11
def expandkw(ui, repo, parent1, node, candidates):
85d1f5bf7cfc updatehook branch: add kwexpander and updatekw modules
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
    12
    '''Important: returns False on success, True on failure.'''
85d1f5bf7cfc updatehook branch: add kwexpander and updatekw modules
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
    13
85d1f5bf7cfc updatehook branch: add kwexpander and updatekw modules
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
    14
    files = []
85d1f5bf7cfc updatehook branch: add kwexpander and updatekw modules
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
    15
    for pat, cmd in repo.ui.configitems('encode'):
85d1f5bf7cfc updatehook branch: add kwexpander and updatekw modules
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
    16
        if cmd.endswith(kwencodefilter):
85d1f5bf7cfc updatehook branch: add kwexpander and updatekw modules
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
    17
            mf = util.matcher(repo.root, '', [pat], [], [])[1]
85d1f5bf7cfc updatehook branch: add kwexpander and updatekw modules
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
    18
            for candidate in candidates:
85d1f5bf7cfc updatehook branch: add kwexpander and updatekw modules
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
    19
                if mf(candidate):
85d1f5bf7cfc updatehook branch: add kwexpander and updatekw modules
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
    20
                    files.append(candidate)
85d1f5bf7cfc updatehook branch: add kwexpander and updatekw modules
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
    21
85d1f5bf7cfc updatehook branch: add kwexpander and updatekw modules
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
    22
    if not files: # nothing to do
85d1f5bf7cfc updatehook branch: add kwexpander and updatekw modules
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
    23
        return False
85d1f5bf7cfc updatehook branch: add kwexpander and updatekw modules
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
    24
85d1f5bf7cfc updatehook branch: add kwexpander and updatekw modules
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
    25
    user, date = repo.changelog.read(parent1)[1:3]
85d1f5bf7cfc updatehook branch: add kwexpander and updatekw modules
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
    26
    user = util.shortuser(user)
85d1f5bf7cfc updatehook branch: add kwexpander and updatekw modules
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
    27
    date = util.datestr(date=date, format=util.defaultdateformats[2])
85d1f5bf7cfc updatehook branch: add kwexpander and updatekw modules
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
    28
                                                 # %Y-%m-%d %H:%M
85d1f5bf7cfc updatehook branch: add kwexpander and updatekw modules
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
    29
    re_kwcheck = re.compile(r'[$]Hg: (.*?),v.*?\$')
85d1f5bf7cfc updatehook branch: add kwexpander and updatekw modules
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
    30
85d1f5bf7cfc updatehook branch: add kwexpander and updatekw modules
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
    31
    for fn in files:
85d1f5bf7cfc updatehook branch: add kwexpander and updatekw modules
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
    32
85d1f5bf7cfc updatehook branch: add kwexpander and updatekw modules
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
    33
        data = repo.wopener(fn, 'rb').read()
85d1f5bf7cfc updatehook branch: add kwexpander and updatekw modules
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
    34
        bn = os.path.basename(fn)
85d1f5bf7cfc updatehook branch: add kwexpander and updatekw modules
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
    35
85d1f5bf7cfc updatehook branch: add kwexpander and updatekw modules
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
    36
        # check for keywords with incorrect basename
85d1f5bf7cfc updatehook branch: add kwexpander and updatekw modules
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
    37
        # eg. if you forgot to update basename manually after "hg mv"
85d1f5bf7cfc updatehook branch: add kwexpander and updatekw modules
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
    38
        failures = [m for m in map(str, re_kwcheck.findall(data)) if m != bn]
85d1f5bf7cfc updatehook branch: add kwexpander and updatekw modules
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
    39
        if failures:
85d1f5bf7cfc updatehook branch: add kwexpander and updatekw modules
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
    40
            failures = ['%sHg: %s,v$' % ('$', nobn) for nobn in failures]
85d1f5bf7cfc updatehook branch: add kwexpander and updatekw modules
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
    41
            ui.warn(_('%d incorrect basenames in file %s:\n'
85d1f5bf7cfc updatehook branch: add kwexpander and updatekw modules
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
    42
                '%s\nplease correct to %sHg: %s,v$\n'
85d1f5bf7cfc updatehook branch: add kwexpander and updatekw modules
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
    43
                % (len(failures), fn, ', '.join(failures), '$', bn)))
85d1f5bf7cfc updatehook branch: add kwexpander and updatekw modules
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
    44
            return True
85d1f5bf7cfc updatehook branch: add kwexpander and updatekw modules
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
    45
85d1f5bf7cfc updatehook branch: add kwexpander and updatekw modules
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
    46
        # substitute <Dollar>(Hg|Id): <basename>,v.*<Dollar>
85d1f5bf7cfc updatehook branch: add kwexpander and updatekw modules
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
    47
        re_kw = re.compile(r'([$]Hg: %s,v).*?\$' % bn)
85d1f5bf7cfc updatehook branch: add kwexpander and updatekw modules
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
    48
        data, kwct = re_kw.subn(r'\1 %s %s %s $' % (node, date, user), data)
85d1f5bf7cfc updatehook branch: add kwexpander and updatekw modules
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
    49
85d1f5bf7cfc updatehook branch: add kwexpander and updatekw modules
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
    50
        if kwct:
85d1f5bf7cfc updatehook branch: add kwexpander and updatekw modules
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
    51
            # backup file and write with expanded keyword
85d1f5bf7cfc updatehook branch: add kwexpander and updatekw modules
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
    52
            ui.note(_('expanding keywords in %s\n' % fn))
85d1f5bf7cfc updatehook branch: add kwexpander and updatekw modules
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
    53
            util.copyfile(fn, fn+'~')
85d1f5bf7cfc updatehook branch: add kwexpander and updatekw modules
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
    54
            repo.wopener(fn, 'wb').write(data)
85d1f5bf7cfc updatehook branch: add kwexpander and updatekw modules
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
    55
85d1f5bf7cfc updatehook branch: add kwexpander and updatekw modules
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
    56
    return False