a
    `Df"                     @   sb  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rlddlmZ ddlmZ eeeef Zeedf Zee ed	geedf f dd
ddZee	e dddZe
e eeeedddZededdddZeeeedddZeeedddZdeeedf  ee	eedf  dddZdeee	e d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)GitCmdObjectDBNr   )entrieswritereturnc           
   	   C   s   t d}d}| D ]\}}}d}tdD ]"}t||d ? |@ | g| }q&|d |krb|dd }t|trx|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_zeroZbit_maskZbinshamodenameZmode_stri
name_bytes r*   \/nfs/NAS7/SABIOD/METHODE/ermites/ermites_venv/lib/python3.9/site-packages/git/objects/fun.pyr   )   s    	 
r   )datar   c                 C   s   t d}t d}t| }d}g }||k rd}| | |krV|d> | | |  }|d7 }q,|d7 }|}| | dkrx|d7 }qb| || }t|}	|d7 }| ||d  }
|d }||
||	f q |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%   Z	space_ordZlen_datar(   outr&   nsr)   r'   shar*   r*   r+   r   M   s*    


r   )	tree_datar'   is_dirstart_atr   c                 C   s   z:| | }|r8|d |kr8t |d |kr8d| |< |W S W n tyL   Y n0 t| D ]8\}}|rV|d |krVt |d |krVd| |< |  S qVd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)r4   r'   r5   r6   itemindexr*   r*   r+   _find_by_name   s     
 
r<   )r:   path_prefixr   c                 C   s   d S Nr*   r:   r=   r*   r*   r+   _to_full_path   s    r@   c                 C   s   d S r>   r*   r?   r*   r*   r+   r@      s    c                 C   s"   | s| S | d | d || d  fS )z%Rebuild entry with given path prefix.r   r   r7   r*   r?   r*   r*   r+   r@      s    r   .)odb	tree_shasr=   r   c              	      s0  g }t |}|D ]2}|du r"g }ntt| | }|| qg }t|D ]\}}	t|	D ]\}
}|snq`dd t|D }|||< |\}}}t|}t|d || D ]"}|| }t	|| |||
||< q|r|
t| dd |D  | d  n|t fdd|D  d|	|
< q`|	dd= qP|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 r>   r*   ).0_r*   r*   r+   
<listcomp>   r   z,traverse_trees_recursive.<locals>.<listcomp>r   c                 S   s   g | ]}|r|d  pdqS )r   Nr*   )rC   eir*   r*   r+   rE      r   /c                 3   s   | ]}t | V  qd S r>   )r@   )rC   er=   r*   r+   	<genexpr>   r   z+traverse_trees_recursive.<locals>.<genexpr>)r/   listr   streamreadr0   r9   r   r   r<   extendr   tuple)rA   rB   r=   Z
trees_datanttree_shar,   r1   Ztir4   iir:   r   Z_shar&   r'   r5   Ztior*   rI   r+   r      s>    


r   )rA   rQ   r=   r   c                 C   sb   g }t | | }|D ]B\}}}t|rH|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.
    rG   )r   rL   rM   r   rN   r   r0   )rA   rQ   r=   r   r,   r3   r&   r'   r*   r*   r+   r      s    r   ) __doc____all__statr   Z
git.compatr   r   typingr	   r
   r   r   r   r   r   r   Z	_typeshedr   gitr   r    intr"   ZEntryTupZEntryTupOrNoner   r   boolr<   r@   r   r   r*   r*   r*   r+   <module>   s*   (($4X