o
    Uhh                    @   s  d Z ddlZddlmZmZ ddlmZ ddlmZm	Z	 zddl
mZ W n ey3   ddlmZ Y nw ddlZddlZddlZddlZddlZddlmZmZmZ dd	lmZ dd
lmZ ddlmZmZ ddlmZ ddlm Z m!Z!m"Z"m#Z#m$Z$m%Z%m&Z& zddl'Z'W n e(y   dZ)Y nw dZ)ej*dkrdZ)ddl+m,Z,m-Z- ddl.m/Z/ ddl0m1Z1 ddl2m3Z3m4Z4m5Z5m6Z6m7Z7 dZ8dZ9e8e9fZ:dZ;dZ<e&e=e&e>e?f e@e> f e@e> f ZAe!e>ge=e?eAf f ZBe!e?eCgdf ZDe=e?e#e> f ZEG dd deFZGG dd de$ZHG dd deHZIG d d! d!ZJ	dd"e!e?ge>f d#e#e? d$e=e@e? e#e? f fd%d&ZKG d'd( d(eFZLG d)d* d*ZMd+ZNdeNfd,e!e?ge>f d-eMd.eOd/e?d$e>f
d0d1ZPd2d3 ZQd4d5 ZRdd6d7ZSd8d9 ZTd:d; ZUe=e>e?e#e? f ZVG d<d= d=ZWG d>d? d?eWZXG d@dA dAeWZYG dBdC dCeYZZG dDdE dEeYZ[d$e=e?e?f fdFdGZ\dHe&e>ee> f d$e?fdIdJZ]dddeNfdKe!e?ge>f d,e#e!e?ge>f  d$e=eMe>f fdLdMZ^dNdO Z_G dPdQ dQZ`G dRdS dSe`ZadTdU ZbddWdXZcG dYdZ dZZde%d[ZeG d\d] d]e"ee ZfG d^d_ d_efeM ZgG d`da daefeV ZhG dbdc dcefe4 ZiG ddde deZjG dfdg dgZkdhdi ZlddkdlZmddmdnZndddjdodpe&ee4 ee=e4e#e> f  f dqe#eO dre#e? dse?fdtduZodvdw ZpddxdyZqdddzd{eId|ere> d}e#ere>  d$eeM fd~dZsddddpe&ee> ee=e4e#e> f  f de#e? d$eeM fddZtdpe&ee4 ee=e4e#eE f  f d$ee4 fddZu	dde#e? fddZvddddddpe&ee4 ee=e4e#e> f  f dqe#eO dre#e? deOd$e=e?eeM f f
ddZw						dd{eId|ee=e3e#eE f  dre#e? dqe#eO deOdeOd}e#ere>  d$eeM fddZxde4d$eMfddZy				j	dd{eId|ee=e3e#eE f  dre#e? dqe#eO deOdse?d}e#ere>  fddZzdddjddpe&ee4 ee=e4e#e> f  f dre#e? dqe#eO dse?fddZ{G dd dZ|dddjddeeM fddZ}dd Z~d$e>fddZdZdd Zdd Zdd ZdeeV de>d$e>fddZeZG dd dZdjddde d|ere3 d$e=e>e@f fddZzddlmZmUZU W dS  e(y   Y dS w )a  Classes for dealing with packed git objects.

A pack is a compact representation of a bunch of objects, stored
using deltas where possible.

They have two parts, the pack file, which stores the data, and an index
that tells you where the data is.

To find an object you look in all of the index files 'til you find a
match for the object name. You then use the pointer got from this as
a pointer in to the corresponding packfile.
    N)defaultdictdeque)suppress)BytesIOUnsupportedOperation)CSequenceMatcher)SequenceMatcher)IterableIteratorSequence)sha1)chain)SEEK_CURSEEK_END)unpack_from)BinaryIOCallableGenericOptionalProtocolTypeVarUnionFTPlan9   )ApplyDeltaErrorChecksumMismatch)GitFile)LRUSizeCache)ObjectIDShaFile
hex_to_shaobject_header
sha_to_hex      
   i   c                   @   s   e Zd ZdZdddZdS )UnresolvedDeltasz$Delta objects could not be resolved.returnNc                 C   
   || _ d S Nshas)selfr+    r-   `/var/www/html/construction_image-detection-poc/venv/lib/python3.10/site-packages/dulwich/pack.py__init__g      
zUnresolvedDeltas.__init__r'   N)__name__
__module____qualname____doc__r/   r-   r-   r-   r.   r&   d   s    r&   c                   @   sz   e Zd ZdeddfddZ	ddeeeee f  dee	egdf  ddfdd	Z
d
edefddZd
edefddZdS )ObjectContainerobjr'   Nc                 C      dS )z)Add a single object to this object store.Nr-   r,   r7   r-   r-   r.   
add_objectl       zObjectContainer.add_objectobjectsprogressc                 C   r8   )zAdd a set of objects to this object store.

        Args:
          objects: Iterable over a list of (object, path) tuples
        Nr-   )r,   r<   r=   r-   r-   r.   add_objectso   r;   zObjectContainer.add_objectsr   c                 C   r8   )zCheck if a hex sha is present.Nr-   r,   r   r-   r-   r.   __contains__z   r;   zObjectContainer.__contains__c                 C   r8   )zRetrieve an object.Nr-   r?   r-   r-   r.   __getitem__}   r;   zObjectContainer.__getitem__r)   )r2   r3   r4   r   r:   r   tupler   strr   r>   bytesboolr@   rA   r-   r-   r-   r.   r6   k   s    
r6   c                   @   sz   e Zd ZdddededdfddZdd	d
ee dedee fddZ				dd
e
e dedededed f
ddZdS )PackedObjectContainerFinclude_compr   rH   r'   UnpackedObjectc                C   
   t | j)zGet a raw unresolved object.)NotImplementedErrorget_unpacked_object)r,   r   rH   r-   r-   r.   rL      s   
z)PackedObjectContainer.get_unpacked_objectallow_missingr+   rN   c                C   rJ   r)   )rK   iterobjects_subsetr,   r+   rN   r-   r-   r.   rO      s   
z(PackedObjectContainer.iterobjects_subsetTconvert_ofs_deltac                 C   rJ   r)   )rK   iter_unpacked_subset)r,   r+   rH   rN   rQ   r-   r-   r.   rR         
z*PackedObjectContainer.iter_unpacked_subsetN)FFT)r2   r3   r4   rD   rE   rL   r	   r
   r   rO   setrR   r-   r-   r-   r.   rF      s>    

rF   c                   @   s,   e Zd Zded fddZdefddZdS )UnpackedObjectStreamr'   rI   c                 C   rJ   r)   )rK   __iter__r,   r-   r-   r.   rV      r0   zUnpackedObjectStream.__iter__c                 C   rJ   r)   rK   __len__rW   r-   r-   r.   rY      r0   zUnpackedObjectStream.__len__N)r2   r3   r4   r
   rV   intrY   r-   r-   r-   r.   rU      s    rU   readcrc32r'   c                 C   sn   g }t |dks|d d@ r3| d}|durt||}|t|dd  t |dks|d d@ s||fS )zVRead bytes marked with most significant bit.

    Args:
      read: Read function
    r      r   N)lenbinasciir\   appendord)r[   r\   retbr-   r-   r.   take_msb_bytes   s   re   c                   @   s   e Zd ZdddZdS )PackFileDisappearedr'   Nc                 C   r(   r)   )r7   r9   r-   r-   r.   r/      r0   zPackFileDisappeared.__init__r1   )r2   r3   r4   r/   r-   r-   r-   r.   rf      s    rf   c                   @   s   e Zd ZU dZg dZee ed< eee	  ed< e
de	ef ed< ee	 ed< eee	  ed< ddddddd		dddZdd Zdd Zd
efddZdd Zdd Zd
efddZdS )rI   a  Class encapsulating an object unpacked from a pack file.

    These objects should only be created from within unpack_object. Most
    members start out as empty and are filled in at various points by
    read_zlib_chunks, unpack_object, DeltaChainIterator, etc.

    End users of this object should take care that the function they're getting
    this object from is guaranteed to set the members they need.
    )
_shacomp_chunksr\   decomp_chunks
decomp_len
delta_base
obj_chunksobj_type_numoffsetpack_type_numrm   rl   Nrk   ri   rh   )rk   rj   r\   shari   rn   r'   c                C   s   || _ || _|| _|| _d | _|pg | _|d ur%|d u r%ttt|| _	n|| _	|| _
|tv r7d | _d | _d S || _| j| _|| _d S r)   )rn   rg   ro   rk   rh   ri   summapr_   rj   r\   DELTA_TYPESrm   rl   )r,   ro   rk   rj   r\   rp   ri   rn   r-   r-   r.   r/      s    


zUnpackedObject.__init__c                 C   s    | j du rt| j| j| _ | j S )z%Return the binary SHA of this object.N)rg   obj_sharm   rl   rW   r-   r-   r.   rp      s   
zUnpackedObject.shac                 C   s(   | j dur
| jdusJ t| j | jS )z"Return a ShaFile from this object.N)rm   rl   r   from_raw_chunksrW   r-   r-   r.   sha_file   s   zUnpackedObject.sha_filec                 C   s0   | j tv rt| jttfsJ | j| jfS | jS )z>Return the decompressed chunks, or (delta base, delta chunks).)ro   rs   
isinstancerk   rD   rZ   ri   rW   r-   r-   r.   _obj  s   
zUnpackedObject._objc                 C   s8   t |tsdS | jD ]}t| |t||kr dS q
dS NFT)rw   rI   	__slots__getattr)r,   otherslotr-   r-   r.   __eq__	  s   

zUnpackedObject.__eq__c                 C   s
   | |k S r)   r-   r,   r|   r-   r-   r.   __ne__  r0   zUnpackedObject.__ne__c                    s*    fdd j D }d jjd|S )Nc                    s    g | ]}| d t  |qS )=)r{   .0srW   r-   r.   
<listcomp>  s     z+UnpackedObject.__repr__.<locals>.<listcomp>z{}({})z, )rz   format	__class__r2   joinr,   datar-   rW   r.   __repr__  s   zUnpackedObject.__repr__r1   )r2   r3   r4   r5   rz   r   rZ   __annotations__listrD   r   r/   rp   rv   OldUnpackedObjectrx   r~   r   rC   r   r-   r-   r-   r.   rI      s.   
 


rI   i   	read_someunpackedrH   buffer_sizec                 C   s  |j dkr	tdt }g }|j}d}|j}	 | |}	|	s#td||	 ||	}
|t	|
7 }||
 |j
}|r\t	|}|durPt|	d|  |}|r[|	d|  |d< n|durft|	|}q|duro|dM }||j krytd||_|r||_|S )	a  Read zlib data from a buffer.

    This function requires that the buffer have additional data following the
    compressed data, which is guaranteed to be the case for git pack files.

    Args:
      read_some: Read function that returns at least one byte, but may
        return less than the requested size.
      unpacked: An UnpackedObject to write result data to. If its crc32
        attr is not None, the CRC32 of the compressed bytes will be computed
        using this starting CRC32.
        After this function, will have the following attrs set:
        * comp_chunks    (if include_comp is True)
        * decomp_chunks
        * decomp_len
        * crc32
      include_comp: If True, include compressed data in the result.
      buffer_size: Size of the read buffer.
    Returns: Leftover unused data from the decompression.

    Raises:
      zlib.error: if a decompression error occurred.
    r]   z+non-negative zlib data stream size expectedr   TzEOF before end of zlib streamN    z.decompressed data does not match expected size)rj   
ValueErrorzlibdecompressobjri   r\   errorra   
decompressr_   unused_datar`   rh   )r   r   rH   r   
decomp_objrh   ri   rj   r\   adddecompunusedleftr-   r-   r.   read_zlib_chunks  sD   






