
    q&f_;                     j   d Z ddlZddlZddlmZ ddlmZ  ej                  d      Z G d de	      Z
ej                   G d	 d
             Zej                   G d d             Z G d d      Z G d de      Z G d de      Z G d de      Z G d de      Z G d de      Zd Z G d d      Zy)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.
    N   )attr)pycompats   >IIc                       e Zd ZdZy)LineLogErrorz>Error raised when something bad happens internally in linelog.N)__name__
__module____qualname____doc__     3/usr/lib/python3/dist-packages/mercurial/linelog.pyr   r      s    Hr   r   c                   r    e Zd Z ej                         Z ej                         Z ej                         Zy)lineinfoN)r   r	   r
   r   ibrevlinenum_offsetr   r   r   r   r   #   s+     $'')CdggiGdggiGr   r   c                   |    e Zd Z ej                         Z ej                  e      Z ej                         Zd Z	y)annotateresulttypec                 ,    t        | j                        S N)iterlinesselfs    r   __iter__zannotateresult.__iter__3   s    DJJr   N)
r   r	   r
   r   r   r   	bytearrayr   _eofr   r   r   r   r   r   -   s1    
$'')CDGG#E4779D r   r   c                       e Zd Zej                  Zej                  d        Zej                  d        Zd Z	ej                  d        Z
ej                  d        Zej                  d        Zy)_llinstructionc                      y r   r   r   op1op2s      r   __init__z_llinstruction.__init__;       r   c                      y r   r   r   s    r   __str__z_llinstruction.__str__?   r)   r   c                     t        |       S r   )strr   s    r   __repr__z_llinstruction.__repr__C   s    4yr   c                      y r   r   r   others     r   __eq__z_llinstruction.__eq__F   r)   r   c                      y)z5Encode this instruction to the binary linelog format.Nr   r   s    r   encodez_llinstruction.encodeJ       r   c                      y)ag  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   executez_llinstruction.executeN   r5   r   N)r   r	   r
   abcABCMeta__metaclass__abstractmethodr(   r+   r.   r2   r4   r:   r   r   r   r#   r#   7   s    KKM  	  	  	D D 	 r   r#   c                   .    e Zd ZdZd Zd Zd Zd Zd Zy)_jgez@If the current rev is greater than or equal to op1, jump to op2.c                      || _         || _        y r   _cmprev_targetr%   s      r   r(   z_jge.__init__`       r   c                 8    d| j                   | j                  fz  S )Nz	JGE %d %drB   r   s    r   r+   z_jge.__str__d   s    dllDLL999r   c                     t        |       t        |      k(  xr4 | j                  |j                  k(  xr | j                  |j                  k(  S r   r   rC   rD   r0   s     r   r2   z_jge.__eq__g   A    J$u+% .-.-	
r   c                 \    t         j                  | j                  dz  | j                        S N   _llentrypackrC   rD   r   s    r   r4   z_jge.encoden   s     }}T\\Q.==r   c                 B    || j                   k\  r| j                  S |dz   S Nr   rB   r7   s       r   r:   z_jge.executeq   s!    $,,<<Avr   N	r   r	   r
   r   r(   r+   r2   r4   r:   r   r   r   r@   r@   ]   s    J:
>r   r@   c                   .    e Zd ZdZd Zd Zd Zd Zd Zy)_jumpz=Unconditional jumps are expressed as a JGE with op1 set to 0.c                 8    |dk7  rt        d|z        || _        y )Nr   s%   malformed JUMP, op1 must be 0, got %d)r   rD   r%   s      r   r(   z_jump.__init__z   s!    !8G#MNNr   c                      d| j                   z  S )NzJUMP %drD   r   s    r   r+   z_jump.__str__   s    DLL))r   c                 f    t        |       t        |      k(  xr | j                  |j                  k(  S r   )r   rD   r0   s     r   r2   z_jump.__eq__   s'    DzT%[(JT\\U]]-JJr   c                 B    t         j                  d| j                        S Nr   )rN   rO   rD   r   s    r   r4   z_jump.encode   s    }}Q--r   c                     | j                   S r   rW   r7   s       r   r:   z_jump.execute   s    ||r   NrR   r   r   r   rT   rT   w   s    G
*K.r   rT   c                   .    e Zd ZdZd Zd Zd Zd Zd Zy)r!   z1EOF is expressed as a JGE that always jumps to 0.c                 P    |dk7  rt        d|z        |dk7  rt        d|z        y )Nr   s$   malformed EOF, op1 must be 0, got %ds$   malformed EOF, op2 must be 0, got %d)r   r%   s      r   r(   z_eof.__init__   s7    !8FLMM!8FLMM r   c                      y)NEOFr   r   s    r   r+   z_eof.__str__   s    r   c                 0    t        |       t        |      k(  S r   r   r0   s     r   r2   z_eof.__eq__   s    DzT%[((r   c                 .    t         j                  dd      S rZ   )rN   rO   r   s    r   r4   z_eof.encode   s    }}Q""r   c                      y r   r   r7   s       r   r:   z_eof.execute   s    r   NrR   r   r   r   r!   r!      s    ;N)#r   r!   c                   .    e Zd ZdZd Zd Zd Zd Zd Zy)_jlz1If the current rev is less than op1, jump to op2.c                      || _         || _        y r   rB   r%   s      r   r(   z_jl.__init__   rE   r   c                 8    d| j                   | j                  fz  S )NzJL %d %drB   r   s    r   r+   z_jl.__str__   s    T\\4<<888r   c                     t        |       t        |      k(  xr4 | j                  |j                  k(  xr | j                  |j                  k(  S r   rH   r0   s     r   r2   z
_jl.__eq__   rI   r   c                 b    t         j                  d| j                  dz  z  | j                        S )Nr   rL   rM   r   s    r   r4   z
_jl.encode   s%    }}Q$,,!"34dllCCr   c                 B    || j                   k  r| j                  S |dz   S rQ   rB   r7   s       r   r:   z_jl.execute   s!    <<Avr   NrR   r   r   r   rd   rd      s    ;9
Dr   rd   c                   .    e Zd ZdZd Zd Zd Zd Zd Zy)_linezEmit a line.c                      || _         || _        y r   _rev_origlinenor%   s      r   r(   z_line.__init__   s    	r   c                 8    d| j                   | j                  fz  S )Nz
LINE %d %drm   r   s    r   r+   z_line.__str__   s    tyy$*:*:;;;r   c                     t        |       t        |      k(  xr4 | j                  |j                  k(  xr | j                  |j                  k(  S r   )r   rn   ro   r0   s     r   r2   z_line.__eq__   sE    J$u+% 6		UZZ'6  E$5$55	
r   c                 b    t         j                  d| j                  dz  z  | j                        S rK   )rN   rO   rn   ro   r   s    r   r4   z_line.encode   s&    }}Q$))q.143C3CDDr   c                 Z     |t        | j                  | j                  |             |dz   S rQ   )r   rn   ro   r7   s       r   r:   z_line.execute   s&    Xdii!1!1267Avr   NrR   r   r   r   rk   rk      s    <
Er   rk   c                 b   	 t         j                  | |      \  }}|dz  }|dz	  }|dk(  r.|dk(  r|dk(  rt        ||      S t        ||      S t        ||      S |dk(  rt        ||      S |dk(  rt        ||      S t        d|z        # t        j                  $ r}t	        d|z        d}~ww xY w)z?Decode a single linelog instruction from an offset in a buffer.s!   reading an instruction failed: %rN   rL   r   r   s   Unimplemented opcode %r)rN   unpack_fromstructerrorr   r!   rT   r@   rd   rk   NotImplementedError)dataoffsetr&   r'   eopcodes         r   
_decodeoner~      s    E''f5S 4ZF
(C{!8axC~%c?"C~	13}	1S#
86A
BB << E?!CDDEs   B B.B))B.c                       e Zd ZdZddZd Zd Zd Zed        Z	d Z
d	 Zd
 ZddZd Zed        Zed        Zd ZddZy)linelogz1Efficient cache for per-line history information.Nc                 b    |t        dd      t        dd      g}|| _        d | _        || _        y rZ   )r!   _program_lastannotate_maxrev)r   programmaxrevs      r   r(   zlinelog.__init__   s5    ? Aqz41:.G!r   c                     t        |       t        |      k(  xr4 | j                  |j                  k(  xr | j                  |j                  k(  S r   )r   r   r   r0   s     r   r2   zlinelog.__eq__   sA    J$u+% ./.-	
r   c                 p    dt        t        |             | j                  t        | j                        fz  S )Nz"<linelog at %s: maxrev=%d size=%d>)hexidr   lenr   r   s    r   r.   zlinelog.__repr__   s2    34MLL7
 
 	
r   c                     dt        t        t        | j                                    z  t        j                  d      j                  fdt        | j                  dd  d      D              S )Nz	%%%dd %%s   
c              3   2   K   | ]  \  }}||fz    y wr   r   ).0idxifmts      r   	<genexpr>z#linelog.debugstr.<locals>.<genexpr>	  s"      +
%sAC3(N+
s   r   )r   r-   r   r   sysstrjoin	enumerate)r   r   s    @r   debugstrzlinelog.debugstr  s[    CC$6 788u%** +
)24==3Da)H+
 
 	
r   c                    t        |      t        j                  z  dk7  r't        dt        |      t        j                  fz        t        |      t        j                  z  }t	        |d      }t        |t              rd}nDt        |t        t        f      r|j                  }n!t        dt        |      j                  z        t        |t        t        t        f      sJ |j                  }||k7  rt        d||fz        t        dd      g}t        d|      D ].  }|j                  t	        ||t        j                  z               0  | ||      S )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   rN   sizer   r~   
isinstancerT   r@   rd   rC   r   r   rD   r!   rangeappend)clsbufexpectedfakejger   
numentriesinstructionsr{   s           r   fromdatazlinelog.fromdata  s?   s8hmm#q(Ls8X]]+,  s8hmm+S!$gu%F$-__F>w-(()  'E4#5666__
z!=Z() 
 Q
|Az* 	IF
30F GH	I<//r   c                     t        | j                  t        | j                              j	                         }|dj                  d | j                  dd  D              z   S )Nr   c              3   <   K   | ]  }|j                           y wr   )r4   )r   r   s     r   r   z!linelog.encode.<locals>.<genexpr>.  s     DQahhjDs   r   )r@   r   r   r   r4   r   )r   hdrs     r   r4   zlinelog.encode,  sI    4<<T]]!34;;=SXXD$--2CDDDDr   c                 .    g | _         d| _        d | _        y rZ   )r   r   r   r   s    r   clearzlinelog.clear0  s    !r   c           	      B    | j                  |||dt        |      |      S )Nr   )_internal_blines)replacelinesr   )r   r   a1a2bliness        r   replacelines_veczlinelog.replacelines_vec5  s+      RCK& ! 
 	
r   c           	         | j                   r| j                   }n| j                  |      }|t        |j                        kD  r$t	        d|t        |j                        |fz        |t        |j                        k(  r$t        dd      }t        dd|j
                        }	n(|j                  |   }	| j                  |	j                     }| j                  j                  }
 |
       }| j                  j                  }g }|j                  }||k  r|||z
  dz   z   } |t        ||             t        ||      D ]a  }|+ |t        || |
                     |t        ||             0||   \  }} |t        || |
                     |t        ||             c ||k  r|t        |j                        kD  r$t	        d|t        |j                        |fz        |t        |j                        k(  r|j
                  }n|j                  |   j                  }|dkD  r.|| j                  k  r|j                  |dz
     j                  dz   } |t        ||              |
       } ||       t!        |t"        t
        f      s |t#        d|	j                  dz                t#        d|      | j                  |	j                  <   t        | j                   j                        |kD  r|| j                   j                  |   _        n#t!        |t
              sJ || j                   _        || j                   j                  || t%        | j                   j&                  |      | j                   _        || j                  kD  r|| _        yy)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   rd   r   rk   r   r@   r   rT   maxr   )r   r   r   r   b1b2r   ara1insta1info
