a
    &0_!^                     @   s>  d dl mZ d dlZd dlZd dlZddlmZ ddlmZ ddl	m
Z
 ddlmZmZmZmZmZmZmZmZ d4d	d
Zdd Zdd Zd5ddZdd Zdd Zd6ddZdd Zdd Zdd ZG dd deZd ZdZ d Z!d!Z"d"d# Z#d$d% Z$ej%d&d' Z&d(d) Z'd*d+ Z(d,d- Z)ej%d.d/ Z*d0d1 Z+d2d3 Z,dS )7    )absolute_importN   )_)open)
repository)	cacheutilerror
narrowspecphasespycompatscmutilstoreutilFc           	      C   s  | j }| j}d}| jr,d| jdg v r,d}|r8|s8dS |rD|sDdS t|rPdS | jrZdS | j}|du rr|d}|szdS t	 }|dr|
d n||d	}|s| j jtd
 dS t	|d}||j }|r| j jtddt|  | j jtd dS |}d|fS )a  Whether it is possible to perform a streaming clone as part of pull.

    ``bundle2`` will cause the function to consider stream clone through
    bundle2 and only through bundle2.

    Returns a tuple of (supported, requirements). ``supported`` is True if
    streaming clone is supported and False otherwise. ``requirements`` is
    a set of repo requirements from the remote, or ``None`` if stream clone
    isn't supported.
    Fs   v2s   streamT)FNNs   stream-preferreds   revlogv1s
   streamreqss=   warning: stream clone requested but server has them disabled
   ,sG   warning: stream clone requested but client is missing requirements: %s
   , sQ   (see https://www.mercurial-scm.org/wiki/MissingRequirement for more information)
)reporemoteZcanusebundle2Zremotebundle2capsgetlenZheadsZstreamclonerequestedcapablesetadduiwarnr   splitsupportedformatsjoinsorted)	pullopZbundle2r   r   Zbundle2supportedZstreamrequestedrequirementsZ
streamreqsmissingreqs r!   7/usr/lib/python3/dist-packages/mercurial/streamclone.pycanperformstreamclone   s`    
	


r#   c              	   C   s  ddl m} t| \}}|s dS | j}| j}d}|drr|  }|di  }W d   n1 sh0    Y  |j	
td |  }|di  }W d   n1 s0    Y  | }	zt|	}
W n" ty   ttd|	Y n0 |
dkrttdn2|
d	kr&ttd
n|
dkr>ttd| }	ztt|	dd\}}W n( ttfy   ttd|	Y n0 | n t|||| ||j|j B |_||j	|j|j|j_t| |r|j || |!  W d   n1 s0    Y  dS )zPossibly perform a legacy stream clone operation.

    Legacy stream clones are performed as part of pull but before all other
    operations.

    A legacy stream clone will not be performed if a bundle2 stream clone is
    supported.
    r   	localrepoNs	   branchmaps   streaming all changes
s
   stream_out'   unexpected response from remote server:s   operation forbidden by server   s$   locking the remote repository failedr   s%   the server sent an unknown error code    )" r%   r#   r   r   r   ZcommandexecutorZcallcommandresultr   statusr   readlineint
ValueErrorr   ResponseErrorAbortmapr   	TypeErrorlock	consumev1r   r   resolvestorevfsoptionsfeaturessvfsoptionsr   writereporequirementsZ_branchcachesreplace
invalidate)r   r%   	supportedr   r   r   Z
rbranchmapefplZresp	filecount	bytecountr!   r!   r"   maybeperformlegacystreamclonez   sZ    	

.
.







rB   c                 C   sF   t j| jvrdS | jjdddds&dS t| }|rB| jddS dS )z5Whether streaming clones are allowed from the server.Fs   servers   uncompressedT)Z	untrusteds   uncompressedallowsecret)r   ZREPO_FEATURE_STREAM_CLONEr6   r   Z
configboolr
   Z	hassecret)r   Zsecretr!   r!   r"   allowservergeneration   s    
rC   c                 C   s   | j |S N)r   walk)r   matcherr!   r!   r"   _walkstreamfiles   s    rG   c                    s   g d}  J jd tD ]$\}}}|r&||f ||7 }q&W d   n1 s`0    Y  jdt|f  jjj  fdd}t|| fS )a  Emit content for version 1 of a streaming clone.

    This returns a 3-tuple of (file count, byte size, data iterator).

    The data iterator consists of N entries for each file being transferred.
    Each file entry starts as a line with the file name and integer size
    delimited by a null byte.

    The raw file data follows. Following the raw file data is the next file
    entry, or EOF.

    When used on the wire protocol, an additional line indicating protocol
    success will be prepended to the stream. This function is not responsible
    for adding it.

    This function will obtain a repository lock to ensure a consistent view of
    the store is captured. It therefore may raise LockError.
    r   	   scanning
