
    .Ph<                        d Z ddlZddlmZmZmZmZ ddlmZm	Z	m
Z
mZmZ ddlmZmZmZmZmZmZmZmZmZmZmZ 	 ddlmZ n# e$ r Y nw xY wddlmZmZmZm Z m!Z!m"Z"m#Z# ddl$m%Z%m&Z&m'Z'm(Z(m)Z) dd	l*m+Z+ dd
l,m-Z- ddl.m/Z/ ddl0Z0ddl1Z1ddl2Z2ddl3Z3dZ4d Z5ddZ6 G d d          Z7 G d de	          Z8 G d de	          Z9 G d de	          Z:dS )z3Contains PackIndexFile and PackFile implementations    N)	BadObjectAmbiguousObjectNameUnsupportedOperation
ParseError)mman	LazyMixinunpack_from
bin_to_hexbyte_ord)create_pack_object_headerpack_object_header_infois_equal_canonical_shatype_id_to_type_mapwrite_objectstream_copy
chunk_sizedelta_types	OFS_DELTA	REF_DELTAmsb_sizePackIndexFile_sha_to_index)OInfoOStream	OPackInfoOPackStreamODeltaStreamODeltaPackInfoODeltaPackStream)DecompressMemMapReaderDeltaApplyReader
Sha1Writer
NullStreamFlexibleSha1Writer)pack)crc32)	NULL_BYTE)PackIndexFilePackFile
PackEntityc                 j   |                      |                                          }t          |          \  }}}d}d}|t          k    rZ|}	t	          ||	                   }
|	dz  }	|
dz  }|
dz  r/t	          ||	                   }
|	dz  }	|dz  }|dz  |
dz  z   }|
dz  /|}|	}n|t
          k    r|dz   }|||         }n|}||z   }|rDt          ||d         d|          }||t          ||||          fS |t          |||||          fS ||t          |||          fS |t          ||||          fS )a&  
    :return: Tuple(abs_data_offset, PackInfo|PackStream)
        an object of the correct type according to the type_id  of the object.
        If as_stream is True, the object will contain a stream, allowing  the
        data to be read decompressed.
    :param data: random accessible data containing all required information
    :parma offset: offset in to the data at which the object information is located
    :param as_stream: if True, a stream object will be returned that can read
        the data, otherwise you receive an info object onlyN               F)
use_regionbufferr   r   r   r   r    r   r   r   r   )cursoroffset	as_streamdatatype_iduncomp_sizedata_rela_offsettotal_rela_offset
delta_infoicdelta_offsetabs_data_offsetstreams                 J/var/www/html/test/jupyter/venv/lib/python3.11/site-packages/gitdb/pack.pypack_object_atrB   L   s    V$$++--D-DT-J-J*G[*J )T!W	Q4x$h 	<a!!AFAAL(A-!d(;L	 $h 	< "
	I		,r1*+<<=

 -00O 
]'->-?-?(@%UU"Kf$U$UUU"$4VWkS]_e$f$fff"Ifg{$K$KKK"N67KQ[$\$\\\    c                    d}d}|du}d}|r|}	  | t                     }|t          |          z  }|                    |          }	|t          |	          z  } ||	           |rt          |	|          }t          |          t           k    rn|                                }	|t          |	          z  } ||	           |rt          |	|          }|||fS )a  Copy a stream as read from read function, zip it, and write the result.
    Count the number of written bytes and return it
    :param base_crc: if not None, the crc will be the base for all compressed data
        we consecutively write and generate a crc32 from. If None, no crc will be generated
    :return: tuple(no bytes read, no bytes written, crc32) crc might be 0 if base_crc
        was falser   N)r   lencompressr&   flush)
readwritezstreambase_crcbrbwwant_crccrcchunk
compresseds
             rA   write_stream_to_packrR      s    
B	
Bt#H
C Z  
c%jj%%e,,

c*ooj 	)
C((C u::## J#j//B	E* %J$$ C=rC   c                   (    e Zd ZdZdZd Zd Zd ZdS )IndexWriterzUtility to cache index information, allowing to write all information later
    in one go to the given stream
    **Note:** currently only writes v2 indices_objsc                 ,    t                      | _        d S N)listrU   selfs    rA   __init__zIndexWriter.__init__   s    VV


rC   c                 @    | j                             |||f           dS )z&Append one piece of object informationN)rU   append)rZ   binsharO   r4   s       rA   r]   zIndexWriter.append   s%    
63/00000rC   c                 p   | j                             d            t          |          }|j        } |t          j                    |t          dt          j                             t          d          }| j         D ]+}|t          |d         d                   xx         dz  cc<   ,t          d          D ]6}||         } |t          d|                     ||dz   xx         |z  cc<   7 |t          d|d                               |d                    d	 | j         D                                  | j         D ]$} |t          d|d         d
z                       %t                      }| j         D ]V}|d         }	|	dk    r*|                    |	           dt          |          z   dz
  }	 |t          d|	d
