o
    Uh\                     @   s   d Z ddlmZ ddlmZmZ ddlmZmZm	Z	 ddl
mZ e	dZG dd	 d	ee Zdd
dZdd Zdd Zdd ZdS )z;Implementation of merge-base following the approach of git.    )Iterator)heappopheappush)GenericOptionalTypeVar   )LRUCacheTc                   @   sd   e Zd ZdddZdeeef ddfddZdeeeef  fdd	Z	de
eeef  fd
dZdS )WorkListreturnNc                 C   s
   g | _ d S Npq)self r   a/var/www/html/construction_image-detection-poc/venv/lib/python3.10/site-packages/dulwich/graph.py__init__$   s   
zWorkList.__init__itemc                 C   s   |\}}t | j| |f d S r   )r   r   )r   r   dtcmtr   r   r   add'   s   zWorkList.addc                 C   s$   t | j}|r|\}}| |fS d S r   )r   r   )r   r   prr   r   r   r   get+   s
   

zWorkList.getc                 c   s"    | j D ]
\}}| |fV  qd S r   r   )r   r   r   r   r   r   iter2   s   zWorkList.iter)r   N)__name__
__module____qualname__r   tupleintr
   r   r   r   r   r   r   r   r   r   r   #   s
    
r   c                    s  g }i }d}d}d d}	dt f fdd}
t }|||< ||||f |D ]}||d}||B ||< ||||f q'|
||r| \}}|| ||B  B @ }|||B kru|| |	@ |	ksq|| |	B ||< |||f | B }| |}|r|D ]%}||d}||@ |krq}||}||k rq}||B ||< |||f q}|
||sDg }|D ]\}}||  @  ks||f|vr|||f q|jd	d
 d dd |D }|S )Nr            r   c                    s4   |   D ]\}}||v r||  @  ks dS qdS )NTF)r   )wlstcstatesr   r   _DNCr   r   _has_candidatesA   s   z#_find_lcas.<locals>._has_candidatesr   c                 S   s   | d S )Nr   r   )xr   r   r   <lambda>u   s    z_find_lcas.<locals>.<lambda>)keyc                 S   s   g | ]\}}|qS r   r   ).0r   r   r   r   r   
<listcomp>v   s    z_find_lcas.<locals>.<listcomp>)boolr   r   r   appendsort)lookup_parentsc1c2slookup_stamp	min_stampcandsr$   	_ANC_OF_1	_ANC_OF_2_LCAr'   r#   c2cflagsr   r   parentspcmtpflagspdtresultslcasr   r%   r   
_find_lcas7   sT   	

rA   c                    s|   t dd   fdd} fdd}|sg S |d }t|dks(|gS |dd	 }||v r5|gS t||||}|S )
zFind lowest common ancestors of commit_ids[0] and *any* of commits_ids[1:].

    Args:
      repo: Repository object
      commit_ids: list of commit ids
    Returns:
      list of lowest common ancestor commit_ids
       	max_cachec                        |  vrj |   | <  |  jS r   object_storecommit_timecmtidcmtcacherepor   r   r3         
z%find_merge_base.<locals>.lookup_stampc                    "   d }|  v r
 |  }j | |dS N)commitget_parentsrJ   rQ   rL   parents_providerr   r   r0         z'find_merge_base.<locals>.lookup_parentsr   r   N)r	   rV   lenrA   )rM   
commit_idsr3   r0   r1   r2   r@   r   rL   rV   rM   r   find_merge_base{   s   
	r[   c           
         s   t dd   fdd} fdd}|sg S t|dkr&t|S |d g}|d	d
 }|D ]}g }|D ]}t|||g|}	||	 q9|d
d
 }q3|S )zFind lowest common ancestors of *all* provided commit_ids.

    Args:
      repo: Repository
      commit_ids:  list of commit ids
    Returns:
      list of lowest common ancestor commit_ids
    rB   rC   c                    rE   r   rF   rI   rK   r   r   r3      rN   z'find_octopus_base.<locals>.lookup_stampc                    rO   rP   rR   rT   rU   r   r   r0      rW   z)find_octopus_base.<locals>.lookup_parentsr    r   r   N)r	   rV   rX   r[   rA   extend)
rM   rY   r3   r0   r@   othersr   	next_lcascaresr   rZ   r   find_octopus_base   s"   
	

ra   c                    s`   t dd   fdd} fdd}||krdS ||}t|||g||d}||gkS )	zIs it possible to fast-forward from c1 to c2?

    Args:
      repo: Repository to retrieve objects from
      c1: Commit id for first commit
      c2: Commit id for second commit
    rB   rC   c                    rE   r   rF   rI   rK   r   r   r3      rN   z&can_fast_forward.<locals>.lookup_stampc                    rO   rP   rR   rT   rU   r   r   r0      rW   z(can_fast_forward.<locals>.lookup_parentsT)r4   )r	   rV   rA   )rM   r1   r9   r3   r0   r4   r@   r   rZ   r   can_fast_forward   s   

rb   N)r   )__doc__collections.abcr   heapqr   r   typingr   r   r   	lru_cacher	   r
   r   rA   r[   ra   rb   r   r   r   r   <module>   s   
D$'