Specific regular expressions depending on read mode
More safeguarding against accidental (un)expansion:
Reading filelog: act only on \$(kw1|kw2|..)\$ as keywords are always
stored unexpanded.
Reading wdir: act only on \$(kw1|kw2|..): [^$\n\r]*? \$ as we only
are interested in expanded keywords in this situation.
Note: we cannot use ..): [^$\n\r]+? \$ because e.g.
the {branch} template might be empty.
hg record is a special case as we read from the working directory and
need one regex each for modified and added files. Therefore test
recording an added file.
This way we finally also forbid sequences like $Id: $ being treated
as keywords.
$cat<<EOF>>$HGRCPATH>[extensions]>keyword=>mq=>notify=>record=>transplant=>[ui]>interactive=true>EOFRunkwdemobefore[keyword]filesaresetupasitwouldsucceedwithoutuisetupotherwise$hg--quietkwdemo[extensions]keyword=[keyword]demo.txt=[keywordmaps]Author={author|user}Date={date|utcdate}Header={root}/{file},v{node|short}{date|utcdate}{author|user}Id={file|basename},v{node|short}{date|utcdate}{author|user}RCSFile={file|basename},vRCSfile={file|basename},vRevision={node|short}Source={root}/{file},v$Author:test$$Date:????/??/????:??:??$(glob)$Header:*/demo.txt,v????????????????/??/????:??:??test$(glob)$Id:demo.txt,v????????????????/??/????:??:??test$(glob)$RCSFile:demo.txt,v$$RCSfile:demo.txt,v$$Revision:????????????$(glob)$Source:*/demo.txt,v$(glob)$hg--quietkwdemo"Branch = {branches}"[extensions]keyword=[keyword]demo.txt=[keywordmaps]Branch={branches}$Branch:demobranch$$cat<<EOF>>$HGRCPATH>[keyword]>**=>b=ignore>i=ignore>[hooks]>EOF$cp$HGRCPATH$HGRCPATH.nohooks>cat<<EOF>>$HGRCPATH>commit=>commit.test=cpahooktest>EOF$hginitTest-bndl$cdTest-bndlkwshrinkshouldexitsilentlyinempty/invalidrepo$hgkwshrinkSymlinkscannotbecreatedonWindows.Abundletotestthiswasmadewith:hginittcdtechoa>aln-sasymhgaddsymhgci-maddsym-umercurialhgbundle--basenull../test-keyword.hg$hgpull-u"$TESTDIR"/test-keyword.hgpullingfrom*test-keyword.hg(glob)requestingallchangesaddingchangesetsaddingmanifestsaddingfilechangesadded1changesetswith1changesto1files1filesupdated,0filesmerged,0filesremoved,0filesunresolved$echo'expand $Id$'>a$echo'do not process $Id:'>>a$echo'xxx $'>>a$echo'ignore $Id$'>bOutputfilesastheywerecreated$catabexpand$Id$donotprocess$Id:xxx$ignore$Id$nokwfiles$hgkwfilesuntrackedcandidates$hg-vkwfiles--unknownkaAddfilesandcheckstatus$hgaddremoveaddingaaddingb$hgstatusAaAbDefaultkeywordexpansionincludingcommithookInterruptedcommitshouldnotchangestateorruncommithook$hg--debugcommitabort:emptycommitmessage[255]$hgstatusAaAbCommitwithseveralchecks$hg--debugcommit-mabsym-u'User Name <user@example.com>'aboverwritingaexpandingkeywordsrunninghookcommit.test:cpahooktestcommittedchangeset1:ef63ca68695bc9495032c6fda1350c71e6d256e9$hgstatus?hooktest$hgdebugrebuildstate$hg--quietidentifyef63ca68695bcatfilesinworkingdirectorywithkeywordsexpanded$catabexpand$Id:a,vef63ca68695b1970/01/0100:00:00user$donotprocess$Id:xxx$ignore$Id$hgcatfilesandsymlink,noexpansion$hgcatsymab&&echoexpand$Id:a,vef63ca68695b1970/01/0100:00:00user$donotprocess$Id:xxx$ignore$Id$a*(glob)Testhookexecution$diffahooktest$cp$HGRCPATH.nohooks$HGRCPATH$rmhooktestbundle$hgbundle--basenull../kw.hg2changesetsfound$cd..$hginitTest$cdTestNotifyonpulltocheckwhetherkeywordsstayasisinemailie.ifpatch.diffwrapperactsasitshould$cat<<EOF>>$HGRCPATH>[hooks]>incoming.notify=python:hgext.notify.hook>[notify]>sources=pull>diffstat=False>[reposubs]>*=Test>EOFPullfrombundleandtriggernotify$hgpull-u../kw.hgpullingfrom../kw.hgrequestingallchangesaddingchangesetsaddingmanifestsaddingfilechangesadded2changesetswith3changesto3filesContent-Type:text/plain;charset="us-ascii"MIME-Version:1.0Content-Transfer-Encoding:7bitDate:*(glob)Subject:changesetin*(glob)From:mercurialX-Hg-Notification:changeseta2392c293916Message-Id:<hg.a2392c293916*>(glob)To:Testchangeseta2392c293916in*(glob)details:*cmd=changeset;node=a2392c293916(glob)description:addsymdiffs(6lines):diff-r000000000000-ra2392c293916sym---/dev/nullThuJan0100:00:001970+0000+++b/symSatFeb0920:25:472008+0100@@-0,0+1,1@@+a \ NonewlineatendoffileContent-Type:text/plain;charset="us-ascii"MIME-Version:1.0Content-Transfer-Encoding:7bitDate:*(glob)Subject:changesetin*(glob)From:UserName<user@example.com>X-Hg-Notification:changesetef63ca68695bMessage-Id:<hg.ef63ca68695b*>(glob)To:Testchangesetef63ca68695bin*(glob)details:*cmd=changeset;node=ef63ca68695b(glob)description:absymdiffs(12lines):diff-ra2392c293916-ref63ca68695ba---/dev/nullThuJan0100:00:001970+0000+++b/aThuJan0100:00:001970+0000@@-0,0+1,3@@+expand$Id$+donotprocess$Id:+xxx$diff-ra2392c293916-ref63ca68695bb---/dev/nullThuJan0100:00:001970+0000+++b/bThuJan0100:00:001970+0000@@-0,0+1,1@@+ignore$Id$3filesupdated,0filesmerged,0filesremoved,0filesunresolved$cp$HGRCPATH.nohooks$HGRCPATHTouchfilesandcheckwithstatus$touchab$hgstatusUpdateandexpand$rmsymab$hgupdate-C3filesupdated,0filesmerged,0filesremoved,0filesunresolved$catabexpand$Id:a,vef63ca68695b1970/01/0100:00:00user$donotprocess$Id:xxx$ignore$Id$Checkwhetherexpansionisfilewise$echo'$Id$'>c$echo'tests for different changenodes'>>ccommitfilec$hgcommit-A-mcndiff-d'1 0'-u'User Name <user@example.com>'addingcforceexpansion$hg-vkwexpandoverwritingaexpandingkeywordsoverwritingcexpandingkeywordscomparechangenodesinaandc$catacexpand$Id:a,vef63ca68695b1970/01/0100:00:00user$donotprocess$Id:xxx$$Id:c,v40a904bbbe4c1970/01/0100:00:01user$testsfordifferentchangenodesrecordchunk$python-c \>'l=open("a").readlines();l.insert(1,"foo\n");l.append("bar\n");open("a","w").writelines(l);'$hgrecord-d'1 10'-mrectest<<EOF>y>y>n>EOFdiff--gita/ab/a2hunks,2lineschangedexaminechangesto'a'?[Ynsfdaq?]@@-1,3+1,4@@expand$Id$+foodonotprocess$Id:xxx$recordchange1/2to'a'?[Ynsfdaq?]@@-2,2+3,3@@donotprocess$Id:xxx$+barrecordchange2/2to'a'?[Ynsfdaq?]$hgidentifyd17e03c92c97+tip$hgstatusMaCatmodifiedfilea$cataexpand$Id:a,vd17e03c92c971970/01/0100:00:01test$foodonotprocess$Id:xxx$barDiffremainingchunk$hgdiffdiff-rd17e03c92c97a---a/aWedDec3123:59:511969-0000+++b/a*(glob)@@-2,3+2,4@@foodonotprocess$Id:xxx$+bar$hgrollbackrollingbacktorevision2(undocommit)Recordallchunksinfilea$echofoo>msg-donotuse"hg record -m"here!$hgrecord-lmsg-d'1 11'<<EOF>y>y>y>EOFdiff--gita/ab/a2hunks,2lineschangedexaminechangesto'a'?[Ynsfdaq?]@@-1,3+1,4@@expand$Id$+foodonotprocess$Id:xxx$recordchange1/2to'a'?[Ynsfdaq?]@@-2,2+3,3@@donotprocess$Id:xxx$+barrecordchange2/2to'a'?[Ynsfdaq?]Fileashouldbeclean$hgstatus-AaCarollbackandrevertexpansion$cataexpand$Id:a,v59f969a3b52c1970/01/0100:00:01test$foodonotprocess$Id:xxx$bar$hg--verboserollbackrollingbacktorevision2(undocommit)overwritingaexpandingkeywords$hgstatusaMa$cataexpand$Id:a,vef63ca68695b1970/01/0100:00:00user$foodonotprocess$Id:xxx$bar$echo'$Id$'>y$echo'$Id$'>z$hgaddy$hgcommit-Am"rollback only"z$catz$Id:z,v45a5d3adce531970/01/0100:00:00test$$hg--verboserollbackrollingbacktorevision2(undocommit)overwritingzshrinkingkeywordsOnlyzshouldbeoverwritten$hgstatusayzMaAyAz$catz$Id$$hgforgetyz$rmyz$hgupdate-C1filesupdated,0filesmerged,0filesremoved,0filesunresolvedrecordaddedfile$echo'$Id$'>r$hgaddr$hg-vrecord-lmsg-d'1 12'r<<EOF>y>EOFdiff--gita/rb/rnewfilemode100644examinechangesto'r'?[Ynsfdaq?]rcommittedchangeset3:899491280810overwritingrexpandingkeywords$hg--verboserollbackrollingbacktorevision2(undocommit)overwritingrshrinkingkeywords$hgforgetr$rmmsgrTestpatchqueuerepo$hginit--mq$hgqimport-rtip-nmqtest.diff$hgcommit--mq-mmqtestKeywordsshouldnotbeexpandedinpatch$cat.hg/patches/mqtest.diff # HG changeset patch # User User Name <user@example.com> # Date 1 0 # Node ID 40a904bbbe4cd4ab0a1f28411e35db26341a40ad # Parent ef63ca68695bc9495032c6fda1350c71e6d256e9cndiffdiff-ref63ca68695b-r40a904bbbe4cc---/dev/nullThuJan0100:00:001970+0000+++b/cThuJan0100:00:011970+0000@@-0,0+1,2@@+$Id$+testsfordifferentchangenodes$hgqpoppoppingmqtest.diffpatchqueuenowemptyqgoto,implyingqpush,shouldexpand$hgqgotomqtest.diffapplyingmqtest.diffnowat:mqtest.diff$catc$Id:c,v40a904bbbe4c1970/01/0100:00:01user$testsfordifferentchangenodes$hgcatc$Id:c,v40a904bbbe4c1970/01/0100:00:01user$testsfordifferentchangenodesKeywordsshouldnotbeexpandedinfilelog$hg--config'extensions.keyword=!'catc$Id$testsfordifferentchangenodesqpopandmoveon$hgqpoppoppingmqtest.diffpatchqueuenowemptyCopyandshowaddedkwfiles$hgcpac$hgkwfilesacCommitandshowexpansioninoriginalandcopy$hg--debugcommit-ma2c-d'1 0'-u'User Name <user@example.com>'cc:copya:0045e12f6c5791aac80ca6cbfd97709a88307292overwritingcexpandingkeywordscommittedchangeset2:25736cf2f5cbe41f6be4e6784ef6ecf9f3bbcc7d$catacexpand$Id:a,vef63ca68695b1970/01/0100:00:00user$donotprocess$Id:xxx$expand$Id:c,v25736cf2f5cb1970/01/0100:00:01user$donotprocess$Id:xxx$Touchcopiedcandcheckitsstatus$touchc$hgstatusCopykwfiletokeywordignoredfileunexpandingkeywords$hg--verbosecopyaicopyingatoioverwritingishrinkingkeywords$head-n1iexpand$Id$$hgforgeti$rmiCopyignoredfiletoignoredfile:nooverwriting$hg--verbosecopybicopyingbtoi$hgforgeti$rmicpsymlink(becomesregularfile),andhgcopyafter$cpsymi$ls-li-rw-r--r--*(glob)$head-1iexpand$Id:a,vef63ca68695b1970/01/0100:00:00user$$hgcopy--after--verbosesymicopyingsymtoioverwritingishrinkingkeywords$head-1iexpand$Id$$hgforgeti$rmiTestdifferentoptionsofhgkwfiles$hgkwfilesac$hg-vkwfiles--ignoreIbIsym$hgkwfiles--allKaKcIbIsymDiffspecificrevision$hgdiff--rev1diff-ref63ca68695bc---/dev/nullThuJan0100:00:001970+0000+++b/c*(glob)@@-0,0+1,3@@+expand$Id$+donotprocess$Id:+xxx$Statusafterrollback:$hgrollbackrollingbacktorevision1(undocommit)$hgstatusAc$hgupdate--clean0filesupdated,0filesmerged,0filesremoved,0filesunresolvedCustomkeywordmapsasargumenttokwdemo$hg--quietkwdemo"Xinfo = {author}: {desc}"[extensions]keyword=[keyword]**=b=ignoredemo.txt=i=ignore[keywordmaps]Xinfo={author}:{desc}$Xinfo:test:hgkeywordconfigurationandexpansionexample$Configurecustomkeywordmaps$cat<<EOF>>$HGRCPATH>[keywordmaps]>Id={file}{node|short}{date|rfc822date}{author|user}>Xinfo={author}:{desc}>EOFCatandhgcatfilesbeforecustomexpansion$catabexpand$Id:a,vef63ca68695b1970/01/0100:00:00user$donotprocess$Id:xxx$ignore$Id$$hgcatsymab&&echoexpand$Id:aef63ca68695bThu,01Jan197000:00:00+0000user$donotprocess$Id:xxx$ignore$Id$a*(glob)Writecustomkeywordandpreparemultilinecommitmessage$echo'$Xinfo$'>>a$cat<<EOF>>log>firstline>secondline>EOFInterruptedcommitshouldnotchangestate$hgcommitabort:emptycommitmessage[255]$hgstatusMa?c?logCommitwithmultilinemessageandcustomexpansion$hg--debugcommit-llog-d'2 0'-u'User Name <user@example.com>'aoverwritingaexpandingkeywordscommittedchangeset2:bb948857c743469b22bbf51f7ec8112279ca5d83$rmlogStat,verifyandshowcustomexpansion(firstline)$hgstatus?c$hgverifycheckingchangesetscheckingmanifestscrosscheckingfilesinchangesetsandmanifestscheckingfiles3files,3changesets,4totalrevisions$catabexpand$Id:abb948857c743Thu,01Jan197000:00:02+0000user$donotprocess$Id:xxx$$Xinfo:UserName<user@example.com>:firstline$ignore$Id$$hgcatsymab&&echoexpand$Id:abb948857c743Thu,01Jan197000:00:02+0000user$donotprocess$Id:xxx$$Xinfo:UserName<user@example.com>:firstline$ignore$Id$a*(glob)annotate$hgannotatea1:expand$Id$1:donotprocess$Id:1:xxx$2:$Xinfo$removewithstatuschecks$hgdebugrebuildstate$hgremovea$hg--debugcommit-mrmacommittedchangeset3:d14c712653769de926994cf7fbb06c8fbd68f012$hgstatus?cRollback,revert,andcheckexpansion$hgrollbackrollingbacktorevision2(undocommit)$hgstatusRa?c$hgrevert--no-backup--revtipa$cataexpand$Id:abb948857c743Thu,01Jan197000:00:02+0000user$donotprocess$Id:xxx$$Xinfo:UserName<user@example.com>:firstline$Clonetotestglobalandlocalconfigurations$cd..Expansionindestinatonwithglobalconfiguration$hg--quietcloneTestglobalconf$catglobalconf/aexpand$Id:abb948857c743Thu,01Jan197000:00:02+0000user$donotprocess$Id:xxx$$Xinfo:UserName<user@example.com>:firstline$Noexpansionindestinationwithlocalconfigurationinoriginonly$hg--quiet--config'keyword.**=ignore'cloneTestlocalconf$catlocalconf/aexpand$Id$donotprocess$Id:xxx$$Xinfo$Clonetotestincoming$hgclone-r1TestTest-arequestingallchangesaddingchangesetsaddingmanifestsaddingfilechangesadded2changesetswith3changesto3filesupdatingtobranchdefault3filesupdated,0filesmerged,0filesremoved,0filesunresolved$cdTest-a$cat<<EOF>>.hg/hgrc>[paths]>default=../Test>EOF$hgincomingcomparingwith*test-keyword.t/Test(glob)searchingforchangeschangeset:2:bb948857c743tag:tipuser:UserName<user@example.com>date:ThuJan0100:00:021970+0000summary:firstlineImportedpatchshouldnotberejected$python-c \>'import re; s=re.sub("(Id.*)","\\1 rejecttest",open("a").read()); open("a","wb").write(s);'$hg--debugcommit-m'rejects?'-d'3 0'-u'User Name <user@example.com>'aoverwritingaexpandingkeywordscommittedchangeset2:85e279d709ffc28c9fdd1b868570985fc3d87082$hgexport-o../rejecttest.difftip$cd../Test$hgimport../rejecttest.diffapplying../rejecttest.diff$catabexpand$Id:a4e0994474d25Thu,01Jan197000:00:03+0000user$rejecttestdonotprocess$Id:rejecttestxxx$$Xinfo:UserName<user@example.com>:rejects?$ignore$Id$$hgrollbackrollingbacktorevision2(undocommit)$hgupdate--clean1filesupdated,0filesmerged,0filesremoved,0filesunresolvedkwexpand/kwshrinkonselectedfiles$mkdirx$hgcopyax/a$hg--verbosekwexpandaoverwritingaexpandingkeywordskwexpandx/ashouldabort$hg--verbosekwexpandx/aabort:outstandinguncommittedchanges[255]$cdx$hg--debugcommit-mxa-d'3 0'-u'User Name <user@example.com>'x/ax/a:copya:779c764182ce5d43e2b1eb66ce06d7b47bfe342eoverwritingx/aexpandingkeywordscommittedchangeset3:b4560182a3f9a358179fd2d835c15e9da379c1e4$cataexpand$Id:x/ab4560182a3f9Thu,01Jan197000:00:03+0000user$donotprocess$Id:xxx$$Xinfo:UserName<user@example.com>:xa$kwshrinkainsidedirectoryx$hg--verbosekwshrinkaoverwritingx/ashrinkingkeywords$cataexpand$Id$donotprocess$Id:xxx$$Xinfo$$cd..kwexpandnonexistent$hgkwexpandnonexistentnonexistent:*(glob)hgserve-expandwithhgwebfile-noexpansionwithhgwebannotate/changeset/filediff-checkerrors$hgserve-p$HGPORT-d--pid-file=hg.pid-Aaccess.log-Eerrors.log$cathg.pid>>$DAEMON_PIDS$$TESTDIR/get-with-headers.pylocalhost:$HGPORT'/file/tip/a/?style=raw'200Scriptoutputfollowsexpand$Id:abb948857c743Thu,01Jan197000:00:02+0000user$donotprocess$Id:xxx$$Xinfo:UserName<user@example.com>:firstline$$$TESTDIR/get-with-headers.pylocalhost:$HGPORT'/annotate/tip/a/?style=raw'200Scriptoutputfollowsuser@1:expand$Id$user@1:donotprocess$Id:user@1:xxx$user@2:$Xinfo$$$TESTDIR/get-with-headers.pylocalhost:$HGPORT'/rev/tip/?style=raw'200Scriptoutputfollows # HG changeset patch # User User Name <user@example.com> # Date 3 0 # Node ID b4560182a3f9a358179fd2d835c15e9da379c1e4 # Parent bb948857c743469b22bbf51f7ec8112279ca5d83xadiff-rbb948857c743-rb4560182a3f9x/a---/dev/nullThuJan0100:00:001970+0000+++b/x/aThuJan0100:00:031970+0000@@-0,0+1,4@@+expand$Id$+donotprocess$Id:+xxx$+$Xinfo$$$TESTDIR/get-with-headers.pylocalhost:$HGPORT'/diff/bb948857c743/a?style=raw'200Scriptoutputfollowsdiff-ref63ca68695b-rbb948857c743a---a/aThuJan0100:00:001970+0000+++b/aThuJan0100:00:021970+0000@@-1,3+1,4@@expand$Id$donotprocess$Id:xxx$+$Xinfo$$caterrors.logPreparemergeandresolvetests$echo'$Id$'>m$hgaddm$hgcommit-m4kw$echofoo>>m$hgcommit-m5foosimplemerge$hgupdate41filesupdated,0filesmerged,0filesremoved,0filesunresolved$echofoo>>m$hgcommit-m6foocreatednewhead$hgmerge0filesupdated,0filesmerged,0filesremoved,0filesunresolved(branchmerge,don't forget to commit) $ hg commit -m simplemerge $ cat m $Id: m 27d48ee14f67 Thu, 01 Jan 1970 00:00:00 +0000 test $ fooconflict: keyword should stay outside conflict zone $ hg update 4 1 files updated, 0 files merged, 0 files removed, 0 files unresolved $ echo bar >> m $ hg commit -m 8bar created new head $ hg merge merging m warning: conflicts during merge. merging m failed! 0 files updated, 0 files merged, 0 files removed, 1 files unresolved use 'hgresolve' to retry unresolved file merges or 'hgupdate-C.' to abandon [1] $ cat m $Id$<<<<<<<localbar=======foo>>>>>>>otherresolvetolocal$HGMERGE=internal:localhgresolve-a$hgcommit-mlocalresolve$catm$Id:m41efa6d38e9bThu,01Jan197000:00:00+0000test$barTestrestrictedmodewithtransplant-b$hgupdate61filesupdated,0filesmerged,0filesremoved,0filesunresolved$hgbranchfoomarkedworkingdirectoryasbranchfoo$mvaa.bak$echofoobranch>a$cata.bak>> a $ rm a.bak $ hg commit -m 9foobranch $ hg update default 2 files updated, 0 files merged, 0 files removed, 0 files unresolved $ hg -y transplant -b foo tip applying 4aa30d025d50 4aa30d025d50 transplanted to 5a4da427c162Expansion in changeset but not in file $ hg tip -p changeset: 11:5a4da427c162 tag: tip parent: 9:41efa6d38e9b user: test date: Thu Jan 01 00:00:00 1970 +0000 summary: 9foobranch diff -r 41efa6d38e9b -r 5a4da427c162 a --- a/a Thu Jan 01 00:00:00 1970 +0000 +++ b/a Thu Jan 01 00:00:00 1970 +0000 @@ -1,3 +1,4 @@ +foobranch expand $Id$ do not process $Id: xxx $ $ head -n 2 a foobranch expand $Id: a 5a4da427c162 Thu, 01 Jan 1970 00:00:00 +0000 test $Turn off expansion $ hg -q rollback $ hg -q update -Ckwshrink with unknown file u $ cp a u $ hg --verbose kwshrink overwriting a shrinking keywords overwriting m shrinking keywords overwriting x/a shrinking keywordsKeywords shrunk in working directory, but not yet disabled - cat shows unexpanded keywords - hg cat shows expanded keywords $ cat a b expand $Id$ do not process $Id: xxx $ $Xinfo$ ignore $Id$ $ hg cat sym a b && echo expand $Id: a bb948857c743 Thu, 01 Jan 1970 00:00:02 +0000 user $ do not process $Id: xxx $ $Xinfo: User Name <user@example.com>: firstline $ ignore $Id$ a* (glob)Now disable keyword expansion $ rm "$HGRCPATH" $ cat a b expand $Id$ do not process $Id: xxx $ $Xinfo$ ignore $Id$ $ hg cat sym a b && echo expand $Id$ do not process $Id: xxx $ $Xinfo$ ignore $Id$ a* (glob)