o
    h"                     @   s   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 deeeZ*d	S )    )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                       s   e Zd ZdZeZeddZej	dkreddZ fddZ
dd	 Zd
d Zdd Zdd Z fddZdd Zdd Zdd Zdd Zdd Zdd Z  ZS )r   z/A database which operates on loose object files444   nt644c                    s&   t  | t | _ttdd| _d S )N	O_NOATIMEr   )super__init__dict_hexsha_to_filegetattros_fd_open_flags)self	root_path	__class__ B/var/www/vscode/kcb/lib/python3.10/site-packages/gitdb/db/loose.pyr&   M   s   zLooseObjectDB.__init__c                 C   s   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,   hexshar0   r0   r1   object_pathV   s   zLooseObjectDB.object_pathc                 C   s`   t t | j| W  d   S 1 sw   Y  | | |}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_pathr4   r   r   )r,   r3   pathr0   r0   r1   readable_db_object_path\   s   
 
z%LooseObjectDB.readable_db_object_pathc                 C   sL   d}|   D ]}t|t|r|durt||}q|du r$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binshar0   r0   r1   partial_to_complete_sha_hexl   s   z)LooseObjectDB.partial_to_complete_sha_hexc                 C   s   |  | t|}zt|| jdW S  tyE } z'|jtkr<zt|W W  Y d}~S  ty; } zt||d}~ww t||d}~ww )z~
        :return: memory map of that file to allow random read access
        :raise BadObject: if object could not be located)flagsN)	r6   r4   r   r   r+   OSErrorerrnor   r   )r,   shar6   enew_er0   r0   r1   _map_loose_object   s   


zLooseObjectDB._map_loose_objectc                    s,   |durt |tstdtj 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%   set_ostream)r,   streamr.   r0   r1   rI      s   zLooseObjectDB.set_ostreamc              	   C   sP   |  |}zt|\}}t|||W t|dr|  S S t|dr'|  w w )Nclose)rE   r   r   hasattrrK   )r,   rB   mtypsizer0   r0   r1   info   s   



zLooseObjectDB.infoc                 C   s,   |  |}tj|dd\}}}t||||S )NT)close_on_deletion)rE   r   newr   )r,   rB   rM   typerO   rJ   r0   r0   r1   rJ      s   
zLooseObjectDB.streamc                 C   s*   z
|  t| W dS  ty   Y dS w )NTF)r8   r   r   )r,   rB   r0   r0   r1   
has_object   s   zLooseObjectDB.has_objectc           	   	   C   s  d}|   }|du r"tjd| jd\}}|jdu rt|}nt|}z2z'|jdur5t|j|j	t
j| j nt|j|j|j|j	| jd W |rJ|  n|rR|  w w W n
   |r^t|  d}|jrh|j}n|jdd}|r| | |}t|}tj|dd t|rt| nt|| dD ]%}tt t|| j 	 W d    n1 sw   Y  t |d	  qtd
!|t"||_|S )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_pathr=   r	   r
   r   readwritesysmaxsizestream_chunk_sizer   rS   rO   rK   r   r3   rB   r6   r4   r   r*   makedirsr   r   r   PermissionErrorr   new_objects_modetimesleepformatr   )	r,   istreamtmp_pathwriterfdr3   obj_pathobj_direxp_backoff_msr0   r0   r1   store   s`   






 
zLooseObjectDB.storec                 c   s\    t |  D ]#\}}}t|}t|dkrq|D ]}t|dkr#qt|| V  qqd S )Nr2   &   )r*   walkr-   r   lenr   )r,   rootdirsfiles	root_basefr0   r0   r1   r9      s   zLooseObjectDB.sha_iterc                 C   s   t t|  S )N)r   tupler9   )r,   r0   r0   r1   rO     s   zLooseObjectDB.size)rH   
__module____qualname____doc__r   rp   intrs   r*   namer&   r4   r8   r>   rE   rI   rP   rJ   rT   r~   r9   rO   __classcell__r0   r0   r.   r1   r   ?   s$    


	
L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   ri   r*   rn   rt   __all__r   r0   r0   r0   r1   <module>   s   8