Ns   %d files, %d bytes to transfer
c               	   3   s   D ]\} } r$j d| |f  dt| |f V  | ddd@}|dkr^||V  ntj||dD ]
}|V  qlW d    q1 s0    Y  qd S )Ns   sending %s (%d bytes)
s   %s %d
   rbF)Z	auditpath   limit)r   debugr   Z	encodedirreadr   filechunkiter)namesizer>   chunk	debugflagentriesr   r7   r!   r"   emitrevlogdata  s    z"generatev1.<locals>.emitrevlogdata)r3   r   rM   rG   appendr   r7   rT   )r   Ztotal_bytesrP   enamerQ   rV   r!   rS   r"   
generatev1   s    
(rY   c                 c   sj   t | sdV  dS zt| \}}}W n tjy@   dV  Y dS 0 dV  d||f V  |D ]
}|V  qZdS )a  Emit content for version 1 of streaming clone suitable for the wire.

    This is the data output from ``generatev1()`` with 2 header lines. The
    first line indicates overall success. The 2nd contains the file count and
    byte size of payload.

    The success line contains "0" for success, "1" for stream generation not
    allowed, and "2" for error locking the repository (possibly indicating
    a permissions error for the server process).
    s   1
Ns   2
s   0
s   %d %d
)rC   rY   r   Z	LockError)r   r@   rA   itrR   r!   r!   r"   generatev1wireproto  s    r[      UNc                    sD    dkrt djj@ }dt| fdd}|| fS )a@  Emit content for version 1 of a stream clone bundle.

    The first 4 bytes of the output ("HGS1") denote this as stream clone
    bundle version 1.

    The next 2 bytes indicate the compression type. Only "UN" is currently
    supported.

    The next 16 bytes are two 64-bit big endian unsigned integers indicating
    file count and byte count, respectively.

    The next 2 bytes is a 16-bit big endian unsigned short declaring the length
    of the requirements string, including a trailing  . The following N bytes
    are the requirements string, which is ASCII containing a comma-delimited
    list of repo requirements that are needed to support the data.

    The remaining content is the output of ``generatev1()`` (which may be
    compressed in the future).

    Returns a tuple of (requirements, data generator).
    r\   s.   we do not support the compression argument yetr   c                  3   s   dV   V  t \} }}jtd|| f  td| |V  tdtd V  d V   dksnJ jjtd|td	d
}|d |D ]}|j	t|d |V  q|
  d S )Ns   HGS1s   writing %d bytes for %d files
   >QQ   >Hr       r\      bundle   bytestotalZunitr   step)rY   r   r+   r   structZpackr   makeprogressupdate	incrementcomplete)r@   rA   rZ   progressrR   compressionr   requiresr!   r"   genK  s$    

