a
    &0_X                     @   sH  d Z ddlmZ ddlZddlZddlmZ ddlmZm	Z	 ddl
mZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZ G dd	 d	ejZG d
d deejZ G dd deej!Z"G dd dejZ#G dd dej$Z%G dd dej&Z'dd Z(G dd de)Z*d!ddZ+dd Z,G dd de)Z-d"dd Z.dS )#zRepository class for viewing uncompressed bundles.

This provides a read-only repository interface to bundles as if they
were part of the actual repository.
    )absolute_importN   )_)nullidnullrev)bundle2changegroup	changelogcmdutil	discoveryencodingerrorexchangefilelog	localrepomanifestmdiffnodepathutilphasespycompatrevlogutilvfsc                       sX   e Zd Zdd ZdddZdd Zd fdd		Zd
d Zdd Zdd Z	dd Z
  ZS )bundlerevlogc              
   C   s:  t |}tj| || || _t| }|d | _t | _|	 D ]}|\}}}	}
}}}t|}|
 | }||
}| j|r| j| j| qB||	fD ]$}| j|st|| jtdq| j|st|| jtd| |}t|||d||| || |	|f}| j| | j| |d7 }qBd S )Nr   s   unknown parents   unknown delta base)vfsmodreadonlyvfsr   __init__bundlelen
repotiprevsetZ
bundlerevs	deltaitertellindexZhas_nodeaddrevr   LookupError	indexfiler   Zoffset_typeappend)selfopenerr)   
cgunpacker
linkmappernZ	deltadatar   p1p2ZcsZ	deltabasedeltaflagssizestartlinkpZbasereve r9   6/usr/lib/python3/dist-packages/mercurial/bundlerepo.pyr   .   sH    




zbundlerevlog.__init__Nc                 C   s<   || j krtj| |S | j| | | j| |S N)r!   r   _chunkr   seekr5   readZlength)r+   r'   Zdfr9   r9   r:   r<   c   s    
zbundlerevlog._chunkc                 C   sr   || j kr6|| j kr6| j| d }||krZ| |S n$|| j krZ|| j krZtj| ||S t| || |S )z1return or calculate a delta between two revisions   )r!   r%   r<   r   revdiffr   ZtextdiffZrawdata)r+   Zrev1Zrev2Zrevbr9   r9   r:   r@   l   s    zbundlerevlog.revdiffc                    s   |d u r|  |}d}d }g }|}|| jkrf| jrL| jd |krL| jd }qf|| | j| d }q"|tkrtd}n.|d u rtt| j| 	|||d}|\}	}}|rd}|r| 
| }
t||
g}q|||fS )NFr      r?       )_df)r'   r!   Z_revisioncacher*   r%   r   superr   _rawtextr   r<   popr   Zpatches)r+   r   r'   rC   Z	validatedZrawtextchainZiterrevr__r2   	__class__r9   r:   rE   x   s2    





zbundlerevlog._rawtextc                 O   s   t d S r;   NotImplementedErrorr+   argskwargsr9   r9   r:   addrevision   s    zbundlerevlog.addrevisionc                 O   s   t d S r;   rL   rN   r9   r9   r:   addgroup   s    zbundlerevlog.addgroupc                 O   s   t d S r;   rL   rN   r9   r9   r:   strip   s    zbundlerevlog.stripc                 C   s   t d S r;   rL   r+   r9   r9   r:   	checksize   s    zbundlerevlog.checksize)N)N)__name__
__module____qualname__r   r<   r@   rE   rQ   rR   rS   rU   __classcell__r9   r9   rJ   r:   r   -   s   5
	r   c                   @   s   e Zd Zdd ZdS )bundlechangelogc                 C   s.   t j | | dd }t| || j|| d S )Nc                 S   s   | S r;   r9   )xr9   r9   r:   <lambda>   rB   z*bundlechangelog.__init__.<locals>.<lambda>)r	   r   r   r)   )r+   r,   r-   r.   r9   r9   r:   r      s
    zbundlechangelog.__init__NrV   rW   rX   r   r9   r9   r9   r:   rZ      s   rZ   c                       s&   e Zd ZdddZ fddZ  ZS )bundlemanifestNrB   c                 C   sX   t jj| ||d t| || j|| |d u rHi }| jjdkrHt| j}|| _|| _	d S )N)Ztrees   03)
