hgkw/kwexpander.py
author Christian Ebert <blacktrash@gmx.net>
Sun, 17 Dec 2006 15:32:34 +0100
branchupdatehook
changeset 29 bda83c13da42
parent 25 aa3fbe81e9ad
child 30 bc3bdd329831
permissions -rw-r--r--
Clean up variable names; do not explicitly return False
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.i18n import _
24
1083d250d1b2 Throw out demandimport -- at least temporarily
Christian Ebert <blacktrash@gmx.net>
parents: 23
diff changeset
     4
from mercurial import util
1083d250d1b2 Throw out demandimport -- at least temporarily
Christian Ebert <blacktrash@gmx.net>
parents: 23
diff changeset
     5
import re
5
85d1f5bf7cfc updatehook branch: add kwexpander and updatekw modules
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
     6
29
bda83c13da42 Clean up variable names; do not explicitly return False
Christian Ebert <blacktrash@gmx.net>
parents: 25
diff changeset
     7
def expandkw(ui, repo, rev, cid, candidates, update=False):
5
85d1f5bf7cfc updatehook branch: add kwexpander and updatekw modules
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
     8
    '''Important: returns False on success, True on failure.'''
85d1f5bf7cfc updatehook branch: add kwexpander and updatekw modules
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
     9
25
aa3fbe81e9ad Make wwritekw() and all vars local
Christian Ebert <blacktrash@gmx.net>
parents: 24
diff changeset
    10
    # name of keyword encode filter:
aa3fbe81e9ad Make wwritekw() and all vars local
Christian Ebert <blacktrash@gmx.net>
parents: 24
diff changeset
    11
    kwencodefilter = 'hgkwencode'
aa3fbe81e9ad Make wwritekw() and all vars local
Christian Ebert <blacktrash@gmx.net>
parents: 24
diff changeset
    12
29
bda83c13da42 Clean up variable names; do not explicitly return False
Christian Ebert <blacktrash@gmx.net>
parents: 25
diff changeset
    13
    # update only needs string search for encoded keyword
bda83c13da42 Clean up variable names; do not explicitly return False
Christian Ebert <blacktrash@gmx.net>
parents: 25
diff changeset
    14
    # as hgkwencode always runs before
bda83c13da42 Clean up variable names; do not explicitly return False
Christian Ebert <blacktrash@gmx.net>
parents: 25
diff changeset
    15
    kwstr = '%sHg$' % '$'
bda83c13da42 Clean up variable names; do not explicitly return False
Christian Ebert <blacktrash@gmx.net>
parents: 25
diff changeset
    16
bda83c13da42 Clean up variable names; do not explicitly return False
Christian Ebert <blacktrash@gmx.net>
parents: 25
diff changeset
    17
    # pretxncommit looks for both encoded and decoded keywords
bda83c13da42 Clean up variable names; do not explicitly return False
Christian Ebert <blacktrash@gmx.net>
parents: 25
diff changeset
    18
    kwpat = r'[$]Hg(: %s,v [a-z0-9]{12} [^$]+? )?\$'
25
aa3fbe81e9ad Make wwritekw() and all vars local
Christian Ebert <blacktrash@gmx.net>
parents: 24
diff changeset
    19
aa3fbe81e9ad Make wwritekw() and all vars local
Christian Ebert <blacktrash@gmx.net>
parents: 24
diff changeset
    20
    def wwritekw(ui, repo, f, text):
aa3fbe81e9ad Make wwritekw() and all vars local
Christian Ebert <blacktrash@gmx.net>
parents: 24
diff changeset
    21
        '''Writes text with kwupdates keywords to f in working directory.'''
aa3fbe81e9ad Make wwritekw() and all vars local
Christian Ebert <blacktrash@gmx.net>
parents: 24
diff changeset
    22
        ui.note(_('expanding keywords in %s\n' % f))
29
bda83c13da42 Clean up variable names; do not explicitly return False
Christian Ebert <blacktrash@gmx.net>
parents: 25
diff changeset
    23
    #   # backup file in case of commit (?)
25
aa3fbe81e9ad Make wwritekw() and all vars local
Christian Ebert <blacktrash@gmx.net>
parents: 24
diff changeset
    24
    #   absfile = repo.wjoin(f)
aa3fbe81e9ad Make wwritekw() and all vars local
Christian Ebert <blacktrash@gmx.net>
parents: 24
diff changeset
    25
    #   util.copyfile(absfile, absfile+'.kwbak')
aa3fbe81e9ad Make wwritekw() and all vars local
Christian Ebert <blacktrash@gmx.net>
parents: 24
diff changeset
    26
        repo.wfile(f, 'w').write(text)
aa3fbe81e9ad Make wwritekw() and all vars local
Christian Ebert <blacktrash@gmx.net>
parents: 24
diff changeset
    27
21
536c1797202d Implement $Hg$ scheme with update hook
Christian Ebert <blacktrash@gmx.net>
parents: 5
diff changeset
    28
    # 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
    29
    files = []
23
9fdf507badfc Double check encode filtered filenames
Christian Ebert <blacktrash@gmx.net>
parents: 21
diff changeset
    30
    # python2.4: files = set()
5
85d1f5bf7cfc updatehook branch: add kwexpander and updatekw modules
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
    31
    for pat, cmd in repo.ui.configitems('encode'):
85d1f5bf7cfc updatehook branch: add kwexpander and updatekw modules
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
    32
        if cmd.endswith(kwencodefilter):
