a
    &0_+                     @   s   d dl mZ d dlZd dlZddlmZ ddlmZmZ ddl	m
Z
 dd Zd	d
 Zdd Zdd Zdd Zdd Zejdd Zdd Zdd Zdd ZdS )    )absolute_importN   )_)hexshort)errorc                    s    jj}fdd|d D fdd}d}||d |d \ }|spd	}||d |d \ } }|st|d d
krt|d d
kr|d |d krttdttd t|f i }t	
 g}g }	|rX| }
||
 g kr|	|
 ||
D ]>}|dkr||v r>|| |
 n|
g||< || qq|	  t|	} fdd|	D }|d
ks|sfdd|	D d|fS |d }d}d}t }|	D ]}
|
|v r|||
g  q||
 p|
g}d||
< t|}|| }t||}||kr@|
vr@|}|
}||kr@ q||k rj|
vrj|||
g  q||
g D ]8}|| rtt|| | ||< n||g ||< qvq|dusJ |}|g||fS )a  find the next node (if any) for testing during a bisect search.
    returns a (nodes, number, good) tuple.

    'nodes' is the final result of the bisect if 'number' is 0.
    Otherwise 'number' indicates the remaining possible candidates for
    the search and 'nodes' contains the next bisect target.
    'good' is True if bisect is searching for a first good changeset, False
    if searching for a first bad one.
    c                    s   h | ]}  |qS  rev.0n	changelogr   3/usr/lib/python3/dist-packages/mercurial/hbisect.py	<setcomp>&       zbisect.<locals>.<setcomp>   skipc                    s\   t  fdd| D }tdd }d||D ]}g ||< q2|| d u rT|d fS ||fS )Nc                    s   g | ]}  |qS r   r	   r   r   r   r   
<listcomp>)   r   z2bisect.<locals>.buildancestors.<locals>.<listcomp>c                   S   s   d S )Nr   r   r   r   r   <lambda>*   r   z0bisect.<locals>.buildancestors.<locals>.<lambda>s!   descendants(%ln) - ancestors(%ln))mincollectionsdefaultdictrevs)badgoodbadrev	ancestorsr
   )r   repor   r   buildancestors(   s    
zbisect.<locals>.buildancestorsF   bad   goodTr   s+   starting revisions are not directly relateds)   inconsistent state, %d:%s is good and badc                    s    g | ]}|vr| kr|qS r   r   r   c)r   skipr   r   r   W   r   zbisect.<locals>.<listcomp>c                    s   g | ]}  |qS r   noder#   r   r   r   r   Y   r   r      N)
unfilteredr   Z
parentrevsr'   lenr   Abortr   r   r   dequepopleftappendsortsetupdategetr   list)r   stateZ	clparentsr   r   r   r   ZchildrenZvisitZ
candidatesr
   prevZtotZ	unskippedZperfectZbest_revZbest_lenZpoisonaxyvaluer$   Z	best_noder   )r   r   r   r%   r   bisect   s    	










r:   c                 C   sj   | |d    }t|dkrf|r*|d }n|d }tdd |D t|@ }|dkrf|d |d S d S )Nr   r   r    r!   c                 S   s   h | ]}|  qS r   r&   )r   ir   r   r   r      r   zextendrange.<locals>.<setcomp>)parentsr*   r0   Zancestor)r   r4   nodesr   r<   ZsideZnumr   r   r   extendrange   s    
r>   c                 C   sn   g g g g d}| j dD ]N}|d d  \}}|  |}||vrZttd| || | q|S )N)   currentr!   r    r      bisect.stater"   s   unknown bisect kind %s)	vfsZtryreadlinessplitr)   lookupr   r+   r   r.   )r   r4   lkindr'   r   r   r   
load_state   s    rF   c              	   C   sv   | j dddd}|  J t|D ](}|| D ]}|d|t|f  q.q"|  W d    n1 sh0    Y  d S )Nr@      wT)Z
atomictemps   %s %s
)rA   Zwlocksortedwriter   close)r   r4   frE   r'   r   r   r   
save_state   s    
rL   c                 C   s   | j dr| j d dS )z+remove any bisect state from the repositoryr@   N)rA   existsunlink)r   r   r   r   
resetstate   s    rO   c                 C   s>   | d r| d rdS | d s,t tdnt tddS )z[check we have both 'good' and 'bad' to define a range

    Raise Abort exception otherwise.r!   r    Ts'   cannot bisect (no known good revisions)s&   cannot bisect (no known bad revisions)N)r   r+   r   )r4   r   r   r   
checkstate   s
    rP   c              	   c   s:   zd V  W |g|d< t | | n|g|d< t | | 0 d S )Nr?   )rL   )r   r4   r'   r   r   r   restore_state   s    

rQ   c                 C   s  t | }|dv r&t|  jj|| S d}| d}t|dkrJd}d}nd}d}d	}d
|||f }d||f }	d}
d}d|
||f }|dkr| |S |dkr| |S |dkr| |	S |dkr| |S |dkr| |S |dk r| |S tt	ddS )a  
    Return a list of revision(s) that match the given status:

    - ``good``, ``bad``, ``skip``: csets explicitly marked as good/bad/skip
    - ``goods``, ``bads``      : csets topologically good/bad
    - ``range``              : csets taking part in the bisection
    - ``pruned``             : csets that are goods, bads or skipped
    - ``untested``           : csets whose fate is yet unknown
    - ``ignored``            : csets ignored due to DAG topology
    - ``current``            : the cset currently being bisected
    )r!   r    r   r?   s9   ( bisect(bad)::bisect(good) | bisect(good)::bisect(bad) )s   bisect(good)::bisect(bad)r   s   bisect(good)::s   ::bisect(bad)s   ::bisect(good)s   bisect(bad)::s   bisect(skip)s   ( (%s) | (%s) | (%s) )s   ( (%s) - (%s) )s   ::bisect(bad) - ::bisect(good)s   ::bisect(good) - ::bisect(bad)s   ( ( (%s) | (%s) ) - (%s) )s   ranges   pruned   untested   ignored   goods   badss   invalid bisect stateN)
rF   mapr)   r   r
   r   r*   r   Z
ParseErrorr   )r   Zstatusr4   rangeZ_tZgoodsZbadsZskipsZprunedZuntestedZibaZigaZignoredr   r   r   r2      s:    








r2   c                 C   s   | j |}|t| dv r"tdS |t| dv r8tdS |t| dv rNtdS |t| dv sj|t| dv rrtdS |t| dv rtdS |t| dv rtd	S |t| d
v rtdS d S )Nr!   r    r   s   skippedrR   r?   rS   rT   s   good (implicit)rU   s   bad (implicit))r   r
   r2   r   )r   r'   r
   r   r   r   label  s     rX   c                 C   s   |  }t|dkrr|r(| td n| td |||d   t||||}|d ur| td|  n:|r| td n| td |D ]}|||  q|  d S )Nr   s   The first good revision is:
s   The first bad revision is:
r   s   Not all ancestors of this changeset have been checked.
Use bisect --extend to continue the bisection from
the common ancestor, %s.
sC   Due to skipped revisions, the first good revision could be any of:
sB   Due to skipped revisions, the first bad revision could be any of:
)r)   r*   rI   r   Zshowr>   rJ   )Zuir   r4   Z	displayerr=   r   Z
extendnoder   r   r   r   printresult$  s:    
rY   )Z
__future__r   r   
contextlibZi18nr   r'   r   r    r   r:   r>   rF   rL   rO   rP   contextmanagerrQ   r2   rX   rY   r   r   r   r   <module>   s    k	
D