r   c                 C   s(   t  }| D ]}|| q| dS )zReturn the hexdigest of the SHA1 over a set of names.

    Args:
      iter: Iterator over string objects
    Returns: 40-byte hex sha1 digest
    ascii)r   update	hexdigestencode)iterrp   namer-   r-   r.   	iter_sha1`  s   r   c                 C   s8   t | d}t| |W  d   S 1 sw   Y  dS )zLoad an index file by path.

    Args:
      path: Path to the index file
    Returns: A PackIndex loaded from the given path
    rbN)r   load_pack_index_file)pathfr-   r-   r.   load_pack_indexm  s   $r   c              	   C   s   z|   }W n ttfy   d }Y nw |d ur<|d u r"t|j}tr<ztj||tjd}W ||fS  t	y;   Y nw | 
 }t|}||fS )N)access)filenor   AttributeErrorosfstatst_sizehas_mmapmmapACCESS_READOSErrorr[   r_   )r   sizefdcontentsr-   r-   r.   _load_file_contentsx  s$   r   c                 C   sj   t |\}}|dd dkr-td|dd d }|dkr&t| |||dS td	| t| |||dS )
zLoad an index file from a file-like object.

    Args:
      path: Path for the index file
      f: File-like object
    Returns: A PackIndex loaded from the given file
    N      tOc   >L   r      )filer   r   zUnknown pack index format )r   structunpack
PackIndex2KeyError
PackIndex1)r   r   r   r   versionr-   r-   r.   r     s   r   c                 C   s\   | |ksJ | |kr,| | d }||}||k r|d } n||kr&|d }n|S | |ks
dS )a  Find a SHA in a data blob with sorted SHAs.

    Args:
      start: Start index of range to search
      end: End index of range to search
      sha: Sha to find
      unpack_name: Callback to retrieve SHA by index
    Returns: Index of the SHA, or None if it wasn't found
    r   r   Nr-   )startendrp   unpack_nameifile_shar-   r-   r.   bisect_find_sha  s   


	r   c                   @   s   e Zd ZdZdd Zdd ZdefddZdee	 fd	d
Z
dee fddZde	fddZde	defddZde	defddZdede	fddZde	defddZde	fddZdee	 fddZd"ddZd"d d!ZdS )#	PackIndexzAn index in to a packfile.

    Given a sha id of an object a pack index can tell you the location in the
    packfile of that object if it has it.
    c                 C   sH   t |tsdS t|  | D ]\\}}}\}}}||kr! dS qdS ry   )rw   r   zipiterentries)r,   r|   name1_name2r-   r-   r.   r~     s   
zPackIndex.__eq__c                 C   s   |  | S r)   )r~   r   r-   r-   r.   r        zPackIndex.__ne__r'   c                 C   rJ   )0Return the number of entries in this pack index.rX   rW   r-   r-   r.   rY        
zPackIndex.__len__c                 C   s   t t|  S )z#Iterate over the SHAs in this pack.)rr   r"   _itersharW   r-   r-   r.   rV        zPackIndex.__iter__c                 C   rJ   )Iterate over the entries in this pack index.

        Returns: iterator over tuples with object name, offset in packfile and
            crc32 checksum.
        )rK   r   rW   r-   r-   r.   r        
zPackIndex.iterentriesc                 C   rJ   )pReturn the SHA1 checksum stored for the corresponding packfile.

        Returns: 20-byte binary digest
        )rK   get_pack_checksumrW   r-   r-   r.   r     s   
zPackIndex.get_pack_checksumrp   c                 C   s   t jdtdd | |S )Nz Please use object_offset insteadr   
stacklevel)warningswarnDeprecationWarningobject_offsetr,   rp   r-   r-   r.   object_index  s   
zPackIndex.object_indexc                 C   rJ   )  Return the offset in to the corresponding packfile for the object.

        Given the name of an object it will return the offset that object
        lives at within the corresponding pack file. If the pack file doesn't
        have the object then None will be returned.
        )rK   r   r   r-   r-   r.   r     rS   zPackIndex.object_offsetindexc                 C   s,   |   D ]\}}}||kr|  S qt|)z<Return the SHA1 corresponding to the index in the pack file.)r   r   )r,   r   r   rn   r\   r-   r-   r.   object_sha1  s
   zPackIndex.object_sha1c                 C   rJ   )fSee object_offset.

        Args:
          sha: A *binary* SHA string. (20 characters long)_
        )rK   _object_offsetr   r-   r-   r.   r     r   zPackIndex._object_offsetc                 C   s   t |  S )zReturn the hex SHA1 over all the shas of all objects in this pack.

        Note: This is used for the filename of the pack.
        )r   r   rW   r-   r-   r.   objects_sha1  s   zPackIndex.objects_sha1c                 C   rJ   )z9Yield all the SHA1's of the objects in the index, sorted.)rK   r   rW   r-   r-   r.   r     r   zPackIndex._itershaNc                 C      d S r)   r-   rW   r-   r-   r.   close     zPackIndex.closec                 C   r   r)   r-   rW   r-   r-   r.   check  r   zPackIndex.checkr1   )r2   r3   r4   r5   r~   r   rZ   rY   r
   rD   rV   PackIndexEntryr   r   r   r   r   r   r   r   r   r   r-   r-   r-   r.   r     s     	
r   c                   @   sj   e Zd ZdZddddZdd Zdefdd	Zd
d Zdd Z	dd Z
dd Zedd Zedd ZdS )MemoryPackIndexz-Pack index that is stored entirely in memory.Nr'   c                 C   s@   i | _ i | _|D ]\}}}|| j |< || j|< q|| _|| _dS )zCreate a new MemoryPackIndex.

        Args:
          entries: Sequence of name, idx, crc32 (sorted)
          pack_checksum: Optional pack checksum
        N)_by_sha
_by_offset_entries_pack_checksum)r,   entriespack_checksumr   rn   r\   r-   r-   r.   r/     s   

zMemoryPackIndex.__init__c                 C      | j S r)   )r   rW   r-   r-   r.   r   *     z!MemoryPackIndex.get_pack_checksumc                 C   
   t | jS r)   )r_   r   rW   r-   r-   r.   rY   -  r0   zMemoryPackIndex.__len__c                 C   s   t |dkr
t|}| j| S )N(   )r_   r    r   r   r-   r-   r.   r   0  s   
zMemoryPackIndex.object_offsetc                 C   s
   | j | S r)   )r   )r,   rn   r-   r-   r.   r   5  r0   zMemoryPackIndex.object_sha1c                 C   r   r)   )r   r   rW   r-   r-   r.   r   8  r0   zMemoryPackIndex._itershac                 C   r   r)   )r   r   rW   r-   r-   r.   r   ;  r0   zMemoryPackIndex.iterentriesc                 C   s   t | | S r)   )r   sorted_entriescalculate_checksum)clspackr-   r-   r.   for_pack>     zMemoryPackIndex.for_packc                 C   s   | |  | S r)   )r   r   )r   other_indexr-   r-   r.   cloneB  r   zMemoryPackIndex.cloner)   r1   )r2   r3   r4   r5   r/   r   rZ   rY   r   r   r   r   classmethodr   r   r-   r-   r-   r.   r     s    
r   c                       sD  e Zd ZU dZee ed< d/d0ddZede	fddZ
 fd	d
Zd0ddZdefddZdedefddZdefddZdefddZdee fddZdee fddZdee fddZdefddZd0dd Zdefd!d"Zdefd#d$Zdefd%d&Zd'edefd(d)Zd'edefd*d+Zd,edee fd-d.Z  Z S )1FilePackIndexa  Pack index that is based on a file.

    To do the loop it opens the file, and indexes first 256 4 byte groups
    with the first byte of the sha id. The value in the four byte group indexed
    is the end of the group that shares the same starting byte. Subtract one
    from the starting byte and index again to find the start of the group.
    The values are sorted by sha id within the group, so do the math to find
    the start and end offset and then bisect in to find if the value is
    present.
    _fan_out_tableNr'   c                 C   sT   || _ |du rt|d| _n|| _|du r!t| j|\| _| _dS ||| _| _dS )zCreate a pack index object.

        Provide it with the name of the index file to consider, and it will map
        it whenever required.
        Nr   )	_filenamer   _filer   	_contents_sizer,   filenamer   r   r   r-   r-   r.   r/   U  s   zFilePackIndex.__init__c                 C   r   r)   r  rW   r-   r-   r.   r   g     zFilePackIndex.pathc                    s&   t |tr| j|jkrdS t |S NF)rw   r  r  superr~   r   r   r-   r.   r~   k  s
   zFilePackIndex.__eq__c                 C   s.   | j   t| jdd d ur| j  d S d S )Nr   )r  r   r{   r  rW   r-   r-   r.   r   u  s   
zFilePackIndex.closec                 C   s
   | j d S )r   r]   )r  rW   r-   r-   r.   rY   z  r   zFilePackIndex.__len__r   c                 C   rJ   )zUnpack the i-th entry in the index file.

        Returns: Tuple with object name (SHA), offset in pack file and CRC32
            checksum (if known).
        )rK   _unpack_entryr,   r   r-   r-   r.   r  ~  r   zFilePackIndex._unpack_entryc                 C   rJ   )z)Unpack the i-th name from the index file.)rK   _unpack_namer  r-   r-   r.   r    r   zFilePackIndex._unpack_namec                 C   rJ   )z2Unpack the i-th object offset from the index file.)rK   _unpack_offsetr  r-   r-   r.   r    r   zFilePackIndex._unpack_offsetc                 C   rJ   )zAUnpack the crc32 checksum for the ith object from the index file.)rK   _unpack_crc32_checksumr  r-   r-   r.   r    r   z$FilePackIndex._unpack_crc32_checksumc                 c   s$    t t| D ]}| |V  qd S r)   )ranger_   r  r  r-   r-   r.   r     s   zFilePackIndex._itershac                 c   s$    t t| D ]}| |V  qdS )r   N)r  r_   r  r  r-   r-   r.   r     s   zFilePackIndex.iterentriesstart_offsetc                 C   sN   g }t dD ]}| j||d  ||d d   }|td|d  q|S )N   r   r   >Lr   )r  r  ra   r   r   )r,   r  rc   r   fanout_entryr-   r-   r.   _read_fan_out_table  s   z!FilePackIndex._read_fan_out_tablec                 C   &   |   }|  }||krt||dS )z;Check that the stored checksum matches the actual checksum.Nr   get_stored_checksumr   r,   actualstoredr-   r-   r.   r     
   
zFilePackIndex.checkc                 C   s   t | jdd  S )zlCalculate the SHA1 checksum over this pack index.

        Returns: This is a 20-byte binary digest
        N)r   r  digestrW   r-   r-   r.   r     s   z FilePackIndex.calculate_checksumc                 C      t | jdd S )r   ir   rD   r  rW   r-   r-   r.   r        zFilePackIndex.get_pack_checksumc                 C   r"  )z`Return the SHA1 checksum stored for this index.

        Returns: 20-byte binary digest
        r   Nr#  rW   r-   r-   r.   r    r$  z!FilePackIndex.get_stored_checksumrp   c              
   C   s\   t |dkr