z                       W|D ]}	 |t          d|	                     t          |          dk    sJ  ||           |                    d          }
 ||
           |
S )zWrite the index file using the given write method
        :param pack_sha: binary sha over the whole pack that we index
        :return: sha1 binary sha over all index file contentsc                     | d         S )Nr    )os    rA   <lambda>z#IndexWriter.write.<locals>.<lambda>   s
    ad rC   )key>L(   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r,      rC   c              3   &   K   | ]}|d          V  dS )r   Nra   ).0ts     rA   	<genexpr>z$IndexWriter.write.<locals>.<genexpr>   s&      44A1Q4444444rC          i        >Qr0   Fas_hex)rU   sortr$   rI   r(   index_v2_signaturer%   index_version_defaultrX   r   rangejoinr]   rE   sha)rZ   pack_sharI   
sha_writer	sha_writetmplistri   r<   vofsrv   s              rA   rI   zIndexWriter.write   s{   
 	
NN+++'..
$		-2333	$t]@AABBB z"" 	, 	,AHQqT!W%%&&&!+&&&&s 	  	 A
AId4mm$$$AENNNaNNNN	$tWS\**+++ 		#((4444444555  	5 	5AId41
!2334444 && 	4 	4AA$CZs### 3w<</!3Id4z!1223333  	' 	'CId4oo&&&& 8}}""""	(nnEn**c



