U
    楡c!                     @   s^  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rdddlmZ ddlmZ eeeef Zeedf Zd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 ) zBModule with functions which are supposed to be as fast as possible    )S_ISDIR)safe_decodedefenc)CallableListMutableSequenceSequenceTupleTYPE_CHECKINGUnionoverload)ReadableBuffer)GitCmdObjectDBN)tree_to_streamtree_entries_from_datatraverse_trees_recursivetraverse_tree_recursiver   )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 give list of entries into a stream using its write method
    :param entries: **sorted** list of tuples with (binsha, mode, name)
    :param write: 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*   3/tmp/pip-unpacked-wheel-_pbxsds5/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 )zReads the binary representation of a tree and returns tuples of 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   E   s*    

r   )	tree_datar'   is_dirstart_atr   c                 C   s   z:| | }|r8|d |kr8t |d |kr8d| |< |W S W n tk
rN   Y nX t| D ]8\}}|rX|d |krXt |d |krXd| |< |  S qXd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_names   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 prefixr   r   r7   r*   r?   r*   r*   r+   r@      s    r   .)odb	tree_shasr=   r   c              	      s0  g }t |}|D ]2}|dk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 lostNc                 S   s   g | ]}d qS r>   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*   )rC   eir*   r*   r+   rE      s     /c                 3   s   | ]}t | V  qd S r>   )r@   )rC   er=   r*   r+   	<genexpr>   s     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 )a&  
    :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 pathsrG   )r   rL   rM   r   rN   r   r0   )rA   rQ   r=   r   r,   r3   r&   r'   r*   r*   r+   r      s    r   ) __doc__statr   Z
git.compatr   r   typingr   r   r   r   r	   r
   r   r   Z	_typeshedr   gitr   r    intr"   ZEntryTupZEntryTupOrNone__all__r   r   boolr<   r@   r   r   r*   r*   r*   r+   <module>   s.   ((.  N