t|}z| |W S  ty- } zt| jdd}|dv r(t| | d}~ww )r   r   closedN)NT)r_   r    r   r   r{   r  rf   )r,   rp   excr%  r-   r-   r.   r     s   
zFilePackIndex.object_offsetc                 C   sp   t |dksJ t|dd }|dkrd}n| j|d  }| j| }t|||| j}|du r3t|| |S )r      Nr   r   )r_   rb   r  r   r  r   r  )r,   rp   idxr   r   r   r-   r-   r.   r     s   

zFilePackIndex._object_offsetprefixc                 c   s    t |dd }|dkrd}n| j|d  }t |dd d }|dkr*t| }n| j| }||ks5J d}t||D ]}| |}||rN|V  d}q<|rS dS q<dS )z-Iterate over all SHA1s with the given prefix.Nr   r   r  FT)rb   r  r_   r  r  
startswith)r,   r)  r   r   startedr   r   r-   r-   r.   iter_prefix  s(   



zFilePackIndex.iter_prefixNNNr1   )!r2   r3   r4   r5   r   rZ   r   r/   propertyrC   r   r~   r   rY   r   r  rD   r  r  r   r  r
   r   r   r  r   r   r   r  r   r   r,  __classcell__r-   r-   r  r.   r  G  s.   
 

	
	r  c                       sN   e Zd ZdZddeddf fddZdd Zd	d
 Zdd ZdddZ	  Z
S )r   zVersion 1 Pack Index file.Nr  r'   c                    s(   t  |||| d| _| d| _d S )Nr   r   )r  r/   r   r  r  r  r  r-   r.   r/     s   zPackIndex1.__init__c                 C   s$   t d| jd|d  \}}||d fS )N>L20s      r   r  )r,   r   rn   r   r-   r-   r.   r    s   
zPackIndex1._unpack_entryc                 C   s"   d|d  d }| j ||d  S )Nr1  r2  r   r'  )r  r,   r   rn   r-   r-   r.   r  
  s   zPackIndex1._unpack_namec                 C   s   d|d  }t d| j|d S )Nr1  r2  r  r   r3  r4  r-   r-   r.   r    s   zPackIndex1._unpack_offsetc                 C   r   r)   r-   r  r-   r-   r.   r    s   z!PackIndex1._unpack_crc32_checksumr-  r1   r2   r3   r4   r5   rC   r/   r  r  r  r  r/  r-   r-   r  r.   r     s    r   c                       sL   e Zd ZdZddeddf fddZdd Zd	d
 Zdd Zdd Z	  Z
S )r   zVersion 2 Pack Index file.Nr  r'   c                    s   t  |||| | jd d dkrtdtd| jd\| _| jdkr,td| j | d| _d| _| jd	t	|   | _
| j
dt	|   | _| jdt	|   | _d S )
Nr   r   zNot a v2 pack index filer   r   Version was r   i  r'  )r  r/   r  AssertionErrorr   r   r  r  _name_table_offsetr_   _crc32_table_offset_pack_offset_table_offset_pack_offset_largetable_offsetr  r  r-   r.   r/     s   
zPackIndex2.__init__c                 C   s   |  || || |fS r)   )r  r  r  r  r-   r-   r.   r  )  s   zPackIndex2._unpack_entryc                 C   s    | j |d  }| j||d  S Nr'  )r8  r  r4  r-   r-   r.   r  0  s   zPackIndex2._unpack_namec                 C   sP   | j |d  }td| j|d }|d@ r&| j|d@ d  }td| j|d }|S )Nr   r  r           ir   z>Q)r:  r   r  r;  r4  r-   r-   r.   r  4  s   zPackIndex2._unpack_offsetc                 C   s   t d| j| j|d  d S )Nr  r   r   )r   r  r9  r  r-   r-   r.   r  <  s   z!PackIndex2._unpack_crc32_checksumr-  r5  r-   r-   r  r.   r     s    r   c                 C   sl   | d}|s
t d|dd dkrt d|td|d\}|dvr+t d	| td|d
\}||fS )zRead the header of a pack file.

    Args:
      read: Read function
    Returns: Tuple of (pack version, number of objects). If no data is
        available to read, returns (None, None).
       zfile too short to contain packNr      PACKzInvalid pack header r   )r      r6  r   )r7  r   )r[   headerr   num_objectsr-   r-   r.   read_pack_header@  s   rC  chunksc                 C   s    t | tr	t| S ttt| S r)   )rw   rD   r_   rq   rr   )rD  r-   r-   r.   chunks_lengthT  s   
rE  read_allc                 C   sR  |du r| }|rd}nd}t | |d\}}|d d? d@ }|d d@ }t|dd D ]\}	}
||
d@ |	d d > 7 }q+t|}|tkrzt | |d\}}|t|7 }|d	 d
@ rZt|d d@ }|dd D ]}
|d7 }|dK }||
d@ 7 }qf|}n|tkr| d}|durt||}|}|d7 }nd}t||||d}t	||||d}||fS )a  Unpack a Git object.

    Args:
      read_all: Read function that blocks until the number of requested
        bytes are read.
      read_some: Read function that returns at least one byte, but may not
        return the number of bytes requested.
      compute_crc32: If True, compute the CRC32 of the compressed data. If
        False, the returned CRC32 will be None.
      include_comp: If True, include compressed data in the result.
      zlib_bufsize: An optional buffer size for zlib operations.
    Returns: A tuple of (unpacked, unused), where unused is the unused data
        leftover from decompression, and unpacked in an UnpackedObject with
        the following attrs set:

        * obj_chunks     (for non-delta types)
        * pack_type_num
        * delta_base     (for delta types)
        * comp_chunks    (if include_comp is True)
        * decomp_chunks
        * decomp_len
        * crc32          (if compute_crc32 is True)
    Nr   )r\   r   r$      r      r]   r^   r'  )rk   rj   r\   )r   rH   )