zgeneratebundlev1.<locals>.gen)r.   r   r   r   r   )r   rm   r   ro   r!   rl   r"   generatebundlev1/  s    rp   c                 C   s  |    | jtd|t|f  | jjtd|tdd}|d t }| 	d: | j
j| j|d t|D ]}| }z|dd\}}	t|	}	W n& ttfy   ttd	|Y n0 | jjr| jd
|t|	f  t|}
| j
|
ddd@}tj||	dD ] }|jt|d || qW d   q1 sV0    Y  qW d   n1 sx0    Y  | jdd W d   n1 s0    Y  t | }|dkrd}|  | jtdt||t|| f  W d   n1 s0    Y  dS )a  Apply the contents from version 1 of a streaming clone file handle.

    This takes the output from "stream_out" and applies it to the specified
    repository.

    Like "stream_out," the status line added by the wire protocol is not
    handled by this function.
    !   %d files to transfer, %s of data
   clonera   rb   r   )Zexpectedcountr_   r   r&   s   adding %s (%s)
   wT)ZbackgroundcloserK   rd   NZclearfilecacheMbP?(   transferred %s in %.1f seconds (%s/sec)
)r3   r   r+   r   r   rA   rg   rh   timertransactionr7   backgroundclosingr   Zxranger,   r   r-   r.   r2   r   r/   rT   rM   r   Z	decodedirrO   ri   r   writer;   rj   )r   r>   r@   rA   rk   startir?   rP   rQ   pathofprR   elapsedr!   r!   r"   r4   i  sX    	


P,
r4   c                 C   s   |  d}|dkr$ttd| td|  d\}}td|  dd }|  |}|dsrttd	t|d	d
}|||fS )Nr'   r\   s<   only uncompressed stream clone bundles are supported; got %sr]      r^   r   r_   s@   malformed stream clone bundle: requirements not properly encodedr   )
rN   r   r0   r   rf   Zunpackendswithr   rstripr   )r>   rm   r@   rA   Zrequireslenrn   r   r!   r!   r"   readbundle1header  s(    


r   c                 C   s`   t | rttdt|\}}}|| j }|rNttddt| t| ||| dS )zApply the content from a stream clone bundle version 1.

    We assume the 4 byte header has been read and validated and the file handle
    is at the 2 byte compression identifier.
    s2   cannot apply stream clone bundle on non-empty repo4   unable to apply stream clone: unsupported format: %sr   N)	r   r   r0   r   r   r   r   r   r4   )r   r>   r@   rA   r   r    r!   r!   r"   applybundlev1  s    
r   c                   @   s    e Zd ZdZdd Zdd ZdS )streamcloneapplierzClass to manage applying streaming clone bundles.

    We need to wrap ``applybundlev1()`` in a dedicated type to enable bundle
    readers to perform bundle type-specific functionality.
    c                 C   s
   || _ d S rD   )_fh)selfZfhr!   r!   r"   __init__  s    zstreamcloneapplier.__init__c                 C   s   t || jS rD   )r   r   )r   r   r!   r!   r"   apply  s    zstreamcloneapplier.applyN)__name__
__module____qualname____doc__r   r   r!   r!   r!   r"   r     s   r      s   cc                 C   s   g }|   s|d |S )z!list snapshot file from the stores
   phaseroots)Z
publishingrW   )r   fnamesr!   r!   r"   _walkstreamfullstorefiles  s    
r   c                 C   s2   | \}}}}|t kr| S |||||| |fS )z actually copy the snapshot files)	_filefullr   )entrycopyvfsmapsrcrP   ftypedatar!   r!   r"   _filterfull  s    r   c               
   #   sJ   g  z* fdd} | V  W  D ]}t | qn D ]}t | q40 dS )z(return a function to temporary copy filec                    s4   t  \}}t|  | tj| |dd |S )NT)Zhardlink)r   ZmkstemposcloserW   r   Z	copyfiles)r   fdZdstfilesr!   r"   r     s
    

zmaketempcopies.<locals>.copyN)r   Z	tryunlink)r   tmpr!   r   r"   maketempcopies  s    r   c                 C   s&   t | jt| ji}| j| vs"J |S )z$make a (src -> vfs) map for the repo)	_srcstorer7   	_srccachecachevfsvfsvalues)r   r   r!   r!   r"   _makemap   s
    r   c                 #   sx  t | | jjtd|tdd}|d t . |  fdd|D }dV  d}|D ]\}}}}| }	|V  tt|V  |t	kr|	|}
|}n|t
krt|d}
t|
j}zht|V  |V  |d	kr|
|f}ntj|
|d
}|D ] }|t|7 }|| |V  qW |
  q`|
  0 q`W d   n1 sJ0    Y  W d   n1 sj0    Y  dS )zactually emit the stream bundler`   ra   rb   r   c                    s   g | ]}t | qS r!   )r   ).0r=   r   r   r!   r"   
