
    #Cc`                         d dl mZmZmZ d dlmZmZ d dlmZm	Z	m
Z
mZ d dlmZmZ d dlmZmZmZmZmZmZmZmZmZmZmZmZmZmZ d dlmZm Z m!Z!m"Z" d dl#m$Z$ d dl%Z%d dl&Z&d dl'Z'd	Z( G d
 deee          Z)dS )    )
FileDBBase	ObjectDBR	ObjectDBW)	BadObjectAmbiguousObjectName)DecompressMemMapReaderFDCompressedSha1WriterFDStream
Sha1Writer)OStreamOInfo)file_contents_ro_filepathENOENT
hex_to_bin
bin_to_hexexistschmodisdirisfileremovemkdirrenamedirnamebasenamejoin)
chunk_sizeloose_object_header_infowrite_objectstream_copy)force_bytesN)LooseObjectDBc                        e Zd ZdZeZ edd          Zej	        dk    r edd          Z f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 xZS )r!   z/A database which operates on loose object files444   nt644c                     t          t          |                               |           t                      | _        t          t          dd          | _        d S )N	O_NOATIMEr   )superr!   __init__dict_hexsha_to_filegetattros_fd_open_flags)self	root_path	__class__s     .lib/python3.11/site-packages/gitdb/db/loose.pyr*   zLooseObjectDB.__init__M   sI    mT""++I666#vv &b+q99    c                 B    t          |dd         |dd                   S )z{
        :return: path at which the object with the given hexsha would be stored,
            relative to the database rootN   )r   )r0   hexshas     r3   object_pathzLooseObjectDB.object_pathV   s$     F2A2Jqrr
+++r4   c                     	 | j         |         S # t          $ r Y nw xY w|                     |                     |                    }t	          |          r|| j         |<   |S t          |          )z
        :return: readable object path to the object identified by hexsha
        :raise BadObject: If the object file does not exist)r,   KeyErrordb_pathr8   r   r   )r0   r7   paths      r3   readable_db_object_pathz%LooseObjectDB.readable_db_object_path\   s    	'// 	 	 	D	
 ||D,,V4455$<< 	+/D (Ks    
c                     d}|                                  D ]D}t          |                              t          |                    r|t	          |          |}E|t          |          |S )z:return: 20 byte binary sha1 string which matches the given name uniquely
        :param name: hexadecimal partial name (bytes or ascii string)
        :raise AmbiguousObjectName:
        :raise BadObject: N)sha_iterr   
startswithr    r   r   )r0   partial_hexsha	candidatebinshas       r3   partial_to_complete_sha_hexz)LooseObjectDB.partial_to_complete_sha_hexn   sz    
 	mmoo 	# 	#F&!!,,[-H-HII #(-n==="	N+++r4   c                 l   |                      |                     t          |                              }	 t          || j                  S # t
          $ r\}|j        t          k    r7	 t          |          cY d}~S # t
          $ r}t          |          |d}~ww xY wt          |          |d}~ww xY w)z~
        :return: memory map of that file to allow random read access
        :raise BadObject: if object could not be located)flagsN)	r;   r8   r   r   r/   OSErrorerrnor   r   )r0   shar;   enew_es        r3   _map_loose_objectzLooseObjectDB._map_loose_object   s     ,,t//
3@@AA	,,WD<OPPPP 
	, 
	, 
	,w&  44W======== 4 4 4#C..e34
  nn!+
	,s;   A 
B3B.(A<6B3<
BBBB..B3c                     |1t          |t                    st          dt          j        z            t	          t
          |                               |          S )zI:raise TypeError: if the stream does not support the Sha1Writer interfaceNz,Output stream musst support the %s interface)
isinstancer   	TypeError__name__r)   r!   set_ostream)r0   streamr2   s     r3   rQ   zLooseObjectDB.set_ostream   sM    j&D&DJZM``aaa]D))55f===r4   c                    |                      |          }	 t          |          \  }}t          |||          t          |d          r|                                 S S # t          |d          r|                                 w w xY w)Nclose)rL   r   r   hasattrrT   )r0   rI   mtypsizes        r3   infozLooseObjectDB.info   s    ""3''	033ICc4((q'"" 				wq'"" 				s   "A 'Bc                     |                      |          }t          j        |d          \  }}}t          ||||          S )NT)close_on_deletion)rL   r   newr   )r0   rI   rV   typerX   rR   s         r3   rR   zLooseObjectDB.stream   sE    ""3''37TRRRdFsD$///r4   c                 n    	 |                      t          |                     dS # t          $ r Y dS w xY w)NTF)r=   r   r   )r0   rI   s     r3   
has_objectzLooseObjectDB.has_object   sH    	((C9994 	 	 	55	s   "& 
44c                    d}|                                  }|Dt          j        d| j                  \  }}|j        t          |          }nt          |          }	 	 |j        ,t          |j        |j	        t          j        | j                   n-t          |j        |j        |j        |j	        | j                   |r|                                 n# |r|                                 w w xY wn#  |rt#          j        |            xY wd}|j        r|j        }n|                    d          }|r|                     |                     |                    }t/          |          }t1          |          st3          |           t5          |          rt%          |           nt7          ||           t9          || j                   t=          |          |_        |S )z.note: The sha we produce will be hex by natureNobj)prefixdir)r   T)as_hex)ostreamtempfilemkstemp
_root_pathrC   r	   r
   r   readwritesysmaxsizestream_chunk_sizer   r]   rX   rT   r.   r   r7   rI   r;   r8   r   r   r   r   r   r   new_objects_moder   )r0   istreamtmp_pathwriterfdr7   obj_pathobj_dirs           r3   storezLooseObjectDB.store   s   >#+5doNNNLB~%/33!"	#>-  flCKI_```` !w|W\6<,0,BD D D D  #LLNNN  #LLNNNN#	 $	(### > 	-^FFZZtZ,,F  	3||D$4$4V$<$<==Hh''G>> g h +x    x***
 (D1222 $F++s   A C ?C3 C//C3 3Dc              #     K   t          j        |                                           D ]V\  }}}t          |          }t	          |          dk    r)|D ]*}t	          |          dk    rt          ||z             V  +Wd S )Nr6   &   )r.   walkr1   r   lenr   )r0   rootdirsfiles	root_basefs         r3   r?   zLooseObjectDB.sha_iter   s      !#)9)9!:!: 	0 	0D$ I9~~"" 0 0q66R<< Q//////0	0 	0r4   c                 ^    t          t          |                                                     S )N)ry   tupler?   )r0   s    r3   rX   zLooseObjectDB.size  s     5))***r4   )rP   
__module____qualname____doc__r   rm   intrn   r.   namer*   r8   r=   rD   rL   rQ   rY   rR   r_   ru   r?   rX   __classcell__)r2   s   @r3   r!   r!   ?   s#       99 # s5!}}	w$3ua==: : : : :, , ,     $  &, , ,*> > > > >  0 0 0
  > > >@
0 
0 
0+ + + + + + +r4   r!   )*gitdb.db.baser   r   r   	gitdb.excr   r   gitdb.streamr   r	   r
   r   
gitdb.baser   r   
gitdb.utilr   r   r   r   r   r   r   r   r   r   r   r   r   r   	gitdb.funr   r   r   r   gitdb.utils.encodingr    rf   r.   rk   __all__r!    r4   r3   <module>r      s  
                
                  
                               "            - , , , , ,  				 



 C+ C+ C+ C+ C+J	9 C+ C+ C+ C+ C+r4   