re   	enumerater_   	OFS_DELTAr7  	REF_DELTAr`   r\   rI   r   )rF  r   compute_crc32rH   zlib_bufsizer\   rawtype_numr   r   byteraw_basedelta_base_offsetrk   delta_base_objr   r   r-   r-   r.   unpack_object[  sN   
rT  c                 C   s$   | \}}|t v rt|d S t|S )zBCompute the size of a unresolved object for use with LRUSizeCache.r   )rs   rE  )valuenumr7   r-   r-   r.   _compute_object_size  s   rW  c                   @   sn   e Zd ZdZdefdddZdd Zdd	 Zed
d Z	dd Z
dd ZdefddZddee fddZdS )PackStreamReaderz|Class to read a pack stream.

    The pack is read from a ReceivableProtocol using read() or recv() as
    appropriate.
    Nr'   c                 C   sD   || _ |d u r|| _n|| _t | _d| _t | _t | _|| _	d S Nr   )
rF  r   r   rp   _offsetr   _rbufr   _trailer_zlib_bufsize)r,   rF  r   rM  r-   r-   r.   r/     s   
zPackStreamReader.__init__c                    s   ||}t |}  j|7  _t  j}|dkr|}d}nt|| d d}|} jtt fddt|D   j	|| d   j|d|   |S )aH  Read up to size bytes using the given callback.

        As a side effect, update the verifier's hash (excluding the last 20
        bytes read).

        Args:
          read: The read callback to read from.
          size: The maximum number of bytes to read; the particular
            behavior is callback-specific.
        r'  r   c                    s   g | ]} j  qS r-   )r\  popleft)r   r   rW   r-   r.   r     s    z*PackStreamReader._read.<locals>.<listcomp>N)
r_   rZ  r\  maxrp   r   rD   	bytearrayr  extend)r,   r[   r   r   ntnto_popto_addr-   rW   r.   _read  s   
zPackStreamReader._readc                 C   s4   | j }| }|dt | }|| || S rY  )r[  tellseekr   )r,   bufr   r   r-   r-   r.   _buf_len  s   
zPackStreamReader._buf_lenc                 C   s   | j |   S r)   )rZ  rj  rW   r-   r-   r.   rn     r   zPackStreamReader.offsetc                 C   sD   |   }||kr| j|S | j }t | _|| | j||  S )z)Read, blocking until size bytes are read.)rj  r[  r[   r   rf  rF  )r,   r   buf_lenbuf_datar-   r-   r.   r[     s   
zPackStreamReader.readc                 C   s:   |   }|r| j|}||krt | _|S | | j|S )z7Read up to size bytes, blocking until one byte is read.)rj  r[  r[   r   rf  r   )r,   r   rk  r   r-   r-   r.   recv  s   zPackStreamReader.recvc                 C   r   r)   _num_objectsrW   r-   r-   r.   rY     r   zPackStreamReader.__len__Fc           	      c   s    t | j\}| _t| jD ]0}| j}t| j| j|| jd\}}||_t }|	| |	| j
  |d || _
|V  q|  dk rJ| d t| j}|| j kr`tt|| j dS )a  Read the objects in this pack file.

        Args:
          compute_crc32: If True, compute the CRC32 of the compressed
            data. If False, the returned CRC32 will be None.
        Returns: Iterator over UnpackedObjects with the following members set:
            offset
            obj_type_num
            obj_chunks (for non-delta types)
            delta_base (for delta types)
            decomp_chunks
            decomp_len
            crc32 (if compute_crc32 is True)

        Raises:
          ChecksumMismatch: if the checksum of the pack contents does not
            match the checksum in the pack trailer.
          zlib.error: if an error occurred during zlib decompression.
          IOError: if an error occurred writing to the output file.
        )r   rL  rM  r   r'  N)rC  r[   ro  r  rn   rT  rm  r]  r   writer[  rh  rj  r`  r\  rp   r!  r   r"   r   )	r,   rL  pack_versionr   rn   r   r   ri  pack_shar-   r-   r.   read_objects  s.   




zPackStreamReader.read_objectsr1   F)r2   r3   r4   r5   _ZLIB_BUFSIZEr/   rf  rj  r.  rn   r[   rm  rZ   rY   r
   rI   rs  r-   r-   r-   r.   rX    s     
	
rX  c                       s<   e Zd ZdZd
d fddZ fddZd
ddd	Z  ZS )PackStreamCopierzClass to verify a pack stream as it is being read.

    The pack is read from a ReceivableProtocol using read() or recv() as
    appropriate and written out to the given file-like object.
    Nr'   c                    s    t  j||d || _|| _dS )a  Initialize the copier.

        Args:
          read_all: Read function that blocks until the number of
            requested bytes are read.
          read_some: Read function that returns at least one byte, but may
            not return the number of bytes requested.
          outfile: File-like object to write output through.
          delta_iter: Optional DeltaChainIterator to record deltas as we
            read them.
        )r   N)r  r/   outfile_delta_iter)r,   rF  r   rw  
delta_iterr  r-   r.   r/   C  s   
zPackStreamCopier.__init__c                    s   t  ||}| j| |S )z:Read data from the read callback and write it to the file.)r  rf  rw  rp  )r,   r[   r   r   r  r-   r.   rf  S  s   zPackStreamCopier._readc                 C   sz   d}t |  D ]!\}}| jr| j| |dur)|d| dt|  dd q|dur;|d| dd dS dS )	zVerify a pack stream and write it to the output file.

        See PackStreamReader.iterobjects for a list of exceptions this may
        throw.
        r   Nzcopying pack entries: /r   zcopied z pack entries
)rI  rs  rx  recordr_   r   )r,   r=   r   r   r-   r-   r.   verifyY  s    zPackStreamCopier.verifyr)   r1   )r2   r3   r4   r5   r/   rf  r}  r/  r-   r-   r  r.   rv  <  s
    rv  c                 C   sR   t  }|t| t| t|tr|| | S |D ]}|| q| S )z5Compute the SHA for a numeric type and object chunks.)r   r   r!   rE  rw   rD   r!  )typerD  rp   chunkr-   r-   r.   rt   i  s   

rt      c                 C   s   t  }| dt |  }|dk r|| |k s||kr(td| d| d| || | }| | |rJ| t||}|| |t|8 }|s5|S )a  Hash a portion of a file into a new SHA.

    Args:
      f: A file-like object to read from that supports seek().
      start_ofs: The offset in the file to start reading at.
      end_ofs: The offset in the file to end reading at, relative to the
        end of the file.
      buffer_size: A buffer size for reading.
    Returns: A new SHA object updated with data read from the file.
    r   z/Attempt to read beyond file length. start_ofs: z, end_ofs: z, file length: )	r   rh  r   rg  r7  r[   minr   r_   )r   	start_ofsend_ofsr   rp   lengthtodor   r-   r-   r.   compute_file_shau  s   

r  c                   @   s>  e Zd ZdZd7d8ddZedd Zedd	 Zed9d
dZ	edd Z
d8ddZdd Zdd Zdd Zdd ZdefddZdd Zdddefdd Z	d7d!ee fd"d#Z		d7d$ee d!ee fd%d&Zd7d'd(Zd7d)d*Zd:d,d-Zd.d/ Zd8d0d1Zddd2ededefd3d4Z d2ede!ee"f fd5d6Z#dS );PackDataaC  The data contained in a packfile.

    Pack files can be accessed both sequentially for exploding a pack, and
    directly with the help of an index to retrieve a specific object.

    The objects within are either complete or a delta against another.

    The header is variable length. If the MSB of each byte is set then it
    indicates that the subsequent byte is still part of the header.
    For the first byte the next MS bits are the type, which tells you the type
    of object, and whether it is a delta. The LS byte is the lowest bits of the
    size. For each subsequent byte the LS 7 bits are the next MS bits of the
    size, i.e. the last byte of the header contains the MS bits of the size.

    For the complete objects the data is stored as zlib deflated data.
    The size in the header is the uncompressed object size, so to uncompress
    you need to just keep feeding data to zlib until you get an object back,
    or it errors on bad data. This is done here by just giving the complete
    buffer from the start of the deflated object on. This is bad, but until I
    get mmap sorted out it will have to do.

    Currently there are no integrity checks done. Also no attempt is made to
    try and detect the delta case, or a request for an object at the wrong
    position.  It will all just throw a zlib or KeyError.
    Nr'   c                 C   sd   || _ || _d| _|du rt| j d| _n|| _t| jj\}| _tt	t
t	tf f dtd| _dS )aa  Create a PackData object representing the pack in the given filename.

        The file must exist and stay readable until the object is disposed of.
        It must also stay the same size. It will be mapped whenever needed.

        Currently there is a restriction on the size of the pack as the python
        mmap implementation is flawed.
        r>  Nr   i  @)compute_size)r  r  _header_sizer   r  rC  r[   ro  r   rZ   rB   r   rW  _offset_cache)r,   r  r   r   r   r-   r-   r.   r/     s   	zPackData.__init__c                 C   s   t j| jS r)   )r   r   basenamer  rW   r-   r-   r.   r    r   zPackData.filenamec                 C   r   r)   r	  rW   r-   r-   r.   r     r
  zPackData.pathc                 C   s   | t |||dS )N)r   r   )rC   )r   r   r   r-   r-   r.   	from_file  r   zPackData.from_filec                 C   s
   | |dS )N)r  r-   )r   r   r-   r-   r.   	from_path  r   zPackData.from_pathc                 C   s   | j   d S r)   )r  r   rW   r-   r-   r.   r     s   zPackData.closec                 C      | S r)   r-   rW   r-   r-   r.   	__enter__  r   zPackData.__enter__c                 C      |    d S r)   r   r,   exc_typeexc_valexc_tbr-   r-   r.   __exit__  r   zPackData.__exit__c                 C   s   t |tr|  | kS dS r  )rw   r  r  r   r-   r-   r.   r~     s   
zPackData.__eq__c                 C   sV   | j d ur| j S tj| j| _ | j | jk r(| j d| j  d| j d}t|| j S )Nz is too small for a packfile (z < ))r  r   r   getsizer  r  r7  )r,   errmsgr-   r-   r.   	_get_size  s   
zPackData._get_sizec                 C   r   )z+Returns the number of objects in this pack.rn  rW   r-   r-   r.   rY     r
  zPackData.__len__c                 C   s   t | jdd S )z[Calculate the checksum for this pack.

        Returns: 20-byte binary SHA1 digest
        r   r  )r  r  r!  rW   r-   r-   r.   r     r$  zPackData.calculate_checksumFrG   rH   c                c   sr    | j | j | jd u rd S t| jD ]"}| j  }t| j jd|d\}}||_|V  | j t	| t
 qd S )NF)rL  rH   )r  rh  r  ro  r  rg  rT  r[   rn   r_   r   )r,   rH   r   rn   r   r   r-   r-   r.   iter_unpacked  s   



