
    6hJ<                       S r SSKJr  SSKrSSKrSSKrSSKJr  SSKJr  \R                  (       a  SSKrSSK
Jr  \R                  " S5      r " S	 S
\5      r\R                    " S S5      5       r\R                    " S S5      5       r " S S5      r " S S\5      r " S S\5      r " S S\5      r " S S\5      r " S S\5      rS r " S S5      rg)a  linelog is an efficient cache for annotate data inspired by SCCS Weaves.

SCCS Weaves are an implementation of
https://en.wikipedia.org/wiki/Interleaved_deltas. See
mercurial/helptext/internals/linelog.txt for an exploration of SCCS weaves
and how linelog works in detail.

Here's a hacker's summary: a linelog is a program which is executed in
the context of a revision. Executing the program emits information
about lines, including the revision that introduced them and the line
number in the file at the introducing revision. When an insertion or
deletion is performed on the file, a jump instruction is used to patch
in a new body of annotate information.
    )annotationsN)List   )attr)pycompats   >IIc                      \ rS rSrSrSrg)LineLogError,   z>Error raised when something bad happens internally in linelog. N)__name__
__module____qualname____firstlineno____doc____static_attributes__r       3/usr/lib/python3/dist-packages/mercurial/linelog.pyr	   r	   ,   s    Hr   r	   c                  z    \ rS rSr\R
                  " 5       r\R
                  " 5       r\R
                  " 5       rSr	g)lineinfo0   r   N)
r   r   r   r   r   ibrevlinenum_offsetr   r   r   r   r   r   0   s%     '')CggiGggiGr   r   c                      \ rS rSr\R
                  " 5       r\R
                  " \\   S9r	\R
                  " 5       r
S rSrg)annotateresult:   typec                ,    [        U R                  5      $ N)iterlinesselfs    r   __iter__annotateresult.__iter__@   s    DJJr   r   N)r   r   r   r   r   r   r   r   r   r#   _eofr&   r   r   r   r   r   r   :   s/    
'')CGGh(E779D r   r   c                      \ rS rSr\R
                  r\R                  S 5       r\R                  S 5       r	S r
\R                  S 5       r\R                  S 5       r\R                  S 5       rSrg	)
_llinstructionD   c                    g r!   r   r%   op1op2s      r   __init___llinstruction.__init__G       r   c                    g r!   r   r$   s    r   __str___llinstruction.__str__K   r2   r   c                    [        U 5      $ r!   )strr$   s    r   __repr___llinstruction.__repr__O   s    4yr   c                    g r!   r   r%   others     r   __eq___llinstruction.__eq__R   r2   r   c                    g)z5Encode this instruction to the binary linelog format.Nr   r$   s    r   encode_llinstruction.encodeV       r   c                    g)a'  Execute this instruction.

Args:
  rev: The revision we're annotating.
  pc: The current offset in the linelog program.
  emit: A function that accepts a single lineinfo object.

