o
    Uh@                     @   s   d Z ddlZddlZddlmZ ddlmZ ddlmZm	Z	m
Z
mZmZ ddlmZ ddlmZmZmZ d	Zd
ZeefZdZG dd dZG dd dZG dd dZdd fddZdS )z=General implementation of walking commits and their contents.    N)chain)Optional   )RENAME_CHANGE_TYPESRenameDetector
TreeChangetree_changestree_changes_for_merge)MissingCommitError)CommitObjectIDTagdatetopo   c                   @   s2   e Zd ZdZd
ddZdddZdefdd	ZdS )	WalkEntryz1Object encapsulating a single result from a walk.returnNc                 C   s(   || _ |j| _|j| _i | _|j| _d S N)commitstore_storeget_parents_get_parents_changesrename_detector_rename_detector)selfwalkerr    r   `/var/www/html/construction_image-detection-poc/venv/lib/python3.10/site-packages/dulwich/walk.py__init__3   s
   zWalkEntry.__init__c              	      sH   j |}|du r j} |st}d}nct |dkr?t} j |d  j}|r>| jj	|\}} j| }n;t
} fdd |D }|rz fdd|D }g }|D ]}	z|	 jj	|\}}
W n	 tys   Y q\w ||
 q\|j}|r j| }| jj	|\}}t| j|| jd}| j |<  j | S )a-  Get the tree changes for this entry.

        Args:
          path_prefix: Portion of the path in the repository to
            use to filter changes. Must be a directory name. Must be
            a full, valid, path reference (no partial names or wildcards).
        Returns: For commits with up to one parent, a list of TreeChange
            objects; if the commit has no parents, these will be relative to
            the empty tree. For merge commits, a list of lists of TreeChange
            objects; see dulwich.diff.tree_changes_for_merge.
        Nr   r   c                    s   g | ]} j | jqS r   )r   tree.0pr   r   r   
<listcomp>W   s    z%WalkEntry.changes.<locals>.<listcomp>c                    s   g | ]} j | qS r   )r   r"   r%   r   r   r&   Y   s    )r   )r   getr   r   r   lenr   r!   lookup_path__getitem__r	   KeyErrorappendlistr   )r   path_prefixcachedr   changes_funcparentmodesubtree_shaparent_treesr$   stcommit_tree_shacommit_treer   r%   r   changes:   s`   




zWalkEntry.changesc                 C   s   d| j j d|  dS )Nz<WalkEntry commit=z
, changes=>)r   idr8   r%   r   r   r   __repr__w   s   zWalkEntry.__repr__r   Nr   )__name__
__module____qualname____doc__r    r8   strr;   r   r   r   r   r   0   s
    

=r   c                   @   sB   e Zd ZdZdddZdeddfd	d
ZdddZdd ZeZ	dS )_CommitTimeQueuez3Priority queue of WalkEntry objects by commit time.r   Walkerr   Nc                 C   sx   || _ |j| _|j| _|j| _g | _t | _	t | _
t | _|j| _d | _t| _d| _t|j|jD ]}| | q2d S )NF)_walkerr   r   r   r   excluded	_excluded_pqset_pq_set_seen_donesince	_min_time_last_MAX_EXTRA_COMMITS_extra_commits_left_is_finishedr   include_push)r   r   	commit_idr   r   r   r    ~   s   z_CommitTimeQueue.__init__	object_idc              
   C   s   z| j | }W n ty } zt||d }~ww t|tr(| |jd  d S |}|j| jvrQ|j| j	vrSt
| j|j |f | j|j | j|j d S d S d S Nr   )r   r+   r
   
isinstancer   rS   objectr:   rI   rK   heapqheappushrG   commit_timeaddrJ   )r   rU   objexcr   r   r   r   rS      s   

z_CommitTimeQueue._pushc                 C   sd   | j }| j}|g}|r0| }| |D ]}||vr&||v r&|| j|  || q|sd S d S r   )rF   rJ   popr   r,   r   r\   )r   r   rE   seentodor1   r   r   r   _exclude_parents   s   z!_CommitTimeQueue._exclude_parentsc                    s,   j rd S  jrt j\}}|j} j| | jv rq j|  	|D ]} 
| q*d}| jv }|re |  jret fdd jD re jd \}} jrc|j jjkrcd}nd} jd urr|j jk rrd}|rxt _n  jd8  _ jsn|s| _t j|S  jsd _ d S )NTc                 3   s     | ]\}}|j  jv V  qd S r   )r:   rF   )r#   _cr%   r   r   	<genexpr>   s    z(_CommitTimeQueue.next.<locals>.<genexpr>r   Fr   )rQ   rG   rY   heappopr:   rI   removerK   r\   r   rS   rF   rb   allrN   r[   rM   rO   rP   r   rD   )r   rc   r   sha	parent_idreset_extra_commitsis_excludednr   r%   r   next   sB   


-z_CommitTimeQueue.next)r   rC   r   Nr<   )
r=   r>   r?   r@   r    bytesrS   rb   rn   __next__r   r   r   r   rB   {   s    

3rB   c                   @   s   e Zd ZdZddddddddddd efdee deee  d	ed
e	dee
 deee  dee de	dee
 dee
 ddfddZde	fddZde	fddZdee	 fddZdd Zdd Zdd ZdS ) rC   zObject for performing a walk of commits in a store.

    Walker objects are initialized with a store and other options and can then
    be treated as iterators of Commit objects.
    Nr   Fc                 C      | j S r   parentsr   r   r   r   <lambda>       zWalker.<lambda>rR   excludeorderreversemax_entriespathsr   followrL   untilr   c                 C   s   |t vrtd| || _t|tr|g}|| _t|pg | _|| _|| _	|| _
