hgkw/kwexpander.py
author Christian Ebert <blacktrash@gmx.net>
Tue, 19 Dec 2006 15:57:03 +0100
branchextension
changeset 37 3dc31476c148
parent 35 a730c9d4ec05
permissions -rw-r--r--
Standalone version for keyword.py Check "expand" patterns in the [keyword] config section. Removed keyword in file itself to simplify patterns. Revert (again) to basename.
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
from mercurial.i18n import _
24
1083d250d1b2 Throw out demandimport -- at least temporarily
Christian Ebert <blacktrash@gmx.net>
parents: 23
diff changeset
     2
from mercurial import util
35
a730c9d4ec05 Interim test version in connection with keyword.py
Christian Ebert <blacktrash@gmx.net>
parents: 32
diff changeset
     3
import os.path, re
5
85d1f5bf7cfc updatehook branch: add kwexpander and updatekw modules
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
     4
29
bda83c13da42 Clean up variable names; do not explicitly return False
Christian Ebert <blacktrash@gmx.net>
parents: 25
diff changeset
     5
def expandkw(ui, repo, rev, cid, candidates, update=False):
32
b70b38b15fa4 Update function descriptions
Christian Ebert <blacktrash@gmx.net>
parents: 31
diff changeset
     6
    '''Expands logged <Dollar>Hg<Dollar> in working directory.'''
5
85d1f5bf7cfc updatehook branch: add kwexpander and updatekw modules
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
     7
25
aa3fbe81e9ad Make wwritekw() and all vars local
Christian Ebert <blacktrash@gmx.net>
parents: 24
diff changeset
     8
    # name of keyword encode filter:
aa3fbe81e9ad Make wwritekw() and all vars local
Christian Ebert <blacktrash@gmx.net>
parents: 24
diff changeset
     9
    kwencodefilter = 'hgkwencode'
aa3fbe81e9ad Make wwritekw() and all vars local
Christian Ebert <blacktrash@gmx.net>
parents: 24
diff changeset
    10
29
bda83c13da42 Clean up variable names; do not explicitly return False
Christian Ebert <blacktrash@gmx.net>
parents: 25
diff changeset
    11
    # 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
    12
    # as hgkwencode always runs before
35
a730c9d4ec05 Interim test version in connection with keyword.py
Christian Ebert <blacktrash@gmx.net>
parents: 32
diff changeset
    13
    kwstr = '$Hg$'
29
bda83c13da42 Clean up variable names; do not explicitly return False
Christian Ebert <blacktrash@gmx.net>
parents: 25
diff changeset
    14
bda83c13da42 Clean up variable names; do not explicitly return False
Christian Ebert <blacktrash@gmx.net>
parents: 25
diff changeset
    15
    # pretxncommit looks for both encoded and decoded keywords
35
a730c9d4ec05 Interim test version in connection with keyword.py
Christian Ebert <blacktrash@gmx.net>
parents: 32
diff changeset
    16
    re_kw = re.compile(r'\$Hg[^$]*?\$')
25
aa3fbe81e9ad Make wwritekw() and all vars local
Christian Ebert <blacktrash@gmx.net>
parents: 24
diff changeset
    17
aa3fbe81e9ad Make wwritekw() and all vars local
Christian Ebert <blacktrash@gmx.net>
parents: 24
diff changeset
    18
    def wwritekw(ui, repo, f, text):
