o ]Lbg@sddlmZddlZddlmZddlmZmZmZddl m Z dZ dZ d Z d Zd d Z dddZGdddejZedZdddZdS))absolute_importN)_)errorpycompatutil) stringutilsalls prefinalizes postfinalizecsfdd}|S)Ncs*|jdkr td|g|Ri|S)Nrs;cannot use transaction when it is already committed/aborted)_countrProgrammingError)selfargskwdsfunc7/usr/lib/python3/dist-packages/mercurial/transaction.py_active"s zactive.._activer)rrrrractive!s rTc Cstt|D]i\}} | s|sO|o|df|v} z&||d| d} | | kr4ttd|| | f| | | Wqt yN|td|wz| |Wqt t fyq} z | j t j krgWYd} ~ qd} ~ wwg} |D]\}}}}||vr|r|d||f||}zp|r|r||}||}|o||f|v} ztj||| d| |WnHt y}zt|}|td||fWYd}~n,d}~ww|p|}z| |Wnt t fy} z | j t j krWYd} ~ nd} ~ wwWqvt t tjfy|sYqvwd|}||r | || |z| D]}||r5| |q(WdSt t tjfyHYdSw) Na) checkambigsBattempted to truncate %s to %d bytes, but it was already %d bytes sfailed to truncate %s s.couldn't handle %s: unknown cache location %s sfailed to recover %s (%s) %s.backupfiles)sorteddictitemstellrAbortrtruncatecloseIOErrorunlinkOSErrorerrnoENOENTjoinrcopyfileappendr forcebytestrexists)journalreportopenervfsmapentries backupentriesr!checkambigfilesforfpinst backupfileslbcvfsfilepath backuppathexce_msgtargetrrr _playback,s                    r?c@seZdZ       dGddZddZddZed d Zed d Z ed dZ eddZ ddZ edHddZ ddZedIddZe   dJddZedd Zdefd!d"Zed#d$Zed%d&Zed'd(ZedKd)d*Zd+d,Zd-d.Zd/d0Zed1d2Zed3d4Zed5d6Zed7d8Zed9d:Zed;d<Z ed=d>Z!ed?d@Z"edAdBZ#dCdDZ$dEdFZ%dS)L transactionN c CsJd|_d|_||_||_|}||d<||_||_i|_t|_ ||_ ||_ g|_ | dur2dd} | |_ t|_| rA|j| | g|_i|_i|_||j d|_g|_i|_d|j |_||jd|_|jd t|dur||j |d @||j|d @i|_i|_d |_i|_i|_ |dur||j d <i|_!i|_"dS) a Begin a new transaction Begins a new transaction that allows rolling back writes in the event of an exception. * `after`: called after the transaction has been committed * `createmode`: the mode of the journal file that will be created * `releasefn`: called after releasing (with transaction and result) `checkambigfiles` is a set of (path, vfs-location) tuples, which determine whether file stat ambiguity should be avoided for corresponded files. rrNcSsdSNr)trsuccessrrrsz&transaction.__init__..sw+rw%d iFs 001-userhooks)#r _usages_report_openercopy_vfsmap_after _offsetmapset _newfiles_journal _undoname_queue _releasefn_checkambigfilesupdate_nameschangeshookargsopen_file_backupentries _backupmap_backupjournal _backupsfilewriteversionchmod_filegenerators_pendingcallback _anypending_finalizecallback_validatecallback_postclosecallback_abortcallback) r r+r,r- journalnameundonameafter createmode validator releasefnr0namerrr__init__xsP    ztransaction.__init__cCsd|j}d||j|jfS)N/z*)r%rWr rHr rprrr__repr__s ztransaction.__repr__cCs|jr |dSdSrB)rQ_abortr rrr__del__s ztransaction.__del__cCs |jduSrBrfrvrrr finalizeds ztransaction.finalizedcCs|jgdS)zdelay registration of file entry This is used by strip to delay vision of strip offset. The transaction sees either none or all of the strip actions to be done.N)rSr'rvrrr startgroupsztransaction.startgroupcCs(|j}|D] \}}|||qdS)zapply delayed registration of file entry. This is used by strip to delay vision of strip offset. The transaction sees either none or all of the strip actions to be done.N)rSpop _addentry)r qr1r2rrrendgroups  ztransaction.endgroupcCsP||jvs||jvs||jvrdS|jr |jd||fdS|||dS)z5record the state of an append-only file before updateN)rPrNr]rSr'r|r fileoffsetrrradds   ztransaction.addcCs`||jvs||jvs||jvrdS|r||j|<n|j||jd||f|jdS)z3add a append-only entry to memory and on-disk stateN%s%d )rPrNr]rr[r`flushrrrrr|s     ztransaction._addentryTrc Cs|jr d}t|||jvs||jvs||jvrdS|j|}||\}}d|j|f}| ||} | |rL| |} | | } t j | | |dnd} |||| dfdS)aWAdds a backup of the file to the transaction Calling addbackup() creates a hardlink backup of the specified file that is used to recover the file in the event of the transaction aborting. * `file`: the file path, relative to .hg/store * `hardlink`: use a hardlink to quickly create the backup s/cannot use transaction.addbackup inside "group"Ns %s.backup.%shardlinkrF)rSrr rPrNr]rLsplitrQreljoinr)r%rr&_addbackupentry) r rrlocationmsgr9dirnamefilenamebackupfilename backupfiler:r;rrr addbackups"          ztransaction.addbackupcCsB|j|t|jd|j|d<|jd||jdS)z0register a new backup entry and write it to diskr %s%s%s%d N)r\r'lenr]r_r`r)r entryrrrr:s ztransaction._addbackupentrycCs||d|dfdS)zregister a temporary transaction file Such files will be deleted when the transaction exits (on both failure and success). rFN)r)r tmpfilerrrr registertmpAsztransaction.registertmprFcCs|||||f}||j|<dS)a]add a function to generates some files at transaction commit The `genfunc` argument is a function capable of generating proper content of each entry in the `filename` tuple. At transaction close time, `genfunc` will be called with one file object argument per entries in `filenames`. The transaction itself is responsible for the backup, creation and final write of such file. The `genid` argument is used to ensure the same set of file is only generated once. Call to `addfilegenerator` for a `genid` already present will overwrite the old entry. The `order` argument may be used to control the order in which multiple generator will be executed. The `location` arguments may be used to indicate the files are located outside of the the standard directory for transaction. It should match one of the key of the `transaction.vfsmap` dictionary. The `post_finalize` argument can be set to `True` for file generation that must be run after the transaction has been finalized. Nrc)r genid filenamesgenfuncorderr post_finalizerrrraddfilegeneratorJs%ztransaction.addfilegeneratorcCs||jvr |j|=dSdS)z=reverse of addfilegenerator, remove a file generator functionNr)r rrrrremovefilegeneratorrs  ztransaction.removefilegeneratorc Cs"d}|tkr d}}n|tk}|tk}tt|jD]s\}}d}|\}} } } } |r-| r-q|r2| s2q|j| } g}zK| D]+}||7}|rN|j|| dd}n|j || d|| f|j v}| | |dd|dq<| ||D]}| qn|dd=W|D]}| q}q|D]}| qw|S)NFT)rrF) atomictempr) GEN_GROUP_ALLGEN_GROUP_POST_FINALIZEGEN_GROUP_PRE_FINALIZErr iteritemsrcrLrrrUr'rdiscard)r suffixgroupany skip_postskip_preidrrrrrrr9filesrprr1rrr_generatefilesxsF      ztransaction._generatefilescCs||jvrdS|j|SNr)rPrNget)r rrrr findoffsets  ztransaction.findoffsetcCsD|jdg}|jD]}|d\}}||t|fq |S)Nr)r[seek readlinesrr'int)r r.r6rtroffsetrrr readjournals ztransaction.readjournalcCs||jvr|s dS|j|||j|<n||jvr-|s'|j|=|j|n ||j|<nt||jd||f|jdS)zn replace can only replace already committed entries that are not pending in the queue Nr)rPremoverNrKeyErrorr[r`rrrrrreplaces     ztransaction.replacecCs,|jd7_|jd7_|j||S)Nr)r rHrWr'rsrrrnests ztransaction.nestcCsP|jdkr |jd8_|jr|j|jdkr$|jdkr&|dSdSdS)Nrr)r rHrWr{rurvrrrreleases   ztransaction.releasecCs |jdkSr)r rvrrrrunnings ztransaction.runningcC||j|<dS)zadd a callback to be called when the transaction is pending The transaction will be given as callback's first argument. Category is a unique identifier to allow overwriting an old callback with a newer callback. N)rdr categorycallbackrrr addpendingsztransaction.addpendingcCsLt|j}|D]}|j||}|jp||_q|j|jddO_|jS)zpwrite pending file to temporary version This is used to allow hooks to view a transaction before commits.pending)r)rrdr{rer)r categoriescatrrrr writependings ztransaction.writependingcCs ||jvS)z0check is a callback already exist for a categoryrxr rrrr hasfinalizes ztransaction.hasfinalizecCr)zadd a callback to be called when the transaction is closed The transaction will be given as callback's first argument. Category is a unique identifier to allow overwriting old callbacks with newer callbacks. Nrxrrrr addfinalize ztransaction.addfinalizecCr)zadd or replace a callback to be called after the transaction closed The transaction will be given as callback's first argument. Category is a unique identifier to allow overwriting an old callback with a newer callback. N)rhrrrr addpostcloserztransaction.addpostclosecCs|j|dS)z1return a postclose callback added before, or NoneN)rhrrrrr getpostclosesztransaction.getpostclosecCr)aadd a callback to be called when the transaction is aborted. The transaction will be given as the first argument to the callback. Category is a unique identifier to allow overwriting an old callback with a newer callback. N)rirrrraddabortrztransaction.addabortcCr)zadds a callback to be called when validating the transaction. The transaction will be given as the first argument to the callback. callback should raise exception if to abort transactionN)rgrrrr addvalidatorsztransaction.addvalidatorc Cs|jdkrAt|jD] }|j||q d|_|jtd|jr8|j}i|_t|}|D]}|||q,|js d|_|jtd|jd8_|jdkrOdS|j|j |j D]R\}}}}||j vrs|rs| d||fq\|j |} |s|r| |rz| |Wq\tttjfy} z|s| d| || fWYd} ~ q\d} ~ wwq\i|_t|_||jr|d|_|j|jr|j|j|j|jr|j|j|j D]T\}} }}||j vr|r| d||fq|j |} |r7| |r7z| |Wqtttjfy6} z|s | d| || fWYd} ~ qd} ~ wwqg|_ d|_||dd|_t|j}|D] }|j||qNd|_dS) zcommit the transactionrN)rrs.couldn't remove %s: unknown cache location %s scouldn't remove %s: %s -couldn't remove %s: unknown cache location%s T)r rrgrrrfrr[rr_r\rLrIr)r!r r"rrr%rNrOrP _writeundorMrJisfiler^rQrTrh) r r callbacksrrr6r1r7r8r9r4_frrrr's            ztransaction.closecCs |dS)zabort the transaction (generally called on error, or when the transaction is not explicitly committed before going out of scope)N)rurvrrrabortys ztransaction.abortc Cs|jdurdSd|j}|j|d}|dt|jD]]\}}}}|s&q|s+d}nD||jvr<|r<|d||fq|j|}||\} } | |j sRJ| | |j |jd} | | | }t j||||dd |d ||||fq|dS) z4write transaction data for possible future undo callNrrFrGrrrTrr)rRrJrZr`rar\rLrIr startswithrQrrrr&r%r) r undo_backup_pathundobackupfiler6r1r7r8ur9baserpunamerrrrs2     ztransaction._writeundoc Cs|}d|_d|_|j|jz|s=|js=|jr$|j |j|j r.|j |j Wd|_ | |dd|_ dS| t dz-t|jD] }|j||qJd|_t|j |j |j|j||jd|jd| t dWn%ty}z| t d| t dt|WYd}~nd}~wwWd|_ | |dd|_ dSWd|_ | |dd|_ dSd|_ | |dd|_ w)NrFstransaction abort! r0srollback completed s(rollback failed - please run hg recover s(failure reason: %s) )rr rHr[rr_r\r^rJr!rQrTrIrrrir?rLrU BaseExceptionrr()r r.rr<rrrrusb           ztransaction._abort)NNNNNNrA)Tr)r)rrF)rA)&__name__ __module__ __qualname__rqrtrwpropertyryrrzr~rr|rrrrrrrrrrrrrrrrrrrrrrrrrurrrrr@ws _     !  ' ,             Q  r@s8journal was created by a different version of Mercurial c CsBg}g}|| }|}Wdn1swY|D](} z| d\} } || t| fWqtyG|tdt| Yqwd|} | | r|| }|}|r|ddd} | dt kro|t n%|ddD]}|r|dd}|d\} } }}|| | |t |fqut |||||||d dS) aRolls back the transaction contained in the given file Reads the entries in the specified file, and the corresponding '*.backupfiles' file, to recover from an incomplete transaction. * `file`: a file containing a list of entries, specifying where to truncate each file. The file should contain a list of fileoffset pairs, delimited by newlines. The corresponding '*.backupfiles' file should contain a list of filebackupfile pairs, delimited by . `checkambigfiles` is a set of (path, vfs-location) tuples, which determine whether file stat ambiguity should be avoided at restoring corresponded files. Nrs couldn't read journal entry %r! rrrs%drr)rZrrr'r ValueErrorrrbytestrr)raBAD_VERSION_MSGboolr?)r,r-rr+r0r.r/r3linesr6r1r2 backupjournalverliner7r8rrrrollbacksJ         r)TNrB) __future__rr#i18nrrrrutilsrrarrrrr? transactionalr@rrrrrrs,    KV