<listcomp>6      z_emit2.<locals>.<listcomp>NrI   rJ   rK   )r   r   rg   r   rh   r   r   Zuvarintencoder   _fileappendr   r   fstatst_sizerN   rO   r   )r   rU   totalfilesizerk   seenr   rP   r   r   r   r>   rQ   chunksrR   r!   r   r"   _emit2-  s<    



r   c                 C   sr  |   H g }d}d}|s |r0t| j||}| jd t| |D ](\}}}	|	rF|t|t	|	f ||	7 }qFt
| D ]4}| j|rx|| j|j7 }|t|tdf qx|r| jdr|| jdj7 }|tdtdf t| D ]4}| j|r|| j|j7 }|t|tdf qt| ||}
t|
}|du sDJ W d   n1 sZ0    Y  t|||
fS )a  Emit content for version 2 of a streaming clone.

    the data stream consists the following entries:
    1) A char representing the file destination (eg: store or cache)
    2) A varint containing the length of the filename
    3) A varint containing the length of file data
    4) N bytes containing the filename (the internal, store-agnostic form)
    5) N bytes containing the file data

    Returns a 3-tuple of (file count, file size, data iterator).
    r   NrH   s   obsstore)r3   r	   matchrootr   rM   rG   rW   r   r   r   r7   existslstatr   r   r   Zcachetocopyr   r   r   nextr   )r   ZincludesZexcludesZincludeobsmarkersrU   r   rF   rP   rX   rQ   r   firstr!   r!   r"   
generatev2S  s4    
.r   c               	   g   sv   | d }| dd  }|J |rNt |  d V  W d    qT1 sB0    Y  nd V  W d    n1 sh0    Y  d S )Nr   r   )nested)ctxsthisrestr!   r!   r"   r     s    
&r   c                    s       jtd|t|f  t } jjtd|tdd}|d t	 } 
d  fdd| D }t|  t|D ]}t|d}	||	 }
t|}t|}t||} jjr jd	|	|t|f  |
|d
@}tj||dD ] }|jt|d || q
W d   q1 sB0    Y  qW d   n1 sd0    Y   jdd W d   n1 s0    Y  t | }|dkrd} jtdt|j|t|j| f  |  W d   n1 s 0    Y  dS )zApply the contents from a version 2 streaming clone.

    Data is read from an object that only needs to provide a ``read(size)``
    method.
    rq   rr   ra   rb   r   c                 3   s   | ]}|  jV  qd S rD   )ry   r   )r   r   r   r!   r"   	<genexpr>  r   zconsumev2.<locals>.<genexpr>r   s   adding [%s] %s (%s)
rs   rK   rd   NTrt   ru   rv   )r3   r   r+   r   r   rA   rw   rg   rh   r   rx   r   r   rangeZreadexactlyZuvarintdecodestreamrT   rM   rO   ri   r   rz   r;   posrj   )r   r>   r@   filesizer{   rk   r   r   r|   r   r   ZnamelenZdatalenrP   r~   rR   r   r!   r   r"   	consumev2  sX    



P,

r   c                    s   ddl m}  fdd|D }|r>ttddt| t ||| t| j	 j
 B  _	| j j	 j j_t  d S )Nr   r$   c                    s   g | ]}| j vr|qS r!   )r<   )r   rr   r!   r"   r     r   z!applybundlev2.<locals>.<listcomp>r   r   )r)   r%   r   r0   r   r   r   r   r   r   r   r5   r   r6   r7   r8   r   r9   )r   r>   r@   r   r   r%   r    r!   r   r"   applybundlev2  s     
r   )F)N)r\   )-Z
__future__r   
contextlibr   rf   Zi18nr   r   r   Z
interfacesr   r)   r   r   r	   r
   r   r   r   r#   rB   rC   rG   rY   r[   rp   r4   r   r   objectr   r   r   r   r   r   r   contextmanagerr   r   r   r   r   r   r   r!   r!   r!   r"   <module>   sB   (
]M
6
:K
&.
<