o ]LbZ @s&dZddlmZmZddlZddlZddlZddlZddlZddl Z ddl Z ddl m Z ddl mZmZddlmZmZmZmZmZmZmZmZmZmZmZmZmZmZddlm Z m!Z!dd l"m#Z#ej$Z$ej%Z%e j&Z'e j(Z)d Z*d Z+d Z,d Z-d Z.d Z/dZ0e1dZ2ddZ3ddZ4ddZ5ddZ6iZ7dddZ8Gddde9Z:Gddde9Z;Gdd d e<Z=d!d"Z>dd#d$Z?Gd%d&d&e9Z@dd(d)ZAd*d+ZBd,d-ZCd.d/ZDd0d1ZEd2d3ZFd4d5ZGd6dd7d8d9d:ZHgd;ZIGdd?d?e9ZKdd@dAZLGdBdCdCeKZMdDeMiZNiZOdEdFZPePdGdHdIZQGdJdKdKe9ZRdLZSGdMdNdNeKZTGdOdPdPe9ZUdQdRZVGdSdTdTeKZWGdUdVdVeWZXdddWddeYeZej[\dXddYdZd[ Z]dd]d^Z^d_d`Z_dadbZ`   ddcddZadedfZbdgdhZcdidjZddkdlZedmdnZfdodpZgdqdrZhdsdtZidudvZjddxdyZk ddzd{Zld|d}Zme8d~dddZneYgdddej[\DZoe8deoddZpe8ddddZqe8dddZre8dddZse8dddZte8dddZue8dddZve8dddZwGdddejxZye8ddddZze8ddddZ{e8ddddZ|e8ddddZ}e8ddddZ~e8ddddZe8dddZe8dddZe8ddddZe8dddÄZe8ddŃddDŽZe8dȃddʄZe dˡZe8d̃dd΄Ze8dσddфZe8ddӃddՄZddׄZdS)aHandling of the new bundle2 format The goal of bundle2 is to act as an atomically packet to transmit a set of payloads in an application agnostic way. It consist in a sequence of "parts" that will be handed to and processed by the application layer. General format architecture =========================== The format is architectured as follow - magic string - stream level parameters - payload parts (any number) - end of stream marker. the Binary format ============================ All numbers are unsigned and big-endian. stream level parameters ------------------------ Binary format is as follow :params size: int32 The total number of Bytes used by the parameters :params value: arbitrary number of Bytes A blob of `params size` containing the serialized version of all stream level parameters. The blob contains a space separated list of parameters. Parameters with value are stored in the form `=`. Both name and value are urlquoted. Empty name are obviously forbidden. Name MUST start with a letter. If this first letter is lower case, the parameter is advisory and can be safely ignored. However when the first letter is capital, the parameter is mandatory and the bundling process MUST stop if he is not able to proceed it. Stream parameters use a simple textual format for two main reasons: - Stream level parameters should remain simple and we want to discourage any crazy usage. - Textual data allow easy human inspection of a bundle2 header in case of troubles. Any Applicative level options MUST go into a bundle2 part instead. Payload part ------------------------ Binary format is as follow :header size: int32 The total number of Bytes used by the part header. When the header is empty (size = 0) this is interpreted as the end of stream marker. :header: The header defines how to interpret the part. It contains two piece of data: the part type, and the part parameters. The part type is used to route an application level handler, that can interpret payload. Part parameters are passed to the application level handler. They are meant to convey information that will help the application level object to interpret the part payload. The binary format of the header is has follow :typesize: (one byte) :parttype: alphanumerical part name (restricted to [a-zA-Z0-9_:-]*) :partid: A 32bits integer (unique in the bundle) that can be used to refer to this part. :parameters: Part's parameter may have arbitrary content, the binary structure is:: :mandatory-count: 1 byte, number of mandatory parameters :advisory-count: 1 byte, number of advisory parameters :param-sizes: N couple of bytes, where N is the total number of parameters. Each couple contains (, `. `chunksize` is an int32, `chunkdata` are plain bytes (as much as `chunksize` says)` The payload part is concluded by a zero size chunk. The current implementation always produces either zero or one chunk. This is an implementation limitation that will ultimately be lifted. `chunksize` can be negative to trigger special case processing. No such processing is in place yet. Bundle processing ============================ Each part is processed in order using a "part handler". Handler are registered for a certain part type. The matching of a part to its handler is case insensitive. The case of the part type is used to know if a part is mandatory or advisory. If the Part type contains any uppercase char it is considered mandatory. When no handler is known for a Mandatory part, the process is aborted and an exception is raised. If the part is advisory and no handler is known, the part is ignored. When the process is aborted, the full bundle is still read from the stream to keep the channel usable. But none of the part read from an abort are processed. In the future, dropping the stream may become an option for channel we do not care to preserve. )absolute_importdivisionN)_)hexshort) bookmarks changegroupencodingerrorobsoletephasespushkeypycompat requirementsscmutil streamclonetagsurlutil) stringutilurlutil) repositorys>is>Bs>Is>BBs[^a-zA-Z0-9_:-]cC"|ddr|d|dSdS)z(debug regarding output stream (bundling)devel bundle2.debugsbundle2-output: %s N configbooldebuguimessager#3/usr/lib/python3/dist-packages/mercurial/bundle2.pyoutdebug r%cCr)z"debug on input stream (unbundling)rrsbundle2-input: %s Nrr r#r#r$indebugr&r'cCst|r t|dS)z9raise ValueError if a parttype contains invalid characterN)_parttypeforbiddensearch ValueErrorparttyper#r#r$validateparttypes r-cCs dd|S)zreturn a struct format to read part parameter sizes The number parameters is variable so we need to build that format dynamically. >sBBr#)nbparamsr#r#r$_makefpartparamsizess r0r#cstfdd}|S)zdecorator that register a function as a bundle2 part handler eg:: @parthandler('myparttype', ('mandatory', 'param', 'handled')) def myparttypehandler(...): '''process a part of type "my part".''' ... cs*}|tvs J|t|<t|_|SN)lowerparthandlermapping frozensetparams)func lparttyper5r,r#r$ _decorators   zparthandler.._decorator)r-)r,r5r9r#r8r$ parthandlers r:c@sNeZdZdZddZdddZddZd d Zd d Zd dZ ddZ e Z dS)unbundlerecordsakeep record of what happens during and unbundle New records are added using `records.add('cat', obj)`. Where 'cat' is a category of record and obj is an arbitrary object. `records['cat']` will return all entries of this category 'cat'. Iterating on the object itself will yield `('category', obj)` tuples for all entries. All iterations happens in chronological order. cCsi|_g|_i|_dSr1) _categories _sequences_repliesselfr#r#r$__init__  zunbundlerecords.__init__NcCsF|j|g||j||f|dur!||||dSdS)zadd a new record of a given category. The entry can then be retrieved in the list returned by self['category'].N)r< setdefaultappendr= getrepliesadd)r@categoryentry inreplytor#r#r$rFs zunbundlerecords.addcCs|j|tS)z3get the records that are replies to a specific part)r>rCr;)r@partidr#r#r$rEszunbundlerecords.getrepliescCst|j|dS)Nr#)tupler<get)r@catr#r#r$ __getitem__ szunbundlerecords.__getitem__cC t|jSr1)iterr=r?r#r#r$__iter__# zunbundlerecords.__iter__cCrOr1)lenr=r?r#r#r$__len__&rRzunbundlerecords.__len__cCrOr1)boolr=r?r#r#r$ __nonzero__)rRzunbundlerecords.__nonzero__r1) __name__ __module__ __qualname____doc__rArFrErNrQrTrV__bool__r#r#r#r$r;s  r;c@s*eZdZdZd ddZddZdd Zd S) bundleoperationaPan object that represents a single bundling process Its purpose is to carry unbundle-related objects and states. A new object should be created at the beginning of each bundle processing. The object is to be returned by the processing function. The object has very little content now it will ultimately contain: * an access to the repo the bundle is applied to, * a ui object, * a way to retrieve a transaction to add changes to the repo, * a way to record the result of processing each part, * a way to construct a bundle response when applicable. TcCs>||_|j|_t|_d|_||_i|_||_i|_||_ dSr1) repor!r;recordsreply captureoutputhookargs_gettransactionmodessource)r@r^transactiongetterrarer#r#r$rA?s zbundleoperation.__init__cCs.|}|jr|j|j|j|_d|_|Sr1)rcrbupdate)r@ transactionr#r#r$gettransactionKs zbundleoperation.gettransactioncCs$|jdur td|j|dS)Ns@attempted to add hookargs to operation after transaction started)rbr ProgrammingErrorrg)r@rbr#r#r$ addhookargsZs zbundleoperation.addhookargsN)Tr])rWrXrYrZrArirkr#r#r#r$r\/s   r\c@s eZdZdS)TransactionUnavailableN)rWrXrYr#r#r#r$rlcsrlcCst)zdefault method to get a transaction while processing a bundle Raise an exception to highlight the fact that no transaction was expected to be createdrlr#r#r#r$_notransactiongsrnc st|tr2djd<|durdjvr|jd<|dur&djvr&|jd<t||fdd|dSt|fdd|d}t||||fi||S) N1bundle2ssourceurlcSr1r#r#trr#r$wzapplybundle..recrrr1r#r#rsr#r$ruzrv) isinstance unbundle20rb processbundler\_processchangegroup)r^ unbundlerrtrerkwargsopr#rsr$ applybundleos    rc@s$eZdZddZddZddZdS) partiteratorcCs(||_||_||_d|_d|_d|_dS)Nr)r^r~r|iteratorcountcurrent)r@r^r~r|r#r#r$rAs  zpartiterator.__init__csfdd}|_jS)Nc3sDtjd}|D]\}}|_|_|V|d_q dS)Nr) enumerater| iterpartsrrconsume)itrrpr?r#r$r6s z$partiterator.__enter__..func)r)r@r6r#r?r$ __enter__s  zpartiterator.__enter__cCs|jsdSt|trNd}z|jr|j|jD]}|qWn ty+d}Ynwd|_g}d}|jjdurD|jj}|jjj }||_ ||_ |rN||j j d|jdS)NFTs%bundle2-input-bundle: %i parts total )rrx Exceptionrrduringunbundle2r~r` salvageoutput capabilities _replycaps_bundle2salvagedoutputr^r!rr)r@typeexctb seekerrorpartsalvaged replycapsr#r#r$__exit__s4        zpartiterator.__exit__N)rWrXrYrArrr#r#r#r$rs rr]cCs|dur|dur t}t|||d}|j|jjrKdg}|jr(|dt|j|jdus2|jtur8|dn|d|d|jd |t ||||S) a,This function process a bundle, apply effect to/from a repo It iterates over each part then searches for and uses the proper handling code to process the part. Parts are processed in order. Unknown Mandatory part will abort the process. It is temporarily possible to provide a prebuilt bundleoperation to the function. This is used to ensure output is properly propagated in case of an error during the unbundling. This output capturing part will likely be reworked and this ability will probably go away in the process. Nrwsbundle2-input-bundle:s %i paramss no-transactions with-transaction r]) rnr\r5r! debugflagrDrSrcrjoin processparts)r^r|rfr~remsgr#r#r$rzs      rzcCsFt|||}|D]}t||q WddS1swYdSr1)r _processpart)r^r~r|partsrr#r#r$rs  "rcKs0|j|j|||fi|}|jdd|i|S)N changegroupreturn)applyr^r_rF)r~cgrtrerr}retr#r#r$r{sr{c Csd}zz?t|j}|durd}tj|jdt|jd|j|j|j}|r@t |}| dd |}tj|j|dd}Wnqtjy}zd|j rPt|jd |WYd}~W|jj rd |jg}|j sp|d t|j}t|j|}|s|r|d |r|d ||r|d||d|d||jd |dSdSd}~wwW|jj rd |jg}|j s|d t|j}t|j|}|s|r|d |r|d ||r|d||d|d||jd ||S|jj r\d |jg}|j s|d t|j}t|j|}|s-|rK|d |r<|d ||rF|d||d|d||jd |ww)Nsunknownsunsupported-typer+sfound a handler for part %ssunsupported-params (%s)s, )r,r5s supporteds%ignoring unsupported advisory part %ssbundle2-input-part: "%s" (advisory) (params: %i mandatory %i advisory)s %s r])r3rLrr BundleUnknownFeatureErrorr'r! mandatorykeysr5listsortr mandatoryrrDrSr) r~rstatushandler unknownparamsrrnbmpnbapr#r#r$ _gethandlers                    rc Cst||}|dur dSd}|jr|jdur|jjdddd}z*|||W|dur/|j}|rH|jjd|dd}|jdt |j dd dSdS|durS|j}|rk|jjd|dd}|jdt |j dd ww) zprocess a single part from a bundle The part is guaranteed to have been fully consumed when the function exits (even if an exception is raised).NT)r subprocr]outputFdatar in-reply-tor) rrar`r! pushbuffer popbuffernewpartaddparamrbytestrid)r~rroutputoutpartr#r#r$r&s2     rcCsji}|D],}|s qd|vr|d}}n |dd\}}|d}t|}dd|D}|||<q|S)zdecode a bundle2 caps bytes blob into a dictionary The blob is a list of capabilities (one per line) Capabilities may have values using a line of the form:: capability=value1,value2,value3 The values are always a list.=r#r,cSg|]}t|qSr#urlrequnquote.0vr#r#r$ Vzdecodecaps..) splitlinessplitrr)blobcapslinekeyvalsr#r#r$ decodecapsCs     rcCs\g}t|D]"}||}t|}dd|D}|r#d|d|f}||qd|S)z2encode a bundle2 caps dictionary into a bytes blobcSrr#)rquoterr#r#r$rarzencodecaps..%s=%srr)sortedrrrrD)rchunkscarr#r#r$ encodecaps[s    r)r]UN)HG10UNr)HG10sBZ)HG10GZsGZ)r]HG20rHG10BZr)rrrc@sneZdZdZdZdddZdddZed d Zdd d Z d dZ ddZ ddZ ddZ ddZddZdS)bundle20zrepresent an outgoing bundle2 container Use the `addparam` method to add stream level parameter. and `newpart` to populate it. Then call `getchunks` to retrieve all the binary chunks of data that compose the bundle2 container.rr#cCs:||_g|_g|_t||_tjd|_d|_ d|_ dS)NrT) r!_params_partsdictrr compengines forbundletype _compengine _compoptsprefercompressed)r@r!rr#r#r$rAs  zbundle20.__init__NcCsH|dvrdStdd|jDrJ|d|tj||_||_dS)z$setup core part compression to )NrNcss |] \}}|dkVqdS) compressionN)r2)rnrr#r#r$ sz*bundle20.setcompression..s Compression)anyrrrrrrr)r@algcompoptsr#r#r$setcompressions   zbundle20.setcompressioncCrO)z*total number of parts added to the bundler)rSrr?r#r#r$nbparts zbundle20.nbpartscCsH|std|ddttjvrtd||j||fdS)zadd a stream level parametersempty parameter namerrsnon letter first character: %sN)r rjrrstring ascii_lettersrrD)r@namevaluer#r#r$rs zbundle20.addparamcCs*|jdusJt|j|_|j|dS)z_add a new part to the bundle2 container Parts contains the actual applicative payload.N)rrSrrD)r@rr#r#r$addparts zbundle20.addpartcOs$t|g|Ri|}|||S)aqcreate a new part and add it to the containers As the part is directly added to the containers. For now, this means that any failure to properly initialize the part after calling ``newpart`` should result in a failure of the whole bundling process. You can still fall back to manually create and add if you need better control.) bundlepartr)r@typeidargsr}rr#r#r$rs zbundle20.newpartccs|jjr+d|jg}|jr|dt|j|dt|j|jd|t |jd|j|jV| }t |jd|t t t|V|rQ|V|j ||jD]}|Vq[dS)Nsbundle2-output-bundle: "%s",s (%i params)s %i parts total r]sstart emission of %s streamsbundle parameter: %s)r!r _magicstringrrDrSrrrr% _paramchunk_pack_fstreamparamsizercompressstream _getcorechunkr)r@rparamchunkr#r#r$ getchunkss&  zbundle20.getchunkscCsPg}|jD]\}}t|}|durt|}d||f}||qd|S)z1return a encoded version of all stream parametersNr )rrrrDr)r@blocksparrr#r#r$rs     zbundle20._paramchunkccsbt|jd|jD]}t|jd|j|j|jdD]}|Vqq t|jdttdVdS)zUyield chunk for the core part of the bundle (all but headers and parameters)sstart of partssbundle part: "%s"r!s end of bundlerN)r%r!rrrr_fpartheadersize)r@rrr#r#r$rs   zbundle20._getcorechunkcCs.g}|jD]}|jdr||q|S)zreturn a list with a copy of all output parts in the bundle This is meant to be used during error handling to make sure we preserve server outputr)rr startswithrDcopy)r@rrr#r#r$rs   zbundle20.salvageoutputr#r1)rWrXrYrZrrArpropertyrrrrrrrrr#r#r#r$rvs      rc@s(eZdZdZddZddZddZdS) unpackermixinz6A mixin to extract bytes and struct data from a streamcCs ||_dSr1)_fp)r@fpr#r#r$rArRzunpackermixin.__init__cC|t|}t||S)aunpack this struct format from the stream This method is meant for internal usage by the bundle2 protocol only. They directly manipulate the low level stream including bundle2 level instruction. Do not use it to implement higher-level logic or methods.) _readexactstructcalcsize_unpackr@formatrr#r#r$rs zunpackermixin._unpackcCst|j|S)aread exactly bytes from the stream This method is meant for internal usage by the bundle2 protocol only. They directly manipulate the low level stream including bundle2 level instruction. Do not use it to implement higher-level logic or methods.)r readexactlyr)r@sizer#r#r$rszunpackermixin._readexactN)rWrXrYrZrArrr#r#r#r$r s  r cCs|dur t|d}|dd|dd}}|dkr+|d||fttdt|}|dur=ttd||||}t|d ||S) z7return a valid unbundler object for a given magicstringNrsHGs6error: invalid magic: %r (version %r), should be 'HG' snot a Mercurial bundlesunknown bundle version %ssstart processing of %s stream) r rrr Abortr formatmaprLr')r!r magicstringmagicversionunbundlerclassr|r#r#r$ getunbundlers    r!csleZdZdZdZfddZejddZddZ d d Z d d Z dddZ ddZ ddZddZZS)ryz|interpret a bundle2 stream This class is fed with a binary stream and yields parts through its `iterparts` methods.rcs.||_tjd|_d|_tt||dS)z.rNr)rsortdictrrSrD _processparam)r@ paramsblockr5rr#r#r$r)?s    zunbundle20._processallparamscCs|std|ddttjvrtd|zt|}Wn!tyA|ddr:t |j d|YdSt j |fdw||||dS)aprocess a parameter, applying its effect if needed Parameter starting with a lower case letter are advisory and will be ignored when unknown. Those starting with an upper case letter are mandatory and will this function will raise a KeyError when unknown. Note: no option are currently supported. Any input will be either ignored or failing. zempty parameter namerrznon letter first character: %ssignoring unknown parameter %s)r5N) r*rrrrb2streamparamsmapr2KeyErrorislowerr'r!r r)r@rrrr#r#r$r.Ks   zunbundle20._processparamc csL|jVdt|vs J|td}|dkrtd||rY||}||g}|dD]}|dd\}}| dkrG| |q2d |}t tt |V|Vnt t|V|j|j|_d}|dkrttkssJ|td}t t|V|rd}n|d7}qi|tkrqi|dkrtd ||V|dksmd Sd S) aWutility to transfer a bundle2 as binary This is made necessary by the fact the 'getbundle' command over 'ssh' have no way to know then the reply end, relying on the bundle to be interpreted to know its end. This is terrible and we are sorry, but we needed to move forward to get general delta enabled. r5rr'rrrrrsnegative chunk size: %iN)rvarsrrr r(rr)rr2rDrrrSrdecompressorreaderrr _fpayloadsize flaginterrupt) r@r*r5 outparamsrkr emptycountrr#r#r$_forwardchunksesJ          zunbundle20._forwardchunksFccs~|rtnt}|j|j|j|_t|jd|}|dur7||j||j}|V| |}|dus t|jddS)z'yield all parts contained in the streams!start extraction of bundle2 partsNsend of bundle2 stream) seekableunbundlepart unbundlepartr5rr4rr'r!_readpartheaderr)r@seekablecls headerblockrr#r#r$rs  zunbundle20.iterpartscCF|td}|dkrtd|t|jd||r!||SdS)Qreads a part header size and return the bytes blob returns None if emptyrnegative part header size: %ispart header size: %iNrrr r(r'r!rr@ headersizer#r#r$r= zunbundle20._readpartheadercCs |j|jSr1)r5r"r?r#r#r$ compressedszunbundle20.compressedcCst|jdr |jSdS)zclose underlying filecloseN)r safehasattrrrIr?r#r#r$rIs zunbundle20.close)F)rWrXrYrZrrAr propertycacher5r)r.r:rr=rHrI __classcell__r#r#r%r$ry!s    1rys20csfdd}|S)z/register a handler for a stream level parametercstvsJ|t<|Sr1)rr0)r6rr#r$ decorators z'b2streamparamhandler..decoratorr#)rrNr#rMr$b2streamparamhandlers rOrcCsB|tjjvrtj|f|fdtj||_|durd|_dSdS)z)r&rXrWrrr)r@r?r#r#r$__repr__szbundlepart.__repr__cCs.t|jdr J||j|j|j|j|jS)zreturn a copy of the part The new part have the very same content but no partid assigned yet. Parts with generated data cannot be copied.next) rrJrr&rrUrVrTrr?r#r#r$r szbundlepart.copycC|jSr1)rTr?r#r#r$rszbundlepart.datacCs|jdur td||_dS)Npart is being generated)rYr ReadOnlyPartErrorrT)r@rr#r#r$r#s   cCrOr1)rKrUr?r#r#r$rZ)rzbundlepart.mandatoryparamscCrOr1)rKrVr?r#r#r$r[.rzbundlepart.advisoryparamscCsX|jdur td||jvrtd||j||j}|r#|j}|||fdS)zadd a parameter to the part If 'mandatory' is set to True, the remote handler must claim support for this parameter or the unbundling will be aborted. The 'name' and 'value' cannot exceed 255 bytes each. NrarS) rYr rbrXr*rFrVrUrD)r@rrrr5r#r#r$r3s     zbundlepart.addparamc cs|jdur tdd|_|jr}d|jg}|js|dt|j}t|j }|s-|rI|d|r;|d||rD|d||d|j sR|d nt |j d s`t |j d rf|d n |d t|j |d| d||jr|j}n|j}t|dt|j|fttt||tt|jg}|j}|j }|ttt|t|g} |D]\} } | t| | t| q|D]\} } | t| | t| qttt| dg| R} || |D]\} } || || q|D]\} } || || q zd|} Wnty0td|wt|dt| ttt| V| Vz|D]}t|dt|ttt|V|VqJWngtyq| dty}zMt !|}| d|t"#d}d|}t$dd|fgdd}d|_ttdV|j%|dD]}|Vqt|dttdVt&||WYd}~nd}~wwt|dttdVd|_dS) Nspart can only be consumed onceFsbundle2-output-part: "%s"rrrrrs empty payloadr___next__s streamed payloads %i bytes payloadrr]s part %s: "%s"rz8Found a non-bytes trying to build bundle part header: %rsheader chunk size: %ipayload chunk size: %isbundle2-generatorexit s5bundle2-input-stream-interrupt: encoding exception %ssunexpected error: %s error:abortmessagerrrsclosing payload chunkT)'rYr rjrrrrDrSrZr[rrrJrrupperr2r%rrrr_fparttypesize_fpartid_fpartparamcountr0 TypeErrorr_payloadchunksr5 GeneratorExit BaseExceptionr forcebytestrsysexc_inforr raisewithtb)r@r!rrrr,headermanparadvparparsizesrr paramsizes headerchunkrrbexcr interpartr#r#r$rFs                                    zbundlepart.getchunksccsnt|jdst|jdr*t|j}|t}|r(|V|t}|sdSdSt|jr5|jVdSdS)zmyield chunks of a the part payload Exists to handle the different methods to provide data to a part.r_rcN)rrJr chunkbufferreadpreferedchunksizerS)r@buffrr#r#r$rms     zbundlepart._payloadchunksN)r#r#r]T)r]T)rWrXrYrZrAr^r r rsetterrZr[rrrmr#r#r#r$rs(        nrrgcs0eZdZdZfddZddZddZZS)interrupthandlerzread one part and process it with restricted capability This allows to transmit exception raised on the producer size during part iteration while the consumer is reading a part. Part processed in this manner only have access to a ui object,cstt||||_dSr1)r#rrAr!r$r%r#r$rAs zinterrupthandler.__init__cCrA)rBrrCspart header size: %i NrDrEr#r#r$r=rGz interrupthandler._readpartheaderc Cs|jdt|jd|}|durt|jddSt|j||j}t|j}d}zzt||Wn tt fy?d}wW|sG| n|sO| ww|jddS)Ns<bundle2-input-stream-interrupt: opening out of band context s0bundle2 stream interruption, looking for a part.s"no part found during interruption.FTs<bundle2-input-stream-interrupt: closing out of band context ) r!rr'r=r<rinterruptoperationr SystemExitKeyboardInterruptr)r@r@rr~ hardabortr#r#r$__call__s6    zinterrupthandler.__call__)rWrXrYrZrAr=rrLr#r#r%r$rs  rc@s,eZdZdZddZeddZddZdS) rzpA limited operation to be use by part handler during interruption It only have access to an ui object. cCs||_d|_d|_dS)NF)r!r`ra)r@r!r#r#r$rArBzinterruptoperation.__init__cCs tdNs'no repo access from stream interruption)r rjr?r#r#r$r^s zinterruptoperation.repocCstdrrmr?r#r#r$ri sz!interruptoperation.gettransactionN)rWrXrYrZrAr r^rir#r#r#r$rs   rc cs|dd}|j}tt}|j}|j}tj}|j }||||d} t |d| | r| dkrL|| } t | | krHt tdt | | f| Vn| tkrWt||nt d| ||} t | |krut tdt | | f|| d} |r|d| | s-dSdS) zReads bundle2 part payload data into chunks. Part payload data consists of framed chunks. This function takes a file handle and emits those chunks. rrrrds6stream ended unexpectedly (got %d bytes, expected %d)snegative payload chunk size: %ss&bundle2-input: payload chunk size: %i N)rrrStructr5runpackr rr}r'rSr rrr6rr() r!fhdologr headerstructrFrrr} chunksizesr#r#r$decodepayloadchunkssL         rcsZeZdZdZfddZddZddZdd Zd d Zd d Z ddZ dddZ Z S)r<z a bundle part read from a bundlecstt||t|dot|d|_||_||_d|_d|_ d|_ d|_ d|_ d|_ d|_d|_d|_|d|_d|_dS)NseekstellrFr#)r#r<rArrJ _seekabler! _headerdata _headeroffset _initializedconsumedrrrZr[r5r _readheader _mandatory_posr@r!rtrr%r#r$rAKs$ zunbundlepart.__init__cCs&|j}|j|||}|||_|S)z+return the next byte from the header)rr)r@roffsetrr#r#r$ _fromheaderas zunbundlepart._fromheadercCr)zaread given format from header This automatically compute the size of the format to read.)rrrrrr#r#r$ _unpackheaderhs zunbundlepart._unpackheadercCsHt||_t||_t|j|_|j|jtdd|D|_dS)z7internal function to setup all logic related parameterscss|]}|dVqdS)rNr#)rrr#r#r$rwz+unbundlepart._initparams..N) rKrZr[rr-r5rgr4r)r@rZr[r#r#r$ _initparamsos  zunbundlepart._initparamsc Csb|td}|||_t|jd|j|td|_t|jdt |j|j|j k|_ |j |_|t \}}t|jd||t ||}||}tt|ddd|ddd}|d|}||d}g}|D]\} } ||| || fqvg} |D]\} } | || || fq||| t||_d|_dS) z$read the header and setup the objectrspart type: "%s"s part id: "%s"spart parameters: %iNrrT)rrirrr'r!rjrrrr2rrkr0rziprDrrr|rm_payloadstreamr) r@typesizemancountadvcount fparamsizesrxmansizesadvsizes manparamsrr advparamsr#r#r$rys.    "      zunbundlepart._readheadercCst|j|jS)z+Generator of decoded chunks in the payload.)rr!rr?r#r#r$rmszunbundlepart._payloadchunkscCs@|jrdS|d}|r|jt|7_|d}|s dSdS)zRead the part payload until completion. By consuming the part data, the underlying stream read offset will be advanced to the next part (or end of stream). Nr)rr}rrS)r@rr#r#r$rs   zunbundlepart.consumeNcCs||js||dur|j}n|j|}|jt|7_|dus*t||kr<|js9|jr9|jd|jd|_|S)zread payload dataNs*bundle2-input-part: total payload size %i T) rrrr}rrSrr!r)r@rrr#r#r$r}s   zunbundlepart.readr1) rWrXrYrZrArrrrrmrr}rLr#r#r%r$r<Hs  #r<csZeZdZdZfddZdddZddZd d Zej fd d Z dd dZ ddZ Z S)r;aA bundle2 part in a bundle that is seekable. Regular ``unbundlepart`` instances can only be read once. This class extends ``unbundlepart`` to enable bi-directional seeking within the part. Bundle2 part data consists of framed chunks. Offsets when seeking refer to the decoded data, not the offsets in the underlying bundle2 stream. To facilitate quickly seeking within the decoded data, instances of this class maintain a mapping between offsets in the underlying stream and the decoded payload. This mapping will consume memory in proportion to the number of chunks within the payload (which almost certainly increases in proportion with the size of the part). csg|_tt||||dSr1) _chunkindexr#r;rArr%r#r$rAszseekableunbundlepart.__init__rccst|jdkr|dksJd|jd|fn|t|jks(Jd|||j|d|j|d}t|j|jD] }|d7}|t|7}|t|jkr]|j||f|Vq@dS)z/seek to specified chunk and start yielding datarsMust start with chunk 0sUnknown chunk %drN)rSrrD_tellfp_seekfprr!r)r@chunknumposrr#r#r$rms  z#seekableunbundlepart._payloadchunkscCs^t|jD]%\}\}}||kr|dfS||kr*|d||j|ddfSqtd)z>for a given payload position, return a chunk number and offsetrrs Unknown chunk)rrr*)r@rrpposfposr#r#r$ _findchunks "zseekableunbundlepart._findchunkcCr`r1)rr?r#r#r$tellszseekableunbundlepart.tellcCs2|tjkr|}n2|tjkr|j|}n'|tjkr3|js)|d}|r)|d}|s"|jdd|}ntd|f||jddkrT|jsT|d}|rT|d}|sMd|krf|jddksktdtd|j|kr| |\}}t | ||_ ||}t||krttd||_dSdS)NrrgrsUnknown whence value: %risOffset out of ranges Seek failed )osSEEK_SETSEEK_CURrSEEK_ENDrr}rr*rrr|rmrrSr rr)r@rwhencenewposrinternaloffsetadjustr#r#r$rs:            zseekableunbundlepart.seekcCs |jr |j||Sttd)amove the underlying file pointer This method is meant for internal usage by the bundle2 protocol only. They directly manipulate the low level stream including bundle2 level instruction. Do not use it to implement higher-level logic or methods.sFile pointer is not seekable)rrrNotImplementedErrorr)r@rrr#r#r$rs zseekableunbundlepart._seekfpc CsT|jr(z|jWSty'}z|jtjkrd|_nWYd}~dSd}~wwdS)a,return the file offset, or None if file is not seekable This method is meant for internal usage by the bundle2 protocol only. They directly manipulate the low level stream including bundle2 level instruction. Do not use it to implement higher-level logic or methods.FN)rrrIOErrorerrnoESPIPE)r@er#r#r$r&s   zseekableunbundlepart._tellfp)r)rWrXrYrZrArmrrrrrrrrLr#r#r%r$r;s     r;)sabortsunsupportedcontents pushracedpushkey)shttpshttps)sheads)sv2) r bookmarksserrorlistkeysrdigestsremote-changegroup hgtagsfnodesphasesstreamFcCs|dvr tdt}ttt||d<t |tj r-tddtj D}||d<|r3d|d<|j d d }|d krBd |d <d|j ddvrP|d|d krm|j jd ddd}|j d d}|rh|sm|d|S)a_return the bundle2 capabilities for a given repo Exists to allow extensions (like evolution) to mutate the capabilities. The returned value is used for servers advertising their capabilities as well as clients advertising their capabilities to servers as part of bundle2 requests. The ``role`` argument specifies which is which. )sclientservers&role argument must be client or serverrcss|]}d|VqdS)sV%iNr#rr#r#r$rZrzgetrepocaps.. obsmarkersr#spushbackrsconcurrent-push-modes check-related)srelateds checkheadsrrslegacy.exchanges uncompressedT) untrustedsbundle2.streamr)r rjrr rKrr supportedincomingversionsr isenabled exchangeoptformatsr!config configlistpopr)r^ allowpushbackrolersupportedformatcpmodestreamsupportedfeaturesupportedr#r#r$ getrepocapsIs0    rcCs2|d}|s |dkr iSt|d}t|S)z0return the bundle capabilities of a peer as dictrpr])capablerrr)remoterawcapsblobr#r#r$ bundle2capsvs  rcCs|dd}dd|DS)zIextract the list of supported obsmarkers versions from a bundle2caps dictrr#cSs&g|]}|drt|ddqS)VrN)r intrcr#r#r$rs&z%obsmarkersversion..rL)robscapsr#r#r$obsmarkersversions rc  Cs|drt||d|} t|| ||||| dS|ds$td|i} d|vr.d| d<t|| } | || t||| |||| } tj || ||d S) Nr01)vfs compressionrrsunknown bundle type: %s obsolescence)sV1rr) r r makechangegroup writebundler rjrr_addpartsfromoptsr writechunks)r!r^refilename bundletypeoutgoingoptsrrrrrbundle chunkiterr#r#r$writenewbundles*    rc Csv|ddrU|d}|durt|}t||||}|jd|d}|d|jd|jvr>|jdd|jdd d |d rU| d |j rU|jd dt j d d t j|jvra|dd|dd rnt||dd|ddrzt||||ddrt||||dd r|j|j} t|| |ddd |d d rt ||j} t | } |jd| ddSdS)NrTs cg.versionrversionclcount nbchanges%dFrrs%ln and secret() targetphase exp-sidedatarosstreamv2)streamstagsfnodescachesrevbranchcachersobsolescence-mandatory phase-heads)rLr safeversionrrrrrextrasrevs ancestorsofr secretrREPO_FEATURE_SIDE_DATAfeaturesaddpartbundlestream2addparttagsfnodescacheaddpartrevbranchcacheobsstorerelevantmarkersmissingbuildobsmarkerspartsubsetphaseheads binaryencode) r!r^bundlerrerr cgversionrr obsmarkers headsbyphase phasedatar#r#r$rsJ               rcCs^t|}g}|jD]}|j|dd}|r|||gq |r-|jdd|ddSdS)NF)computemissingrr]r)rhgtagsfnodescache unfilteredrgetfnodeextendrr)r^r rcachernodefnoder#r#r$rs rc st|}|j}tdd|jD]}|||\}}|||qfdd}|j d|dddS)NcSs ttfSr1)rWr#r#r#r$rus z'addpartrevbranchcache..c3sttD]0\}\}}t|}tt|t|t|V|Vt|D]}|Vq't|D]}|Vq1qdSr1)ritemsr fromlocal rbcstructpackrS)branchnodesclosed utf8branchr branchesdatar#r$generates   z'addpartrevbranchcache..generatecache:rev-branch-cacheFr) revbranchcacher changelog collections defaultdictr  branchinforevrFr) r^r rrclrrrIr$r#r"r$rs    rcCs"dd|D}tdt|S)NcSsg|]}|dkr|qS)ssharedr#)rreqr#r#r$rz+_formatrequirementsspec..r)rrrr)rr#r#r$_formatrequirementsspecsr/cCst|}dtd|f}|S)Ns%s%ss requirements=)r/rr)rr5r#r#r$_formatrequirementsparamssr0cCs(d}|jrdddt|jD}|S)z\Formats a repo's wanted sidedata categories into a bytestring for capabilities exchange.r]rcss|]}t|VqdSr1)rrrr#r#r$rs  z0format_remote_wanted_sidedata..)_wanted_sidedatarr)r^wantedr#r#r$format_remote_wanted_sidedatas  r3cCs|d}t|S)Nexp-wanted-sidedata)rread_wanted_sidedata)rsidedata_categoriesr#r#r$read_remote_wanted_sidedata%s r7cCs|r t|dStS)Nr)rWr) formattedr#r#r$r5*sr5c Ks|ddsdSt|stjtdtddd|_|d}|d}|jdd }|s0|r9|s9ttd d}|j rTt |j }|sLttd |j j |vrTd }t ||||\}} } t|} t| } |jd | d} | jdd| d d| jdd|d d| jd| d ddS)NrFs<stream data requested but server does not allow this featuresrwell-behaved clients should not be requesting stream data from servers not advertising it; the client may be buggyhint includepats excludepats experimentalsserver.stream-narrow-cloness,server does not support narrow stream clonessPserver has obsolescence markers, but client cannot receive them via stream cloneTstream2r bytecountrr filecount requirements)rLrallowservergenerationr rrrr!rrrr_version generatev2streamed_requirementsr/rr) r r^r}r;r< narrowstreamincludeobsmarkersremoteversions filecount bytecountitrrr#r#r$r0sN        rTcCsL|sdSt|j}t|}|durtdtj|d|d}|jd||dS)zadd an obsmarker part to the bundler with No part is created if markers is empty. Raises ValueError if the bundler doesn't support any known obsmarker format. Ns0bundler does not support common obsmarker formatT)rrr)rrr commonversionr* encodemarkersr)r markersrrHrrr#r#r$r is  r c s|dkr5t|}|||jdd}|djdjvr0|jddjddd |} n9|d us;Jjd krGtt d t |\} | t j j vr\tt d | t j | fdd} | } tj|| ||dS)zWrite a bundle file and return its filename. Existing files will not be overwritten. If no filename is specified, a temporary file is created. bz2 compression can be turned off. The bundle file will be deleted in case of errors. rrrrrrrFrNrs.old bundle types only supports v1 changegroupss#unknown stream compression type: %sc3s(VD]}|Vq dSr1)rr)rr compenginerrtr#r$rs zwritebundle..chunkiterr)rrrrrrrr rr bundletypesrrrQrr r) r!rrrrrrrrrcompr#rOr$rzs,         rcCsdd|jdD}d}d}|D]}|dkrd}n|dkr%||d7}q|dkr/||d7}q|dkr:d|}|S|dkrBd|}|S)z:logic to combine 0 or more addchangegroup results into onecSsg|]}|ddqS)rrr)rrr#r#r$rr.z-combinechangegroupresults..rrrrg)r_)r~results changedheadsresultrr#r#r$combinechangegroupresultss$ rWr)rrrr4 treemanifestrc Csnddlm}|}|jdd}t||d}d}d|jvr't|jd}d|jvr_t |j s_t |j j dkrAt td |j jtj||j j|j j|j j|j j_t|j i}|jd }|durqt||d <|jd } t| |d <t||||jdfd|i|} |jdur|jjddd} | jdt |j!dd| jdd| dd|"rJdS)z$apply a changegroup part on the repor) localreporrNrrXrs[bundle contains tree manifests, but local repo is non-empty and does not use tree manifestsr targetphaser4r6rp expectedtotalreply:changegroupFrrr%i)#rYrir5rLr r!rristreemanifestr^rSr'r rrrrFTREEMANIFEST_REQUIREMENTresolvestorevfsoptionsr!rsvfsoptionswritereporequirementsr5r{rer`rrrrrr}) r~inpartrYrtunpackerversionr nbchangesets extrakwargsrZremote_sidedatarrr#r#r$handlechangegroupsV        rj)rqsizercCsg|]}d|qS) digest:%sr#)rr8r#r#r$rsrrc Csz|jd}Wntyttddwt|}|jtdvr.ttd|jz t |jd}Wnt yGttddtyVttddwi}|j dd D]"}d |}z|j|}Wntyttd|w|||<qbt t|j|||} |} d d lm} | |jj| |} t| tjsttd t|t|| | |jd } |jdur|jd}|jdt |j!dd|jdd| ddz| "Wntjy}zttdt||j#fd}~ww|$r JdS)aapply a bundle10 on the repo, given an url and validation information All the information about the remote bundle to import are given as parameters. The parameters include: - url: the url to the bundle10. - size: the bundle10 file size. It is used to validate what was retrieved by the client matches the server knowledge about the bundle. - digests: a space separated list of the digest types provided as parameters. - digest:: the hexadecimal representation of the digest with that name. Like the size, it is used to validate what was retrieved by the client matches what the server knows about the bundle. When multiple digest types are given, all of them are checked. rqs&remote-changegroup: missing "%s" paramrs+remote-changegroup does not support %s urlsrks0remote-changegroup: invalid value for param "%s"rr]rlr)exchanges%s: not a bundle version 1.0rpNr\rFrrr]sbundle at %s is corrupted: %s)%r5r1r rrrrschemerrr*rLrr digestcheckeropenr!rir^rm readbundler^rxr cg1unpacker hidepasswordr{rer`rrrrrvalidater"r})r~reraw_url parsed_urlrdigeststyprr real_partrtrmrrrrr#r#r$handleremotechangegroupsx              rzr\)rrcCs4t|jd}t|jd}|jdd|i|dS)Nrrrrr5r_rF)r~rerreplytor#r#r$handlereplychangegroupSsr}scheck:bookmarksc Cst|j|}d}d}d}|D]9\}}|jj|}||krH|dur+||t|f} n|dur8||t|f} n ||t|t|f} t| qdS)zcheck location of bookmarks This part is to be used to detect push race regarding bookmark, it contains binary encoded (bookmark, node) tuple. If the local state does not marks the one in the part, a PushRaced exception is raised s]remote repository changed while pushing - please try again (bookmark "%s" move from %s to %s)sbremote repository changed while pushing - please try again (bookmark "%s" is missing, expected %s)sfremote repository changed while pushing - please try again (bookmark "%s" set on %s, expected missing)N)r binarydecoder^ _bookmarksrLrr PushRaced) r~rebookdata msgstandard msgmissingmsgexistbookr currentnodefinalmsgr#r#r$handlecheckbookmarksZs,  rs check:headscCs||d}g}t|dkr|||d}t|dks |r!J|jddr,|t|t|jkr.runhooksrecords)sbookmarksnodesunknown bookmark mode: %s)r)rr~r^r!rrdrLrirrbr rrDrrr isdivergentrr r applychanges _afterlockr_rFrj) r~rechanges pushkeycompat bookmarksmodert bookstorerrrbrrrr#rr$handlebookmark7 sX         rrcCs$t|}t|j|j|dS)z%apply phases from bundle part to repoN)r r~ updatephasesr^rri)r~rerr#r#r$ handlephases{ s rrcC4t|jd}t|jd}|jdd|i|dS)(retrieve the result of a pushkey requestrrrNr{r~rerrJr#r#r$handlepushkeyreply rrcCs|}|}|jddr|jdt||js*|jjj r*|jj ddS|jj ||}|j |j dd|i|jdurc|jd}|jd t|jd d |jdd |d d dSdS) z&add a stream of obsmarkers to the repor=sobsmarkers-exchange-debugs&obsmarker-exchange: %i bytes received s3ignoring obsolescence markers, feature not enabled Nrrreply:obsmarkersrFrr])rir}r!r writenoi18nrSrr^rreadonlyr mergemarkersinvalidatevolatilesetsr_rFr`rrrrr)r~rert markerdatarrr#r#r$handleobsmarker s*    rr)rrcCr)rrrrNr{rr#r#r$handleobsmarkerreply rrrcCs|jddr |t|j}d} |d}|d}t|dks,t|dkr3|j dn | |||d7}q| |j d|d S) z|Applies .hgtags fnodes cache entries to the local repo. Payload is pairs of 20 byte changeset nodes and filenodes. r=rrTrs1ignoring incomplete received .hgtags fnodes data rs'applied %i hgtags fnodes cache entries N) r!rrirrr^rr}rSrsetfnodewrite)r~rerrrrr#r#r$handlehgtagsfnodes s     rs>IIIr%cCsdS)zLegacy part, ignored for compatibility with bundles from or for Mercurial before 5.7. Newer Mercurial computes the cache efficiently enough during unbundling that the additional transfer is unnecessary.Nr#rr#r#r$ handlerbc rvrspushvarscCsL|jddr$i}|jD]\}}|}d|}|||<q ||dSdS)z5unbundle a bundle2 containing shellvars on the serverspushspushvars.serversUSERVAR_N)r!rr[rhrk)r~rrbrrr#r#r$bundle2getvars s rr>)rAr@r?cCst|jd}|r|dng}t|jd}t|jd}|j}t|r/td}t ||j dt |||||dS)NrArr@r?s1cannot apply stream clone to non empty repositorysapplying stream bundle )rrr5rrr^rSrr rr!rr applybundlev2)r~rrrIrJr^rr#r#r$handlestreamv2bundle s  rcCst}|j} |d|D] } || | q |rItj|||||d} | j|jht |dddd} |j d| d} | d|t |rI| d d tj|jvr_| d d t|}| d ||S) aKgenerates bundle2 for widening a narrow clone bundler is the bundle to which data should be added repo is the localrepository instance oldmatcher matches what the client already has newmatcher matches what the client needs (including what it already has) common is set of common heads between server and client known is a set of revs known on the client side (used in ellipses) cgversion is the changegroup version to send ellipses is boolean value telling whether to send ellipses data or not returns bundle2 of the data required for extending s::%ln) oldmatchermatcher fullnodesFs narrow_widen)r'rrrrXrorr4)rWr'rrFrr getbundlerr$nullidrrrrr_rrrr3)r r^r newmatchercommonknownrellipses commonnodesr,rSpackercgdatarr2r#r#r$ widen_bundle s8      rr r1)NNr])FN)NNN)T)rZ __future__rrr(rrrerrrqi18nrrrrr^rr r r r r rrrrrrrrutilsrr interfacesrurlerrrrrrrrrrirjr5rkr~compiler(r%r'r-r0r3r:objectr;r\ RuntimeErrorrlrnrrrzrr{rrrrrQbundlepriorityrr r!ryrr0rOrRrr6rrrr<r;rKrDIGESTSkeysrrrrrrrrr/r0r3r7r5rr rrWrj_remotechangegroupparamsrzr}rrrrrrrrrrrrrrrrrrrrrrrrrrr#r#r#r$sH @   04  G% * | !  l48x| -  %5  9 - 7 N  %             C