Returns:
  The new value of pc. Returns None if exeuction should stop
  (that is, we've found the end of the file.)
Nr   r%   r   pcemits       r   execute_llinstruction.executeZ   rB   r   r   N)r   r   r   r   abcABCMeta__metaclass__abstractmethodr0   r4   r8   r=   r@   rG   r   r   r   r   r*   r*   D   s    KKM  	  	  	D D 	 r   r*   c                  6    \ rS rSrSrS rS rS rS rS r	Sr
g	)
_jgei   z@If the current rev is greater than or equal to op1, jump to op2.c                    Xl         X l        g r!   _cmprev_targetr-   s      r   r0   _jge.__init__l       r   c                8    SU R                   U R                  4-  $ )Nz	JGE %d %drQ   r$   s    r   r4   _jge.__str__p   s    dllDLL999r   c                    [        U 5      [        U5      :H  =(       a9    U R                  UR                  :H  =(       a    U R                  UR                  :H  $ r!   r   rR   rS   r;   s     r   r=   _jge.__eq__s   A    J$u+% .-.-	
r   c                \    [         R                  U R                  S-  U R                  5      $ N   _llentrypackrR   rS   r$   s    r   r@   _jge.encodez   s     }}T\\Q.==r   c                B    XR                   :  a  U R                  $ US-   $ Nr   rQ   rD   s       r   rG   _jge.execute}   s    ,,<<Avr   rQ   Nr   r   r   r   r   r0   r4   r=   r@   rG   r   r   r   r   rN   rN   i   s    J:
>r   rN   c                  6    \ rS rSrSrS rS rS rS rS r	Sr
g	)
_jump   z=Unconditional jumps are expressed as a JGE with op1 set to 0.c                8    US:w  a  [        SU-  5      eX l        g )Nr   s%   malformed JUMP, op1 must be 0, got %d)r	   rS   r-   s      r   r0   _jump.__init__   s    !8G#MNNr   c                     SU R                   -  $ )NzJUMP %drS   r$   s    r   r4   _jump.__str__   s    DLL))r   c                p    [        U 5      [        U5      :H  =(       a    U R                  UR                  :H  $ r!   )r   rS   r;   s     r   r=   _jump.__eq__   s'    DzT%[(JT\\U]]-JJr   c                B    [         R                  SU R                  5      $ Nr   )r`   ra   rS   r$   s    r   r@   _jump.encode   s    }}Q--r   c                    U R                   $ r!   rm   rD   s       r   rG   _jump.execute   s    ||r   rm   Nrf   r   r   r   rh   rh      s    G
*K.r   rh   c                  6    \ rS rSrSrS rS rS rS rS r	Sr
g	)
r(      z1EOF is expressed as a JGE that always jumps to 0.c                T    US:w  a  [        SU-  5      eUS:w  a  [        SU-  5      eg )Nr   s$   malformed EOF, op1 must be 0, got %ds$   malformed EOF, op2 must be 0, got %d)r	   r-   s      r   r0   _eof.__init__   s7    !8FLMM!8FLMM r   c                    g)NEOFr   r$   s    r   r4   _eof.__str__   s    r   c                0    [        U 5      [        U5      :H  $ r!   r   r;   s     r   r=   _eof.__eq__   s    DzT%[((r   c                .    [         R                  SS5      $ rr   )r`   ra   r$   s    r   r@   _eof.encode   s    }}Q""r   c                    g r!   r   rD   s       r   rG   _eof.execute   s    r   r   Nrf   r   r   r   r(   r(      s    ;N)#r   r(   c                  6    \ rS rSrSrS rS rS rS rS r	Sr
g	)
_jl   z1If the current rev is less than op1, jump to op2.c                    Xl         X l        g r!   rQ   r-   s      r   r0   _jl.__init__   rU   r   c                8    SU R                   U R                  4-  $ )NzJL %d %drQ   r$   s    r   r4   _jl.__str__   s    T\\4<<888r   c                    [        U 5      [        U5      :H  =(       a9    U R                  UR                  :H  =(       a    U R                  UR                  :H  $ r!   rY   r;   s     r   r=   
_jl.__eq__   r[   r   c                b    [         R                  SU R                  S-  -  U R                  5      $ )Nr   r^   r_   r$   s    r   r@   
_jl.encode   s%    }}Q$,,!"34dllCCr   c                B    XR                   :  a  U R                  $ US-   $ rd   rQ   rD   s       r   rG   _jl.execute   s    <<Avr   rQ   Nrf   r   r   r   r   r      s    ;9
Dr   r   c                  6    \ rS rSrSrS rS rS rS rS r	Sr
g	)
_line   zEmit a line.c                    Xl         X l        g r!   _rev_origlinenor-   s      r   r0   _line.__init__   s    	r   c                8    SU R                   U R                  4-  $ )Nz
LINE %d %dr   r$   s    r   r4   _line.__str__   s    tyy$*:*:;;;r   c                    [        U 5      [        U5      :H  =(       a9    U R                  UR                  :H  =(       a    U R                  UR                  :H  $ r!   )r   r   r   r;   s     r   r=   _line.__eq__   sE    J$u+% 6		UZZ'6  E$5$55	
r   c                b    [         R                  SU R                  S-  -  U R                  5      $ r]   )r`   ra   r   r   r$   s    r   r@   _line.encode   s&    }}Q$))q.143C3CDDr   c                Z    U" [        U R                  U R                  U5      5        US-   $ rd   )r   r   r   rD   s       r   rG   _line.execute   s&    Xdii!1!1267Avr   )r   r   Nrf   r   r   r   r   r      s    <
Er   r   c                `    [         R                  X5      u  p#US-  nUS-	  nUS:X  a-  US:X  a  US:X  a  [        X#5      $ [        X#5      $ [        X#5      $ US:X  a  [        X#5      $ US:X  a  [        X#5      $ [        SU-  5      e! [        R                   a  n[	        SU-  5      eSnAff = f)z?Decode a single linelog instruction from an offset in a buffer.s!   reading an instruction failed: %rN   r^   r   r   s   Unimplemented opcode %r)r`   unpack_fromstructerrorr	   r(   rh   rN   r   r   NotImplementedError)dataoffsetr.   r/   eopcodes         r   
_decodeoner      s    E''5 4ZF
(C{!8axC~%?"C~	13}	1S
86A
BB << E?!CDDEs   B B-B((B-c                      \ rS rSrSrSS jrS rS rS r\	S 5       r
S	 rS
 rS rSS jrS r\S 5       r\S 5       rS rSS jrSrg)linelog   z1Efficient cache for per-line history information.Nc                `    Uc  [        SS5      [        SS5      /nXl        S U l        X l        g rr   )r(   _program_lastannotate_maxrev)r%   programmaxrevs      r   r0   linelog.__init__   s1    ? Aqz41:.G!r   c                    [        U 5      [        U5      :H  =(       a9    U R                  UR                  :H  =(       a    U R                  UR                  :H  $ r!   )r   r   r   r;   s     r   r=   linelog.__eq__  sA    J$u+% ./.-	
r   c                p    S[        [        U 5      5      U R                  [        U R                  5      4-  $ )Nz"<linelog at %s: maxrev=%d size=%d>)hexidr   lenr   r$   s    r   r8   linelog.__repr__  s2    34MLL7
 
 	
r   c                   ^ S[        [        [        U R                  5      5      5      -  m[        R                  " S5      R                  U4S j[        U R                  SS  S5       5       5      $ )Nz	%%%dd %%s   
c              3  4   >#    U  H  u  pTX4-  v   M     g 7fr!   r   ).0idxifmts      r   	<genexpr>#linelog.debugstr.<locals>.<genexpr>  s      +
)HvsC3(N)Hs   r   )r   r7   r   r   sysstrjoin	enumerate)r%   r   s    @r   debugstrlinelog.debugstr  s[    CC$6 788u%** +
)24==3Da)H+
 
 	
r   c                   [        U5      [        R                  -  S:w  a'  [        S[        U5      [        R                  4-  5      e[        U5      [        R                  -  n[	        US5      n[        U[        5      (       a  SnOI[        U[        [        45      (       a  UR                  nO![        S[        U5      R                  -  5      e[        U[        [        [        45      (       d   eUR                  nX%:w  a  [        SX%4-  5      e[        SS5      /n[        SU5       H.  nUR                  [	        X[        R                  -  5      5        M0     U " XdS9$ )Nr   s9   invalid linelog buffer size %d (must be a multiple of %d)z,Expected one of _jump, _jge, or _jl. Got %s.sF   corrupt linelog data: claimed %d entries but given data for %d entriesr   )r   )r   r`   sizer	   r   
isinstancerh   rN   r   rR   r   r   rS   r(   rangeappend)clsbufexpectedfakejger   
numentriesinstructionsr   s           r   fromdatalinelog.fromdata  s9   s8hmm#q(Ls8X]]+,  s8hmm+S!$gu%%F$--__F>w-(()  'E4#56666__
!=() 
 Q
|Az*F
30F GH +<//r   c                    [        U R                  [        U R                  5      5      R	                  5       nUSR                  S U R                  SS   5       5      -   $ )Nr   c              3  @   #    U  H  oR                  5       v   M     g 7fr!   )r@   )r   r   s     r   r   !linelog.encode.<locals>.<genexpr>:  s     D2CQhhjj2Cs   r   )rN   r   r   r   r@   r   )r%   hdrs     r   r@   linelog.encode8  sI    4<<T]]!34;;=SXXD$--2CDDDDr   c                .    / U l         SU l        S U l        g rr   )r   r   r   r$   s    r   clearlinelog.clear<  s    !r   c           	     :    U R                  XUS[        U5      US9$ )Nr   )_internal_blines)replacelinesr   )r%   r   a1a2bliness        r   replacelines_veclinelog.replacelines_vecA  s)      RCK& ! 
 	
r   c           	     @   U R                   (       a  U R                   nOU R                  U5      nU[        UR                  5      :  a$  [	        SU[        UR                  5      U4-  5      eU[        UR                  5      :X  a$  [        SS5      n[        SSUR
                  5      n	O(UR                  U   n	U R                  U	R                     nU R                  R                  n
U
" 5       nU R                  R                  n/ nUR                  nXE:  a  XU-
  S-   -   nU" [        X5      5        [        XE5       Hd  nUc,  U" [        UUU
" 5       5      5        U" [        UU5      5        M2  UU   u  nnU" [        UUU
" 5       5      5        U" [        UU5      5        Mf     X#:  a  U[        UR                  5      :  a$  [	        SU[        UR                  5      U4-  5      eU[        UR                  5      :X  a  UR
                  nOUR                  U   R                  nUS:  a.  XR                  :  a  UR                  US-
     R                  S-   nU" [        UU5      5        U
" 5       nU" U5        [!        U["        [
        45      (       d  U" [#        SU	R                  S-   5      5        [#        SU5      U R                  U	R                  '   [        U R                   R                  5      U:  a  UU R                   R                  U   l        O([!        U[
        5      (       d   eUU R                   l        XR                   R                  X#& [%        U R                   R&                  U5      U R                   l        XR                  :  a  Xl        gg)z+Replace lines [a1, a2) with lines [b1, b2).s-   %d contains %d lines, tried to access line %dr   r   N)r   annotater   r#   r	   r(   r   r   r   __len__r   r   r   r   r   rN   r   rh   maxr   )r%   r   r   r   b1b2r   ara1insta1info
programlen
oldproglen
appendinst
blineinfosbappendtgtr   newrev
newlinenumendaddra1instpcs                        r   r   linelog.replacelinesF  s	    ##Bs#BBHH@BHHr*+  3rxx=  !QZFaBGG,FXXb\F]]6>>2F]]**
\
]]))
 
##7 R!,Cs3}% =#+HS':<@AuS'23)9')B&FJHVZFGuVZ89 ) 7CM!"DCM2./  s288}$''((2,..Av#,
 ((26*22Q6tC)*<6 &5$-00uQ 234(-a(<fnn%
 t!!''(2-3;D$$R(0fd++++&.D#*4  '!$T%7%7%;%;S!AL r   c                L   Sn/ nSnUbi  U[        U R                  5      :  aP  U R                  U   nUnUR                  XUR                  5      nUS-  nUb  U[        U R                  5      :  a  MP  Ub  [	        SU R                  5       -   5      e[        XW5      nXpl        U$ )Nr   r   z4Probably hit an infinite loop in linelog. Program:\n)r   r   rG   r   r	   r   r   r   )r%   r   rE   r#   executedinstlastpcr   s           r   r   linelog.annotate  s     " nC,>!>==$DFcu||4BMH	 nC,>!>
 >G--/"  C/	r   c                    U R                   $ r!   )r   r$   s    r   r   linelog.maxrev  s    ||r   c                    U R                   R                   Vs/ s H  oR                  UR                  4PM     sn$ s  snf )z?Return the last annotation result. C linelog code exposed this.)r   r#   r   r   )r%   ls     r   r   linelog.annotateresult  s5     -1,>,>,D,DE,Dq		",DEEEs   !=c                H    U R                   R                  U   R                  $ r!   )r   r#   r   )r%   lines     r   	getoffsetlinelog.getoffset  s    !!''-555r   c                   U=(       d    Sn/ n[        [        U R                  5      5       H  nU R                  U   nUS-   n[        U[        5      (       a  UR
                  nO[        U[        5      (       a  Us  $ [        U[        [        45      (       a  OK[        U[        5      (       a(  UR                  UR                  UR                  45        O[        SU-  5      eXr:X  a  Us  $ UnM     [        S5      e)zGet all lines that ever occurred in [start, end).

Passing start == end == 0 means "all lines ever".

This works in terms of *internal* program offsets, not line numbers.
r   s   Illegal instruction %rs   Failed to perform getalllines)r   r   r   r   rh   rS   r(   r   rN   r   r   r   r   r	   )r%   startendrE   r#   stepr   nextpcs           r   getalllineslinelog.getalllines  s     Za #dmm,-D==$D!VF$&&D$''D3+..D%((dii)9)9:;"#<t#CDD}B .  ;<<r   )r   r   r   rr   r!   )r   r   )r   r   r   r   r   r0   r=   r8   r   classmethodr   r@   r   r   r   r   propertyr   r   r  r  r   r   r   r   r   r      s}    ;


 0 0<E"


Vp*   F F6=r   r   )r   
__future__r   rI   r   typingr   
thirdpartyr   TYPE_CHECKING r   Structr`   	Exceptionr	   sr   r   r*   rN   rh   r(   r   r   r   r   r   r   r   <module>r     s    # 
    
 == I9 I         " "J> 4N *> ,. 4N 6C*f= f=r   