B
    %d!                 @   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}x| D ]\}}}d}x.tdD ]"}t||d ? |@ | g| }q*W |d |krh|dd }t|tr~|t}	n|}	|d|d	|	d
|f qW 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*   \/work/yifan.wang/ringdown/master-ringdown-env/lib/python3.7/site-packages/git/objects/fun.pyr   &   s    "
r   )datar   c             C   s   t d}t d}t| }d}g }x||k rd}x,| | |krZ|d> | | |  }|d7 }q0W |d7 }|}x| | dkr|d7 }qjW | || }t|}	|d7 }| ||d  }
|d }||
||	f q"W |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   F   s*    
r   )	tree_datar'   is_dirstart_atr   c             C   s   y8| | }|r6|d |kr6t |d |kr6d| |< |S W n tk
rL   Y nX x@t| D ]4\}}|rX|d |krXt |d |krXd| |< |S qXW 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)r4   r'   r5   r6   itemindexr*   r*   r+   _find_by_nameu   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 )Nr*   )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+   r>      s    r   .)odb	tree_shasr=   r   c          	      sB  g }t |}x:|D ]2}|dkr$g }ntt| | }|| qW g }xt|D ]\}}	xt|	D ]\}
}|svqhdd t|D }|||< |\}}}t|}x8t|d || D ]"}|| }t	|| |||
||< qW |r|
t| dd |D  | d  n|t fdd|D  d|	|
< qhW |	dd= qVW |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 )Nr*   ).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*   )rA   eir*   r*   r+   rC      s    /c             3   s   | ]}t | V  qd S )N)r>   )rA   e)r=   r*   r+   	<genexpr>   s    z+traverse_trees_recursive.<locals>.<genexpr>)r/   listr   streamreadr0   r9   r   r   r<   extendr   tuple)r?   r@   r=   Z
trees_datanttree_shar,   r1   tir4   iir:   r   Z_shar&   r'   r5   Ztior*   )r=   r+   r      s:    

r   )r?   rN   r=   r   c             C   sf   g }t | | }xJ|D ]B\}}}t|rJ|t| ||| d  q||||| f qW |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 pathsrE   )r   rI   rJ   r   rK   r   r0   )r?   rN   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(   (( /L