
    q&f"                     z    d dl Z d dlZd dlZd dlZd dlmZ d dlmZ d dlm	Z
 ddlm	Z	 dZdZdZ G d d	      Zd
 Zy)    N)hex)open)error         c                       e Zd ZdZdZddZd Zed        ZddZ	d Z
d	 Zd
 Zd ZddZd Zd Zd Zed        Zd Zy)revmapz~trivial hg bin hash - linelog rev bidirectional map

    also stores a flag (uint8) for each revision, and track renames.
    s   REVMAP1 Nc                     || _         dg| _        dg| _        i | _        dg| _        dg| _        d| _        |rAt        j                   j                  |      r| j                          y| j                          yy)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__zrevmap.__init__:   sn     	 3 Eww~~d#

 

 r   c                     |j                   dd | _         |j                  dd | _        |j                  j                         | _        |j                  dd | _        |j
                  dd | _        d| _        y)z>copy the map data from another revmap. do not affect self.pathNr   )r   r   r   copyr   r   r   )r   rhss     r   copyfromzrevmap.copyfromQ   sa    Qq)))+??1-,,Q/r   c                 2    t        | j                        dz
  S )z"return max linelog revision numberr   )lenr   )r   s    r   maxrevzrevmap.maxrevZ   s     4==!A%%r   c                 X   || j                   v r!t        j                  dt        |      z        t	        |      t
        k7  rt        j                  dt
        z        t	        | j                        }d}|r	|t        z  }|Q|| j                  d   k7  r?|t        z  }| j                  j                  |       | j                  j                  |       | j                  j                  |       | j                  j                  |       || j                   |<   |r| j                          |S )z~add 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*   zrevmap.append_   s    $--***SX5  s8w**+g5  $-- N"D(9(9"(= =JD##C($$T*S!d# cJJL
r   c                 J    || j                   kD  s|dk  ry| j                  |   S )z>convert linelog revision to hg hash. return None if not found.r   N)r"   r   r   revs     r   rev2hshzrevmap.rev2hshz   s&    a}}S!!r   c                 J    || j                   kD  s|dk  ry| j                  |   S )zkget the flag (uint8) for a given linelog revision.
        return None if revision does not exist.
        r   N)r"   r   r0   s     r   rev2flagzrevmap.rev2flag   s(     a~~c""r   c                     || j                   kD  s|dk  ryt        j                  | j                  |      dz
  }| j                  |   S )zcget the path for a given linelog revision.
        return None if revision does not exist.
        r   Nr   )r"   bisectbisect_rightr   r   )r   r1   r-   s      r   rev2pathzrevmap.rev2path   sG     a!!$"2"2C81<  %%r   c                 8    | j                   j                  |      S )z>convert hg hash to linelog revision. return None if not found.)r   get)r   r+   s     r   hsh2revzrevmap.hsh2rev   s    }}  %%r   c                 v    dg| _         dg| _        i | _        dg| _        d| _        |r| j                          yy)z3make the map empty. if flush is True, write to diskNr   r   )r   r   r   	_rev2pathr   r   )r   r   s     r   clearzrevmap.clear   s?     JJL r   c                 8   | j                   sy| j                  dk(  rrt        | j                   d      5 }|j                  | j                         t        dt        | j                              D ]  }| j                  ||        	 ddd       nct        | j                   d      5 }t        | j                  dz   t        | j                              D ]  }| j                  ||        	 ddd       | j                  | _        y# 1 sw Y   xY w# 1 sw Y   'xY w)z write the state down to the fileNr   s   wbr   s   ab)
r   r   r   writeHEADERranger!   r   	_writerevr"   )r   fis      r   r   zrevmap.flush   s    yyr!dii' )1$q#dmm"45 )ANN1a()) )
 dii' )1t//!3S5GH )ANN1a())  ;;) )
) )s   AD%ADDDc                 R   | j                   syd}t        }t        | j                   d      5 }|j                  t	        | j
                              | j
                  k7  rt        j                         | j                  d       	 |j                  |      }|snt        |      }t	        | j                        }|t        z  rG| j                  |      }| j                  j                  |       | j                  j                  |       |j                  |      }t	        |      |k7  rt        j                         || j                   |<   | j"                  j                  |       | j                  j                  |       	 ddd       | j$                  | _        y# 1 sw Y   xY w)zload state from fileNr      rbF)r   )r   r%   r   readr!   rA   r   r$   r>   ordr   r)   	_readcstrr   r*   r   r   r   r"   r   )	r   flaglenhshlenrD   bufr.   r1   r   r+   s	            r   r   zrevmap._load   sU   yy $))U# 	*q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                 <   | j                   |   }| j                  |   }|j                  t        j                  d|             |t
        z  r?| j                  |      }|t        j                  d|z        |j                  |dz          |j                  |       y)zappend a revision data to file   BNs   cannot find path for %s    )	r   r   r@   structpackr)   r8   r   r$   )r   r1   rD   r.   r+   r   s         r   rC   zrevmap._writerev   s    ~~c"mmC 	D$'(*==%D|../IC/OPPGGD5L!	r   c                 r    d}	 | j                  d      }|st        j                         |dk(  r	 |S ||z  }5)z,read a C-language-like ' '-terminated stringr   r   rP   )rH   r   r$   )rD   rM   chs      r   rJ   zrevmap._readcstr   sJ     B..00U{
 2IC r   c                     t        |t              r|\  }}n |j                         |j                         }}| j	                  |      }|y||| j                  |      k7  ry| j                  |      t        z  dk(  S )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   r;   r8   r4   r(   )r   rD   r+   r   r1   s        r   __contains__zrevmap.__contains__   st    
 aIC!&&(Cll3;c(: :c"^399r   )N)FNF)F)__name__
__module____qualname____doc__rA   r   r   propertyr"   r*   r2   r4   r8   r;   r>   r   r   rC   staticmethodrJ   rY    r   r   r
   r
   2   sr    
 F. & &6"#&&	''<
 
 
:r   r
   c                 8   d}	 t        | d      5 }|j                  t         t        j                         |j                         t        t        j                        kD  r|j                  t              }ddd       |S # 1 sw Y   |S xY w# t        $ r Y |S w xY w)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.
    NrG   )r   seekr%   ioSEEK_ENDtellr!   r
   rA   rH   IOError)r   r+   rD   s      r   getlastnoderg      s     C$ 	&!FFG8R[[)vvx#fmm,,ffWo	& J	& J  Js/   B A%A?5B ?B	B 	B 	BB)r6   rc   r   rQ   mercurial.noder   mercurial.pycompatr   	mercurialr   r&    r(   r)   r%   r
   rg   r`   r   r   <module>rl      sJ     	 	   # ,  
 B: B:Jr   