frommercurial.i18nimport_frommercurialimportcmdutil,commands,utilimportos.path,re,syshgkeywords='Id|Header|Author|Date|Revision|RCSFile|Source'defpretxnkw(ui,repo,hooktype,**args):'''Collects candidates for keyword expansion on commit and expands keywords in working dir. NOTE: for use in combination with hgext.keyword!'''ifhooktype!='pretxncommit':# bail out with errorreturnTrue# reparse args, opts again as pretxncommit hook is silent about themcmd,sysargs,globalopts,cmdopts=commands.parse(ui,sys.argv[1:])[1:]# exclude tag and importifrepr(cmd).split()[1]in('tag','import_'):returnFalsefiles,match,anypats=cmdutil.matchpats(repo,sysargs,cmdopts)# validity checks should have been done alreadymodified,added=repo.status(files=files,match=match)[:2]candidates=[fforfinmodified+addedifnotf.startswith('.hg')]ifnotcandidates:returnFalse# only check files that have hgkwencode assigned as encode filterfiles=[]# python2.4: files = set()forpat,optinrepo.ui.configitems('keyword'):ifopt=='expand':mf=util.matcher(repo.root,'',[pat],[],[])[1]forcandidateincandidates:ifmf(candidate)andcandidatenotinfiles:files.append(candidate)# python2.4:# if mf(candidate): files.add(candidate)ifnotfiles:# nothing to doreturnFalseuser,date=repo.changelog.read(repo.changelog.tip())[1:3]strdate=util.datestr(date=date)shortuser=util.shortuser(user)shortdate=util.datestr(date=date,format=util.defaultdateformats[0])# %Y-%m-%d %H:%M:%Sre_kw=re.compile(r'\$(%s)(: [^$]+? )?\$'%hgkeywords)forfinfiles:data=repo.wfile(f).read()ifnotutil.binary(data):defkwexpand(matchobj):RCSFile=os.path.basename(f)+',v'Source=os.path.join(repo.root,f)+',v'Revision=args['node'][:12]Date=strdateAuthor=userrevdateauth='%s%s%s'%(Revision,shortdate,shortuser)Header='%s%s'%(Source,revdateauth)Id='%s%s'%(RCSFile,revdateauth)return'$%s: %s $'%(matchobj.group(1),eval(matchobj.group(1)))data,kwct=re_kw.subn(kwexpand,data)ifkwct:ui.note(_('expanding keywords in %s\n'%f))# backup file?repo.wfile(f,'w').write(data)