o ]Lbw @sdZddlmZddlZddlZddlZddlmZddlm Z ddl m Z ddl m Z mZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZddlmZm Z dd l!m"Z"iZ#e$e#Z$d Z%d Z&d Z'd Z(dZ)ddddddZ*e+Z+iZ,e-e,Z-e-dddde+dej.dddZ/e+dej.dddZ0e+d ej.dd!d"Z1d#d$iZ2d%d&Z3d'd(Z4d)d*Z5Gd+d,d,e6Z7Gd-d.d.ejZ8d/d0Z9d1d2Z:e$d3d4d5ded6fd7d8d$ed9ed:fged;dd?Z;e$d@e je$dRe j{file|basename},v {node|short} {date|svnutcdate} {author|user})r+r,sLastChangedRevisions LastChangedBysLastChangedDaterr)update configbool)ui templateskwsetsr$r$r%_defaultkwmapss& r2cCs |d|S)zlHelper for keyword expansion removal in text. Depending on subfunc also returns number of substitutions.s$\1$r$)textsubfuncr$r$r% _shrinktexts r5cs0fdd|jD}fdd|jD}||fS)zRetrieves modified and added files from a working directory state and returns the subset of each contained in given changed files retrieved from a change context.cg|]}|vr|qSr$r$.0fchangedr$r% z_preselect..cr6r$r$r7r:r$r%r<r=)modifiedadded)wstatusr;r>r?r$r:r% _preselectsrAc@seZdZdZddZeddZejddZ ejdd Z ejd d Z d d Z ddZ ddZddZdddZddZddZddZdS) kwtemplaterzr Sets up keyword templates, corresponding keyword regex, and provides keyword substitution functions. cCsn||_t||_t|jdg|||_tdtv|_ d|_ |j d}|r/t ||_ dSt|j|_ dS)Nr*r)F keywordmaps)r/weakrefref_reporrootkwtools restrictedsplitrestrict postcommit configitemsdictr0r2)selfr/repoincexckwmapsr$r$r%__init__s  zkwtemplater.__init__cCs|SN)rFrOr$r$r%rPszkwtemplater.repocCsdttj|jS)z+Returns bar-separated and escaped keywords.|)joinmaprreescaper0keysrVr$r$r%escapeszkwtemplater.escapecCtd|jS)z&Returns regex for unexpanded keywords.s\$(%s)\$recompiler\rVr$r$r%rekw zkwtemplater.rekwcCr])z$Returns regex for expanded keywords.s\$(%s): [^$\n\r]*? \$r^rVr$r$r%rekwexprbzkwtemplater.rekwexpcsfdd}|||S)z1Replaces keywords in data with expanded template.cs\|d}tjjj|}j|jjjdt j }d||fS)N)rGfiles $%s: %s $) groupr maketemplaterr/rPr0 pushbuffershowrGr firstline popbuffer)mobjkwctekwctxpathrOr$r%kwsubs   z%kwtemplater.substitute..kwsubr$)rOdatarrrqr4rsr$rpr% substitutes zkwtemplater.substitutecCs|jj||dS)z4Similar to filelog.linkrev, but returns a changectx.)fileid)rPfilectx changectx)rOrrrvr$r$r%linkctx%szkwtemplater.linkctxcCs>|js||rt|s|||}|||||jjS|S)z$Returns data with keywords expanded.)rKrrbinaryryrurasub)rOrrnodertrqr$r$r%expand)s zkwtemplater.expandcsfdd|DS)zkReturns subset of candidates which are configured for keyword expansion but are not symbolic links.cs(g|]}|rd|vr|qSl)rflagsr7rqrOr$r%r<7s(z(kwtemplater.iskwfile..r$)rOcandrqr$rr%iskwfile4szkwtemplater.iskwfileFcCs|js|s|jr|||}|sdS|jo|}|js|r"|r"|}|js'|r+|j}n|j}|r5td} ntd} |jd} |D]} |jrQ|j|  || } n|j | } t | r]q@|r| } |rn|| || }n#|jrt| dkr| D]}| |vr|| || s|| }nqy|| | ||j\} }n|jr|| }nt| |j\} }|r|j| | |jj| ddd}|| ||r| | }|j}|j}t|}|||f}|jj !| |q@|jr|jj j"| ddq@dS) z7Overwrites selected files expanding/shrinking keywords.Ns"overwriting %s expanding keywords s"overwriting %s shrinking keywords rdswbT) atomictemp) p1_tracked)#rKrLrmanifestrarcrrPrereadwreadrrzparentsrylencmprxrusubnsearchr5r/notewvfswritecloselstatst_modest_sizermtime_ofdirstate set_cleanupdate_file_p1)rOrq candidateslookupr}rakwcmdmfre_kwmsgwctxr9rtrpfoundfpsmodesizemtime cache_datar$r$r% overwrite9sd             zkwtemplater.overwritecCs&||rt|st||jjS|S)z4Returns text with all keyword substitutions removed.)rrrzr5rcr{)rOfnamer3r$r$r%shrinkrszkwtemplater.shrinkcCs6||rd|}t|st||jjdS|S)z1Returns lines with keyword substitutions removed.r*T)rrXrrzr5rcr{ splitlines)rOrlinesr3r$r$r% shrinklinesxs   zkwtemplater.shrinklinescCs|jr |||S|S)z]If in restricted mode returns data read from wdir with keyword substitutions removed.)rKr)rOrrtr$r$r%rs zkwtemplater.wreadNF)__name__ __module__ __qualname____doc__rTpropertyrPr propertycacher\rarcruryr}rrrrrr$r$r$r%rBs&     9 rBcsFeZdZdZfddZfddZd fdd Zfd d ZZS) kwfilelogz Subclass of filelog to hook into its read, add, cmp methods. Keywords are "stored" unexpanded, and processed on reading. cs"tt|||||_||_dSrU)superrrTkwtrr)rOopenerrrr __class__r$r%rTs zkwfilelog.__init__cs0tt||}||r|S|j|j||S)z&Expands keywords when reading filelog.)rrrrenamedrr}rr)rOr|rtrr$r%rs zkwfilelog.readNcs*|j|j|}tt|||||||S)z5Removes keyword substitutions when adding to filelog.)rrrrrradd)rOr3metatrlinkp1p2rr$r%rsz kwfilelog.addcs"|j|j|}tt|||S)z-Removes keyword substitutions for comparison.)rrrrrrr)rOr|r3rr$r%rsz kwfilelog.cmp)NN) rrrrrTrrr __classcell__r$r$rr%rs   rcOs^|rt|}|jt|||d|dp|ddS|dr(tt dtt d)z\Bails out if [keyword] configuration is not active. Returns status of working directory.Tunknownall)rcleanunknownkeywords[keyword] patterns cannot matchs no [keyword] patterns configured) r byteskwargsstatusrrgetrMr Abortr)r/rPrrpatsoptsr$r$r%_statuss   rcOs|d}t|dkrttdt|dd}|3t||||g|Ri|}|js8|j s8|j s8|j r?ttd| ||j d|WddS1sSwYdS)z7Selects files and passes them to kwtemplater.overwrite.Nrdsoutstanding uncommitted merge _keywordkwtsoutstanding uncommitted changesT)rrr rrrwlockrr>r?removeddeletedrr)r/rPr}rrrrrr$r$r% _kwfwrites  "rskwdemodsdefaults"show default keyword template mapsfrcfilesread maps from rcfilesFILEs+hg kwdemo [-d] [-f RCFILE] [TEMPLATEMAP]...T) optionalrepocs>fdd}d}tdd}td||dur}n|j}tj||dd }d |dd d d }d d |d  d } |sK| dr td| r[ td| dsb| ss|rl tdn td| dr | d|rdd |} |jd|  |j dt d } nH| dr|r tdn tdt} | rֈ tdt| D] \} } d | | d qn td| rt| } nt} tt|d|d d |d d |d t| dd t| d}|j|||d|gtd ||||jd!Wdn 1sOwY d"D]\}}|d#d$d%d&d'krt|jd"|dd qYtd(}d)||j |d* td+|!||j"|j#dS),aprint [keywordmaps] configuration and an expansion example Show current, custom, or default keyword template maps and their expansions. Extend the current configuration by specifying maps as arguments and using -f/--rcfile to source an external hgrc file. Use -d/--default to disable current configuration. See :hg:`help templates` for information on templates and filters. csJd|t|D]\}}t|trt|}d||fq dS)Ns[%s] s%s = %s )rsorted isinstanceboolrpprint)sectionitemskvr/r$r% demoitemss   zdemo..demoitemssdemo.txtr*skwdemo.s$creating temporary repository at %s NT)createrrrrCrcfiles3 configuration using custom keyword template maps s! extending current template maps rs# overriding default svn keywordset s# overriding default cvs keywordset rs[keywordmaps] %s  shgrcs- configuration using default svn keywordset s- configuration using default cvs keywordset s! disabling current template maps s4 configuration using current keyword template maps s[extensions] keyword = $s$ $s$ s keywords written to %s: s demobranchshooks.rdrscommits.hg keyword configuration and expansion exampleshg ci -m '%s' )r3s keywords expanded )$rmkdtemprrbaseuir instance setconfigr.rMrr readconfigrXvfsrrNr2 iteritemsuisetup reposetup writenoi18nrr[rrrr setbranchrJfindr/commitrrmtreerG)r/rPargsrrfntmpdirrsvnuikwmapsrcmapsrSrrkeywordsnamecmdrr$rr%demos            rskwexpands!hg kwexpand [OPTION]... [FILE]...) inferrepocOt||dg|Ri|dS)zexpand keywords in the working directory Run after (re)enabling keyword expansion. kwexpand refuses to run if given files contain local changes. TNrr/rPrrr$r$r%r}'r}kwfilesArs&show keyword status flags of all filesiignores"show files excluded from expansionurs%only show unknown (not tracked) filess hg kwfiles [OPTION]... [FILE]...c st|dd}|d}t||||g|Ri|}|r|}nd}g}t|}|dr2|dr=t|j|j|j }| ||| |j |} | |j ||dr[|dra| f} ngggf} |dsp|dr| fdd|Dfd d|j Df7} d } t| td | } |d |} d }|ds|jrd}| D](\}}}d|}|D]}| | j||d| j|||||f|dqq| dS)ashow files configured for keyword expansion List which files in the working directory are matched by the [keyword] configuration patterns. Useful to prevent inadvertent keyword expansion and to speed up execution by including only files that are actual candidates for expansion. See :hg:`help keyword` on how to construct patterns both for inclusion and exclusion of files. With -A/--all and -v/--verbose the codes used to show the status of files are:: K = keyword expansion candidate k = keyword expansion candidate (not tracked) I = ignored i = ignored (not tracked) rNr*rrrcg|]}|vr|qSr$r$r7)kwfilesr$r%r<lr=zfiles..cr r$r$r7) kwunknownr$r%r<mr=s5enabled deleted enabledunknown ignored ignoredunknownsK!kIirs%.0s%s s%s %s skwfiles.)kwstatusrr)label)rrgetcwdrrrrr>r?rrrrrJzipbytestr formatterverbose startitemrtplainpathtoend)r/rPrrrrrcwdfiles kwdeleted showfileskwlabelskwstatesfmfmtkwstatechar filenamesrr9r$)r r r%r8sF         rskwshrinks!hg kwshrink [OPTION]... [FILE]...cOr)zrevert expanded keywords in the working directory Must be run before changing/disabling active keywords. kwshrink refuses to run if given files contain local changes. FNrrr$r$r%r~rrcCsD|||||||tt|dddd}|r ||j|j|_dSdS)ztMonkeypatch/wrap patch.patchfile.__init__ to avoid rejects or conflicts due to expanded keywords in working dir.rPNr)rrrr)origrOr/gpbackendstoreeolmoderr$r$r%kwpatchfile_inits r(cosbt|dd}|r|j}d|_z||g|Ri|D]}|VqW|r)||_dSdS|r0||_w)z*Monkeypatch patch.diff to avoid expansion.rNT)rrK)r#rPrkwargsrrKchunkr$r$r%kwdiffs  r+ccsXt|jdd}|r|j}tj|_z||D]}|VqW|r$||_dSdS|r+||_w)z2Wraps webcommands.x turning off keyword expansion.rN)rrPrrnever)r#webr origmatchr*r$r$r% kwweb_skips  r/c Cst|dd}|dur|||||||S|M|j7d|_|||||||}||krD||} d|_|| | ddd|_|WdWdS1sWwYWddS1sgwYdS)z3Wraps cmdutil.amend expanding keywords after amend.rNTF) rrr parentchangerLr|rKrr) r#r/rPoldextrarrrnewidrqr$r$r%kw_amends  Rr4cstdddur|||||SE||||||dr- WddSdfddfddjD}|ddWddS1s^wYdS) aWraps cmdutil.copy so that copy/rename destinations do not contain expanded keywords. Note that the source of a regular file destination may also be a symlink: hg cp sym x -> x is symlink cp sym x; hg cp -A sym x -> x is file (maybe expanded keywords) For the latter we have to follow the symlink to find out whether its target is configured for expansion and we therefore must unexpand the keywords in the destination.rNsdry_runcs<j|}d|vrtjtj|} |S)zReturns true if dest is a regular file and configured for expansion or a symlink which points to a file configured for expansion.r) rcopiedrr canonpathrGosrrrealpathr)destsource)rrrPrr$r% haskwsources  zkw_copy..haskwsourcecs&g|]}d|vr|r|qSr~)rr7)r;rr$r%r<s zkw_copy..F)rrrrrcopiesr)r#r/rPrrrenamerr$)rr;rrPrr%kw_copys    "r>c Ost|dd}|dur||||g|Ri|S|_d|_|d}|}||||g|Ri|} |d} || krqt|| \} } d|_|j| | | dd| | | dddWdn1siwYd|_| WdS1s}wYdS)z9Wraps record.dorecord expanding keywords after recording.rNTrF) rrrLrrArrKrr0r) r#r/rP commitfuncrrrrqr@retrecctxr>r?r$r$r% kw_dorecords&   $rBcCs|jr||St|jdd}|dur|||S|jdur8|jjs@||r.d|vs@| d| ks@| | krJ|j |j| SdS)NrrT) _customcmprrrF _filenode_encodefilterpatsrrrrr_filelogrt)r#rOfctxrr$r$r% kwfilectx_cmps     rIcCsdd}ttd|ttjdtttjdtttdt tt dt tt dt tt d t tD] }tt|tq;d S) a Monkeypatches dispatch._parse to retrieve user command. Overrides file method to return kwfilelog instead of filelog if file matches user configuration. Wraps commit to overwrite configured files with updated keyword substitutions. Monkeypatches patch and webcommands.cSs*|||\}}}}}|td<|||||fS)z9Monkeypatch dispatch._parse to obtain running hg command.r))rH)r#r/rrfuncoptions cmdoptionsr$r$r%kwdispatch_parse$sz!uisetup..kwdispatch_parses_parsescmps__init__sdiffsamendscopysdorecordN)r wrapfunctionrrrwrIr patchfiler(r+rr4r>rBnokwwebcommandsrJrr/)r/rMcr$r$r%rs rcsz|rtdtvsdt|jvs|jdrWdSWn t y(Ynwgdg}}| dD]\}}|dkrB| |q4| |q4|sLdSt ||||Gfdd d |j |_ |_dS) z0Sets up repo as kwrepo for keyword substitution.r)s.hgsbundle:Ns.hg*rrcs^eZdZfddZfddZfddZd fd d Zdfd d ZZS)zreposetup..kwrepocs&|ddkr |dd}t|j|S)Nr/rd)rsvfs)rOr9)rr$r%rePs  zreposetup..kwrepo.filecst||}||SrU)rr)rOfilenamertrkwreporr$r%rUs zreposetup..kwrepo.wreadcs,|j|_zt|j|i|W|`S|`wrU) kwcommitctx commitctxrr)rOrr)rrVr$r%rYs z reposetup..kwrepo.commitFNcsTt||||}js(j}d_||t||dd|_|S)NTF)rrXrLrKrrr?r>)rOrqr origctxnrKrUr$r%rWbsz%reposetup..kwrepo.kwcommitctxc s|Ij}z@|s|d}t|||}|s<|d}t||\}}d_||dd||dd|W|_WdS|_w1sPwYdS)NrFT)rrKrrrollbackrArr) rOdryrunforce origrestrictr;r@rqr>r?rUr$r%r[ns"  z"reposetup..kwrepo.rollback)FN)FF) rrrrerrrWr[rr$rVrrr%rVOs    rV)localrH nokwcommandsrJr splitpathrG_url startswithAttributeErrorrMappendrBrr)r/rPrQrRpatoptr$r_r%r6s.     0 rrUr)Ir __future__rr7r_rDmercurial.i18nrmercurial.pycompatrmercurial.hgwebr mercurialrrrr r r r r rrrrrrrrrmercurial.utilsrrmercurial.dirstateutilsrcmdtablecommand testedwithrarPrIrecordextensions colortabletemplatefilter configtable configitemr"r&r'r(rHr2r5rAobjectrBrrrrwalkoptsr}rrr(r+r/r4r>rBrIrrr$r$r$r%s 9   L             ]   ;    '