
    q&fu                         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mZ d dlmZ ddlmZ d Zd	 Zd
 Zd Zd Zd Zd Ze j0                  d        ZddZd Zd Zd Zy)    N)_)open)error
extensionshgutilwireprotov1peerwireprotov1server)urlutil   )contextc                 j    | j                  dd      }|st        j                  t        d            |S )z)get the mainbranch, and enforce it is set   fastannotates
   mainbranchsF   fastannotate.mainbranch is required for both the client and the server)configr   Abortr   )uimasters     =/usr/lib/python3/dist-packages/hgext/fastannotate/protocol.py
_getmasterr      s:    YY6Fkk6
 	
 M    c                 :     | ||      }|j                  d       |S )N   getannotate)append)origrepoprotoresults       r   _capabilitiesr   -   s    $F
MM.!Mr   c                 r   d}| j                   j                  ddd      }t        j                  | |      5 }|rIt	        | j                         }	 |j                  |      s|j                  ||       |j                          |j                  |k7  r|j                  |j                  fD ]  }t        j                  j                  |      s#t!        |d      5 }	|	j#                         }
d d d        t%        | j&                  j(                  dz         }||d  }|d|t%        
      |
fz  z  } d d d        |S # t        $ rG |j                          	 |j                  ||       n# t        $ r |j                          Y nw xY wY w xY w# |j                          w xY w# 1 sw Y   xY w# 1 sw Y   |S xY w)Nr   r   s   serverbuildondemandTs   rb   /s   %s %d %s)r   
configboolr   annotatecontextr   
isuptodateannotate	Exceptionrebuildcloselastnode
revmappathlinelogpathospathexistsr   readlenvfsbase)r   r   r,   r(   r   buildondemandactxr   pfcontent
vfsbaselenrelpaths                r   _getannotater9   3   s    FGG&&/M 
	 	 t	, K(Fv.MM&&1 


 ==H$oot'7'78 Kww~~a(!U^ 'qffhG' !56
JK.-7CL'*JJJK1K@ M3  ##MM&&1  #LLN## 

' '7K@ Msy   F,#D82A&F,F )AF,8FE%$F%F>F FFFFFFF, F)%F,,F6c                  j    dt         j                  v ry  t        j                  dd      t               y )Nr   s   path lastnode)r
   commandswireprotocommandr9    r   r   _registerwireprotocommandr>   ^   s0    *333H&&~7GHr   c                 V    t                t        j                  t        dt               y )Nr   )r>   r   wrapfunctionr
   r   r   s    r   serveruisetuprB   f   s    -Nr   c                    i }d}t        |       dz
  }d}dx}}||k  ri| ||dz    }|dk(  r<|dk(  r/| |dz   |dz   t        |      z    ||<   |t        |      z  }d}dx}}n|dk(  rd}n|dk(  r||z  }n
|dk(  r||z  }|dz  }||k  ri|S )Nr   r   r       )r/   int)payloadr   ilstatevfspathsizechs           r   _parseresponserM   n   s    F	AGqAEGd
a%QQ;z")!a%!a%#d)2C"DwSY!$$$!z
!2	Q a%  Mr   c                 B      G  fdd|j                         }||_         y )Nc                   8    e Zd Zej                  d fd	       Zy)#peersetup.<locals>.fastannotatepeerNc                 ~    | j                  d      sj                  t        d             y||xs dd}|t        fS )Nr   s*   remote peer cannot provide annotate cache
NNr   s   paths   lastnode)capablewarnr   rM   )selfr,   r(   argsr   s       r   getannotatez/peersetup.<locals>.fastannotatepeer.getannotate   s=    <</HIJ!!%HOD^++r   N)__name__
__module____qualname__r	   	batchablerX   rA   s   r   fastannotatepeerrP      s    		"	"	, 
#	,r   r^   	__class__)r   peerr^   s   `  r   	peersetuprb      s    ,4>> , &DNr   c              #      K   | j                   }|j                  ddd      }t        j                  d||      }t	        j
                  |i |      }	 | |j                          y # |j                          w xY ww)Nr   s
   remotepaths   default)r   r   r   get_unique_pull_path_objr   ra   r'   )r   r   
