hgkw/kwexpander.py
author Christian Ebert <blacktrash@gmx.net>
Sat, 16 Dec 2006 15:53:45 +0100
branchupdatehook
changeset 21 536c1797202d
parent 5 85d1f5bf7cfc
child 23 9fdf507badfc
permissions -rw-r--r--
Implement $Hg$ scheme with update hook The pivotal line for update hook is: repo.dirstate.update(kwupdates, 'n') This forces hg to consider the freshly written files as not modified. Thanks to wfile(), this keeps executable bits etc. Still needs more testing. No need to check for basename/filename in keyword trigger. update hook does not need re. TODO: Walk back in history, if last change of file didn't happen in 1 of the provided changesets (update/merge?)?
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
21
536c1797202d Implement $Hg$ scheme with update hook
Christian Ebert <blacktrash@gmx.net>
parents: 5
diff changeset
     1
# $Hg$
5
85d1f5bf7cfc updatehook branch: add kwexpander and updatekw modules
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
     2
21
536c1797202d Implement $Hg$ scheme with update hook
Christian Ebert <blacktrash@gmx.net>
parents: 5
diff changeset
     3
from mercurial import demandimport
536c1797202d Implement $Hg$ scheme with update hook
Christian Ebert <blacktrash@gmx.net>
parents: 5
diff changeset
     4
demandimport.enable()
536c1797202d Implement $Hg$ scheme with update hook
Christian Ebert <blacktrash@gmx.net>
parents: 5
diff changeset
     5
536c1797202d Implement $Hg$ scheme with update hook
Christian Ebert <blacktrash@gmx.net>
parents: 5
diff changeset
     6
from mercurial.i18n import _
536c1797202d Implement $Hg$ scheme with update hook
Christian Ebert <blacktrash@gmx.net>
parents: 5
diff changeset
     7
import mercurial.util, re
5
85d1f5bf7cfc updatehook branch: add kwexpander and updatekw modules
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
     8
85d1f5bf7cfc updatehook branch: add kwexpander and updatekw modules
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
     9
# name of keyword encode filter:
85d1f5bf7cfc updatehook branch: add kwexpander and updatekw modules
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
    10
kwencodefilter = 'hgkwencode'
21
536c1797202d Implement $Hg$ scheme with update hook
Christian Ebert <blacktrash@gmx.net>
parents: 5
diff changeset
    11
# look for <Dollar>Hg<Dollar>
536c1797202d Implement $Hg$ scheme with update hook
Christian Ebert <blacktrash@gmx.net>
parents: 5
diff changeset
    12
kwtrigger = '%sHg$' % '$'
5
85d1f5bf7cfc updatehook branch: add kwexpander and updatekw modules
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
    13
21
536c1797202d Implement $Hg$ scheme with update hook
Christian Ebert <blacktrash@gmx.net>
parents: 5
diff changeset
    14
def wwritekw(ui, repo, f, text):
536c1797202d Implement $Hg$ scheme with update hook
Christian Ebert <blacktrash@gmx.net>
parents: 5
diff changeset
    15
    '''Writes text with kwupdates keywords to f in working directory.'''
536c1797202d Implement $Hg$ scheme with update hook
Christian Ebert <blacktrash@gmx.net>
parents: 5
diff changeset
    16
    ui.note(_('expanding keywords in %s\n' % f))
536c1797202d Implement $Hg$ scheme with update hook
Christian Ebert <blacktrash@gmx.net>
parents: 5
diff changeset
    17
#   # backup file (?)
536c1797202d Implement $Hg$ scheme with update hook
Christian Ebert <blacktrash@gmx.net>
parents: 5
diff changeset
    18
#   absfile = repo.wjoin(f)
536c1797202d Implement $Hg$ scheme with update hook
Christian Ebert <blacktrash@gmx.net>
parents: 5
diff changeset
    19