zPackData.iter_unpackedresolve_ext_refc                 c   sD    | j }tj| |d}t|D ]\}}|dur||| |V  qdS )Yield entries summarizing the contents of this pack.

        Args:
          progress: Progress function, called with current and total
            object count.
        Returns: iterator of tuples with (sha, offset, crc32)
        r  N)ro  PackIndexerfor_pack_datarI  )r,   r=   r  rB  indexerr   resultr-   r-   r.   r     s   

zPackData.iterentriesr=   c                 C   s   t | j||dS zReturn entries in this pack, sorted by SHA.

        Args:
          progress: Progress function, called with current and total
            object count
        Returns: Iterator of tuples with (sha, offset, crc32)
        r=   r  )sortedr   )r,   r=   r  r-   r-   r.   r     s   zPackData.sorted_entriesc                 C   L   | j ||d}t|d}t|||  W  d   S 1 sw   Y  dS )zCreate a version 1 file for this data file.

        Args:
          filename: Index filename.
          progress: Progress report function
        Returns: Checksum of index file
        r  wbN)r   r   write_pack_index_v1r   r,   r  r=   r  r   r   r-   r-   r.   create_index_v1#     $zPackData.create_index_v1c                 C   r  )zCreate a version 2 index file for this data file.

        Args:
          filename: Index filename.
          progress: Progress report function
        Returns: Checksum of index file
        r  r  N)r   r   write_pack_index_v2r   r  r-   r-   r.   create_index_v21  r  zPackData.create_index_v2r   c                 C   s>   |dkr| j |||dS |dkr| j|||dS td| )zCreate an  index file for this data file.

        Args:
          filename: Index filename.
          progress: Progress report function
        Returns: Checksum of index file
        r   r  r   zunknown index format )r  r  r   )r,   r  r=   r   r  r-   r-   r.   create_index?  s   zPackData.create_indexc                 C   s   | j dt | j dS )z1Return the expected checksum stored in this pack.r   r'  )r  rh  r   r[   rW   r-   r-   r.   r  R  s   zPackData.get_stored_checksumc                 C   r  )z#Check the consistency of this pack.Nr  r  r-   r-   r.   r   W  r  zPackData.checkrn   c                C   s8   || j ksJ | j| t| jj|d\}}||_|S )z5Given offset in the packfile return a UnpackedObject.rG   )r  r  rh  rT  r[   rn   )r,   rn   rH   r   r   r-   r-   r.   get_unpacked_object_at^  s
   zPackData.get_unpacked_object_atc                 C   s<   z| j | W S  ty   Y nw | j|dd}|j| fS )zGiven an offset in to the packfile return the object that is there.

        Using the associated index the location of an object can be looked up,
        and then the packfile can be asked directly for that object using this
        function.
        FrG   )r  r   r  ro   rx   )r,   rn   r   r-   r-   r.   get_object_ath  s   zPackData.get_object_atNNr1   r)   )Nr   N)$r2   r3   r4   r5   r/   r.  r  r   r   r  r  r   r  r  r~   r  rZ   rY   r   rE   r  r   ResolveExtRefFnr   
ProgressFnr   r  r  r  r  r   rI   r  rB   r   r  r-   r-   r-   r.   r    sX    



	







r  Tc                   @   s  e Zd ZdZdZdZddd*ddZed+defd	d
Z	edddddde
e defddZdeddfddZdeddfddZdd Zd*ddZdd ZdedefddZded ed!ee defd"d#Zded ed!ee fd$d%Zdee fd&d'Zd(d) ZdS ),DeltaChainIteratora  Abstract iterator over pack data based on delta chains.

    Each object in the pack is guaranteed to be inflated exactly once,
    regardless of how many objects reference it as a delta base. As a result,
    memory usage is proportional to the length of the longest delta chain.

    Subclasses can override _result to define the result type of the iterator.
    By default, results are UnpackedObjects with the following members set:

    * offset
    * obj_type_num
    * obj_chunks
    * pack_type_num
    * delta_base     (for delta types)
    * comp_chunks    (if _include_comp is True)
    * decomp_chunks
    * decomp_len
    * crc32          (if _compute_crc32 is True)
    FNr  r'   c                C   s0   || _ || _tt| _tt| _g | _g | _d S r)   )r  _resolve_ext_refr   r   _pending_ofs_pending_ref	_full_ofs	_ext_refs)r,   file_objr  r-   r-   r.   r/     s   


zDeltaChainIterator.__init__	pack_datac                 C   s6   | d |d}| | |jddD ]}|| q|S )Nr  FrG   )set_pack_datar  r|  )r   r  r  walkerr   r-   r-   r.   r    s
   
z DeltaChainIterator.for_pack_datarN   r  r   Packr+   rN   c             	   C   s*  | d |d}| |j t }|D ]#}t|tsJ z|j|}W n ty.   |s, Y qw || qt }	|r|	 }|j
|}
||
 |	| d }|
jtkr\|
j|
j }n(|
jtkrtt t|
jtsnJ |j|
j}W d    n1 sw   Y  |d ur||	vr|| |s:|S )Nr  )r  r   rT   rw   rD   r   r   r   r   popr  r|  ro   rJ  rn   rk   rK  r   r   )r   r   r+   rN   r  r  r  rp   offdoner   base_ofsr-   r-   r.   for_pack_subset  s>   	





z"DeltaChainIterator.for_pack_subsetr   c                 C   st   |j }|j}|tkr||j }| j| | d S |tkr0t|jts%J | j	|j | d S | j
||f d S r)   )ro   rn   rJ  rk   r  ra   rK  rw   rD   r  r  )r,   r   rO  rn   base_offsetr-   r-   r.   r|    s   
zDeltaChainIterator.recordc                 C   s   |j | _ d S r)   )r  )r,   r  r-   r-   r.   r    r   z DeltaChainIterator.set_pack_datac                 c   sL    | j D ]\}}| ||d E d H  q|  E d H  | jr$J t| jd S r)   )r  _follow_chain_walk_ref_chainsr  repr)r,   rn   rO  r-   r-   r.   _walk_all_chains  s
   z#DeltaChainIterator._walk_all_chainsc                 C   s   | j rtdd | j D d S )Nc                 S   s   g | ]}t |qS r-   )r"   r   r-   r-   r.   r         z9DeltaChainIterator._ensure_no_pending.<locals>.<listcomp>)r  r&   rW   r-   r-   r.   _ensure_no_pending  s   z%DeltaChainIterator._ensure_no_pendingc              	   c   s    | j s
|   d S t| j D ]8\}}|| jvrqz	|  |\}}W n	 ty-   Y qw | j| | j| |D ]}| 	|||E d H  q<q|   d S r)   )
r  r  r  r  itemsr   r  ra   r  r  )r,   base_shapendingrO  rD  
new_offsetr-   r-   r.   r    s$   
z#DeltaChainIterator._walk_ref_chainsc                 C   s   t r)   )rK   r,   r   r-   r-   r.   _result  r   zDeltaChainIterator._resultrn   rm   base_chunksc                 C   sl   | j | t| j j| j| jd\}}||_|d u r#|j|ks!J |S |jtv s*J ||_	t
||j|_|S )N)rH   rL  )r  rh  rT  r[   _include_comp_compute_crc32rn   ro   rs   rm   apply_deltari   rl   )r,   rn   rm   r  r   r   r-   r-   r.   _resolve_object  s   
z"DeltaChainIterator._resolve_objectc                 #   s    |||fg}|r?|  \}}}| ||| |  V  t| j  jg | j   g }| fdd|D  |s	d S d S )Nc                 3   s    | ]
}| j  jfV  qd S r)   )rm   rl   )r   r  r   r-   r.   	<genexpr>  s
    
z3DeltaChainIterator._follow_chain.<locals>.<genexpr>)	r  r  r  r   r  rn   r  rp   ra  )r,   rn   rm   r  r  	unblockedr-   r  r.   r    s   z DeltaChainIterator._follow_chainc                 C   s   |   S r)   )r  rW   r-   r-   r.   rV        zDeltaChainIterator.__iter__c                 C   r   r)   )r  rW   r-   r-   r.   ext_refs!  r   zDeltaChainIterator.ext_refsr1   r)   )r2   r3   r4   r5   r  r  r/   r   r  r  r	   rD   rE   r  rI   r|  r  r  r  r  r  r  rZ   r   r  r  r
   rV   r  r-   r-   r-   r.   r  z  sF    %

r  c                   @      e Zd ZdZdd ZdS )UnpackedObjectIteratorz1Delta chain iterator that yield unpacked objects.c                 C   s   |S r)   r-   r  r-   r-   r.   r  (  r   zUnpackedObjectIterator._resultNr2   r3   r4   r5   r  r-   r-   r-   r.   r  %      r  c                   @   s   e Zd ZdZdZdd ZdS )r  z/Delta chain iterator that yields index entries.Tc                 C   s   |  |j|jfS r)   )rp   rn   r\   r  r-   r-   r.   r  1     zPackIndexer._resultN)r2   r3   r4   r5   r  r  r-   r-   r-   r.   r  ,  s    r  c                   @   r  )PackInflaterz1Delta chain iterator that yields ShaFile objects.c                 C   s   |  S r)   )rv   r  r-   r-   r.   r  8  r  zPackInflater._resultNr  r-   r-   r-   r.   r  5  r  r  c                   @   sH   e Zd ZdZdddZdddZdd	eddfd
dZdd Zdd Z	dS )
SHA1ReaderAWrapper for file-like object that remembers the SHA1 of its data.r'   Nc                 C   s   || _ td| _d S N    )r   r   r,   r   r-   r-   r.   r/   ?  s   zSHA1Reader.__init__c                 C   s   | j |}| j| |S r)   )r   r[   r   r   )r,   rV  r   r-   r-   r.   r[   C  s   zSHA1Reader.readFallow_emptyc                 C   sF   | j d}|| j kr|rt|dkr!t| j t|d S d S )Nr'  s(   0000000000000000000000000000000000000000)r   r[   r   r!  r"   r   r   )r,   r  r  r-   r-   r.   	check_shaH  s   zSHA1Reader.check_shac                 C   
   | j  S r)   )r   r   rW   r-   r-   r.   r   Q  r0   zSHA1Reader.closec                 C   r  r)   r   rg  rW   r-   r-   r.   rg  T  r0   zSHA1Reader.tellr1   r)   rt  )