aa3fbe81e9ad Make wwritekw() and all vars local
Christian Ebert <blacktrash@gmx.net>
parents: 24
diff changeset
    19
        '''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
    20
        ui.note(_('expanding keywords in %s\n' % f))
30
bc3bdd329831 Test for binary
Christian Ebert <blacktrash@gmx.net>
parents: 29
diff changeset
    21
        # backup file, at least when commiting (?)
25
aa3fbe81e9ad Make wwritekw() and all vars local
Christian Ebert <blacktrash@gmx.net>
parents: 24
diff changeset
    22
        repo.wfile(f, 'w').write(text)
aa3fbe81e9ad Make wwritekw() and all vars local
Christian Ebert <blacktrash@gmx.net>
parents: 24
diff changeset
    23
21
536c1797202d Implement $Hg$ scheme with update hook
Christian Ebert <blacktrash@gmx.net>
parents: 5
diff changeset
    24
    # only check files that have hgkwencode assigned as encode filter
35
a730c9d4ec05 Interim test version in connection with keyword.py
Christian Ebert <blacktrash@gmx.net>
parents: 32
diff changeset
    25
#    files = []
23
9fdf507badfc Double check encode filtered filenames
Christian Ebert <blacktrash@gmx.net>
parents: 21
diff changeset
    26
    # python2.4: files = set()
35
a730c9d4ec05 Interim test version in connection with keyword.py
Christian Ebert <blacktrash@gmx.net>
parents: 32
diff changeset
    27
#    for pat, cmd in repo.ui.configitems('encode'):
a730c9d4ec05 Interim test version in connection with keyword.py
Christian Ebert <blacktrash@gmx.net>
parents: 32
diff changeset
    28
#        if cmd.endswith(kwencodefilter):
a730c9d4ec05 Interim test version in connection with keyword.py
Christian Ebert <blacktrash@gmx.net>
parents: 32
diff changeset
    29
#            mf = util.matcher(repo.root, '', [pat], [], [])[1]
a730c9d4ec05 Interim test version in connection with keyword.py
Christian Ebert <blacktrash@gmx.net>
parents: 32
diff changeset
    30
#            for candidate in candidates:
a730c9d4ec05 Interim test version in connection with keyword.py
Christian Ebert <blacktrash@gmx.net>
parents: 32
diff changeset
    31
#                if mf(candidate) and candidate not in files:
a730c9d4ec05 Interim test version in connection with keyword.py
Christian Ebert <blacktrash@gmx.net>
parents: 32
diff changeset
    32
#                    files.append(candidate)
23
9fdf507badfc Double check encode filtered filenames
Christian Ebert <blacktrash@gmx.net>
parents: 21
diff changeset
    33
                # python2.4:
9fdf507badfc Double check encode filtered filenames
Christian Ebert <blacktrash@gmx.net>
parents: 21
diff changeset
    34
                # if mf(candidate): files.add(candidate)
5
85d1f5bf7cfc updatehook branch: add kwexpander and updatekw modules
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
    35
35
a730c9d4ec05 Interim test version in connection with keyword.py
Christian Ebert <blacktrash@gmx.net>
parents: 32
diff changeset
    36
#    if not files: # nothing to do
a730c9d4ec05 Interim test version in connection with keyword.py
Christian Ebert <blacktrash@gmx.net>
parents: 32
diff changeset
    37
#        return False
a730c9d4ec05 Interim test version in connection with keyword.py
Christian Ebert <blacktrash@gmx.net>
parents: 32
diff changeset
    38
    files = candidates
5
85d1f5bf7cfc updatehook branch: add kwexpander and updatekw modules
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
    39
29
bda83c13da42 Clean up variable names; do not explicitly return False
Christian Ebert <blacktrash@gmx.net>
parents: 25
diff changeset
    40
    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
    41
    user = util.shortuser(user)
35
a730c9d4ec05 Interim test version in connection with keyword.py
Christian Ebert <blacktrash@gmx.net>
parents: 32
diff changeset
    42
    date = util.datestr(date=date, format=util.defaultdateformats[0])
a730c9d4ec05 Interim test version in connection with keyword.py
Christian Ebert <blacktrash@gmx.net>
parents: 32
diff changeset
    43
                                               # %Y-%m-%d %H:%M:%S
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()
30
bc3bdd329831 Test for binary
Christian Ebert <blacktrash@gmx.net>
parents: 29
diff changeset
    51
        if not util.binary(text):
bc3bdd329831 Test for binary
Christian Ebert <blacktrash@gmx.net>
parents: 29
diff changeset
    52
31
28e8bd971c02 Add TODO for walk back through file history on update
Christian Ebert <blacktrash@gmx.net>
parents: 30
diff changeset
    53
            # TODO for update:
28e8bd971c02 Add TODO for walk back through file history on update
Christian Ebert <blacktrash@gmx.net>
parents: 30
diff changeset
    54
            # walk back through file history onto last add/modify
28e8bd971c02 Add TODO for walk back through file history on update
Christian Ebert <blacktrash@gmx.net>
parents: 30
diff changeset
    55
            # like "hg log -l1 f"
35
a730c9d4ec05 Interim test version in connection with keyword.py
Christian Ebert <blacktrash@gmx.net>
parents: 32
diff changeset
    56
            kw = '$Hg: %s,v %s %s %s $' % (
a730c9d4ec05 Interim test version in connection with keyword.py
Christian Ebert <blacktrash@gmx.net>
parents: 32
diff changeset
    57
                    os.path.basename(f), cid, date, user)
5
85d1f5bf7cfc updatehook branch: add kwexpander and updatekw modules
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
    58
30
bc3bdd329831 Test for binary
Christian Ebert <blacktrash@gmx.net>
parents: 29
diff changeset
    59
            if update and text.find(kwstr) > -1:
bc3bdd329831 Test for binary
Christian Ebert <blacktrash@gmx.net>
parents: 29
diff changeset
    60
                text = text.replace(kwstr, kw)
21
536c1797202d Implement $Hg$ scheme with update hook
Christian Ebert <blacktrash@gmx.net>
parents: 5
diff changeset
    61
                wwritekw(ui, repo, f, text)
30
bc3bdd329831 Test for binary
Christian Ebert <blacktrash@gmx.net>
parents: 29
diff changeset
    62
                kwupdates.append(f)
bc3bdd329831 Test for binary
Christian Ebert <blacktrash@gmx.net>
parents: 29
diff changeset
    63
            
bc3bdd329831 Test for binary
Christian Ebert <blacktrash@gmx.net>
parents: 29
diff changeset
    64
            elif not update:
bc3bdd329831 Test for binary
Christian Ebert <blacktrash@gmx.net>
parents: 29
diff changeset
    65
                text, kwct = re_kw.subn(kw, text)
bc3bdd329831 Test for binary
Christian Ebert <blacktrash@gmx.net>
parents: 29
diff changeset
    66
                if kwct:
bc3bdd329831 Test for binary
Christian Ebert <blacktrash@gmx.net>
parents: 29
diff changeset
    67
                    wwritekw(ui, repo, f, text)
5
85d1f5bf7cfc updatehook branch: add kwexpander and updatekw modules
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
    68
21
536c1797202d Implement $Hg$ scheme with update hook
Christian Ebert <blacktrash@gmx.net>
parents: 5
diff changeset
    69
    if kwupdates:
536c1797202d Implement $Hg$ scheme with update hook
Christian Ebert <blacktrash@gmx.net>
parents: 5
diff changeset
    70
        # 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
    71
        repo.dirstate.update(kwupdates, 'n')