#   mercurial.util.copyfile(absfile, absfile+'.kwbak')
536c1797202d Implement $Hg$ scheme with update hook
Christian Ebert <blacktrash@gmx.net>
parents: 5
diff changeset
    20
    repo.wfile(f, 'w').write(text)
536c1797202d Implement $Hg$ scheme with update hook
Christian Ebert <blacktrash@gmx.net>
parents: 5
diff changeset
    21
536c1797202d Implement $Hg$ scheme with update hook
Christian Ebert <blacktrash@gmx.net>
parents: 5
diff changeset
    22
def expandkw(ui, repo, node, cid, candidates, update=False):
5
85d1f5bf7cfc updatehook branch: add kwexpander and updatekw modules
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
    23
    '''Important: returns False on success, True on failure.'''
85d1f5bf7cfc updatehook branch: add kwexpander and updatekw modules
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
    24
21
536c1797202d Implement $Hg$ scheme with update hook
Christian Ebert <blacktrash@gmx.net>
parents: 5
diff changeset
    25
    # only check files that have hgkwencode assigned as encode filter
5
85d1f5bf7cfc updatehook branch: add kwexpander and updatekw modules
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
    26
    files = []
85d1f5bf7cfc updatehook branch: add kwexpander and updatekw modules
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
    27
    for pat, cmd in repo.ui.configitems('encode'):
85d1f5bf7cfc updatehook branch: add kwexpander and updatekw modules
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
    28
        if cmd.endswith(kwencodefilter):
21
536c1797202d Implement $Hg$ scheme with update hook
Christian Ebert <blacktrash@gmx.net>
parents: 5
diff changeset
    29
            mf = mercurial.util.matcher(repo.root, '', [pat], [], [])[1]
5
85d1f5bf7cfc updatehook branch: add kwexpander and updatekw modules
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
    30
            for candidate in candidates:
85d1f5bf7cfc updatehook branch: add kwexpander and updatekw modules
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
    31
                if mf(candidate):
21
536c1797202d Implement $Hg$ scheme with update hook
Christian Ebert <blacktrash@gmx.net>
parents: 5
diff changeset
    32
                    # check again that there really are no duplicates
536c1797202d Implement $Hg$ scheme with update hook
Christian Ebert <blacktrash@gmx.net>
parents: 5
diff changeset
    33
                    # if candidate not in files ???
5
85d1f5bf7cfc updatehook branch: add kwexpander and updatekw modules
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
    34
                    files.append(candidate)
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
    if not files: # nothing to do
85d1f5bf7cfc updatehook branch: add kwexpander and updatekw modules
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
    37
        return False
85d1f5bf7cfc updatehook branch: add kwexpander and updatekw modules
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
    38
21
536c1797202d Implement $Hg$ scheme with update hook
Christian Ebert <blacktrash@gmx.net>
parents: 5
diff changeset
    39
    user, date = repo.changelog.read(node)[1:3]
536c1797202d Implement $Hg$ scheme with update hook
Christian Ebert <blacktrash@gmx.net>
parents: 5
diff changeset
    40
    user = mercurial.util.shortuser(user)
536c1797202d Implement $Hg$ scheme with update hook
Christian Ebert <blacktrash@gmx.net>
parents: 5
diff changeset
    41
    date = mercurial.util.datestr(date=date,
536c1797202d Implement $Hg$ scheme with update hook
Christian Ebert <blacktrash@gmx.net>
parents: 5
diff changeset
    42
            format=mercurial.util.defaultdateformats[2])
536c1797202d Implement $Hg$ scheme with update hook
Christian Ebert <blacktrash@gmx.net>
parents: 5
diff changeset
    43
                                  # %Y-%m-%d %H:%M
5
85d1f5bf7cfc updatehook branch: add kwexpander and updatekw modules
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
    44
21
536c1797202d Implement $Hg$ scheme with update hook
Christian Ebert <blacktrash@gmx.net>
parents: 5
diff changeset
    45
    # collect filenames that were changed by hg update