24
1083d250d1b2 Throw out demandimport -- at least temporarily
Christian Ebert <blacktrash@gmx.net>
parents: 23
diff changeset
    33
            mf = util.matcher(repo.root, '', [pat], [], [])[1]
5
85d1f5bf7cfc updatehook branch: add kwexpander and updatekw modules
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
    34
            for candidate in candidates:
23
9fdf507badfc Double check encode filtered filenames
Christian Ebert <blacktrash@gmx.net>
parents: 21
diff changeset
    35
                if mf(candidate) and candidate not in files:
5
85d1f5bf7cfc updatehook branch: add kwexpander and updatekw modules
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
    36
                    files.append(candidate)
23
9fdf507badfc Double check encode filtered filenames
Christian Ebert <blacktrash@gmx.net>
parents: 21
diff changeset
    37
                # python2.4:
9fdf507badfc Double check encode filtered filenames
Christian Ebert <blacktrash@gmx.net>
parents: 21
diff changeset
    38
                # if mf(candidate): files.add(candidate)
5
85d1f5bf7cfc updatehook branch: add kwexpander and updatekw modules
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
    39
85d1f5bf7cfc updatehook branch: add kwexpander and updatekw modules
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
    40
    if not files: # nothing to do
85d1f5bf7cfc updatehook branch: add kwexpander and updatekw modules
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
    41
        return False
85d1f5bf7cfc updatehook branch: add kwexpander and updatekw modules
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
    42
29
bda83c13da42 Clean up variable names; do not explicitly return False
Christian Ebert <blacktrash@gmx.net>
parents: 25
diff changeset
    43
    user, date = repo.changelog.read(rev)[1:3]
24
1083d250d1b2 Throw out demandimport -- at least temporarily
Christian Ebert <blacktrash@gmx.net>
parents: 23
diff changeset
    44
    user = util.shortuser(user)
1083d250d1b2 Throw out demandimport -- at least temporarily
Christian Ebert <blacktrash@gmx.net>
parents: 23
diff changeset
    45
    date = util.datestr(date=date, format=util.defaultdateformats[2])
1083d250d1b2 Throw out demandimport -- at least temporarily
Christian Ebert <blacktrash@gmx.net>
parents: 23
diff changeset
    46
                                               # %Y-%m-%d %H:%M
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
    # collect filenames that were changed by hg update
536c1797202d Implement $Hg$ scheme with update hook
Christian Ebert <blacktrash@gmx.net>
parents: 5
diff changeset
    49
    kwupdates = []
5
85d1f5bf7cfc updatehook branch: add kwexpander and updatekw modules
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
    50
21
536c1797202d Implement $Hg$ scheme with update hook
Christian Ebert <blacktrash@gmx.net>
parents: 5
diff changeset
    51
    for f in files:
536c1797202d Implement $Hg$ scheme with update hook
Christian Ebert <blacktrash@gmx.net>
parents: 5
diff changeset
    52
536c1797202d Implement $Hg$ scheme with update hook
Christian Ebert <blacktrash@gmx.net>
parents: 5
diff changeset
    53
        text = repo.wfile(f).read()
29
bda83c13da42 Clean up variable names; do not explicitly return False
Christian Ebert <blacktrash@gmx.net>
parents: 25
diff changeset
    54
        kw = '%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
    55
29
bda83c13da42 Clean up variable names; do not explicitly return False
Christian Ebert <blacktrash@gmx.net>
parents: 25
diff changeset
    56
        if update and text.find(kwstr) > -1:
bda83c13da42 Clean up variable names; do not explicitly return False
Christian Ebert <blacktrash@gmx.net>
parents: 25
diff changeset
    57
            text = text.replace(kwstr, kw)
21
536c1797202d Implement $Hg$ scheme with update hook
Christian Ebert <blacktrash@gmx.net>
parents: 5
diff changeset
    58
            wwritekw(ui, repo, f, text)
536c1797202d Implement $Hg$ scheme with update hook
Christian Ebert <blacktrash@gmx.net>
parents: 5
diff changeset
    59
            kwupdates.append(f)
536c1797202d Implement $Hg$ scheme with update hook
Christian Ebert <blacktrash@gmx.net>
parents: 5
diff changeset
    60
        
536c1797202d Implement $Hg$ scheme with update hook
Christian Ebert <blacktrash@gmx.net>
parents: 5
diff changeset
    61
        elif not update:
29
bda83c13da42 Clean up variable names; do not explicitly return False
Christian Ebert <blacktrash@gmx.net>
parents: 25
diff changeset
    62
            re_kw = re.compile(kwpat % f)
bda83c13da42 Clean up variable names; do not explicitly return False
Christian Ebert <blacktrash@gmx.net>
parents: 25
diff changeset
    63
            text, kwct = re_kw.subn(kw, text)
21
536c1797202d Implement $Hg$ scheme with update hook
Christian Ebert <blacktrash@gmx.net>
parents: 5
diff changeset
    64
            if kwct:
536c1797202d Implement $Hg$ scheme with update hook
Christian Ebert <blacktrash@gmx.net>
parents: 5
diff changeset
    65
                wwritekw(ui, repo, f, text)
5
85d1f5bf7cfc updatehook branch: add kwexpander and updatekw modules
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
    66
21
536c1797202d Implement $Hg$ scheme with update hook
Christian Ebert <blacktrash@gmx.net>
parents: 5
diff changeset
    67
    if kwupdates:
536c1797202d Implement $Hg$ scheme with update hook
Christian Ebert <blacktrash@gmx.net>
parents: 5
diff changeset
    68
        # 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
    69
        repo.dirstate.update(kwupdates, 'n')