
    ge#                         d Z ddlmZmZmZ ddlmZ ddlmZm	Z	 ddl
mZ ddlmZ dZ G d d	      Z G d
 d      Z G d d      Z G d d      Zd Z G d deee      Zy)z8Contains implementations of database retrieveing objects    )join	LazyMixin
hex_to_bin)
force_text)	BadObjectAmbiguousObjectName)chain)reduce)	ObjectDBR	ObjectDBW
FileDBBase
CompoundDB	CachingDBc                   4    e Zd ZdZd Zd Zd Zd Zd Zd Z	y)	r   zkDefines an interface for object database lookup.
    Objects are identified either by their 20 byte bin shac                     | j                   S N)has_objselfshas     B/var/www/openai/venv/lib/python3.12/site-packages/gitdb/db/base.py__contains__zObjectDBR.__contains__   s    ||    c                     t        d      )z
        Whether the object identified by the given 20 bytes
            binary sha is contained in the database

        :return: True if the object identified by the given 20 bytes
            binary sha is contained in the databaseTo be implemented in subclassNotImplementedErrorr   s     r   
has_objectzObjectDBR.has_object"   s     ""ABBr   c                     t        d      )zW :return: OInfo instance
        :param sha: bytes binary sha
        :raise BadObject:r   r   r   s     r   infozObjectDBR.info+        ""ABBr   c                     t        d      )z[:return: OStream instance
        :param sha: 20 bytes binary sha
        :raise BadObject:r   r   r   s     r   streamzObjectDBR.stream1   r!   r   c                     t               )z+:return: amount of objects in this databaser   r   s    r   sizezObjectDBR.size7       !##r   c                     t               )zGReturn iterator yielding 20 byte shas for all objects in this data baser   r%   s    r   sha_iterzObjectDBR.sha_iter;   r'   r   N)
__name__
__module____qualname____doc__r   r   r    r#   r&   r)    r   r   r   r      s(    >CCC$$r   r   c                   (    e Zd ZdZd Zd Zd Zd Zy)r   z6Defines an interface to create objects in the databasec                     d | _         y r   _ostream)r   argskwargss      r   __init__zObjectDBW.__init__F   s	    r   c                 ,    | j                   }|| _         |S )ab  
        Adjusts the stream to which all data should be sent when storing new objects

        :param stream: if not None, the stream to use, if None the default stream
            will be used.
        :return: previously installed stream, or None if there was no override
        :raise TypeError: if the stream doesn't have the supported functionalityr1   )r   r#   cstreams      r   set_ostreamzObjectDBW.set_ostreamJ   s     --r   c                     | j                   S )z
        Return the output stream

        :return: overridden output stream this instance will write to, or None
            if it will write to the default streamr1   r%   s    r   ostreamzObjectDBW.ostreamV   s     }}r   c                     t        d      )a  
        Create a new object in the database
        :return: the input istream object with its sha set to its corresponding value

        :param istream: IStream compatible instance. If its sha is already set
            to a value, the object will just be stored in the our database format,
            in which case the input stream is expected to be in object format ( header + contents ).
        :raise IOError: if data could not be writtenr   r   )r   istreams     r   storezObjectDBW.store^   s     ""ABBr   N)r*   r+   r,   r-   r5   r8   r:   r=   r.   r   r   r   r   B   s    @
	Cr   r   c                   .     e Zd ZdZ fdZd Zd Z xZS )r   z}Provides basic facilities to retrieve files of interest, including
    caching facilities to help mapping hexsha's to objectsc                 0    t         |           || _        y)aY  Initialize this instance to look for its files at the given root path
        All subsequent operations will be relative to this path
        :raise InvalidDBRoot:
        **Note:** The base will not perform any accessablity checking as the base
            might not yet be accessible, but become accessible before the first
            access.N)superr5   
_root_path)r   	root_path	__class__s     r   r5   zFileDBBase.__init__q   s     	#r   c                     | j                   S )z':return: path at which this db operates)rA   r%   s    r   rB   zFileDBBase.root_path|   s    r   c                 @    t        | j                  t        |            S )z~
        :return: the given relative path relative to our database root, allowing
            to pontentially access datafiles)r   rA   r   )r   	rela_paths     r   db_pathzFileDBBase.db_path   s     DOOZ	%:;;r   )r*   r+   r,   r-   r5   rB   rG   __classcell__rC   s   @r   r   r   l   s    >$<r   r   c                       e Zd ZdZddZy)r   z/A database which uses caches to speed-up accessc                      y)ar  
        Call this method if the underlying data changed to trigger an update
        of the internal caching structures.

        :param force: if True, the update must be performed. Otherwise the implementation
            may decide not to perform an update if it thinks nothing has changed.
        :return: True if an update was performed as something change indeedNr.   )r   forces     r   update_cachezCachingDB.update_cache   s    r   NF)r*   r+   r,   r-   rM   r.   r   r   r   r      s    9Or   r   c                     t        | t              rC| j                         }|j                  d |D               d |D        D ]  }t	        ||        y|j                  |        y)zfFill output list with database from db, in order. Deals with Loose, Packed
    and compound databases.c              3   B   K   | ]  }t        |t              r|  y wr   