536c1797202d Implement $Hg$ scheme with update hook
Christian Ebert <blacktrash@gmx.net>
parents: 5
diff changeset
    46
    kwupdates = []
5
85d1f5bf7cfc updatehook branch: add kwexpander and updatekw modules
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
    47
21
536c1797202d Implement $Hg$ scheme with update hook
Christian Ebert <blacktrash@gmx.net>
parents: 5
diff changeset
    48
    for f in files:
536c1797202d Implement $Hg$ scheme with update hook
Christian Ebert <blacktrash@gmx.net>
parents: 5
diff changeset
    49
536c1797202d Implement $Hg$ scheme with update hook
Christian Ebert <blacktrash@gmx.net>
parents: 5
diff changeset
    50
        text = repo.wfile(f).read()
536c1797202d Implement $Hg$ scheme with update hook
Christian Ebert <blacktrash@gmx.net>
parents: 5
diff changeset
    51
        kwrepl = '%sHg: %s,v %s %s %s $' % ('$', f, cid, date, user)
5
85d1f5bf7cfc updatehook branch: add kwexpander and updatekw modules
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
    52
21
536c1797202d Implement $Hg$ scheme with update hook
Christian Ebert <blacktrash@gmx.net>
parents: 5
diff changeset
    53
        if update and text.find(kwtrigger) > -1:
536c1797202d Implement $Hg$ scheme with update hook
Christian Ebert <blacktrash@gmx.net>
parents: 5
diff changeset
    54
            text = text.replace(kwtrigger, kwrepl)
536c1797202d Implement $Hg$ scheme with update hook
Christian Ebert <blacktrash@gmx.net>
parents: 5
diff changeset
    55
            wwritekw(ui, repo, f, text)
536c1797202d Implement $Hg$ scheme with update hook
Christian Ebert <blacktrash@gmx.net>
parents: 5
diff changeset
    56
            kwupdates.append(f)
536c1797202d Implement $Hg$ scheme with update hook
Christian Ebert <blacktrash@gmx.net>
parents: 5
diff changeset
    57
        
536c1797202d Implement $Hg$ scheme with update hook
Christian Ebert <blacktrash@gmx.net>
parents: 5
diff changeset
    58
        elif not update:
536c1797202d Implement $Hg$ scheme with update hook
Christian Ebert <blacktrash@gmx.net>
parents: 5
diff changeset
    59
            re_kw = re.compile(r'[$]Hg(: %s,v [a-z0-9]{12} [^$]+? )?\$' % f)
536c1797202d Implement $Hg$ scheme with update hook
Christian Ebert <blacktrash@gmx.net>
parents: 5
diff changeset
    60
            text, kwct = re_kw.subn(kwrepl, text)
536c1797202d Implement $Hg$ scheme with update hook
Christian Ebert <blacktrash@gmx.net>
parents: 5
diff changeset
    61
            if kwct:
536c1797202d Implement $Hg$ scheme with update hook
Christian Ebert <blacktrash@gmx.net>
parents: 5
diff changeset
    62
                wwritekw(ui, repo, f, text)
5
85d1f5bf7cfc updatehook branch: add kwexpander and updatekw modules
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
    63
21
536c1797202d Implement $Hg$ scheme with update hook
Christian Ebert <blacktrash@gmx.net>
parents: 5
diff changeset
    64
    if kwupdates:
536c1797202d Implement $Hg$ scheme with update hook
Christian Ebert <blacktrash@gmx.net>
parents: 5
diff changeset
    65
        # cheat hg to believe that updated files were not modified
536c1797202d Implement $Hg$ scheme with update hook
Christian Ebert <blacktrash@gmx.net>
parents: 5
diff changeset
    66
        repo.dirstate.update(kwupdates, 'n')
5
85d1f5bf7cfc updatehook branch: add kwexpander and updatekw modules
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
    67
85d1f5bf7cfc updatehook branch: add kwexpander and updatekw modules
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
    68
    return False