author | Christian Ebert <blacktrash@gmx.net> |
Thu, 14 Dec 2006 10:18:41 +0100 | |
branch | decodefilter |
changeset 6 | 3ee39807daa5 |
child 8 | 180d8484503a |
permissions | -rw-r--r-- |
6
3ee39807daa5
Add pretxnkw module for commits
Christian Ebert <blacktrash@gmx.net>
parents:
diff
changeset
|
1 |
# $Hg: pretxnkw.py,v$ |
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 |
3ee39807daa5
Add pretxnkw module for commits
Christian Ebert <blacktrash@gmx.net>
parents:
diff
changeset
|
5 |
demandload(globals(), 'hgkw:kwutil mercurial:util os.path re') |
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 |
|
3ee39807daa5
Add pretxnkw module for commits
Christian Ebert <blacktrash@gmx.net>
parents:
diff
changeset
|
18 |
modified, added = repo.status()[1:3] |
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 |
|
3ee39807daa5
Add pretxnkw module for commits
Christian Ebert <blacktrash@gmx.net>
parents:
diff
changeset
|
39 |
data = repo.wopener(filename, 'rb').read() |
3ee39807daa5
Add pretxnkw module for commits
Christian Ebert <blacktrash@gmx.net>
parents:
diff
changeset
|
40 |
bn = os.path.basename(filename) |
3ee39807daa5
Add pretxnkw module for commits
Christian Ebert <blacktrash@gmx.net>
parents:
diff
changeset
|
41 |
|
3ee39807daa5
Add pretxnkw module for commits
Christian Ebert <blacktrash@gmx.net>
parents:
diff
changeset
|
42 |
# check for keywords with incorrect basename |
3ee39807daa5
Add pretxnkw module for commits
Christian Ebert <blacktrash@gmx.net>
parents:
diff
changeset
|
43 |
# eg. if you forgot to update basename manually after "hg mv" |
3ee39807daa5
Add pretxnkw module for commits
Christian Ebert <blacktrash@gmx.net>
parents:
diff
changeset
|
44 |
failures = [m for m in map(str, re_kwcheck.findall(data)) if m != bn] |
3ee39807daa5
Add pretxnkw module for commits
Christian Ebert <blacktrash@gmx.net>
parents:
diff
changeset
|
45 |
if failures: |
3ee39807daa5
Add pretxnkw module for commits
Christian Ebert <blacktrash@gmx.net>
parents:
diff
changeset
|
46 |
failures = ['%sHg: %s,v$' % ('$', nobn) for nobn in failures] |
3ee39807daa5
Add pretxnkw module for commits
Christian Ebert <blacktrash@gmx.net>
parents:
diff
changeset
|
47 |
ui.warn(_('%d incorrect basenames in file %s:\n' |
3ee39807daa5
Add pretxnkw module for commits
Christian Ebert <blacktrash@gmx.net>
parents:
diff
changeset
|
48 |
'%s\nplease correct to %sHg: %s,v$\n' |
3ee39807daa5
Add pretxnkw module for commits
Christian Ebert <blacktrash@gmx.net>
parents:
diff
changeset
|
49 |
% (len(failures), filename, ', '.join(failures), '$', bn))) |
3ee39807daa5
Add pretxnkw module for commits
Christian Ebert <blacktrash@gmx.net>
parents:
diff
changeset
|
50 |
return True |
3ee39807daa5
Add pretxnkw module for commits
Christian Ebert <blacktrash@gmx.net>
parents:
diff
changeset
|
51 |
|
3ee39807daa5
Add pretxnkw module for commits
Christian Ebert <blacktrash@gmx.net>
parents:
diff
changeset
|
52 |
# substitute <Dollar>(Hg|Id): <basename>,v.*<Dollar> |
3ee39807daa5
Add pretxnkw module for commits
Christian Ebert <blacktrash@gmx.net>
parents:
diff
changeset
|
53 |
data, kwct = re_kw.subn(kword, data) |
3ee39807daa5
Add pretxnkw module for commits
Christian Ebert <blacktrash@gmx.net>
parents:
diff
changeset
|
54 |
|
3ee39807daa5
Add pretxnkw module for commits
Christian Ebert <blacktrash@gmx.net>
parents:
diff
changeset
|
55 |
if kwct: |
3ee39807daa5
Add pretxnkw module for commits
Christian Ebert <blacktrash@gmx.net>
parents:
diff
changeset
|
56 |
# backup file and write with expanded keyword |
3ee39807daa5
Add pretxnkw module for commits
Christian Ebert <blacktrash@gmx.net>
parents:
diff
changeset
|
57 |
ui.note(_('expanding keywords in %s\n' % filename)) |
3ee39807daa5
Add pretxnkw module for commits
Christian Ebert <blacktrash@gmx.net>
parents:
diff
changeset
|
58 |
util.copyfile(filename, filename+'~') |
3ee39807daa5
Add pretxnkw module for commits
Christian Ebert <blacktrash@gmx.net>
parents:
diff
changeset
|
59 |
repo.wopener(filename, 'wb').write(data) |
3ee39807daa5
Add pretxnkw module for commits
Christian Ebert <blacktrash@gmx.net>
parents:
diff
changeset
|
60 |
|
3ee39807daa5
Add pretxnkw module for commits
Christian Ebert <blacktrash@gmx.net>
parents:
diff
changeset
|
61 |
return False |