
    6h"                    z    S SK Jr  S SKrS SKrS SKrS SKrS SKJr  S SKJ	r
  SSKJ	r	  SrSrSr " S S	5      rS
 rg)    )annotationsN)hex)error         c                      \ rS rSrSrSrSS jrS r\S 5       r	SS jr
S	 rS
 rS rS rSS jrS rS rS r\S 5       rS rSrg)revmap2   zvtrivial hg bin hash - linelog rev bidirectional map

also stores a flag (uint8) for each revision, and track renames.
s   REVMAP1 Nc                   Xl         S/U l        S/U l        0 U l        S/U l        S/U l        SU l        U(       aF  [        R                   R                  U5      (       a  U R                  5         gU R                  5         gg)a  create or load the revmap, optionally associate to a file

if path is None, the revmap is entirely in-memory. the caller is
responsible for locking. concurrent writes to a same file is unsafe.
the caller needs to make sure one file is associated to at most one
revmap object at a time.Nr       )path_rev2hsh	_rev2flag_hsh2rev_renamerevs_renamepaths_lastmaxrevosexists_loadflush)selfr   s     ;/usr/lib/python3/dist-packages/hgext/fastannotate/revmap.py__init__revmap.__init__:   so     	 3 Eww~~d##

 

 r   c                    UR                   SS U l         UR                  SS U l        UR                  R                  5       U l        UR                  SS U l        UR
                  SS U l        SU l        g)z>copy the map data from another revmap. do not affect self.pathNr   )r   r   r   copyr   r   r   )r   rhss     r   copyfromrevmap.copyfromQ   sa    Qq)))+??1-,,Q/r   c                2    [        U R                  5      S-
  $ )z"return max linelog revision numberr   )lenr   )r   s    r   maxrevrevmap.maxrevZ   s     4==!A%%r   c                t   XR                   ;   a"  [        R                  " S[        U5      -  5      e[	        U5      [
        :w  a  [        R                  " S[
        -  5      e[	        U R                  5      nSnU(       a	  U[        -  nUbQ  X0R                  S   :w  a?  U[        -  nU R                  R                  U5        U R                  R                  U5        U R                  R                  U5        U R                  R                  U5        XPR                   U'   U(       a  U R                  5         U$ )znadd a binary hg hash and return the mapped linelog revision.
if flush is True, incrementally update the file.
s   %r is in revmap alreadys   hsh must be %d-char longr   r   )r   r   CorruptedFileErrorr   r$   _hshlenhgerrorProgrammingErrorr   sidebranchflagr   
renameflagr   appendr   r   )r   hsh
sidebranchr   r   idxflags          r   r.   revmap.append_   s    --***SX5  s8w**+g5  $-- N"D(9(9"(= =JD##C($$T*S!d# cJJL
r   c                L    XR                   :  d  US:  a  gU R                  U   $ )z>convert linelog revision to hg hash. return None if not found.r   N)r%   r   r   revs     r   rev2hshrevmap.rev2hshz   s$    a}}S!!r   c                L    XR                   :  d  US:  a  gU R                  U   $ )z[get the flag (uint8) for a given linelog revision.
return None if revision does not exist.
r   N)r%   r   r5   s     r   rev2flagrevmap.rev2flag   s&     a~~c""r   c                    XR                   :  d  US:  a  g[        R                  " U R                  U5      S-
  nU R                  U   $ )zSget the path for a given linelog revision.
