
    g"                         d dl mZ 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 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 d	l(Z(d
Z) G d deee      Z*y	)    )suppress)
FileDBBase	ObjectDBR	ObjectDBW)	BadObjectAmbiguousObjectName)DecompressMemMapReaderFDCompressedSha1WriterFDStream
Sha1Writer)OStreamOInfo)file_contents_ro_filepathENOENT
hex_to_bin
bin_to_hexexistschmodisfileremove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                 n    t         |   |       t               | _        t	        t
        dd      | _        y )N	O_NOATIMEr   )super__init__dict_hexsha_to_filegetattros_fd_open_flags)self	root_path	__class__s     C/var/www/openai/venv/lib/python3.12/site-packages/gitdb/db/loose.pyr)   zLooseObjectDB.__init__M   s-    ##v &b+q9    c                 &    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   )r/   hexshas     r2   object_pathzLooseObjectDB.object_pathV   s     F2AJqr
++r3   c                     t        t              5  | j                  |   cddd       S # 1 sw Y   nxY w| j                  | j	                  |            }t        |      r|| j                  |<   |S t        |      )z
        :return: readable object path to the object identified by hexsha
        :raise BadObject: If the object file does not existN)r   KeyErrorr+   db_pathr7   r   r   )r/   r6   paths      r2   readable_db_object_pathz%LooseObjectDB.readable_db_object_path\   sh     h''/  
 ||D,,V45$<+/D  (Ks   )2c                     d}| j                         D ]5  }t        |      j                  t        |            s'|t	        |      |}7 |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   )r/   partial_hexsha	candidatebinshas       r2   partial_to_complete_sha_hexz)LooseObjectDB.partial_to_complete_sha_hexl   s`    
 	mmoF&!,,[-HI(-n=="	 & N++r3   c                 @   | j                  | j                  t        |                  }	 t        || j                        S # t
        $ rR}|j                  t        k7  r.	 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:   r7   r   r   r.   OSErrorerrnor   r   )r/   shar:   enew_es        r2   _map_loose_objectzLooseObjectDB._map_loose_object   s     ,,t//
3@A	,,WD<O<OPP 
	,ww& 44W== 4#C.e34
  n!+
	,s;   A 	BB
A/)B/	B	8BB		BBc                 ~    |,t        |t              st        dt        j                  z        t        |   |      S )zI:raise TypeError: if the stream does not support the Sha1Writer interfacez,Output stream musst support the %s interface)
isinstancer   	TypeError__name__r(   set_ostream)r/   streamr1   s     r2   rP   zLooseObjectDB.set_ostream   s9    j&DJZM`M``aaw"6**r3   c                     | j                  |      }	 t        |      \  }}t        |||      t        |d      r|j	                          S S # t        |d      r|j	                          w w xY w)Nclose)rK   r   r   hasattrrS   )r/   rH   mtypsizes        r2   infozLooseObjectDB.info   sb    ""3'	03ICc4(q'"	 #wq'"	 #s   A A*c                 v    | j                  |      }t        j                  |d      \  }}}t        ||||      S )NT)close_on_deletion)rK   r	   newr   )r/   rH   rU   typerW   rQ   s         r2   rQ   zLooseObjectDB.stream   s<    ""3'377TRdFsD$//r3   c                 X    	 | j                  t        |             y# t        $ r Y yw xY w)NTF)r<   r   r   )r/   rH   s     r2   
has_objectzLooseObjectDB.has_object   s.    	((C9 		s    	))c                    d}| j                         }|Gt        j                  d| j                        \  }}|j                  t        |      }nt        |      }	 	 |j                  ;t        |j                  |j                  t        j                  | j                         nBt        |j                  |j                  |j                  |j                  | j                         |r|j!                          	 d}|j                  r|j$                  }n|j'                  d      }|r| j)                  | j+                  |            }t-        |      }t/        j0                  |d       t3        |      rt#        |       nt5        ||       dD ]2  }t7        t8              5  t;        || j<                         	 ddd        n t9        d
jC                  |            tE        |      |_        |S # |r|j!                          w w xY w#  |rt#        |        xY w# 1 sw Y   nxY wt?        j@                  |d	z         )z.note: The sha we produce will be hex by natureNobj)prefixdir)r   T)as_hex)exist_ok)      	         $   1   @   Q   d   y            g     @@z&Impossible to apply `chmod` to file {})#ostreamtempfilemkstemp
_root_pathrB   r
   r   r   readwritesysmaxsizestream_chunk_sizer   r\   rW   rS   r   r6   rH   r:   r7   r   r-   makedirsr   r   r   PermissionErrorr   new_objects_modetimesleepformatr   )	r/   istreamtmp_pathwriterfdr6   obj_pathobj_direxp_backoff_mss	            r2   storezLooseObjectDB.store   s   >#++5dooNLB~~%/3!"	#>>-  fllCKKI_I_` !w||W\\6<<,0,B,BD LLN >>^^FZZtZ,F ||D$4$4V$<=Hh'GKK$/ hx x* #]o. (D$9$9:	 /. #] &<CCHM  $F+_ LLN 	x 6 /.
 

>F23s+   B	G1 'H	 "H1HH	 	HH%	c              #      K   t        j                  | j                               D ]F  \  }}}t        |      }t	        |      dk7  r!|D ]!  }t	        |      dk7  rt        ||z          # H y w)Nr5   &   )r-   walkr0   r   lenr   )r/   rootdirsfiles	root_basefs         r2   r>   zLooseObjectDB.sha_iter   sf     !#)9!:D$ I9~"q6R< Q//  ";s   A.A0c                 F    t        t        | j                                     S )N)r   tupler>   )r/   s    r2   rW   zLooseObjectDB.size  s    5)**r3   )rO   
__module____qualname____doc__r   r{   intr~   r-   namer)   r7   r<   rC   rK   rP   rX   rQ   r^   r   r>   rW   __classcell__)r1   s   @r2   r    r    ?   sq    9 # 5!}	ww$ua=:,  &,*+0
JX
0+r3   r    )+
contextlibr   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   	gitdb.funr   r   r   r   gitdb.utils.encodingr   rt   r-   ry   r   __all__r     r3   r2   <module>r      sp   
   
 
     -  	 
  M+J	9 M+r3   