
    gu                         d Z ddlmZmZmZ ddlmZ ddlmZm	Z	m
Z
 ddlmZ ddlmZ ddlZddlZdZ G d	 d
eeee      Zy)z/Module containing a database to deal with packs    )
FileDBBase	ObjectDBR	CachingDB)	LazyMixin)	BadObjectUnsupportedOperationAmbiguousObjectName)
PackEntity)reduceN)PackedDBc                   p     e 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dZd Zd Z xZS )r   z-A database operating on a set of object packsi  c                 @    t         |   |       d| _        d| _        y Nr   )super__init__
_hit_count	_st_mtime)self	root_path	__class__s     B/var/www/openai/venv/lib/python3.12/site-packages/gitdb/db/pack.pyr   zPackedDB.__init__)   s     #     c                 R    |dk(  r"t               | _        | j                  d       y y )N	_entitiesT)force)listr   update_cache)r   attrs     r   _set_cache_zPackedDB._set_cache_3   s)    ;!VDND) r   c                 @    | j                   j                  d d       y )Nc                     | d   S r    )ls    r   <lambda>z)PackedDB._sort_entities.<locals>.<lambda>:   s    !A$r   T)keyreverse)r   sort)r   s    r   _sort_entitieszPackedDB._sort_entities9   s    =r   c                    | j                   | j                  z  dk(  r| j                          | j                  D ]:  } |d   |      }||dxx   dz  cc<   | xj                   dz  c_         |d   |fc S  t	        |      )a  :return: tuple(entity, index) for an item at the given sha
        :param sha: 20 or 40 byte sha
        :raise BadObject:
        **Note:** This method is not thread-safe, but may be hit in multi-threaded
            operation. The worst thing that can happen though is a counter that
            was not incremented, or the list being in wrong order. So we safe
            the time for locking here, lets see how that goesr         )r   _sort_intervalr(   r   r   )r   shaitemindexs       r   
_pack_infozPackedDB._pack_info<   s     ??T000A5! NNDDGCLE Q11$Q'' # nr   c                 F    	 | j                  |       y# t        $ r Y yw xY w)NTF)r0   r   )r   r-   s     r   
has_objectzPackedDB.has_objectY   s(    	OOC  		s    	  c                 L    | j                  |      \  }}|j                  |      S N)r0   info_at_indexr   r-   entityr/   s       r   infozPackedDB.infoa   s%    ,##E**r   c                 L    | j                  |      \  }}|j                  |      S r4   )r0   stream_at_indexr6   s       r   streamzPackedDB.streame   s%    ,%%e,,r   c              #      K   | j                         D ]F  }|j                         }|j                  }t        |j	                               D ]  } ||        H y wr4   )entitiesr/   r-   rangesize)r   r7   r/   sha_by_indexs       r   sha_iterzPackedDB.sha_iteri   sJ     mmoFLLNE 99Luzz|,"5)) - &s   AAc                     | j                   D cg c]#  }|d   j                         j                         % }}t        d |d      S c c}w )Nr+   c                     | |z   S r4   r"   )xys     r   r$   zPackedDB.size.<locals>.<lambda>t   s    1q5r   r   )r   r/   r?   r   )r   r.   sizess      r   r?   zPackedDB.sizer   sC    48NNCNDa%%'NC(%33 Ds   (Ac                     t               )zStoring individual objects is not feasible as a pack is designed to
        hold multiple objects. Writing or rewriting packs for single objects is
        inefficient)r   )r   istreams     r   storezPackedDB.storez   s     #$$r   c                 j   t        j                  | j                               }|s|j                  | j                  k  ry|j                  | _        t        t        j                  t         j                  j                  | j                         d                  }| j                  D ch c]#  }|d   j                         j                         % }}||z
  D ]_  }t        |      }| j                  j                  |j                         j                         ||j                         j                  g       a ||z
  D ]]  }d}t!        | j                        D ]-  \  }	}|d   j                         j                         |k(  s+|	} n |dk7  sJ | j                  |= _ | j#                          yc c}w )a  
        Update our cache with the actually existing packs on disk. Add new ones,
        and remove deleted ones. We keep the unchanged ones

        :param force: If True, the cache will be updated even though the directory
            does not appear to have changed according to its modification timestamp.
        :return: True if the packs have been updated so there is new information,
            False if there was no change to the pack databaseFzpack-*.packr+   T)osstatr   st_mtimer   setglobpathjoinr   packr
   appendr?   r/   sha_to_index	enumerater(   )
r   r   rM   
pack_filesr.   our_pack_files	pack_filer7   	del_indexis
             r   r   zPackedDB.update_cache   sa    wwt~~'($..8 277<<0@-#PQR
<@NNKND$q',,.--/NK %~5I  	*FNN!!6;;=#5#5#7A\A\"]^	 6 ):5II$T^^447<<>&&(I5 !I 5 ?"?y) 6 	3 Ls   )(F0c                 F    | j                   D cg c]  }|d   	 c}S c c}w )z=:return: list of pack entities operated upon by this databaser+   )r   )r   r.   s     r   r=   zPackedDB.entities   s"    $(NN3NDQN333s   c                     d}| j                   D ]^  }|d   j                         j                  ||      }|)|d   j                         j                  |      }|r||k7  rt	        |      |}` |r|S t        |      )a  :return: 20 byte sha as inferred by the given partial binary sha
        :param partial_binsha: binary sha with less than 20 bytes
        :param canonical_length: length of the corresponding canonical representation.
            It is required as binary sha's cannot display whether the original hex sha
            had an odd or even number of characters
        :raise AmbiguousObjectName:
        :raise BadObject: Nr+   )r   r/   partial_sha_to_indexr-   r	   r   )r   partial_binshacanonical_length	candidater.   
item_indexr-   s          r   partial_to_complete_shaz PackedDB.partial_to_complete_sha   s     	NNDa==nN^_J%1gmmo))*5c!1-n==	 #  ''r   )F)__name__
__module____qualname____doc__r,   r   r   r(   r0   r2   r8   r;   rA   r?   rI   r   r=   rc   __classcell__)r   s   @r   r   r       sO    7
 N*>:+-*4%+Z4(r   r   )rg   gitdb.db.baser   r   r   
gitdb.utilr   	gitdb.excr   r   r	   
gitdb.packr
   	functoolsr   rL   rP   __all__r   r"   r   r   <module>ro      sJ   
 6  !  "  	 

l(z9i l(r   