o
    ﯪg"                     @   s  d Z g dZddl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 er6ddlmZ ddlmZ eeeef Zeedf Zd	ee d
edgeedf f ddfddZdede	e fddZde
e dedededef
ddZedddeddfddZedededefddZdededefddZddd eeedf  dede	eed!f  fd"d#Zddd$edede	e fd%d&ZdS )'z6Functions that are supposed to be as fast as possible.)tree_to_streamtree_entries_from_datatraverse_trees_recursivetraverse_tree_recursive    )S_ISDIR)safe_decodedefenc)CallableListMutableSequenceSequenceTupleTYPE_CHECKINGUnionoverload)ReadableBuffer)GitCmdObjectDBNentrieswriter   returnc           
   	   C   s   t d}d}| D ]B\}}}d}tdD ]}t||d ? |@ | g| }q|d |kr1|dd }t|tr<|t}	n|}	|d|d	|	d
|f qdS )zWrite the given list of entries into a stream using its ``write`` method.

    :param entries:
        **Sorted** list of tuples with (binsha, mode, name).

    :param write:
        A ``write`` method which takes a data string.
    0             r      N        )ordrangebytes
isinstancestrencoder   join)
r   r   ord_zerobit_maskbinshamodenamemode_stri
name_bytes r-   K/var/www/html/chatdoc2/venv/lib/python3.10/site-packages/git/objects/fun.pyr   )   s   	 
r   datac                 C   s   t d}t d}t| }d}g }||k rld}| | |kr0|d> | | |  }|d7 }| | |ks|d7 }|}| | dkrF|d7 }| | dks<| || }t|}	|d7 }| ||d  }
|d }||
||	f ||k s|S )zRead the binary representation of a tree and returns tuples of
    :class:`~git.objects.tree.Tree` items.

    :param data:
        Data block with tree data (as bytes).

    :return:
        list(tuple(binsha, mode, tree_relative_path), ...)
    r    r   r   r      )r   lenr   append)r/   r%   	space_ordlen_datar+   outr(   nsr,   r)   shar-   r-   r.   r   M   s0   
"r   	tree_datar)   is_dirstart_atc                 C   s   z| | }|r|d |krt |d |krd| |< |W S W n	 ty&   Y nw t| D ]\}}|rG|d |krGt |d |krGd| |< |  S q+dS )zReturn data entry matching the given name and tree mode or ``None``.

    Before the item is returned, the respective data item is set None in the `tree_data`
    list to mark it done.
       r   N)r   
IndexError	enumerate)r9   r)   r:   r;   itemindexr-   r-   r.   _find_by_name   s     rA   r?   path_prefixc                 C      d S Nr-   r?   rB   r-   r-   r.   _to_full_path      rF   c                 C   rC   rD   r-   rE   r-   r-   r.   rF      rG   c                 C   s"   | s| S | d | d || d  fS )z%Rebuild entry with given path prefix.r   r   r<   r-   rE   r-   r-   r.   rF      s   odbr   	tree_shas.c              	      s0  g }t |}|D ]}|du rg }ntt| | }|| qg }t|D ]m\}}	t|	D ]_\}
}|s7q0dd t|D }|||< |\}}}t|}t|d || D ]}|| }t	|| |||
||< qV|r}|
t| dd |D  | d  n|t fdd|D  d|	|
< q0|	dd= q(|S )	a%  
    :return:
        List of list with entries according to the given binary tree-shas.

        The result is encoded in a list
        of n tuple|None per blob/commit, (n == len(tree_shas)), where:

        * [0] == 20 byte sha
        * [1] == mode as int
        * [2] == path relative to working tree root

        The entry tuple is ``None`` if the respective blob/commit did not exist in the
        given tree.

    :param tree_shas:
        Iterable of shas pointing to trees. All trees must be on the same level.
        A tree-sha may be ``None``, in which case ``None``.

    :param path_prefix:
        A prefix to be added to the returned paths on this level.
        Set it ``""`` for the first iteration.

    :note:
        The ordering of the returned items will be partially lost.
    Nc                 S   s   g | ]}d qS rD   r-   ).0_r-   r-   r.   
<listcomp>   s    z,traverse_trees_recursive.<locals>.<listcomp>r   c                 S   s   g | ]
}|r
|d  pdqS )r   Nr-   )rJ   eir-   r-   r.   rL      s    /c                 3   s    | ]}t | V  qd S rD   )rF   )rJ   erB   r-   r.   	<genexpr>   s    z+traverse_trees_recursive.<locals>.<genexpr>)r2   listr   streamreadr3   r>   r   r   rA   extendr   tuple)rH   rI   rB   
trees_datanttree_shar/   r6   tir9   iir?   r   _shar(   r)   r:   tior-   rP   r.   r      s>   


r   rY   c                 C   sb   g }t | | }|D ]!\}}}t|r$|t| ||| d  q||||| f q|S )aA  
    :return:
        List of entries of the tree pointed to by the binary `tree_sha`.

        An entry has the following format:

        * [0] 20 byte sha
        * [1] mode as int
        * [2] path relative to the repository

    :param path_prefix:
        Prefix to prepend to the front of all returned paths.
    rN   )r   rS   rT   r   rU   r   r3   )rH   rY   rB   r   r/   r8   r(   r)   r-   r-   r.   r      s   r   ) __doc____all__statr   
git.compatr   r   typingr	   r
   r   r   r   r   r   r   	_typeshedr   gitr   r    intr"   EntryTupEntryTupOrNoner   r   boolrA   rF   r   r   r-   r-   r-   r.   <module>   s8   (,$"4
"X