r   manifestrevlogr   r   r)   r   version_getfilestarts_dirlogstarts_linkmapper)r+   r,   r-   r.   Zdirlogstartsdirr9   r9   r:   r      s    
zbundlemanifest.__init__c                    sF   || j v r6| j| j |  t| j| j| j| j |dS tt| |S )N)rd   )rb   r   r=   r^   r,   rc   rD   dirlog)r+   drJ   r9   r:   re      s    
zbundlemanifest.dirlog)NrB   )rV   rW   rX   r   re   rY   r9   r9   rJ   r:   r^      s    
r^   c                   @   s   e Zd Zdd ZdS )bundlefilelogc                 C   s&   t j | || t|| j||| _d S r;   )r   r   r   r)   Z_revlog)r+   r,   pathr-   r.   r9   r9   r:   r      s    
zbundlefilelog.__init__Nr]   r9   r9   r9   r:   rg      s   rg   c                   @   s   e Zd Zdd ZdS )
bundlepeerc                 C   s   dS NFr9   rT   r9   r9   r:   canpush   s    zbundlepeer.canpushN)rV   rW   rX   rk   r9   r9   r9   r:   ri      s   ri   c                       s4   e Zd Z fddZdd Zdd Zdd Z  ZS )	bundlephasecachec                    s4   t t| j|i | t| dr0t| j| _d S )Nr,   )rD   rl   r   r   Zsafehasattrr   r   r,   rN   rJ   r9   r:   r      s    zbundlephasecache.__init__c                 C   s   t d S r;   rL   rT   r9   r9   r:   write   s    zbundlephasecache.writec                 C   s   t d S r;   rL   )r+   fpr9   r9   r:   _write   s    zbundlephasecache._writec                 C   s   || j |< |   d| _d S )NT)Z
phaserootsZ
invalidateZdirty)r+   ZphaseZnewrootsZtrr9   r9   r:   _updateroots   s    
zbundlephasecache._updateroots)rV   rW   rX   r   rm   ro   rp   rY   r9   r9   rJ   r:   rl      s   rl   c                    sF   i }t  ji D ]0}|d }  ||< t  fddi D ]}q:q|S )Ns   filenamec                      s
     d S r;   )Z
deltachunkr9   r-   r9   r:   r\      rB   z _getfilestarts.<locals>.<lambda>)iterZfilelogheaderr$   )r-   ZfilesposZ	chunkdatafnamechunkr9   rq   r:   ra      s    ra   c                       s   e Zd ZdZdd Zdd Zd%ddZejd	d
 Z	ejdd Z
dd Zejdd Zdd Zejdd Zejdd Zdd Z fddZdd Zdd Zdd  Zd!d" Zef fd#d$	Z  ZS )&bundlerepositorya%  A repository instance that is a union of a local repo and a bundle.

    Instances represent a read-only repository composed of a local repository
    with the contents of a bundle file applied. The repository instance is
    conceptually similar to the state of a repository after an
    ``hg unbundle`` operation. However, the contents of the bundle are never
    applied to the actual base repository.

    Instances constructed directly are not usable as repository objects.
    Use instance() or makebundlerepository() to create instances.
    c              	   C   sR  || _ || _| jdddd d | _t|d}t| j||}t	|t
jr|| _d | _d }|jddD ]*}|jdkr|rtd	|}| || qh|sttd
|dtj n^t	|tjr| r| j|jddd}t| j||| j}|| _|| _nttdt| i | _| jj d | _!t"#| d t"j$dd | | j!d  D  d S )N   phasess   publishFs
   bundlerepo   rbT)seekable   changegroups#   can't process multiple changegroupss   No changegroups foundr   s   .hg10un   HG10UN)headers   bundle type %s cannot be readr   c                 S   s   g | ]}|  qS r9   r   .0ctxr9   r9   r:   
<listcomp>5  rB   z-bundlerepository.__init__.<locals>.<listcomp>)%_tempparent_urluiZ	setconfigtempfiler   Z	posixfiler   Z
readbundle
isinstancer   Z
unbundle20_bundlefile_cgunpackerZ	iterpartstyperM   _handlebundle2partr   Abortr   r=   osSEEK_SETr   Zcg1unpacker
compressed_writetempbundler>   r   _cgfilesposr	   r!   firstnewrevr   ZretractboundaryZdraft)r+   
bundlepathurl
tempparentfr   Zcgpartpartr9   r9   r:   r      sP    
zbundlerepository.__init__c                 C   sv   |j dkrd S |}|jdd}t| }||vrHtd}t|| | rb| 	|j
d| }t||d| _d S )Nry   s   versions   01s#   Unsupported changegroup version: %ss   .cg%suns   UN)r   paramsgetr   Zsupportedincomingversionsr   r   r   r   r   r>   Zgetunbundlerr   )r+   r   r   Zcgstreamr`   Zlegalcgversmsgr9   r9   r:   r   8  s    

