
    	^c"                         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dS )    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dS )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| _        |rKt          j                             |          r| 	                                 dS | 
                                 dS dS )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__:   s     	 3 E 	w~~d## 

 

	 	r   c                     |j         dd         | _         |j        dd         | _        |j                                        | _        |j        dd         | _        |j        dd         | _        d| _        dS )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   so    QQQqqq)))++?111-,QQQ/r   c                 0    t          | j                  dz
  S )z"return max linelog revision numberr   )lenr   )r   s    r   maxrevzrevmap.maxrevZ   s     4=!!A%%r   Fc                 V   || j         v r$t          j        dt          |          z            t	          |          t
          k    rt          j        dt
          z            t	          | j                  }d}|r
|t          z  }|O|| j
        d         k    r>|t          z  }| j                            |           | j
                            |           | j                            |           | j                            |           || j         |<   |r|                                  |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   Nr   )r   r   CorruptedFileErrorr   r!   _hshlenhgerrorProgrammingErrorr   sidebranchflagr   
renameflagr   appendr   r   )r   hsh
sidebranchr   r   idxflags          r   r*   zrevmap.append_   s.    $-**SXX5   s88w*+g5   $-   	#N"D(9"(= = =JD##C((($$T***S!!!d### c 	JJLLL
r   c                 B    || j         k    s|dk     rdS | 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4}S!!r   c                 B    || j         k    s|dk     rdS | 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4~c""r   c                 |    || j         k    s|dk     rdS 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   sF     a4!$"2C881< %%r   c                 6    | 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                     dg| _         dg| _        i | _        dg| _        d| _        |r|                                  dS dS )z3make the map empty. if flush is True, write to diskNr   r   )r   r   r   	_rev2pathr   r   )r   r   s     r   clearzrevmap.clear   sQ      	JJLLLLL	 	r   c                 0   | j         sdS | j        dk    rt          | j         d          5 }|                    | j                   t          dt          | j                            D ]}|                     ||           	 ddd           n# 1 swxY w Y   nqt          | j         d          5 }t          | j        dz   t          | j                            D ]}|                     ||           	 ddd           n# 1 swxY w Y   | j	        | _        dS )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   y 	Fr!!di'' )1$$$q#dm"4"455 ) )ANN1a(((()) ) ) ) ) ) ) ) ) ) ) ) ) ) )
 di'' )1t/!3S5G5GHH ) )ANN1a(((()) ) ) ) ) ) ) ) ) ) ) ) ) ) )  ;s%   ABBB.AC??DDc                 d   | j         sdS d}t          }t          | j         d          5 }|                    t	          | j                            | j        k    rt          j                    |                     d           	 |                    |          }|snt          |          }t	          | j
                  }|t          z  rI|                     |          }| j                            |           | j                            |           |                    |          }t	          |          |k    rt          j                    || j        |<   | j                            |           | j
                            |           		 ddd           n# 1 swxY w Y   | j        | _        dS )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   s   y 	F $)U## 	*qvvc$+&&''4;66.000JJUJ###*ffWoo 3xx$-((*$ 3>>!,,D$++C000%,,T222ffVnns88v%%2444%(c"%%d+++$$S)))* 	* 	* 	* 	* 	* 	* 	* 	* 	* 	* 	* 	* 	* 	* 	*(  ;s   E$FF Fc                 T   | j         |         }| j        |         }|                    t          j        d|                     |t
          z  rF|                     |          }|t          j        d|z            |                    |dz              |                    |           dS )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"mC 	D$''(((* 	"==%%D|./IC/OPPPGGD5L!!!	r   c                 z    d}	 |                      d          }|st          j                    |dk    rn||z  }7|S )z,read a C-language-like ' '-terminated stringr   Tr   rP   )rH   r   r$   )rD   rM   chs      r   rJ   zrevmap._readcstr   sU     	B 1.000U{{2IC	 
r   c                 6   t          |t                    r|\  }}n(|                                |                                }}|                     |          }|dS |||                     |          k    rdS |                     |          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.
        NFr   )
isinstancetuplenoder   r;   r8   r4   r(   )r   rD   r+   r   r1   s        r   __contains__zrevmap.__contains__   s    
 a 	+IC!&&((Cll3;5c(:(: : :5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   s        
 F   .   & & X&   6" " "# # #& & && & &	 	 	 	' ' '' ' '<
 
 
 
 
 \
: : : : :r   r
   c                 \   d}	 t          | d          5 }|                    t           t          j                   |                                t          t          j                  k    r|	                    t                    }ddd           n# 1 swxY w Y   n# t          $ r Y nw xY w|S )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[)))vvxx#fm,,,,ffWoo	& 	& 	& 	& 	& 	& 	& 	& 	& 	& 	& 	& 	& 	& 	&    Js5   B A0BB 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      s     				 				        # # # # # #           ,  
 B: B: B: B: B: B: B: B:J    r   