programlen
oldproglen
appendinst
blineinfosbappendtgtr   newrev
newlinenumendaddra1instpcs                        r   r   zlinelog.replacelines:  s    ##Bs#BBHH@BHHr*+  3rxx=  !QZFaBGG,FXXb\F]]6>>2F]]**
\
]]))
 
##7 R!,Cs3}% R= :#+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uQ 234(-a(<fnn%
 t!!''(2-3;D$$R(0fd+++&.D#*4  B'!$T%7%7%;%;S!ADL r   c                 F   d}g }d}|f|t        | j                        k  rN| j                  |   }|}|j                  |||j                        }|dz  }||t        | j                        k  rN|t	        d| j                         z         t        ||      }|| _        |S )Nr   r   z4Probably hit an infinite loop in linelog. Program:\n)r   r   r:   r   r   r   r   r   )r   r   r8   r   executedinstlastpcr   s           r   r   zlinelog.annotate  s     nC,>!>==$DFc2u||4BMH	 nC,>!>
 >G--/"  C/	r   c                     | j                   S r   )r   r   s    r   r   zlinelog.maxrev  s    ||r   c                     | j                   j                  D cg c]  }|j                  |j                  f c}S c c}w )z?Return the last annotation result. C linelog code exposed this.)r   r   r   r   )r   ls     r   r   zlinelog.annotateresult  s1     -1,>,>,D,DEq		"EEEs   ;c                 H    | j                   j                  |   j                  S r   )r   r   r   )r   lines     r   	getoffsetzlinelog.getoffset  s    !!''-555r   c                    |xs d}g }t        t        | j                              D ]  }| j                  |   }|dz   }t        |t              r|j
                  }nqt        |t              r|c S t        |t        t        f      rnFt        |t              r(|j                  |j                  |j                  f       nt        d|z        ||k(  r|c S |} t        d      )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   rT   rD   r!   rd   r@   rk   r   rn   ro   r   )r   startendr8   r   stepr   nextpcs           r   getalllineszlinelog.getalllines  s     Za #dmm,- 	D==$D!VF$&D$'D3+.D%(dii)9)9:;"#<t#CDD}B	  ;<<r   rZ   r   )r   r   )r   r	   r
   r   r(   r2   r.   r   classmethodr   r4   r   r   r   r   propertyr   r   r   r   r   r   r   r   r      s}    ;


 0 0<E"


Vp*   F F6=r   r   )r   r;   rw   
thirdpartyr    r   StructrN   	Exceptionr   sr   r   r#   r@   rT   r!   rd   rk   r~   r   r   r   r   <module>r      s       6== I9 I         # #L> 4N *> ,. 4N 6C*f= f=r   