z#bundlerepository._handlebundle2partrB   c                 C   s|   | j jd|d\}}|| _t|d4}|| |d}|s@qL|| q2W d   n1 s`0    Y  | j j| jddS )z'Write a temporary file to disk
        s
   hg-bundle-)prefixsuffixwbi   Nrw   )mode)r   Zmkstempr   r   fdopenrm   open)r+   Zreadfnr   r{   ZfdtempZtempZfptemprt   r9   r9   r:   r   G  s    
*z!bundlerepository._writetempbundlec                 C   s   t | | jS r;   )rl   Z_phasedefaultsrT   r9   r9   r:   _phasecacheW  s    zbundlerepository._phasecachec                 C   s(   | j   t| j| j }| j  | _|S r;   )r   ZchangelogheaderrZ   svfsr$   manstart)r+   cr9   r9   r:   r	   [  s    
zbundlerepository.changelogc                 C   s   d S r;   r9   rT   r9   r9   r:   _refreshchangelogc  s    z"bundlerepository._refreshchangelogc                 C   sV   | j | j | j   |  jj}t| j| j |}| j 	 | _
t| j| ||  S r;   )r   r=   r   manifestheader
unfilteredr	   r'   r^   r   r$   	filestartr   manifestlogZnarrowmatch)r+   r.   Z	rootstorer9   r9   r:   r   h  s    
zbundlerepository.manifestlogc                 C   s8   | j | j | j   | j  D ]}q"| j  | _dS )zgConsumes the manifest portion of the bundle, setting filestart so the
        file portion can be read.N)r   r=   r   r   r#   r$   r   )r+   r2   r9   r9   r:   _consumemanifestu  s
    
z!bundlerepository._consumemanifestc                 C   s   | j  | jS r;   )r	   r   rT   r9   r9   r:   r   ~  s    zbundlerepository.manstartc                 C   s   | j  d| jvr|   | jS )Nr   )r   __dict__r   r   rT   r9   r9   r:   r     s    
zbundlerepository.filestartc                 C   s   | j S r;   )r   rT   r9   r9   r:   r     s    zbundlerepository.urlc                    sn   | j s | j| j t| j| _ || j v rZ| j| j |  |  jj}t| j	|| j|S t
t| |S d S r;   )r   r   r=   r   ra   r   r	   r'   rg   r   rD   ru   file)r+   r   r.   rJ   r9   r:   r     s    
zbundlerepository.filec                 C   s:   | j   | jdur"| j| j | jr6t| jd dS )z'Close assigned bundle file immediately.NT)r   closer   r   unlinkr   shutilrmtreerT   r9   r9   r:   r     s
    

zbundlerepository.closec                 C   s   dS rj   r9   rT   r9   r9   r:   cancopy  s    zbundlerepository.cancopyc                 C   s   t | S r;   )ri   rT   r9   r9   r:   peer  s    zbundlerepository.peerc                 C   s   t  S r;   )r   getcwdrT   r9   r9   r:   r     s    zbundlerepository.getcwdc                    sv   | j |}| j |}td}| j j|k rB| j|t|  | j j|k rd| j|t|  tt	| 
||S )Ns9   setting parent to node %s that only exists in the bundle
)r	   r'   r   r!   r   warnnodemodhexrD   ru   
setparents)r+   r0   r1   Zp1revZp2revr   rJ   r9   r:   r     s    zbundlerepository.setparents)rB   )rV   rW   rX   __doc__r   r   r   r   Zunfilteredpropertycacher   r	   r   r   r   r   r   r   r   r   r   r   r   r   r   rY   r9   r9   rJ   r:   ru      s.   <



	

ru   c                 C   s   |rt td| dd}|s<tt }|d u r<d}|rzt }||krVd}n$t	|}|
|rz|t|d  }t|}| }|jdkr|dd}t|dkr||d  }	}
q|\}	}
n
|| }	}
t| |	|
S )Ns#   cannot create new bundle repositorys   bundles   mainreporootrB      +r   r   )r   r   r   Zconfigr
   Zfindrepor   r   r   Znormasprefix
startswithr    r   r   Z	localpathZschemesplitmakebundlerepository)r   rh   createZintentsZ
createoptsZ
parentpathcwdusrepopath
bundlenamer9   r9   r:   instance  s.    





