Switch to complete filename in first keyword field
This will hopefully allow to iterate over the stream in hgkwdecode,
and lookup the relatively latest revision of corresponding file.
# $Hg: hgkw/pretxnkw.py,v$
from mercurial.i18n import gettext as _
from mercurial.demandload import demandload
demandload(globals(), 'hgkw:kwutil mercurial:util re')
kwencodefilter = 'hgkwencode'
def pretxnkw(ui=None, repo=None, hooktype='', **args):
'''Important: returns False on success, True on failure.'''
node = args['node'][0:12]
if not ui or not repo or not node or hooktype != 'pretxncommit':
# bail out with error
return True
modified, added = repo.status()[:2]
candidates = modified + added
files = []
for pat, cmd in repo.ui.configitems('encode'):
if cmd.endswith(kwencodefilter):
mf = util.matcher(repo.root, '', [pat], [], [])[1]
for candidate in candidates:
if mf(candidate):
files.append(candidate)
if not files: # nothing to do
return False
re_kw = kwutil.rekw()
kword = kwutil.mkkw(repo, tip=True, node=node)
re_kwcheck = re.compile(r'[$]Hg: (.*?),v.*?\$')
for filename in files:
data = repo.wfile(filename).read()
# check for keywords with incorrect filename
# eg. if you forgot to update filename manually after "hg mv"
invalids = [m for m in map(str, re_kwcheck.findall(data))
if m != filename]
if invalids:
invalids = ['%sHg: %s,v$' % ('$', i) for i in invalids]
ui.warn(_('%d invalid keyword filenames in file %s:\n'
'%s\nplease correct to %sHg: %s,v$\n'
% (len(invalids), filename, ', '.join(invalids), '$', filename)
))
return True
# substitute <Dollar>(Hg|Id): <basename>,v.*<Dollar>
data, kwct = re_kw.subn(kword, data)
if kwct:
# backup file and write with expanded keyword
ui.note(_('expanding keywords in %s\n' % filename))
absname = repo.wjoin(filename)
util.copyfile(absname, absname+'~')
repo.wfile(filename, 'w').write(data)
return False