return None if revision does not exist.
r   Nr   )r%   bisectbisect_rightr   r   )r   r6   r1   s      r   rev2pathrevmap.rev2path   sE     a!!$"2"2C81<  %%r   c                8    U R                   R                  U5      $ )z>convert hg hash to linelog revision. return None if not found.)r   get)r   r/   s     r   hsh2revrevmap.hsh2rev   s    }}  %%r   c                    S/U l         S/U l        0 U l        S/U l        SU l        U(       a  U R                  5         gg)z3make the map empty. if flush is True, write to diskNr   r   )r   r   r   	_rev2pathr   r   )r   r   s     r   clearrevmap.clear   s?     JJL r   c                X   U R                   (       d  gU R                  S:X  ar  [        U R                   S5       nUR                  U R                  5        [        S[        U R                  5      5       H  nU R                  X!5        M     SSS5        Oc[        U R                   S5       n[        U R                  S-   [        U R                  5      5       H  nU R                  X!5        M     SSS5        U R                  U l        g! , (       d  f       N = f! , (       d  f       N1= f)z write the state down to the fileNr   wbr   ab)
r   r   openwriteHEADERranger$   r   	_writerevr%   )r   fis      r   r   revmap.flush   s    yyr!dii&!$q#dmm"45ANN1( 6 '&
 dii&!t//!3S5GHANN1( I '  ;; '&
 '&s   AD
+AD

D
D)c                |   U R                   (       d  gSn[        n[        U R                   S5       nUR                  [	        U R
                  5      5      U R
                  :w  a  [        R                  " 5       eU R                  SS9   UR                  U5      nU(       d  O[        U5      n[	        U R                  5      nU[        -  (       aG  U R                  U5      nU R                  R                  U5        U R                  R                  U5        UR                  U5      n[	        U5      U:w  a  [        R                  " 5       eX`R                   U'   U R"                  R                  U5        U R                  R                  U5        GM
  SSS5        U R$                  U l        g! , (       d  f       N = f)zload state from fileNr   rbF)r   )r   r)   rL   readr$   rN   r   r(   rG   ordr   r-   	_readcstrr   r.   r   r   r   r%   r   )	r   flaglenhshlenrQ   bufr2   r6   r   r/   s	            r   r   revmap._load   sG   yy $))T"avvc$++&'4;;6..00JJUJ#ffWo3x$--(*$>>!,D$$++C0%%,,T2ffVns8v%2244%(c"%%d+$$S) 	 #(  ;;) #"s   E"F--
F;c                L   U R                   U   nU R                  U   nUR                  [        R                  " SU5      5        U[
        -  (       aA  U R                  U5      nUc  [        R                  " SU-  5      eUR                  US-   5        UR                  U5        g)zappend a revision data to file   BNs   cannot find path for %s    )	r   r   rM   structpackr-   r?   r   r(   )r   r6   rQ   r2   r/   r   s         r   rP   revmap._writerev   s    ~~c"mmC 	D$'(*==%D|../IC/OPPGGD5L!	r   c                    Sn U R                  S5      nU(       d  [        R                  " 5       eUS:X  a   U$ X-  nM<  )z,read a C-language-like ' '-terminated stringr   r   r_   )rV   r   r(   )rQ   r[   chs      r   rX   revmap._readcstr   sH     B..00U{
 IC r   c                   [        U[        5      (       a  Uu  p#OUR                  5       UR                  5       p2U R	                  U5      nUc  gUb  X0R                  U5      :w  a  gU R                  U5      [        -  S:H  $ )z(fctx or (node, path)) -> bool.
test if (node, path) is in the map, and is not in a side branch.
f can be either a tuple of (node, path), or a fctx.
Fr   )
isinstancetuplenoder   rC   r?   r:   r,   )r   rQ   r/   r   r6   s        r   __contains__revmap.__contains__   ss    
 aIC!&&(ll3;c(: :c"^399r   )r   r   r   r   r   r   rF   r   )N)FNF)F)__name__
__module____qualname____firstlineno____doc__rN   r   r!   propertyr%   r.   r7   r:   r?   rC   rG   r   r   rP   staticmethodrX   rj   __static_attributes__ r   r   r
   r
   2   sr    
 F. & &6"#&&	''<
 
 
:r   r
   c                F   Sn [        U S5       nUR                  [        * [        R                  5        UR                  5       [        [        R                  5      :  a  UR                  [        5      nSSS5        U$ ! , (       d  f       U$ = f! [         a     U$ f = f)zreturn the last hash in a revmap, without loading its full content.
this is equivalent to `m = revmap(path); m.rev2hsh(m.maxrev)`, but faster.
NrU   )rL   seekr)   ioSEEK_ENDtellr$   r
   rN   rV   OSError)r   r/   rQ   s      r   getlastnoder{      s     C$FFG8R[[)vvx#fmm,,ffWo  J  J  Js/   B A&B 6B  
B
B B 
B B )
__future__r   r=   rw   r   r`   mercurial.noder   	mercurialr   r*    r,   r-   r)   r
   r{   rt   r   r   <module>r      sJ    #  	 	   ,  
 B: B:Jr   