r   c                 C   s   |rdt ||f }nd| }ztj| |dd}d}W nP tjy   t }ztj| |dd}W n ty   t	
|  Y n0 Y n0 G dd dt|j}||_t|||| |S )	z?Make a bundle repository object based on repo and bundle paths.s   bundle:%s+%ss	   bundle:%sF)r   NTc                   @   s   e Zd ZdS )z5makebundlerepository.<locals>.derivedbundlerepositoryN)rV   rW   rX   r9   r9   r9   r:   derivedbundlerepository  s   r   )r   Z
expandpathr   r   r   Z	RepoErrorr   Zmkdtemp	Exceptionr   r   ru   rK   r   )r   r   r   r   repor   r   r9   r9   r:   r     s"    
r   c                   @   s$   e Zd Zdd Zdd Zdd ZdS )bundletransactionmanagerc                 C   s   d S r;   r9   rT   r9   r9   r:   transaction  s    z$bundletransactionmanager.transactionc                 C   s   t d S r;   rL   rT   r9   r9   r:   r     s    zbundletransactionmanager.closec                 C   s   t d S r;   rL   rT   r9   r9   r:   release  s    z bundletransactionmanager.releaseN)rV   rW   rX   r   r   r   r9   r9   r9   r:   r     s   r   Fc                    s   t j|||d}|\}}}	|sRz|r0t| W n tyD   Y n0 |g jfS t|fdd|	D }	d d }
|s|
sj| dd}d|vod|v }| o	d	o	d}|r,
 L}|d	d
||	tj|dddd }t| | | } W d   n1 s0    Y  n	d	r|
 (}|d	d
||	d }W d   n1 sp0    Y  n|du r؈	ds؈
 &}|d|d
d }W d   n1 s0    Y  d}	nB
 (}|d||	d
d }W d   n1 s0    Y  |
r&d}nd}t| ||| } |rHd |
sjt|j|j| }
| }|
rx|
 }
|
j||	}rdd jd D }
 $}|dddi }W d   n1 s0    Y  tj|d}t |_t||  fdd}|
||fS )aD  obtains a bundle of changes incoming from peer

    "onlyheads" restricts the returned changes to those reachable from the
      specified heads.
    "bundlename", if given, stores the bundle to this file path permanently;
      otherwise it's stored to a temp file and gets deleted again when you call
      the returned "cleanupfn".
    "force" indicates whether to proceed on unrelated repos.

    Returns a tuple (local, csets, cleanupfn):

    "local" is a local repo from which to obtain the actual incoming
      changesets; it is a bundlerepo for the obtained bundle when the
      original "peer" is remote.
    "csets" lists the incoming changeset node ids.
    "cleanupfn" must be called without arguments when you're done processing
      the changes; it closes both the original "peer" and the one returned
      here.
    )headsforcec                    s   g | ]}| vr|qS r9   r9   )r~   r[   )	commonsetr9   r:   r   0  rB   z$getremotechanges.<locals>.<listcomp>Ns   devels   legacy.exchanges   bundle2s   bundle1s	   getbundles   incomings   client)ZroleT)   source   common   headss
   bundlecapss   cg)r   r   r   s   changegroupsubsetry   )s   nodesr   )s   basesr   r   s   HG10BZrz   c                 S   s   g | ]}|  qS r9   r|   r}   r9   r9   r:   r     rB   s   listkeyss	   namespacerv   )r   c                      s&   r    rt     d S r;   )r   r   r   r9   )r   
bundlerepor   r9   r:   cleanup  s
    
z!getremotechanges.<locals>.cleanup) r   Zfindcommonincomingr   r   OSErrorr   r"   ZlocalZ
configlistZcapableZcommandexecutorZcallcommandr   Z
caps20to10resultr   ZwritechunksZ_forwardchunksr   Zwritebundler   Zbaseuirootr   r   r	   Zfindmissingr   Zpulloperationr   Z	trmanagerZ_pullapplyphases)r   r   r   Z	onlyheadsr   r   tmpcommonZincomingZrheadsr   ZlegexcZforcebundle1Z
canbundle2r8   Zb2rs   ZcgZ
bundletypeZcsetsZ	reponodesZremotephasesZpullopr   r9   )r   r   r   r   r:   getremotechanges  s    



,
*
(
(	

(r   )NN)NNF)/r   Z
__future__r   r   r   Zi18nr   r   r   r    r   r   r	   r
   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   rZ   r_   r^   rg   Z	localpeerri   Z
phasecacherl   ra   objectru   r   r   r   r   r9   r9   r9   r:   <module>   s(   Tt	
 N
"& 