r2   r3   r4   r5   r/   r[   rE   r  r   rg  r-   r-   r-   r.   r  <  s    

	r  c                   @   sD   e Zd ZdZdddZdddZdd	 Zd
d Zdd Zdd Z	dS )
SHA1Writerr  r'   Nc                 C   s   || _ d| _td| _d S )Nr   r  )r   r  r   r  r-   r-   r.   r/   [  s   zSHA1Writer.__init__c                 C   s.   | j | | j| |  jt|7  _d S r)   )r   r   r   rp  r  r_   r   r-   r-   r.   rp  `  s   zSHA1Writer.writec                 C   s<   | j  }t|dksJ | j| |  jt|7  _|S r<  )r   r!  r_   r   rp  r  r   r-   r-   r.   	write_shae  s
   
zSHA1Writer.write_shac                 C   s   |   }| j  |S r)   )r  r   r   r   r-   r-   r.   r   l  s   
zSHA1Writer.closec                 C   r   r)   )r  rW   r-   r-   r.   rn   q  r   zSHA1Writer.offsetc                 C   r  r)   r  rW   r-   r-   r.   rg  t  r0   zSHA1Writer.tellr1   )
r2   r3   r4   r5   r/   rp  r  r   rn   rg  r-   r-   r-   r.   r  X  s    

r  c                 C   s   g }| d> |d@ B }|dL }|r!| |dB  |d@ }|dL }|s| | | tkrR|d@ g}|dL }|rI|d8 }|dd|d@ B  |dL }|s5|| t|S | tkrbt|dks^J ||7 }t|S )	a
  Create a pack object header for the given object info.

    Args:
      type_num: Numeric type of the object.
      delta_base: Delta base offset or ref, or None for whole objects.
      size: Uncompressed object size.
    Returns: A header for a packed object.
    r   rG  r^   rH  r$   r   r   r'  )ra   rJ  insertra  rK  r_   r`  )rO  rk   r   rA  crc   r-   r-   r.   pack_object_headerx  s.   	


r  r]   c                 c   st    | t v r
|\}}nd}t|tr|g}tt| |ttt|V  tj|d}|D ]}|	|V  q*|
 V  dS )zGenerate chunks for a pack object.

    Args:
      type: Numeric type of the object
      object: Object to write
      compression_level: the zlib compression level
    Returns: Chunks
    N)level)rs   rw   rD   r  rq   rr   r_   r   compressobjcompressflush)r~  objectcompression_levelrk   
compressorr   r-   r-   r.   pack_object_chunks  s   	

r  c                 C   sF   d}t |||dD ]}| | |dur|| t||}q	|d@ S )a  Write pack object to a file.

    Args:
      write: Write function to use
      type: Numeric type of the object
      object: Object to write
      compression_level: the zlib compression level
    Returns: Tuple with offset at which the object was written, and crc32
    r   r  Nr   )r  r   r`   r\   )rp  r~  r  rp   r  r\   r  r-   r-   r.   write_pack_object  s   

r  )deltifydelta_window_sizer  r<   r  r  r  c                C   s   t | d d}t|j||||d\}}W d   n1 sw   Y  tdd | D }t | d d}|t|||fW  d   S 1 sHw   Y  dS )a  Write a new pack data file.

    Args:
      filename: Path to the new pack file (without .pack extension)
      container: PackedObjectContainer
      entries: Sequence of (object_id, path) tuples to write
      delta_window_size: Delta window size
      deltify: Whether to deltify pack objects
      compression_level: the zlib compression level
    Returns: Tuple with checksum of pack file and index file
    .packr  r  r  r  Nc                 S   s"   g | ]\}}||d  |d fqS )r   r   r-   )r   kvr-   r-   r.   r     s   " zwrite_pack.<locals>.<listcomp>.idx)r   write_pack_objectsrp  r  r  r  )r  r<   r  r  r  r   r   data_sumr-   r-   r.   
write_pack  s   $r  c                 c   s(    dV  t ddV  t d| V  dS )zYield chunks for a pack header.r?  r   r   N)r   r   )rB  r-   r-   r.   pack_header_chunks  s   r  c                 C   s:   t | dr| j} tjdtdd t|D ]}| | qdS )z4Write a pack header for the given number of objects.rp  z?write_pack_header() now takes a write rather than file argumentr   r   N)hasattrrp  r   r   r   r  )rp  rB  r  r-   r-   r.   write_pack_header  s   

r  other_havesr=   	container
object_idsr
  c                c   s    |d u rt  }d}t| j|dddD ]6\}}|d ur1|d dkr1|d| dt| d  |jtkrJt|j}||v sC||v rJ|V  |d7 }q|d ur[|d	| d
  d S d S )Nr   T)rN   rQ     zchecking for reusable deltas: rz  r{  r   zfound z deltas to reuse
)	rT   rI  rR   r_   r   ro   rK  r"   rk   )r  r  r
  r=   reusedr   r   hexshar-   r-   r.   find_reusable_deltas  s(   

r  window_sizer=   r  c                #   s,     fdd}t t| ||dE dH  dS )a  Generate deltas for pack objects.

    Args:
      objects: An iterable of (object, path) tuples to deltify.
      window_size: Window size; None for default
    Returns: Iterator over type_num, object id, delta_base, content
        delta_base is None for full text entries
    c                  3   sH     D ]} t | tr| | jd ffV  q| d | d j| d ffV  qd S )Nr   r   )rw   r   rO  )er<   r-   r.   objects_with_hints  s   
z0deltify_pack_objects.<locals>.objects_with_hintsr  N)deltas_from_sorted_objectssort_objects_for_delta)r<   r  r=   r  r-   r  r.   deltify_pack_objects  s   r  c                 C   sn   g }| D ]'}t |tr|\}}|d u rd }d }n|\}}n|}||||  |f q|  dd |D S )Nc                 s   s    | ]}|d  V  qdS )r@  Nr-   )r   xr-   r-   r.   r  =  s    z)sort_objects_for_delta.<locals>.<genexpr>)rw   rB   ra   
raw_lengthsort)r<   magicentryr7   hintrO  r   r-   r-   r.   r  +  s   

r  c                 c   s4   |d u rt }t }t| D ]\}}|d ur&|d dkr&|d| d  | }|}ttt|}d }	|D ]3\}
}}||jkrBq7d}g }t	||D ]}|t|7 }||krY n|
| qK|
}	|}ttt|}q7t|j|  |	||dV  ||  |j|f t||kr|  t||ksqd S )Nr  r   zgenerating deltas: r{  )rp   rk   rj   ri   )DEFAULT_PACK_DELTA_WINDOW_SIZEr   rI  r   as_raw_chunksrq   rr   r_   rO  create_deltara   rI   rp   r!  
appendleftr  )r<   r  r=   possible_basesr   orN  winner
winner_lenwinner_basebase_idbase_type_numbase	delta_lendeltar  r-   r-   r.   r  @  sL   

r  )r  r  	ofs_deltar=   r-  c                   sD   t  }|du r
d}|r|tt ||dfS  fdd}|| fS )Create pack data from objects.

    Args:
      objects: Pack objects
    Returns: Tuples with (type_num, hexdigest, delta base, object chunks)
    NFr  c                  3   s4     D ]} t | trt| d V  qt| V  qd S rY  )rw   rB   full_unpacked_objectr$  r  r-   r.   iter_without_path  s   
z/pack_objects_to_data.<locals>.iter_without_path)r_   r  r   )r<   r  r  r-  r=   countr1  r-   r  r.   pack_objects_to_datai  s   

r3  reuse_deltasc                 #   s    t | |rt| t ||dD ]} t| = |V  q|du r$d}|rD| j  dd}	tt fdd|	D ||dE dH  dS  D ]	}
t	| |
 V  qFdS )r.  r	  NFrM   c                 3   s    | ]
}| |j  fV  qd S r)   idr   r$  r  r-   r.   r    s    z,generate_unpacked_objects.<locals>.<genexpr>r  )
dictr  rT   r"   rp   rO   keysr  r  r/  )r  r  r  r  r4  r-  r
  r=   r   objects_to_deltaoidr-   r8  r.   generate_unpacked_objects  s,   
r=  r$  c                 C   s    t | jd d |  |   dS )N)rk   r\   ri   rp   )rI   rO  r   rp   r!  r0  r-   r-   r.   r/    s   
r/  c           
      C   s,   t |}t||||||d}	t| |	||dS )a  Write a new pack data file.

    Args:
      write: write function to use
      container: PackedObjectContainer
      entries: Sequence of (object_id, path) tuples to write
      delta_window_size: Sliding window size for searching for deltas;
                         Set to None for default window size.
      deltify: Whether to deltify objects
      compression_level: the zlib compression level to use
    Returns: Dict mapping id -> (offset, crc32 checksum), pack checksum
    )r  r  r4  r
  num_recordsr  )r_   r=  write_pack_data)
rp  r  r  r  r  r4  r  r
  pack_contents_countpack_contentsr-   r-   r.   write_pack_from_container  s   	rC  r  c                C   s    t ||d\}}t| |||dS )a  Write a new pack data file.

    Args:
      write: write function to use
      objects: Sequence of (object, path) tuples to write
      delta_window_size: Sliding window size for searching for deltas;
                         Set to None for default window size.
      deltify: Whether to deltify objects
      compression_level: the zlib compression level to use
    Returns: Dict mapping id -> (offset, crc32 checksum), pack checksum
    )r  r>  )r3  r@  )rp  r<   r  r  r  rA  rB  r-   r-   r.   r    s   r  c                
   @   sb   e Zd Z					d	dddZdd Zd	d
 Zddddddee dede	dee
 fddZdS )PackChunkGeneratorNr]   Tr'   c                 C   s*   t d| _i | _| j|||||d| _d S )Nr  )r?  recordsr=   r  reuse_compressed)r   csr   _pack_data_chunks_it)r,   r?  rE  r=   r  rF  r-   r-   r.   r/     s   
zPackChunkGenerator.__init__c                 C   r  r)   )rG  r!  rW   r-   r-   r.   
sha1digest  r0   zPackChunkGenerator.sha1digestc                 C   r   r)   )rI  rW   r-   r-   r.   rV      r   zPackChunkGenerator.__iter__)r?  r=   r  rF  rE  r  rF  c             	   c   s   |du r	t |}d}t|D ]}|V  | j| |t |7 }qd}t|D ]\}	}
|
j}|durF|	d dkrF|d|	 d| dd |
jduryz
| j|
j \}}W n t	yn   t
}t|
jtsfJ |
j|
jf}Y nw t}|| |
jf}n|
j}|
jdur|r|
j}nt|||d}d}d}|D ]}|V  t||}| j| |t |7 }q|d	7 }||f| j|
 < ||7 }q'||krtd
| d| | j V  dS )ar  Iterate pack data file chunks.

        Args:
          records: Iterator over UnpackedObject
          num_records: Number of records (defaults to len(records) if not specified)
          progress: Function to report progress to
          compression_level: the zlib compression level
        Returns: Dict mapping id -> (offset, crc32 checksum), pack checksum
        Nr   r  zwriting pack data: rz  r{  r   r  r   z actual records written differs: z != )r_   r  rG  r   rI  ro   r   rk   r   r   rK  rw   rD   ri   rJ  rh   r  r`   r\   rp   r7  r!  )r,   rE  r?  r=   r  rF  rn   r  actual_num_recordsr   r   rO  r  