rC   N)__name__
__module____qualname____doc__	__slots__r[   r]   rI   ra   rC   rA   rT   rT      sR        2 2 I  1 1 16 6 6 6 6rC   rT   c                        e Zd ZdZdZdZdZ fdZd Zd Z	d Z
d	 Zd
 Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd e            v rd Z xZS  xZS )r(   zmA pack index provides offsets into the corresponding pack, allowing to find
    locations for offsets faster.i  s   tOcrl   c                 V    t                                                       || _        d S rW   )superr[   
_indexpath)rZ   	indexpath	__class__s     rA   r[   zPackIndexFile.__init__  s$    #rC   c                 F    t          j        | j                   d | _        d S rW   )r   force_map_handle_removal_winr   _cursorrY   s    rA   closezPackIndexFile.close  s     )$/:::rC   c                    |dk    r(| j                                         dd         | _        d S |dk    r(| j                                         dd          | _        d S |dk    rt          j        | j                                                  | _         t          j                    dk    rp| j                                         t          j                    k    rDt          d| j        | j                                         t          j                    fz            d S d S | j                                         }|d d         | j
        k    rdpd	| _        | j        dk    r/t          d
|d          d         }|| j        k    sJ d|z              dD ]+}t          | |t          | d|| j        fz                       ,|                                  d S )N_packfile_checksumr   r   zsThe index file at %s is too large to fit into a mapped window (%i > %i). This is a limitation of the implementation   rl   r,   re   zUnsupported index version: %i)entryr4   rv   rO   z_%s_v%i)r   mapr   r   make_cursorr   r1   window_size	file_sizeAssertionErrorrr   _versionr	   setattrgetattr_initialize)rZ   attrmmap
version_idfnames        rA   _set_cache_zPackIndexFile._set_cache_  s   '''&*l&6&6&8&8S&AD###)))&*l&6&6&8&8&>D###Y  +DO<<GGIIDL!!A%%$,*@*@*B*BTEUEWEW*W*W$  &[OT\%;%;%=%=t?O?Q?Q^S &S T T T &%*W*W <##%%D!"1"X)@@FQL1DM}!!(tQ77:
!T]2224SV`4`222
 ; X XeWT9t}?U3U%V%VWWWW
 rC   c                 d    t          d| j                                        d|dz  z             dz   S )z!:return: tuple(offset, binsha, 0)z>L20s      r   r	   r   r   rZ   r<   s     rA   	_entry_v1zPackIndexFile._entry_v13  s/    7DL$4$4$6$6q2vFFNNrC   c                 j    t          d| j                                        d|dz  z             d         S )zsee ``_offset_v2``re   r   r   r   r   r   s     rA   
_offset_v1zPackIndexFile._offset_v17  s/    4!1!1!3!3TAF]CCAFFrC   c                 `    d|dz  z   dz   }| j                                         ||dz            S )zsee ``_sha_v2``r   r   r   r0   r   r   rZ   r<   bases      rA   _sha_v1zPackIndexFile._sha_v1;  s5    q2v"|!!$tby.11rC   c                     dS )unsupportedr   ra   r   s     rA   _crc_v1zPackIndexFile._crc_v1@  s    qrC   c                 ~    |                      |          |                     |          |                     |          fS )z#:return: tuple(offset, binsha, crc))
_offset_v2_sha_v2_crc_v2r   s     rA   	_entry_v2zPackIndexFile._entry_v2G  s/    ""DLLOOT\\!__EErC   c                     t          d| j                                        | j        |dz  z             d         }|dz  r<t          d| j                                        | j        |dz  dz  z             d         }|S )z:return: 32 or 64 byte offset into pack files. 64 byte offsets will only
            be returned if the pack is larger than 4 GiB, or 2^32re   r   r   rm   rn   l       )r	   r   r   _pack_offset_pack_64_offset)rZ   r<   r4   s      rA   r   zPackIndexFile._offset_v2K  s     T4<#3#3#5#5t7H1q57PQQRST
 J 	q t|'7'7'9'94;OSY\gSgklRl;lmmnopF rC   c                 d    | j         |dz  z   }| j                                        ||dz            S )z;:return: sha at the given index of this file index instancer0   )_sha_list_offsetr   r   r   s      rA   r   zPackIndexFile._sha_v2Y  s4    $q2v-|!!$tby.11rC   c                 t    t          d| j                                        | j        |dz  z             d         S )z.:return: 4 bytes crc for the object at index ire   r   r   )r	   r   r   _crc_list_offsetr   s     rA   r   zPackIndexFile._crc_v2^  s5    4!1!1!3!3T5JQQRU5RSSTUVVrC   c                 D   |                      | j        dk    dz            | _        | j        dk    rn| j        |                                 dz  z   | _        | j        |                                 dz  z   | _        | j        |                                 dz  z   | _        dS dS )zinitialize base datarl   r   r0   r   N)_read_fanoutr   _fanout_tabler   sizer   r   r   rY   s    rA   r   zPackIndexFile._initializef  s    !..0Ba/GHH=A$($9DIIKK"<L$LD! $ 5		a GD#'#4tyy{{Q#FD    rC   c           
          | j                                         }t                      }|j        }t	          d          D ](} |t          d|||dz  z             d                    )|S )z%Generate a fanout table from our data   re   r   r   )r   r   rX   r]   rt   r	   )rZ   byte_offsetdoutr]   r<   s         rA   r   zPackIndexFile._read_fanoutp  sn    Lffs 	A 	AAF;tQa!e(;<<Q?@@@@
rC   c                     | j         S rW   r   rY   s    rA   versionzPackIndexFile.version}  s
    }rC   c                     | j         d         S )z4:return: amount of objects referred to by this indexrf   )r   rY   s    rA   r   zPackIndexFile.size  s    !#&&rC   c                     | j         S )z":return: path to the packindexfile)r   rY   s    rA   pathzPackIndexFile.path  s
    rC   c                 D    | j                                         dd         S )z@:return: 20 byte sha representing the sha1 hash of the pack filer   r   r   rY   s    rA   packfile_checksumzPackIndexFile.packfile_checksum  s    |!!#c'**rC   c                 D    | j                                         dd         S )zB:return: 20 byte sha representing the sha1 hash of this index filer   Nr   rY   s    rA   indexfile_checksumz PackIndexFile.indexfile_checksum  s    |!!#$$''rC   c                 ~     j         dk    rxt          j        d          }|                     j                                         j         j                            t          j        dk    r|	                                 |S t           fdt                                                     D                       S )z:return: sequence of all offsets in the order in which they were written

        **Note:** return value can be random accessed, but may be immmutablerl   Ilittlec              3   B   K   | ]}                     |          V  d S rW   )r4   )rh   indexrZ   s     rA   rj   z(PackIndexFile.offsets.<locals>.<genexpr>  s/      LLU++LLLLLLrC   )r   array	frombytesr   r   r   r   sys	byteorderbyteswaptuplert   r   )rZ   as   ` rA   offsetszPackIndexFile.offsets  s     =AC  AKK((**4+<T=Q+QRSSS }((

HLLLLtyy{{9K9KLLLLLLrC   c                     t          |d                   }| j        }d}|dk    r| j        |dz
           }| j        |         }||k     r/||z   dz  } ||          }||k     r|}n||k    r|S |dz   }||k     /dS )z
        :return: index usable with the ``offset`` or ``entry`` method, or None
            if the sha was not found in this pack index
        :param sha: 20 byte sha to lookupr   r,   rl   N)r   rv   r   )rZ   rv   
first_byteget_shalohimidmid_shas           rA   sha_to_indexzPackIndexFile.sha_to_index  s    
 c!f%%
(??#JN3B
+ 2gg7q.CgcllGW}}
1W 2gg trC   c                    t          |          dk     rt          d          t          |t                    s
J d            t	          |d                   }| j        }d}|dk    r| j        |dz
           }| j        |         }|t          dt          |          z
  z  z   }||k     r0||z   dz  } ||          }	||	k     r|}n||	k    r|}n|dz   }||k     0||                                 k     r` ||          }
t          |||
          rDd}|dz   |                                 k     r ||dz             }|r||
k    rt          |          |S dS )af  
        :return: index as in `sha_to_index` or None if the sha was not found in this
            index file
        :param partial_bin_sha: an at least two bytes of a partial binary sha as bytes
        :param canonical_length: length of the original hexadecimal representation of the
            given partial binary sha
        :raise AmbiguousObjectName:rl   z'Require at least 2 bytes of partial shazpartial_bin_sha must be bytesr   r,   r0   N)rE   
ValueError
isinstancebytesr   rv   r   r'   r   r   r   )rZ   partial_bin_shacanonical_lengthr   r   r   r   
filled_shar   r   cur_shanext_shas               rA   partial_sha_to_indexz"PackIndexFile.partial_sha_to_index  s    !##FGGG/511RR3RRRRoa011
(??#JN3B
+ %yB_9M9M4M'NN
 2gg7q.CgcllGG##w&&1W 2gg 		gbkkG%&6QQ 6DIIKK''&wrAvH ?G 3 3-o>>>	 trC   r   c                 "    t          | |          S rW   r   rZ   rv   s     rA   r   zPackIndexFile.sha_to_index  s    -dC888rC   )r}   r~   r   r   r   rr   rs   r[   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   globals__classcell__)r   s   @rA   r(   r(      s       % %  #$ $ $ $ $  # # #PO O OG G G2 2 2
  F F F  2 2 2
W W WG G G    ' ' '  + + +( ( (M M M"  41 1 1f $wwyy00	9 	9 	9 	9 	9 	9 	9 1000rC   r(   c                   x    e Zd ZdZdZdZdZdZdZd Z	d Z
d	 ZddZd Zd Zd Zd Zd Zd Zd Zd ZddZdS )r)   a  A pack is a file written according to the Version 2 for git packs

    As we currently use memory maps, it could be assumed that the maximum size of
    packs therefore is 32 bit on 32 bit systems. On 64 bit systems, this should be
    fine though.

    **Note:** at some point, this might be implemented using streams as well, or
    streams are an alternate path in the case memory maps cannot be created
    for some reason - one clearly doesn't want to read 10GB at once in that
    case)	_packpathr   _sizer   iKCAPrl      r0   c                     || _         d S rW   r   )rZ   packpaths     rA   r[   zPackFile.__init__  s    !rC   c                 F    t          j        | j                   d | _        d S rW   )r   r   r   r   rY   s    rA   r   zPackFile.close  s     )$.999rC   c                    t          j        | j                                                  | _        t          d| j                                        d          \  }| _        | _        || j	        k    rt          d|z            d S )N>LLLr   zInvalid pack signature: %i)r   r   r   r1   r   r	   r   r   r   pack_signaturer   )rZ   r   r7   s      rA   r   zPackFile._set_cache_  s{    '77BBDD .9AQAQASASUV-W-W*
 d)))9GCDDD *)rC   Tc              #     K   | j         }|                                | j        z
  }|p| j        }t	                      }||k     rt          ||d          \  }}t          |j        |j        |j	        t                     |j        j        |j	        k    sJ |||j        z
  |j                                        z   z  }|r|j                            d           |V  ||k     dS dS )z7Handle the actual iteration of objects within this packTr   N)r   r   footer_sizefirst_object_offsetr#   rB   r   rH   rI   r   r   r@   _brpack_offsetcompressed_bytes_readseek)	rZ   start_offsetr5   r=   content_size
cur_offsetnulldata_offsetostreams	            rA   _iter_objectszPackFile._iter_objects   s      L{{}}t'77!=T%=
||<''#1!Z#F#F K dj',
KKK>%5555;)<<@d@d@f@fffJ
  '##A&&&MMM <''''''rC   c                     | j         S )z2:return: The amount of objects stored in this pack)r   rY   s    rA   r   zPackFile.size:  
    zrC   c                     | j         S )z!:return: the version of this packr   rY   s    rA   r   zPackFile.version>  s
    }rC   c                 X    | j                                                                         S )z
        :return: read-only data of this pack. It provides random access and usually
            is a memory map.
        :note: This method is unsafe as it returns a window into a file which might be larger than than the actual window size)r   r1   r   rY   s    rA   r6   zPackFile.dataB  s$     |&&((,,...rC   c                     | j                             | j                                         dz
                                            dd         S )zE:return: 20 byte sha1 hash on all object sha's contained in this filer0   N)r   r1   r   r2   rY   s    rA   checksumzPackFile.checksumJ  s@    |&&t|'='='?'?"'DEELLNNqqqQQrC   c                     | j         S )z:return: path to the packfiler   rY   s    rA   r   zPackFile.pathN  s
    ~rC   c                     t                      }| j        }	 t          ||d          d         }|                    |           |j        t
          k    r|j        |j        z
  }nnN|S )a  
        :return: list of pack streams which are required to build the object
            at the given offset. The first entry of the list is the object at offset,
            the last one is either a full object, or a REF_Delta stream. The latter
            type needs its reference object to be locked up in an ODB to form a valid
            delta chain.
            If the object at offset is no delta, the size of the list is 1.
        :param offset: specifies the first byte of the object within this packTr,   )rX   r   rB   r]   r7   r   r  r;   )rZ   r4   r   r=   r  s        rA   collect_streamszPackFile.collect_streamsU  sq     ffL		$Q55a8GJJw)++ ,w/AA
 		 
rC   c                 H    t          | j        |p| j        d          d         S )zRetrieve information about the object at the given file-absolute offset

        :param offset: byte offset
        :return: OPackInfo instance, the actual type differs depending on the type_id attributeFr,   rB   r   r  rZ   r4   s     rA   infozPackFile.infor  s'    
 dlF,Nd6NPUVVWXYYrC   c                 H    t          | j        |p| j        d          d         S )zRetrieve an object at the given file-relative offset as stream along with its information

        :param offset: byte offset
        :return: OPackStream instance, the actual type differs depending on the type_id attributeTr,   r  r  s     rA   r@   zPackFile.streamy  s'    
 dlF,Nd6NPTUUVWXXrC   r   c                 0    |                      |d          S )a  
        :return: iterator yielding OPackStream compatible instances, allowing
            to access the data in the pack directly.
        :param start_offset: offset to the first object to iterate. If 0, iteration
            starts at the very first object in the pack.

        **Note:** Iterating a pack directly is costly as the datastream has to be decompressed
        to determine the bounds between the objectsTr5   r  )rZ   r  s     rA   stream_iterzPackFile.stream_iter  s     !!,$!???rC   N)Tr   )r}   r~   r   r   r   r   pack_version_defaultr  r   r[   r   r   r  r   r   r6   r  r   r  r  r@   r  ra   rC   rA   r)   r)     s       	 	 >IN  K" " "  
E 
E 
E   4    / / /R R R    :Z Z ZY Y Y	@ 	@ 	@ 	@ 	@ 	@rC   r)   c                       e Zd ZdZdZeZeZd Z	d Z
d Zd Zd Zdd	Zd
 Zd Zd Zd Zd Zd ZddZd Zd Zd Zd Zeddej        fd            Zedej        fd            ZdS )r*   zqCombines the PackIndexFile and the PackFile into one, allowing the
    actual objects to be resolved and iterated)_index_pack_offset_mapc                     t           j                            |          \  }}|                     d|z            | _        |                     d|z            | _        dS )zGInitialize ourselves with the path to the respective pack or index filez%s.idxz%s.packN)osr   splitextIndexFileClsr!  PackFileClsr"  )rZ   pack_or_index_pathbasenameexts       rA   r[   zPackEntity.__init__  sT    (();<<#''8(;<<%%i(&:;;


rC   c                 j    | j                                          | j                                         d S rW   )r!  r   r"  rY   s    rA   r   zPackEntity.close  s0    
rC   c                    t          | j                                                  }t          | j                                                  | j        j        z
  }|s
J d            d }t          |          dk    r|d         |i}nWt          |          }t          |          }t          |           t          ||          }t          |          }|||d         <   || _        d S )NzCannot handle empty indicesr,   r   )sortedr!  r   rE   r"  r6   r   iternextzipdictr#  )rZ   r   offsets_sortedlast_offset
offset_mapiter_offsetsiter_offsets_plus_oneconsecutives           rA   r   zPackEntity._set_cache_  s    
   3 3 5 566$*//++,,tz/EE<<<<<<
~!##(+[9JJ//L$($8$8!&'''l,ABBKk**J .9J~b)*%rC   c                 \    | j                             |          }|t          |          |S )z*:return: index for the given sha, or raise)r!  r   r   )rZ   rv   r   s      rA   _sha_to_indexzPackEntity._sha_to_index  s-    ((--=C.. rC   c              #      K   | j         j        }| j        }t          | j                                                   D ]} | ||          ||          V  dS )zPIterate over all objects in our index and yield their OInfo or OStream instencesN)r!  rv   _objectrt   r   )rZ   r5   _shar=  r   s        rA   r  zPackEntity._iter_objects  sj      {,4;++--.. 	9 	9E'$$u++y%888888	9 	9rC   r.  c                    |dk     r|                      |          }|| j                            |          }| j                            |          }t	          | j        j                            |                                                    \  }}}|r|t          vr;| j        
                    |          }t          ||j        |j        |j
                  S |                     |          }	t          j        |	          }
