1 from mercurial.i18n import _ |
|
2 from mercurial import util |
|
3 import os.path, re |
|
4 |
|
5 def expandkw(ui, repo, rev, cid, candidates, update=False): |
|
6 '''Expands logged <Dollar>Hg<Dollar> in working directory.''' |
|
7 |
|
8 # name of keyword encode filter: |
|
9 kwencodefilter = 'hgkwencode' |
|
10 |
|
11 # update only needs string search for encoded keyword |
|
12 # as hgkwencode always runs before |
|
13 kwstr = '$Hg$' |
|
14 |
|
15 # pretxncommit looks for both encoded and decoded keywords |
|
16 re_kw = re.compile(r'\$Hg[^$]*?\$') |
|
17 |
|
18 def wwritekw(ui, repo, f, text): |
|
19 '''Writes text with kwupdates keywords to f in working directory.''' |
|
20 ui.note(_('expanding keywords in %s\n' % f)) |
|
21 # backup file, at least when commiting (?) |
|
22 repo.wfile(f, 'w').write(text) |
|
23 |
|
24 # only check files that have hgkwencode assigned as encode filter |
|
25 # files = [] |
|
26 # python2.4: files = set() |
|
27 # for pat, cmd in repo.ui.configitems('encode'): |
|
28 # if cmd.endswith(kwencodefilter): |
|
29 # mf = util.matcher(repo.root, '', [pat], [], [])[1] |
|
30 # for candidate in candidates: |
|
31 # if mf(candidate) and candidate not in files: |
|
32 # files.append(candidate) |
|
33 # python2.4: |
|
34 # if mf(candidate): files.add(candidate) |
|
35 |
|
36 # if not files: # nothing to do |
|
37 # return False |
|
38 files = candidates |
|
39 |
|
40 user, date = repo.changelog.read(rev)[1:3] |
|
41 user = util.shortuser(user) |
|
42 date = util.datestr(date=date, format=util.defaultdateformats[0]) |
|
43 # %Y-%m-%d %H:%M:%S |
|
44 |
|
45 # collect filenames that were changed by hg update |
|
46 kwupdates = [] |
|
47 |
|
48 for f in files: |
|
49 |
|
50 text = repo.wfile(f).read() |
|
51 if not util.binary(text): |
|
52 |
|
53 # TODO for update: |
|
54 # walk back through file history onto last add/modify |
|
55 # like "hg log -l1 f" |
|
56 kw = '$Hg: %s,v %s %s %s $' % ( |
|
57 os.path.basename(f), cid, date, user) |
|
58 |
|
59 if update and text.find(kwstr) > -1: |
|
60 text = text.replace(kwstr, kw) |
|
61 wwritekw(ui, repo, f, text) |
|
62 kwupdates.append(f) |
|
63 |
|
64 elif not update: |
|
65 text, kwct = re_kw.subn(kw, text) |
|
66 if kwct: |
|
67 wwritekw(ui, repo, f, text) |
|
68 |
|
69 if kwupdates: |
|
70 # cheat hg to believe that updated files were not modified |
|
71 repo.dirstate.update(kwupdates, 'n') |
|