base_crc32rN  rD  r\   object_sizer-   r-   r.   rH  #  sX   

z$PackChunkGenerator._pack_data_chunks)NNNr]   Tr1   )r2   r3   r4   r/   rJ  rV   r
   rI   rZ   rE   rD   rH  r-   r-   r-   r.   rD  
  s0    
rD  )r?  r=   r  rE  c                C   s0   t ||||d}|D ]}| | q
|j| fS )a  Write a new pack data file.

    Args:
      write: Write function to use
      num_records: Number of records (defaults to len(records) if None)
      records: Iterator over type_num, object_id, delta_base, raw
      progress: Function to report progress to
      compression_level: the zlib compression level
    Returns: Dict mapping id -> (offset, crc32 checksum), pack checksum
    )r?  rE  r=   r  )rD  r   rJ  )rp  rE  r?  r=   r  chunk_generatorr  r-   r-   r.   r@  g  s   
r@  c                 C   s   t | } tdd }|D ]\}}}|t|dd   d7  < qtdD ]}| td||  ||d   || 7  < q$|D ]\}}}|dksMtd| td	|| q@t|d
ks`J | | | 	 S )a  Write a new pack index file.

    Args:
      f: A file-like object to write to
      entries: List of tuples with object name (sha), offset_in_pack,
        and crc32_checksum.
      pack_checksum: Checksum of the pack file.
    Returns: The SHA of the written index file
    c                   S   r8   rY  r-   r-   r-   r-   r.   <lambda>  r;   z%write_pack_index_v1.<locals>.<lambda>Nr   r  r  r   z)pack format 1 only supports offsets < 2Gbr0  r'  )
r  r   rb   r  rp  r   r   	TypeErrorr_   r  )r   r   r   fan_out_tabler   rn   entry_checksumr   r-   r-   r.   r    s   

r  c                 C   sN   t  }| d@ }| dL } | r||dB  | d@ }| dL } | s|| t|S )NrH  r$   r^   )r`  ra   rD   )r   rc   r  r-   r-   r.   _delta_encode_size  s   
rS  i  c                 C   s   t dg}tdD ]}| d|d > @ r(|| |d ? d@  |d  d|> O  < q	tdD ]!}|d|d > @ rN|||d ? d@  |d  dd| > O  < q-t|S )Nr^   r      r   r   r   r   )r`  r  ra   rD   )r   r  scratchr   r-   r-   r.   _encode_copy_operation  s   
rV  c                 c   sV   t | trd| } t |trd|}t | tsJ t |ts#J tt| V  tt|V  td| |d}| D ]l\}}}}}|dkrh|}|| }	|	dkrht|	t	}
t
||
V  ||
7 }|	|
8 }	|	dksQ|dksp|dkr|| }|}|dkrtdgV  t|||d  V  |d8 }|d7 }|dkszt|gV  t||||  V  q<dS )	zUse python difflib to work out how to transform base_buf to target_buf.

    Args:
      base_buf: Base buffer
      target_buf: Target buffer
    r  N)isjunkard   equalr   replacer  rH  )rw   r   r   rD   rS  r_   r   get_opcodesr  _MAX_COPY_LENrV  
memoryview)base_buf
target_bufseqopcodei1i2j1j2
copy_startcopy_lento_copyr   r$  r-   r-   r.   r!    sB   




r!  c                 C   s  t | ts
d| } t |tsd|}g }d}t|}dd }|||\}}|||\}}|t| ks?J | dt|  ||k rt|||d  }|d7 }|d@ rd}	tdD ]}
|d|
> @ ryt|||d  }|d7 }|	||
d	 > O }	q[d}td
D ] }
|dd|
 > @ rt|||d  }|d7 }|||
d	 > O }q|dkrd}|	| |k s|	| |ks||krn(|| |	|	|   n|dkr|||||   ||7 }ntd||k sC||krtd||d |t|krtd|S )zBased on the similar function in git's patch-delta.c.

    Args:
      src_buf: Source buffer
      delta: Delta instructions
    r  r   c                 S   s^   d}d}| r+t | ||d  }|d7 }||d@ |> O }|d7 }|d@ s)	 ||fS | s||fS )Nr   r   ir$   r^   )rb   )r,  r   r   r   cmdr-   r-   r.   get_delta_header_size  s   z*apply_delta.<locals>.get_delta_header_sizez vs r   r^   r   r   r@  r  zInvalid opcode 0zdelta not empty: Nzdest size incorrect)	rw   rD   r   r_   rb   r  ra   r   rE  )src_bufr,  outr   delta_lengthrj  src_size	dest_sizeri  cp_offr   r  cp_sizer-   r-   r.   r    s\   



"
 r  r   r   c           	   	   C   sf  t | } | d | tdd tdd }|D ]\}}}|t|dd   d7  < qg }tdD ]}| td	||  ||d   || 7  < q4|D ]
\}}}| | qP|D ]\}}}| td	| q]|D ]%\}}}|d
k r| td	| qn| td	d
t|  || qn|D ]}| td| qt|dksJ | | | 	 S )a  Write a new pack index file.

    Args:
      f: File-like object to write to
      entries: List of tuples with object name (sha), offset_in_pack, and
        crc32_checksum.
      pack_checksum: Checksum of the pack file.
    Returns: The SHA of the index file written
    r   r  r   c                   S   r8   rY  r-   r-   r-   r-   r.   rO  B	  r;   z%write_pack_index_v2.<locals>.<lambda>Nr   r  r   r=  s   >Qr'  )
r  rp  r   r   r   rb   r  r_   ra   r  )	r   r   r   rQ  r   rn   rR  
largetabler   r-   r-   r.   r  3	  s0   

r  c                   @   s  e Zd ZU dZeeg ef  ed< eeg ef  ed< ee ed< ee ed< 	dTdee	 ddfd	d
Z
edd Zedd Zdd ZedefddZedefddZdUddZdd Zdd Zdd ZdefddZdefdd Zd!d" ZdUd#d$ZdUd%d&Zdefd'd(Zd)d* Zd+ede fd,d-Z!d+ede"eef fd.d/Z#d+ede$fd0d1Z%de&e$ fd2d3Z'd4d5d6e(e) d7e de&e$ fd8d9Z*d4d4d4d:d6e(e) d;e d7e d<e de&e+ f
d=d>Z,dVd?d@Z-dTdAee defdBdCZ.dDede"ee ee/f fdEdFZ0	dTdGedHede"ee(e f fdIdJZ1	dTdKee2 de&e3 fdLdMZ4	dTdKee2 de&e3 fdNdOZ5d4dPdQdDed;e d<e de+fdRdSZ6dS )Wr  zA Git pack object.
_data_load	_idx_load_data_idxNr  r'   c                    sP   | _ d  _d  _ j d  _ j d  _ fdd _ fdd _| _d S )Nr  r  c                      
   t  jS r)   )r  
_data_pathr-   rW   r-   r.   rO  o	     
 zPack.__init__.<locals>.<lambda>c                      rw  r)   )r   	_idx_pathr-   rW   r-   r.   rO  p	  ry  )	_basenameru  rv  rz  rx  rs  rt  r  )r,   r  r  r-   rW   r.   r/   g	  s   
zPack.__init__c                 C   s   | d}||_ ||_|S )z]Create a new pack object from callables to load pack data and
        index objects.
         )rs  rt  )r   data_fnidx_fnrc   r-   r-   r.   from_lazy_objectss	  s   zPack.from_lazy_objectsc                 C   s,   | d}||_ d|_||_d|_|  |S )z:Create a new pack object from pack data and index objects.r|  N)ru  rs  rv  rt  check_length_and_checksum)r   r   r(  rc   r-   r-   r.   from_objects}	  s   zPack.from_objectsc                 C   r  )z2The SHA over the SHAs of the objects in this pack.)r   r   rW   r-   r-   r.   r   	  r   z	Pack.namec                 C   s,   | j du r| js
J |  | _ |   | j S )z The pack data object being used.N)ru  rs  r  rW   r-   r-   r.   r   	  s
   


z	Pack.datac                 C   s$   | j du r| js
J |  | _ | j S )zLThe index being used.

        Note: This may be an in-memory index
        N)rv  rt  rW   r-   r-   r.   r   	  s   


z
Pack.indexc                 C   s0   | j d ur
| j   | jd ur| j  d S d S r)   )ru  r   rv  rW   r-   r-   r.   r   	  s
   


z
Pack.closec                 C   r  r)   r-   rW   r-   r-   r.   r  	  r   zPack.__enter__c                 C   r  r)   r  r  r-   r-   r.   r  	  r   zPack.__exit__c                 C   s   t | t|o| j|jkS r)   )rw   r~  r   r   r-   r-   r.   r~   	  s   zPack.__eq__c                 C   r   )zNumber of entries in this pack.)r_   r   rW   r-   r-   r.   rY   	  r   zPack.__len__c                 C   s   | j j d| jdS )N(r  )r   r2   r{  rW   r-   r-   r.   r   	  s   zPack.__repr__c                 C   r   )z7Iterate over all the sha1s of the objects in this pack.)r   r   rW   r-   r-   r.   rV   	  r   zPack.__iter__c                 C   sh   t | jt | jksJ dt | j dt | j d| j }| j }||kr2tt|t|dS )z@Sanity check the length and checksum of the pack index and data.zLength mismatch: z (index) != z (data)N)r_   r   r   r   r  r   r"   )r,   idx_stored_checksumdata_stored_checksumr-   r-   r.   r  	  s   

zPack.check_length_and_checksumc                 C   s.   | j   | j  |  D ]}|  qdS )zCheck the integrity of this pack.

        Raises:
          ChecksumMismatch: if a checksum for the index or data is wrong
        N)r   r   r   iterobjectsr9   r-   r-   r.   r   	  s
   


z
Pack.checkc                 C   r  r)   )r   r  rW   r-   r-   r.   r  	  r0   zPack.get_stored_checksumc                 C   s   dd |   D S )Nc                 S   s   g | ]}|d fqS r)   r-   r7  r-   r-   r.   r   	  r  z$Pack.pack_tuples.<locals>.<listcomp>)r  rW   r-   r-   r.   pack_tuples	  r  zPack.pack_tuplesr   c                 C   s(   z	| j | W dS  ty   Y dS w )z3Check whether this pack contains a particular SHA1.TF)r   r   r   r?   r-   r-   r.   r@   	  s   zPack.__contains__c                 C   s<   | j |}| j|\}}| |||\}}|d|fS r  )r   r   r   r  resolve_objectr   )r,   r   rn   obj_typer7   rO  rD  r-   r-   r.   get_raw	  s   zPack.get_rawc                 C   s   |  |\}}tj|||dS )zRetrieve the specified SHA1.)rp   )r  r   from_raw_string)r,   r   r~  uncompr-   r-   r.   rA   	  s   zPack.__getitem__c                 C   s   t tj| j| jdS )z&Iterate over the objects in this pack.r  )r   r  r  r   r  rW   r-   r-   r.   r  	  s   zPack.iterobjectsFrM   r+   rN   c                   s"    fddt j|  || jdD S )Nc                 3   s    | ]
}|j  v r|V  qd S r)   r5  )r   uor*   r-   r.   r  	  s    
z*Pack.iterobjects_subset.<locals>.<genexpr>r  )r  r  r  rP   r-   r*   r.   rO   	  s   
zPack.iterobjects_subset)rH   rN   rQ   rH   rQ   c             	   c   s    t t}i }t|}| j|dD ]\}| }	|	||j< t|	}
|
|v rZ|jtkrRt	|j
ts1J |j|j
 }z|| |_
W n tyN   || | Y qw t|_|V  ||
 ||jg D ]}t|_|	|_
|V  qaq|rrJ |sz|r|t|d S d S )NrG   )r   r   rT   r  rp   rn   r"   ro   rJ  rw   rk   rZ   r   ra   rK  remover  r&   )r,   r+   rH   rN   rQ   ofs_pendingofsr  r   rp   r  r  childr-   r-   r.   rR    
  s<   


zPack.iter_unpacked_subsetc                 c   sN    dd | j  D }| jj|dD ]}||j \}}||_||_|V  qd S )Nc                 S   s   i | ]
\}}}|||fqS r-   r-   )r   rp   r  r\   r-   r-   r.   
<dictcomp>%
  s    z&Pack.iter_unpacked.<locals>.<dictcomp>rG   )r   r   r   r  rn   rg   r\   )r,   rH   ofs_to_entriesr   rp   r\   r-   r-   r.   r  $
  s   zPack.iter_unpackedmsgc                 C   sd   | j  d}t|d}|r || |d W d   |S W d   |S 1 s+w   Y  |S )a'  Add a .keep file for the pack, preventing git from garbage collecting it.

        Args:
          msg: A message written inside the .keep file; can be used later
            to determine whether or not a .keep file is obsolete.
        Returns: The path of the .keep file, as a string.
        z.keepr     
N)r{  r   rp  )r,   r  keepfile_namekeepfiler-   r-   r.   keep.
  s   


z	Pack.keeprp   c                 C   sf   z| j |}W n ty   d}Y nw |r| j|\}}n| jr*| |\}}nt||||fS )z8Get the object for a ref SHA, only looking in this pack.N)r   r   r   r   r  r  )r,   rp   rn   r~  r7   r-   r-   r.   get_ref=
  s   
zPack.get_refrn   r~  c                 C   s  |}|}|}g }|t v rj|}	|du r| j}|tkr1|\}
}||
 }| j|\}}t|ts0J n-|tkr^|\}}t|trDt	|dksFJ ||\}}}t|tsTJ ||	kr^t
t|||	||f |t v s|}t|D ]\}	}}t||}|	dur||f| jj|	< qp||fS )z|Resolve an object, possibly resolving deltas when necessary.

        Returns: Tuple with object type and contents.
        Nr'  )rs   r  rJ  r   r  rw   rZ   rK  rD   r_   r&   r"   ra   reversedr  r  )r,   rn   r~  r7   r  r  	base_typebase_objdelta_stackprev_offsetdelta_offsetr,  r  rD  
delta_typer-   r-   r.   r  L
  s:   	
zPack.resolve_objectr=   c                 C      | j j|| jdS )r  r  )r   r   r  r,   r=   r-   r-   r.   r   z
     
zPack.entriesc                 C   r  r  )r   r   r  r  r-   r-   r.   r   
  r  zPack.sorted_entriesT)rH   rQ   c                C   sX   | j |}| jj||d}|jtkr*|r*t|jtsJ | j 	||j |_t
|_|S )zGet the unpacked object for a sha.

        Args:
          sha: SHA of object to fetch
          include_comp: Whether to include compression data in UnpackedObject
        rG   )r   r   r   r  ro   rJ  rw   rk   rZ   r   rK  )r,   rp   rH   rQ   rn   r   r-   r-   r.   rL   
  s   	zPack.get_unpacked_objectr)   r1   rt  )7r2   r3   r4   r5   r   r   r  r   r   r  r/   r   r  r  r   r.  r   r   r   r  r  r~   rZ   rY   rC   r   rV   r  r   rD   r  r  rE   r@   rB   r  r   rA   r
   r  r	   r   rO   rI   rR   r  r  r   r  r  r  r   r   r   rL   r-   r-   r-   r.   r  ^	  s   
 

	








$
 
/

r  )r  r=   r   c                C   s   |  d t| j\}}|r!|  d t| j|t|  |   t| dd}|  dtj	 g }t
|D ]<\}	}
|durL|d|	 dt| dd t|
d	ksTJ ||
\}}|  }t| j||||d
}||
||f q4| }| | ||fS )zExtend a pack file with more objects.

    The caller should make sure that object_ids does not contain any objects
    that are already in the pack
    r   r   r  Nzwriting extra base objects: rz  r{  r   r'  )rp   r  )rh  rC  r[   r  rp  r_   r  r  r   r   rI  r   rg  r  ra   r!  )r   r  r  r  r=   _versionrB  new_shaextra_entriesr   	object_idrO  r   rn   r\   rr  r-   r-   r.   extend_pack
  s8   


r  )r  r   r)   )r   r   r  )r]   )Nr]   r1   r  )NNTTNN)NNTr]   N)r5   r`   collectionsr   r   
contextlibr   ior   r   cdifflibr   r   ModuleNotFoundErrordifflibr   r   sysr   r   collections.abcr	   r
   r   hashlibr   	itertoolsr   r   r   r   typingr   r   r   r   r   r   r   r   ImportErrorr   platformerrorsr   r   r   r   	lru_cacher   r<   r   r   r    r!   r"   rJ  rK  rs   r  PACK_SPOOL_FILE_MAX_SIZErB   rD   rZ   r   r   r  rC   r  PackHint	Exceptionr&   r6   rF   rU   re   rf   rI   ru  rE   r   r   r   r   r   r   r   r   r   r  r   r   rC  rE  rT  rW  rX  rv  rt   r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  rT   r  r  r  r  r3  r=  r/  rC  r  rD  r@  r  rS  r\  rV  r!  r  r  write_pack_indexr  r  dulwich._packr-   r-   r-   r.   <module>   s  $

$	
d
D
]/ 8)	

M -
 h ,	 


 





,
,
	
)

,
a
.G
(  Q

6