o ]Lb@s@s\ddlZddlZddlZddlZddlmZddlmZmZm Z m Z m Z m Z m Z mZmZmZmZmZmZmZmZmZddlmZddlmZmZmZmZmZddlm Z dd lmZm!Z!d0d d Z"d0d dZ#d0ddZ$ddZ%ddZ&ddZ'ddZ(ddZ)ddZ*ddZ+dd Z,d1d!d"Z- d1d#d$Z.d%d&Z/d'd(Z0d)d*Z1d+d,Z2 -d2d.d/Z3dS)3N)nullrev)COMP_MODE_PLAINENTRY_DATA_COMPRESSED_LENGTHENTRY_DATA_COMPRESSION_MODEENTRY_DATA_OFFSETENTRY_DATA_UNCOMPRESSED_LENGTHENTRY_DELTA_BASEENTRY_LINK_REV ENTRY_NODE_IDENTRY_PARENT_1ENTRY_PARENT_2 ENTRY_SIDEDATA_COMPRESSED_LENGTHENTRY_SIDEDATA_COMPRESSION_MODEENTRY_SIDEDATA_OFFSETREVIDX_ISCENSOREDREVLOGV0REVLOGV1)_)errormdiffpycompat revlogutilsutil) storageutil) constantsdeltasc Cs|jtjks J|jddlm}||}td|id}|j|j|j |j ddd}|j|_|j |_ |j |_ |j |_ |D]i}||}||\} } ||krw|||||| | |tj||tkrvtd} td } tj| | d q?||r||tkrtd } t| ||} n||} || |||| | |||q?|j|jd d |js|j|j d d |j!|j|j|js|j!|j |j |"|#dS)z*censors a revision in a "version 1" revlogr)revlogcensoredrs tmpcensoredT)targetradixpostfix censorables0censored revision stored as delta; cannot censorsEcensoring of revlogs is not fully implemented; please report this bug)hints:cannot censor due to censored revision having delta storedsstore)locationN)$_format_versionrrrrevrpackmetaopenerr!r" _format_flags _generaldelta _parse_indexrevsnodeparentsaddrawrevisionlinkrevr deltaparentrrrAbort iscensored_chunkrawdataflags addbackup _indexfile_inline _datafilerename clearcaches _loadindex)rltr censornode tombstoner censorrevnewrlr)r0p1p2mhrawtextrL?/usr/lib/python3/dist-packages/mercurial/revlogutils/rewrite.py v1_censor6sl          rNcCsF|jtks J|j|jtksJ|j||h}t||||dS)z*censors a revision in a "version 2" revlogN)r'rrr) _rewrite_v2)rrBrCrD censor_revsrLrLrM v2_censors rQc CsF|jtks J|j|jtksJ|j|j}|j}td|id}t|}|j|}|jj|}|t d?} | |} t j ddP} t |||| } t||| | } | /}|\}}}}}t|t|D]}||vrrt|||||qct||||| | qcWdn1swYWdn1swY|jddddS) a[rewrite a revlog to censor some of its content General principle We create new revlog files (index/data/sidedata) to copy the content of the existing data without the censored data. We need to recompute new delta for any revision that used the censored revision as delta base. As the cumulative size of the new delta may be large, we store them in a temporary file until they are stored in their final destination. All data before the censored data can be blindly copied. The rest needs to be copied as we go and the associated index entry needs adjustement. r rsw+bmodeNT) transaction stripping)r'rrindex_docketrr*min entry_sizersidedata_cut_offrunnamedtempfile_precompute_rewritten_delta_setup_new_filesrangelen_rewrite_censor_rewrite_simplewrite)rrBrPrD old_indexdocketfirst_excl_revfirst_excl_entry index_cutoff data_cutoffsidedata_cutoff tmp_storagerewritten_entries all_files open_files old_data_fileold_sidedata_filenew_index_file new_data_filenew_sidedata_filer)rLrLrMrOsj     0rOc Cs@t|}i}t|}|j}t|t|D]u}||vrq||} | t|vr*q| tdkr9t ddt f||<q|j ||d} t j | t|| t|| t| gt| d| td@d} |j| |||d} |jj} t| | \}} |}|| jd|}| j|||f||<qWd|S1swY|S)uCompute new delta for revisions whose delta is based on revision that will not survive as is. Return a mapping: {rev → (new_base, data_start, data_end, compression_mode)} r)_dfN)r0rGrHbtexttextlen cachedeltar9)excluded_bases target_revr)r deltacomputerrY _segmentfile _open_readr_r`r r rrr8r revisioninfor r0r rr finddeltainforXdefault_compression_headerdelta_compressiontellrcdatabase)rrd excluded_revsrkdcrlrfdfhr)entrytextinfod default_comp comp_modestartendrLrLrMr]sH         $$r]c s|j}|j|}|j||j||j||j||j|t j |dt j dt j d|j ||j ||j ||_ |_ |_||j|dtjfdd}|S)a+ return a context manager to open all the relevant files: - old_data_file, - old_sidedata_file, - new_index_file, - new_data_file, - new_sidedata_file, The old_index_file is not here because it is accessed through the `old_index` object if the caller function. )nb_bytes)rec 3sTtd}td|}tdg}tdR}td=}|dtj|ks.J|dtj|ks=J|dtj|ksLJ|||||fVWdn1s^wYWdn1smwYWdn1s|wYWdn1swYWddSWddS1swYdS)Nrbzr+br)openseekosSEEK_ENDr)rorprqrrrsrirhnew_data_filepathnew_index_filepathnew_sidedata_filepathold_data_filepathold_sidedata_filepathrjrLrMall_files_openerBs>    "z*_setup_new_files..all_files_opener)rXr+joinindex_filepath data_filepathsidedata_filepathrqrrrsrcopyfile register_file index_enddata_end sidedata_endr?r@ contextlibcontextmanager)rrhrirjreold_index_filepathrrLrrMr^s2 r^cCs|\}}}} } ||} | td@} | td?} ||vr2|| | t}||}| t}| t}n||\}}}}||}||||}| }| || t}| }d|krp| t }||||}| || t }| t }||ksJ||ft j | ||||| t| t| t| t||||d }|j||j|}||||j_| |j_| |j_dS)z@append a normal revision to the index after the rewritten one(s)rurRr r9 data_offsetdata_compressed_lengthdata_uncompressed_lengthdata_delta_baselink_rev parent_rev_1 parent_rev_2node_idsidedata_offsetsidedata_compressed_lengthdata_compression_modesidedata_compression_modeN)rrrreadr rrrcrrr rrrr r rr rWappend entry_binaryrXrrr)rrdrmr)rlrkrorprqrrrsrr9old_data_offset new_data_sizenew_datar d_comp_moderrnew_data_offset sidedata_sizenew_sidedata_offsetold_sidedata_offset new_sidedatar sd_com_mode new_entry entry_binrLrLrMrb^st                rbcCs|\}}}}} ||} t|} |} ||| t} | t}| t}tjtj | | | || ||| t ddt t d }|j ||j |}||||j_||j_dS)z&rewrite and append a censored revisionrrN)r`rrcr r rrrrrr rrWrrrXrr)rrdrmr)rDrorprqrrrsrrrrrGrHrrrLrLrMrasD     racCsF|ddddd}t|dkrtd}||;}t||dS)N.rr/rs,cannot recognize filelog from filename: '%s')rsplitsplitr`rrr5)path path_partmsgrLrLrM _get_filename_from_filelog_indexs   rcCsddlm}||j|}|S)zAReturns the filelog for the given `path`. Stolen from `engine.py`r)filelog)r(rsvfs)reporrflrLrLrM_filelog_from_filenames rc Csddlm}||jdurd}t||jj}|j|}t |} |d|d| d<| d<|j | dd} | || | dS)zASwaps p1 and p2 and overwrites the revlog entry for `rev` in `fp`rparsersNz*repo needs to be locked to rewrite parents) purer _currentlock_lockrefrProgrammingError IndexObject index_formatrWlistpackrrc) rrAr)offsetfprrrrrpackedrLrLrM_write_swapped_parentss    rc Csddlm}|j}t|dksJ|j}|jtjkr$d|j}t ||j }|d}t d} | zt j|j||j||jd|j|dd S} |jrz|| } |D]} | |vrx| | } t||| | | || | |fq\n|jj}|D]} | |j} t||| | | || | |fqWd n1swY|j||||Wt |nt |wWd d S1swYd S) z Swaps p1 and p2 for all `to_fix` revisions of filelog `fl` and writes the new version to disk, overwriting the old one with a rename. rrrz+expected version 1 revlog, got version '%d's.tmp-parents-fixs%repaired revision %d of 'filelog %s' ) checkambigsr+rSN) rruir`_revlogr'rrrrr;runinterruptiblerrr+r _checkambigr<InlinedIndexObjectrr/_calculate_indexrrcrrsizer>r?r@ tryunlink)rrto_fixrrrAr index_file new_file_path repaired_msgrrWr)rrrLrLrM_reorder_filelog_parentssN           "rcs*fdd}fdd}t|||S)Nc jSN)rr8rLfilerevrrLrM8 z'_is_revision_affected..crr)r parentrevsrLrrLrMr9r)_is_revision_affected_inner)rrmetadata_cache full_text parent_revsrLrrM_is_revision_affected7s rcCslz|}Wn tjyYdSwt|dddk}|dur#|||<|r4|\}}|tkr4|tkr4dSdS)aMercurial currently (5.9rc0) uses `p1 == nullrev and p2 != nullrev` as a special meaning compared to the reverse in the context of filelog-based copytracing. issue6528 exists because new code assumed that parent ordering didn't matter, so this detects if the revision contains metadata (since it's only used for filelog-based copytracing) and its parents are in the "wrong" order.FNrs T)rCensoredNodeErrorbytesr)r parents_revsrrraw_texthas_metarGrHrLrLrMr?s  rc s`|jfdd}fdd}fdd}fdd}fdd}t||||||S)Nc Sr)r6rLrrArLrMr^ z,_is_revision_affected_fast..crr)r4rLrrLrMr_rcrr)r7rLrrLrMr`rcrr)r8rLrrLrMrarcrr)rrLrrLrMrbr)r _is_revision_affected_fast_inner) rrrr is_censored delta_basedeltarrrLrrM_is_revision_affected_fast\sr cCs|r d||<dS|\}}|tks|tkrdS|} || } | dur+t||||S|} t| s4| Sd} t| | krCttdtd| d| \} }}| dkr]t||||}|S| ||<| S)aGOptimization fast-path for `_is_revision_affected`. `metadata_cache` is a dict of `{rev: has_metadata}` which allows any revision to check if its base has metadata, saving computation of the full text, instead looking at the current delta. This optimization only works if the revisions are looked at in order.FN spatch cannot be decodeds>lllr) rgetrr`rr5rstructunpack)rrrrrrrrGrH delta_parentparent_has_metadatachunk header_lengthr_end_length is_affectedrLrLrMrns@   rc sh|td||t|dd}|dD]o}|s q|dd\}}t||tfdd|d D} t} | D]-} t| rSd } || | |fq@td } | t | |f;} || | | q@| | } | std } || |q|st |t| qWd n1swYWd d SWd d S1swYd S)a Fix the revisions given in the `from_report` file, but still checks if the revisions are indeed affected to prevent an unfortunate cyclic situation where we'd swap well-ordered parents again. See the doc for `debug_fix_issue6528` for the format documentation. sloading report file '%s' rrS  rc3s |] }t|VqdSr)r)binascii unhexlify).0nrrLrM s z_from_report..,,found affected revision %d for filelog '%s' s)revision %s of file '%s' is not affected s*no affected revisions were found for '%s' N)rcrrrrrsetrwarnrhexlifyr0addrsorted) rrcontext from_reportdry_runfline filenodesfilenamerexcludedrrrLrrM _from_reports:     Pr,c #sFi}t t|t D]\}}|\}}} j|s-t| jt d | jsBt jt d  jsWt jt d  fdd} fddfdd fdd}  fdd} t |fd d| | ||} | r|||f}|Vqd S) z7filter incomind deltas to repaire issue 6528 on the flysunknown parentcs tt@Sr)boolrrL)r9rLrMrrz(filter_delta_issue6528..crr)r)rL)rrrLrMrrcSrrLrL)base_revrLrMrcsfSrrLrL)p1_revp2_revrLrMrsc sdt}tdg|f}jdusJjr'jd}njd}||S)Nrr)r patchedsizerrr~_writinghandlesr< buildtext)rwrevinfofh)r/rr{r9r0p1_nodep2_noderrLrMrs    z)filter_delta_issue6528..full_textcr.rrLrL)rrLrMrr0N) rr{ enumerater`rWhas_noder LookupErrorr"rr)r) r deltas_iterrr)rlinknode deltabasesidedatarrrrrL) r/rrr{r9r0r8r1r9r2rrMfilter_delta_issue6528sf              rAFc s4ddlmtjfdd}|rt|||Sg}|tfddjD}|jtdtdt |d } d } |D]\} } | d rO| j @sPqA| t | } t| }t}i}|D]M}t|||}|rt||}||krtd }t||}t|| |f|rd }|||| fd} sr|t||qf||qf|rt|}r|| |fqAt||qA| r|tdr|rtdd}|D]\} }|dd|| fqWdn1swY| WddS1swYdS)Nrstorec 3ssr dVdS( dVWdn1s!wYWddSWddS1s9wYdSr)wlocklockrL)r&r to_reportrLrMr$-s  Pz!repair_issue6528..contextc3s2|]\}}}|dr|j@r||fVqdS).iN)endswithFILEFLAGS_FILELOG)r file_typer_srBrLrMr;s z#repair_issue6528..slooking for affected revisionssfilelogs)unittotalTrGs)paranoid check failed for '%s' at node %srFs!no affected revisions were found wbrSs%s %s r)!r(rCrrr,r datafiles makeprogressrr`rHrI incrementrrrr/r rrr!r0rr5r r"r#rrrcrrcomplete)rrr&rFr%paranoidr$report_entriesfilesprogress found_nothingrJrr*rrrraffectedslowrr0r'rL)r&rrCrFrMrepair_issue6528(s|           $rZ)rr)FNNF)4rrrr r0rrrrrrr r r r r rrrrrrri18nrr(rrrrrutilsrrrNrQrOr]r^rbrarrrrrrr rr,rArZrLrLrLrMs<  H    N R5JP3   0 @(S