remotedest
remotepathra   s        r   annotatepeerrg      sj     	B?M:FJ11
J
 772r:&D




s   AA;A& A;&A88A;c                    |sy|#t        |       5 }t        | |||      cddd       S |i }| j                  }g }|j                         5 }|j	                  dt        |      z         |D ]5  }|j                  |j                  d||j                  |      d             7 |D ]  }|j                         }	|	j                         D 
ci c]  \  }}
t        j                  |      |
 }	}}
t        |	      D ]  }|j                  d      rd|dz   v r|j	                  d|z         0|	|   }|j                  r|j	                  d	t        |      |fz         | j                   j#                  t$        j&                  j)                  |             | j!                  |d
      5 }|j+                  |       ddd         	 ddd       y# 1 sw Y   xY wc c}
}w # 1 sw Y   xY w# 1 sw Y   yxY w)z1download annotate cache from the server for pathsNs"   fastannotate: requesting %d files
r   rS   s   fastannotate/s   /../r    s(   fastannotate: ignored malicious path %s
s%   fastannotate: writing %d bytes to %s
s   wb)rg   clientfetchr   commandexecutordebugr/   r   callcommandgetr   itemsr   pconvertsorted
startswith	debugflagr0   makedirsr+   r,   dirnamewrite)r   pathslastnodemapra   r   resultsbatcherr4   r   rvr,   r6   r5   s                 r   ri   ri      s   |$ 	?4tUK>	? 	? 	BG				 %7
7#e*DE 	ANN##"kooa.@A	  	%FA12;Aq!1$;A;q	 %'78G4K= HHDtK D'<<HHAw<./ !!"''//$"78XXdE* %aGGG$% %!%		%% %	? 	?* <"% %=% %sC   GBG, G
,B8G,$G 	6G,GG, G)%G,,G5c                     | j                   j                  ddd      }|dk  r|S g }|D ]1  }	 t        | j                  |            |k\  r|j	                  |       3 |S # t
        $ r |j	                  |       Y Rw xY w)zreturn a subset of paths whose history is long and need to fetch linelog
    from the server. works with remotefilelog and non-remotefilelog repos.
    r   s   clientfetchthreshold
   r   )r   	configintr/   filer   r%   )r   rv   	thresholdr   r,   s        r   _filterfetchpathsr      s     !!/3JBOIA~F  	 499T?#y0d#  M  	 MM$	 s   .AA<;A<c                 <     G d d|j                         }||_         y )Nc                       e Zd ZddZy)(localreposetup.<locals>.fastannotaterepoNc                    t        | j                        }g }i }	 t        | |      D ]T  }t        j                  | |      5 }|j                  |d      s |j                  |       |j                  ||<   d d d        V |rt        | |||       y y # 1 sw Y   rxY w# t        $ r(}| j                  j                  d|z         Y d }~y d }~ww xY w)NF)stricts"   fastannotate: prefetch failed: %r
)r   r   r   r   r"   r#   r   r(   ri   r%   rk   )	rV   rv   ra   r   needupdatepathsrw   r,   r3   exs	            r   prefetchfastannotatez=localreposetup.<locals>.fastannotaterepo.prefetchfastannotate   s    (F OK
K-dE: >D 00t< >#veD+224804K-> >>
 #o{DI #	> >  KDrIJJKs/   %B  4B4B B	B 	C%CCrY   )rZ   r[   r\   r   r=   r   r   fastannotaterepor      s    	Kr   r   r_   )r   r   r   s      r   localreposetupr      s    K4>> K" &DNr   c                     t                |j                         rt        | |       t        t        j
                  vr$t        j
                  j                  t               y y rY   )r>   localr   rb   r   wirepeersetupfuncsr   )r   r   s     r   clientreposetupr      sD    zz|r4  ---
$$Y/ .r   rR   )
contextlibr+   mercurial.i18nr   mercurial.pycompatr   	mercurialr   r   r   r   r	   r
   mercurial.utilsr    r   r   r   r9   r>   rB   rM   rb   contextmanagerrg   ri   r   r   r   r=   r   r   <module>r      s{     	  #  

 (VO2&  "-%`&&*0r   