|r/t|p0d| _|	r:|s:t|}|| _|| _|	| _|
| _|| _d| _|| | _t | _dS )a  Constructor.

        Args:
          store: ObjectStore instance for looking up objects.
          include: Iterable of SHAs of commits to include along with their
            ancestors.
          exclude: Iterable of SHAs of commits to exclude along with their
            ancestors, overriding includes.
          order: ORDER_* constant specifying the order of results.
            Anything other than ORDER_DATE may result in O(n) memory usage.
          reverse: If True, reverse the order of output, requiring O(n)
            memory.
          max_entries: The maximum number of entries to yield, or None for
            no limit.
          paths: Iterable of file or subtree paths to show entries for.
          rename_detector: diff.RenameDetector object for detecting
            renames.
          follow: If True, follow path across renames/copies. Forces a
            default rename_detector.
          since: Timestamp to list commits after.
          until: Timestamp to list commits before.
          get_parents: Method to retrieve the parents of a commit
          queue_cls: A class to use for a queue of commits, supporting the
            iterator protocol. The constructor takes a single argument, the
            Walker.
        zUnknown walk order Nr   )
ALL_ORDERS
ValueErrorr   rW   ro   rR   rH   rE   rx   ry   rz   r{   r   r   r   r|   rL   r}   _num_entries_queuecollectionsdeque
_out_queue)r   r   rR   rw   rx   ry   rz   r{   r   r|   rL   r}   r   	queue_clsr   r   r   r       s*   ,

zWalker.__init__c                 C   sX   |d u rdS | j d u rdS | j D ]}||kr dS ||r)|t| dkr) dS qdS )NFT/   )r{   
startswithr(   )r   changed_pathfollowed_pathr   r   r   _path_matches/  s   

zWalker._path_matchesc                 C   sj   | j sJ |s	dS |jj}|jj}| |r,| jr*|jtv r*| j | | j 	| dS | |r3dS dS )NFT)
r{   oldpathnewr   r|   typer   r\   rg   )r   changeold_pathnew_pathr   r   r   _change_matches>  s   


zWalker._change_matchesc                 C   s   |j }| jdur|j| jk rdS | jdur|j| jkrdS |j| jv r%dS | jdu r,dS t| |dkrL|	 D ]}|D ]}| 
|rH  dS q=q9dS |	 D ]
}| 
|rZ dS qPdS )a	  Determine if a walk entry should be returned..

        Args:
          entry: The WalkEntry to consider.
        Returns: True if the WalkEntry should be returned by this walk, or
            False otherwise (e.g. if it doesn't match any requested paths).
        NFTr   )r   rL   r[   r}   r:   rE   r{   r(   r   r8   r   )r   entryr   path_changesr   r   r   r   _should_returnN  s,   


zWalker._should_returnc                 C   s   | j }|d u s| j|k rGt| j}|d ur| j| |d u s&t| jtkr>| js+d S | j }| 	|r>|  jd7  _|S |d u s| j|k sd S rV   )
rz   r   rn   r   r   r,   r(   rO   popleftr   )r   rz   r   r   r   r   _nexto  s   


zWalker._nextc                 C   s,   | j tkrt|| j}| jrtt|}|S )a  Possibly reorder a results iterator.

        Args:
          results: An iterator of WalkEntry objects, in the order returned
            from the queue_cls.
        Returns: An iterator or list of WalkEntry objects, in the order
            required by the Walker.
        )rx   
ORDER_TOPO_topo_reorderr   ry   reversedr-   )r   resultsr   r   r   _reorder~  s
   
	zWalker._reorderc                 C   s   t | t | jd S r   )iterr   r   r%   r   r   r   __iter__  s   zWalker.__iter__)r=   r>   r?   r@   rB   r-   ro   r   rA   boolintr   r    r   r   r   r   r   r   r   r   r   r   rC      sT    


	

E!rC   c                 C   rq   r   rr   rt   r   r   r   ru     rv   ru   c                 c   s    t  }i }t t}| D ]}|| ||jD ]
}||  d7  < qq|rb| }|j}|j}|| r;|||< q&||D ]}	||	  d8  < ||	 sZ||	d}
|
rZ|	|
 q?|V  |s(dS dS )a  Reorder an iterable of entries topologically.

    This works best assuming the entries are already in almost-topological
    order, e.g. in commit time order.

    Args:
      entries: An iterable of WalkEntry objects.
      get_parents: Optional function for getting the parents of a commit.
    Returns: iterator over WalkEntry objects from entries in FIFO order, except
        where a parent would be yielded before any of its children.
    r   N)
r   r   defaultdictr   r,   r   r   r:   r_   
appendleft)entriesr   ra   pendingnum_childrenr   r$   r   rT   rj   parent_entryr   r   r   r     s2   


r   )r@   r   rY   	itertoolsr   typingr   	diff_treer   r   r   r   r	   errorsr
   objectsr   r   r   
ORDER_DATEr   r~   rO   r   rB   rC   r   r   r   r   r   <module>   s"   Kh /