isinstancer   .0dbs     r   	<genexpr>z'_databases_recursive.<locals>.<genexpr>   s     I3RjZ.Hb3   c              3   B   K   | ]  }t        |t              s|  y wr   rQ   rS   s     r   rV   z'_databases_recursive.<locals>.<genexpr>   s     C2
2z(BBrW   N)rR   r   	databasesextend_databases_recursiveappend)databaseoutputdbscdbs       r   r[   r[      sV     (J'  "I3IICCC f- D 	hr   c                   Z     e Zd ZdZ fdZd Zd Zd Zd Zd Z	d Z
d	 Zdd
Zd Z xZS )r   zA database which delegates calls to sub-databases.

    Databases are stored in the lazy-loaded _dbs attribute.
    Define _set_cache_ to update it with your databasesc                 x    |dk(  rt               | _        y |dk(  rt               | _        y t        |   |       y )N_dbs	_db_cache)listrc   dictrd   r@   _set_cache_)r   attrrC   s     r   rg   zCompoundDB._set_cache_   s1    6>DI[ !VDNG%r   c                     	 | j                   |   S # t        $ r Y nw xY w| j                  D ]&  }|j                  |      s|| j                   |<   |c S  t	        |      )zL:return: database containing the given 20 byte sha
        :raise BadObject:)rd   KeyErrorrc   r   r   )r   r   rU   s      r   	_db_queryzCompoundDB._db_query   sc    
	>>#&& 		 ))B}}S!&(s#	 
 ns    	c                 F    	 | j                  |       y# t        $ r Y yw xY w)NTF)rk   r   r   s     r   r   zCompoundDB.has_object   s(    	NN3 		s    	  c                 B    | j                  |      j                  |      S r   )rk   r    r   s     r   r    zCompoundDB.info   s    ~~c"'',,r   c                 B    | j                  |      j                  |      S r   )rk   r#   r   s     r   r#   zCompoundDB.stream   s    ~~c"))#..r   c                 @    t        d d | j                  D        d      S )z.:return: total size of all contained databasesc                     | |z   S r   r.   )xys     r   <lambda>z!CompoundDB.size.<locals>.<lambda>   s    1q5r   c              3   <   K   | ]  }|j                           y wr   )r&   rS   s     r   rV   z"CompoundDB.size.<locals>.<genexpr>   s     *Iy2779y   r   )r
   rc   r%   s    r   r&   zCompoundDB.size   s    (*Ityy*I1MMr   c                 4    t        d | j                  D         S )Nc              3   <   K   | ]  }|j                           y wr   )r)   rS   s     r   rV   z&CompoundDB.sha_iter.<locals>.<genexpr>   s     9yr{{}yru   )r	   rc   r%   s    r   r)   zCompoundDB.sha_iter   s    9tyy9::r   c                 ,    t        | j                        S )z7:return: tuple of database instances we use for lookups)tuplerc   r%   s    r   rY   zCompoundDB.databases   s    TYYr   c                     | j                   j                          d}| j                  D ]'  }t        |t              s||j                  |      z  }) |S )NF)rd   clearrc   rR   r   rM   )r   rL   statrU   s       r   rM   zCompoundDB.update_cache   sI    ))B"i(.. 
 r   c                    t               }t        | |       t        |      }t        |      }|dz  dk7  rt	        |dz         }nt	        |      }d}|D ]L  }d}	 t        |d      r|j                  |      }n|j                  ||      }|s9|r||k7  rt        |      |}N |st        |      |S # t        $ r Y iw xY w)z
        :return: 20 byte binary sha1 from the given less-than-40 byte hexsha (bytes or str)
        :param partial_hexsha: hexsha with less than 40 byte
        :raise AmbiguousObjectName:    r   0Npartial_to_complete_sha_hex)
re   r[   r   lenr   hasattrr   partial_to_complete_shar   r   )r   partial_hexsharY   len_partial_hexshapartial_binsha	candidaterU   full_bin_shas           r   r   z&CompoundDB.partial_to_complete_sha_hex   s    
 F	T9-#N3 0!Q&'(<=N'7N 	BL2<=#%#A#A.#QL#%#=#=nN`#aL
 l!:-n==(	 " N++  s   0B11	B=<B=rN   )r*   r+   r,   r-   rg   rk   r   r    r#   r&   r)   rY   rM   r   rH   rI   s   @r   r   r      s;    ;
&(-/N; 	#r   r   N)r-   
gitdb.utilr   r   r   gitdb.utils.encodingr   	gitdb.excr   r   	itertoolsr	   	functoolsr
   __all__r   r   r   r   r[   r   r.   r   r   <module>r      ss   
 ?  ,
   N$$ $$R%C %CT< <8O O"	 nIy nr   