t#          ||
j        d|
          S |t          vrt%          |t&          |         |          S |                     |          }	|	d                             d          }t+          |          \  }}t+          ||          \  }}|	d         j        t          v rt/          |d          t%          ||	d         j        |          S )z:return: OInfo or OStream object providing information about the given sha
        :param index: if not -1, its assumed to be the sha's index in the IndexFiler   Ni   r.  zCould not resolve delta object)r;  r!  rv   r4   r   r"  r   r1   r2   r   r@   r   typer   collect_streams_at_offsetr!   newr   r   r   rH   r   r7   r   )rZ   rv   r5   r   r4   r7   r8   r9   
packstreamstreamsdstreambufsrc_sizetarget_sizes                 rA   r=  zPackEntity._object  s    199&&s++E;+//%((C##E**1HI[IfIfgmInInIuIuIwIw1x1x.. 	=k))!Z..v66
sJOZ_jFWXXX 44V<<G&*733GW\4AAAk))S"5g">LLL
 44V<<G!*//#&&C'}}FH"*3"7"7FK r{"k11%EFFFgbk.<<<rC   c                 .    |                      |d          S )zRetrieve information about the object identified by the given sha

        :param sha: 20 byte sha1
        :raise BadObject:
        :return: OInfo instance, with 20 byte shaFr=  r   s     rA   r  zPackEntity.info  s     ||C'''rC   c                 .    |                      |d          S )zRetrieve an object stream along with its information as identified by the given sha

        :param sha: 20 byte sha1
        :raise BadObject:
        :return: OStream instance, with 20 byte shaTrJ  r   s     rA   r@   zPackEntity.stream  s     ||C&&&rC   c                 0    |                      dd|          S )zMAs ``info``, but uses a PackIndexFile compatible index to refer to the objectNFrJ  rZ   r   s     rA   info_at_indexzPackEntity.info_at_index	  s    ||D%///rC   c                 0    |                      dd|          S )zWAs ``stream``, but uses a PackIndexFile compatible index to refer to the
        objectNTrJ  rM  s     rA   stream_at_indexzPackEntity.stream_at_index  s     ||D$...rC   c                     | j         S )z*:return: the underlying pack file instance)r"  rY   s    rA   r%   zPackEntity.pack  r  rC   c                     | j         S )z0:return: the underlying pack index file instance)r!  rY   s    rA   r   zPackEntity.index  s
    {rC   Fc                    |r| j                                         dk     rt          d          |                     |          }| j                             |          }| j        |         }| j                             |          }t          j        }| j	        
                                }|}	d}
|	|k     r?t          |	t          z   |          }||	z
  } |||	|	|z            |
          }
|	|z  }	|	|k     ?|
dz  |k    S t                      }|                     |d          }t          |j        |j        |j        |j                   |                    d          |k    sJ |                    d          |k    S )	a  
        Verify that the stream at the given sha is valid.

        :param use_crc: if True, the index' crc is run over the compressed stream of
            the object, which is much faster than checking the sha1. It is also
            more prone to unnoticed corruption or manipulation.
        :param sha: 20 byte sha1 of the object whose stream to verify
            whether the compressed stream of the object is valid. If it is
            a delta, this only verifies that the delta's data is valid, not the
            data of the actual undeltified object, as it depends on more than
            just this stream.
            If False, the object will be decompressed and the sha generated. It must
            match the given sha

        :return: True if the stream is valid
        :raise UnsupportedOperation: If the index is version 1 only
        :raise BadObject: sha was not foundrl   z=Version 1 indices do not contain crc's, verify by sha insteadr   rk   Tr  Fro   )r!  r   r   r;  r4   r#  rO   zlibr&   r"  r6   minr   r"   r=  r   r@  r   rH   rI   rv   )rZ   rv   use_crcr   r4   next_offset	crc_value
crc_update	pack_datacur_posthis_crc_valuerboundr   	shawriterr@   s                  rA   is_valid_streamzPackEntity.is_valid_stream  s   $  !	6{""$$q((*+jkkk &&s++E[''..F*62K..I J
))IGNK''Wz1;??'!+Iggn6L,M~!^!^4	 K'' #Z/I=="I\\#\66Ffk6;	PPP===..#5555===..#55rC   c                 .    |                      d          S )zi
        :return: Iterator over all objects in this pack. The iterator yields
            OInfo instancesFr  r  rY   s    rA   	info_iterzPackEntity.info_iterU  s     !!E!222rC   c                 .    |                      d          S )zk
        :return: iterator over all objects in this pack. The iterator yields
            OStream instancesTr  r  rY   s    rA   r  zPackEntity.stream_iter[  s     !!D!111rC   c                    | j                             |          }|d         j        t          k    r |d         }|j        t          v r	|j        t          k    rt          |j        t                    r2| j        	                    |j        
                                          }n| j        	                    |j                  }|n| j                             | j                            |                    }|                    |           n4| j                             |j                  }|                    |           |j        t          v 	|S )z
        As the version in the PackFile, but can resolve REF deltas within this pack
        For more info, see ``collect_streams``

        :param offset: offset into the pack file at which the object can be foundr.  )r"  r  r7   r   r   r   r;   
memoryviewr!  r   tobytesr@   r4   r]   )rZ   r4   rD  r@   sindexs        rA   rA  z$PackEntity.collect_streams_at_offseta  s(    *,,V44 2;)++R[F.K//>Y..!&"3Z@@ M!%!9!9&:K:S:S:U:U!V!V!%!9!9&:K!L!L~!Z..t{/A/A&/I/IJJFNN6****
 "Z..v/@AAFNN6***! .K//* rC   c                     |                      | j                            |                     |                              S )ab  
        As ``PackFile.collect_streams``, but takes a sha instead of an offset.
        Additionally, ref_delta streams will be resolved within this pack.
        If this is not possible, the stream will be left alone, hence it is adivsed
        to check for unresolved ref-deltas and resolve them before attempting to
        construct a delta stream.

        :param sha: 20 byte sha1 specifying the object whose related streams you want to collect
        :return: list of streams, first being the actual object delta, the last being
            a possibly unresolved base object.
        :raise BadObject:)rA  r!  r4   r;  r   s     rA   r  zPackEntity.collect_streams  s7     --dk.@.@ASASTWAXAX.Y.YZZZrC   Nc                    |}|s:t          |t          t          f          st          |          }t          |          }t	          |          }|j        }d}	d}
|du} |t          dt          j        t          j	        |                     |	dz  }	|rt                      }
d}|D ]}|dz  }d}t          |j        |j                  }|rt          |          }nd} ||           t          j        |          }|j        }t%          |j        |||          \  }}}||j        k    sJ |r|
                    |j        ||	           |	t          |          |z   z  }	||k    r n||k    rt-          d||fz            |                    d	          }t          |          d
k    sJ  ||           |	t          |          z  }	d}|r|
                    ||          }||fS )a  
        Create a new pack by putting all objects obtained by the object_iterator
        into a pack which is written using the pack_write method.
        The respective index is produced as well if index_write is not Non.

        :param object_iter: iterator yielding odb output objects
        :param pack_write: function to receive strings to write into the pack stream
        :param indx_write: if not None, the function writes the index file corresponding
            to the pack.
        :param object_count: if you can provide the amount of objects in your iteration,
            this would be the place to put it. Otherwise we have to pre-iterate and store
            all items into a list to get the number, which uses more memory than necessary.
        :param zlib_compression: the zlib compression level to use
        :return: tuple(pack_sha, index_binsha) binary sha over all the contents of the pack
            and over all contents of the index. If index_write was None, index_binsha will be None

        **Note:** The destination of the write functions is up to the user. It could
        be a socket, or a file for instance

        **Note:** writes only undeltified objectsr   Nr   r   r,   )rK   zKExpected to write %i objects into pack, but received only %i from iteratorsFro   r0   )r   r   rX   rE   r$   rI   r%   r)   r   r  rT   r   r7   r   r&   rT  compressobjr@   rR   rH   r]   r^   r   rv   )clsobject_iter
pack_writeindex_writeobject_countzlib_compressionobjspack_writerpwriter|   r   wants_indexactual_countobjrO   hdrrJ   r  rL   rM   rw   	index_shas                         rA   
write_packzPackEntity.write_pack  sI   .  	%kE4=99 )K((t99L )44"!- 	tFH3X5RT`aabbbr	 	"MME  	 	CALC ,CKBBC CjjF3KKK &'788GjG.w|VWWZ[[[KBC>>>> 3SZc222 3s88b= C|++ ,
 <'']amo{`||~ ~ ~
 ??%?008}}""""
8s8}}	 	;Hk::I ""rC   c                 <   t          j        dd|          \  }t          j        dd|          \  }fd}fd}|                     |||||          \  }	}
t          j                   t          j                   d}t          j                            ||t          |	          dfz            }t          j                            ||t          |	          dfz            }t          j        ||           t          j        ||            | |          S )a  Create a new on-disk entity comprised of a properly named pack file and a properly named
        and corresponding index file. The pack contains all OStream objects contained in object iter.
        :param base_dir: directory which is to contain the files
        :return: PackEntity instance initialized with the new pack

        **Note:** for more information on the other parameters see the write_pack method r%   r   c                 .    t          j        |           S rW   r%  rI   )r   pack_fds    rA   rc   z#PackEntity.create.<locals>.<lambda>  s    rx33 rC   c                 .    t          j        |           S rW   r|  )r   index_fds    rA   rc   z#PackEntity.create.<locals>.<lambda>  s    1 5 5 rC   z
pack-%s.%sidx)	tempfilemkstemprx  r%  r   r   ru   r
   rename)rj  rk  base_dirrn  ro  	pack_path
index_pathrl  rm  pack_binshaindex_binshafmtnew_pack_pathnew_index_pathr  r}  s                 @@rA   createzPackEntity.create  s    &-b&(CC'/GXFF*3333
5555$'NN;
KYegw$x$x!\

Xsj6M6Mv5V/VWWhz+7N7NPU6V0VWW
	)]+++
	*n---s=!!!rC   )r.  )F)r}   r~   r   r   r   r(   r'  r)   r(  r[   r   r   r;  r  r=  r  r@   rN  rP  r%   r   r_  ra  r  rA  r  classmethodrT  Z_BEST_SPEEDrx  r  ra   rC   rA   r*   r*     s       2 2I
 !LK< < <  & & &2  9 9 9'= '= '= '=X( ( (' ' '0 0 0/ / /    5 5 5 5n3 3 32 2 2! ! !F[ [ [ =A $t7HY# Y# Y# [Y#v 8<tO` " " " [" " "rC   r*   rW   );r   rT  	gitdb.excr   r   r   r   
gitdb.utilr   r   r	   r
   r   	gitdb.funr   r   r   r   r   r   r   r   r   r   r   gitdb_speedups._perfr   ImportError
gitdb.baser   r   r   r   r   r   r   gitdb.streamr    r!   r"   r#   r$   structr%   binasciir&   gitdb.constr'   r  r   r%  r   __all__rB   rR   rT   r(   r)   r*   ra   rC   rA   <module>r     sZ  
 : 9                                                  	??????? 	 	 	D	                                           ! ! ! ! ! !   				 




5
1] 1] 1]l% % % %VD D D D D D D DN~9 ~9 ~9 ~9 ~9I ~9 ~9 ~9HQ@ Q@ Q@ Q@ Q@y Q@ Q@ Q@lw" w" w" w" w" w" w" w" w" w"s   A A
A