|
1 from hgkw import kwutil |
1 from mercurial.i18n import _ |
2 from mercurial.i18n import _ |
2 from mercurial import cmdutil, commands, util |
3 from mercurial import cmdutil, commands, util |
3 import os.path, re, sys |
4 import re, sys |
4 |
|
5 hgkeywords = 'Id|Header|Author|Date|Revision|RCSFile|Source' |
|
6 |
5 |
7 def pretxnkw(ui, repo, hooktype, **args): |
6 def pretxnkw(ui, repo, hooktype, **args): |
8 '''Collects candidates for keyword expansion on commit |
7 '''Collects candidates for keyword expansion on commit |
9 and expands keywords in working dir. |
8 and expands keywords in working dir. |
10 NOTE: for use in combination with hgext.keyword!''' |
9 NOTE: for use in combination with hgext.keyword!''' |
41 |
40 |
42 if not files: # nothing to do |
41 if not files: # nothing to do |
43 return False |
42 return False |
44 |
43 |
45 user, date = repo.changelog.read(repo.changelog.tip())[1:3] |
44 user, date = repo.changelog.read(repo.changelog.tip())[1:3] |
46 strdate = util.datestr(date=date) |
45 re_kw = re.compile(r'\$(%s)(: [^$]+? )?\$' % kwutil.hgkeywords) |
47 shortuser = util.shortuser(user) |
|
48 shortdate = util.datestr(date=date, format=util.defaultdateformats[0]) |
|
49 # %Y-%m-%d %H:%M:%S |
|
50 |
|
51 re_kw = re.compile(r'\$(%s)(: [^$]+? )?\$' % hgkeywords) |
|
52 |
46 |
53 for f in files: |
47 for f in files: |
54 data = repo.wfile(f).read() |
48 data = repo.wfile(f).read() |
55 if not util.binary(data): |
49 if not util.binary(data): |
56 |
50 |
57 def kwexpand(matchobj): |
51 def kwexpand(matchobj): |
58 RCSFile = os.path.basename(f)+',v' |
52 return kwutil.kwexpand(matchobj, |
59 Source = os.path.join(repo.root, f)+',v' |
53 repo, args['node'][:12], f, date, user) |
60 Revision = args['node'][:12] |
|
61 Date = strdate |
|
62 Author = user |
|
63 revdateauth = '%s %s %s' % (Revision, shortdate, shortuser) |
|
64 Header = '%s %s' % (Source, revdateauth) |
|
65 Id = '%s %s' % (RCSFile, revdateauth) |
|
66 return '$%s: %s $' % ( |
|
67 matchobj.group(1), eval(matchobj.group(1))) |
|
68 |
54 |
69 data, kwct = re_kw.subn(kwexpand, data) |
55 data, kwct = re_kw.subn(kwexpand, data) |
70 if kwct: |
56 if kwct: |
71 ui.note(_('expanding keywords in %s\n' % f)) |
57 ui.note(_('expanding keywords in %s\n' % f)) |
72 # backup file? |
58 # backup file? |