
    q&f                        d dl Z ddlmZmZmZ d dlmZmZmZm	Z	m
Z
mZmZmZmZ ddlmZmZmZmZmZ ddlmZmZ erddlmZ egsJ ej2                  Ze j4                  Ze j6                  Ze j8                  Z G d d	      Zd
 Zd Z G d d      Z  G d de       Z!dZ"de"z   Z#de"z   Z$dZ% ee%      Z&de&z  Z'dZ(dZ)dZ* G d d      Z+ G d d      Z,y)    N   )binhexnullrev)	CallableDictIterableListOptionalSetTYPE_CHECKINGTupleUnion)encodingerrorobsoletescmutilutil)repoviewutil
stringutil)	localrepoc                   4    e Zd ZdZd Zd Zd Zd Zd Zd Z	y)	BranchMapCachez8mapping of filtered views of repo with their branchcachec                     i | _         y N)_per_filterselfs    5/usr/lib/python3/dist-packages/mercurial/branchmap.py__init__zBranchMapCache.__init__9   s
        c                 V    | j                  |       | j                  |j                     S r   )updatecacher   
filternamer   repos     r   __getitem__zBranchMapCache.__getitem__<   s%    00r!   c                    |j                   }|j                  }| j                  j                  |      j	                  |      st
        j                  |      g }t        j                  |      }|b|j                  |      }| |   j                         |j                   j                  |j                  z
  }|j                  fd|D               nt        |      |j                  |j                  j                  dz                |rj                  ||       j	                  |      sJ |       | j                  |j                  <   y)z<Update the cache for the given filtered view on a repositoryNc              3   B   K   | ]  }|j                   k  s|  y wr   )tiprev).0rbcaches     r   	<genexpr>z-BranchMapCache.updatecache.<locals>.<genexpr>W   s     G!A4FAGs   r   )start)	changelogr$   r   getvalidforbranchcachefromfilesubsettablefilteredcopyfilteredrevsextendrevsr*   update)	r   r&   clr$   r:   
subsetnamesubset	extrarevsr-   s	           @r   r#   zBranchMapCache.updatecache@   s!    ^^__
!!%%j1>!6 ))$/F> %4J%z2f**,",,99BOOK	GyGG %T*BGG&--!"3G45MM$%t$0j0$,2)r!   c                    |j                   }|j                  |j                  }g }t               }|j	                         D ]5  }||z  }|D ])  } |      }	 ||	      \  }
}|s|j                  |       + 7 |rt        fd|D              }t        ||||   j                         ||      }dD ]F  }|j                  |      }|j                  |      s&|| j                  |<   |j                  |        y yy)zReplace the branchmap cache for a repo with a branch mapping.

        This is likely only called during clone with a branch map from a
        remote.

        c              3   @   K   | ]  }t         |              y wr   )int)r+   nodeclrevs     r   r.   z)BranchMapCache.replace.<locals>.<genexpr>x   s     @3uT{+@   )closednodes)s   bases	   immutables   servedN)r0   rev
branchinfosetvaluesaddmaxr3   rC   r6   r2   r   write)r   r&   remotebranchmapr<   clbranchinforbheadsclosedbheadshr,   bcrtiprevcache	candidaterviewrD   s                   @r   replacezBranchMapCache.replacec   s    ^^}}%,,. 	"FvG "!H#A1JJqM	"	" @@AGW""$"E @ 	i0>>%(27D$$Y/KK& r!   c                 8    | j                   j                          y r   )r   clearr   s    r   r\   zBranchMapCache.clear   s     r!   c                     |j                         }| j                  j                         D ]4  \  }}|j                  s|j	                  |      }|j                  |       6 y r   )
unfilteredr   items_delayedr6   rM   )r   r&   unfir$   rW   s        r   write_delayedzBranchMapCache.write_delayed   sR     !%!1!1!7!7!9 	"J~~}}Z0D!	"r!   N)
__name__
__module____qualname____doc__r    r'   r#   rZ   r\   rb    r!   r   r   r   6   s%    B1!3F%N!"r!   r   c                 :    t        d| j                         z        )zEraises ValueError when branchcache found a node which does not existsznode %s does not exist)
ValueErrorr   )rC   s    r   _unknownnoderj      s    
-
:
;;r!   c                 :    | j                   d| j                   z  S y)Ns   branch cache (%s)s   branch cacher$   )r&   s    r   _branchcachedescrm      s    "#doo55r!   c                   D   e Zd ZdZddedddfdddeeeee   f   e	e
eee   f      f   dee   dee   d	ee   d
eee      deeegef      ddfdZd Zd Zd Zd Zd Zd Zd Zd ZeZd Zed        Zd Zed        Zd Z d Z!d Z"d Z#d$dZ$d Z%d  Z&d! Z'd" Z(d# Z)y)%r3   a<  A dict like object that hold branches heads cache.

    This cache is used to avoid costly computations to determine all the
    branch heads of a repo.

    The cache is serialized on disk in the following format:

    <tip hex node> <tip rev number> [optional filtered repo hex hash]
    <branch head hex node> <open/closed state> <branch name>
    <branch head hex node> <open/closed state> <branch name>
    ...

    The first line is used to check if the cache is still valid. If the
    branch cache is for a filtered repo view, an optional third hash is
    included that hashes the hashes of all filtered and obsolete revisions.

    The open/closed state is represented by a single letter 'o' or 'c'.
    This field can be used to avoid changelog reads when determining if a
    branch head closes a branch or not.
    rg   Nr&   zlocalrepo.localrepositoryentriestipnoder*   filteredhashrF   hasnodereturnc                 (   || _         d| _        ||j                  | _        n|| _        || _        || _        |t               | _        n|| _        t        |      | _	        d| _
        t               | _        || _        | j                  	d | _        yy)zhasnode is a function which can be used to verify whether changelog
        has a given node or not. If it's not provided, we assume that every node
        we have exists in changelogFNc                      y)NTrg   )xs    r   <lambda>z&branchcache.__init__.<locals>.<lambda>   s    r!   )_repor`   nullidrp   r*   rq   rI   _closednodesdict_entries_closedverified_verifiedbranches_hasnode)r   r&   ro   rp   r*   rq   rF   rr   s           r   r    zbranchcache.__init__   s     
?;;DL"DL(  #D +DW$!$== *DM !r!   c                     | j                   ry| j                  D ]  }| j                  |      rt        |       ! d| _         y)zverify the closed nodes we haveNT)r}   rz   r   rj   )r   rC   s     r   _verifyclosedzbranchcache._verifyclosed   sA    %% 	#D==&T"	#  $r!   c                     || j                   vs|| j                  v ry| j                   |   D ]  }| j                  |      rt        |       ! | j                  j	                  |       y)z'verify head nodes for the given branch.N)r|   r~   r   rj   rK   )r   branchns      r   _verifybranchzbranchcache._verifybranch   s_    &&D4J4J*Jv& 	 A==#Q	  	""6*r!   c                     t        | j                  j                               | j                  z
  }|D ]  }| j	                  |        y)z"verifies nodes of all the branchesN)rI   r|   keysr~   r   )r   needverificationrT   s      r   
_verifyallzbranchcache._verifyall   sB    t}}1134t7M7MM! 	"Aq!	"r!   c                 ,    t        | j                        S r   )iterr|   r   s    r   __iter__zbranchcache.__iter__   s    DMM""r!   c                 "    || j                   |<   y r   )r|   )r   keyvalues      r   __setitem__zbranchcache.__setitem__   s    "cr!   c                 B    | j                  |       | j                  |   S r   r   r|   r   r   s     r   r'   zbranchcache.__getitem__   s    3}}S!!r!   c                 @    | j                  |       || j                  v S r   r   r   s     r   __contains__zbranchcache.__contains__  s    3dmm##r!   c              #   ~   K   | j                   j                         D ]  \  }}| j                  |       ||f  y wr   )r|   r_   r   )r   kvs      r   	iteritemszbranchcache.iteritems  s=     MM'') 	DAqq!Q$J	s   ;=c                 @    | j                  |       || j                  v S )z2checks whether a branch of this name exists or notr   )r   labels     r   	hasbranchzbranchcache.hasbranch  s    5!%%r!   c                 B   d }	 |j                  | j                  |            }t        |      }t        |      j	                  d      j                  dd      }|d d \  }}t        |      t        |      }}d }|j                  j                  }t        |      dkD  rt        |d         } | |||||      }	|	j                  |      st        d      |	j                  ||       |r|j!                          |	S # t        t        f$ r Y |r|j!                          y y t"        $ r`}
|j$                  j&                  r>d}|j$                  j)                  |t+        |      t-        j.                  |
      fz         d }	Y d }
~
d }
~
ww xY w# |r|j!                          w w xY w)N   
       )rp   r*   rq   rr   ztip differss   invalid %s: %s
)cachevfs	_filenamer   nextrstripsplitr   rB   r0   rr   lenr2   ri   loadIOErrorOSErrorclose	Exceptionui	debugflagdebugrm   r   forcebytestr)clsr&   flineitercachekeylastlrevrq   rr   r-   instmsgs               r   r4   zbranchcache.fromfile  s   &	cmmD12AAwHH~,,U399$BH!"1JD$TCI$DLnn,,G8}q "8A;/)F ??4( //KKh'" 	' ! 	 	   
	ww  )(."//5 F
	 	 s7   C"C: :F	F	 F&AF<F	 FF	 	Fc                 x   |D ]  }|j                  d      }|s|j                  dd      \  }}}|dvrt        d      t        j                  |j                               }t        |      }| j                  j                  |g       j                  |       |dk(  s| j                  j                  |        y)z[fully loads the branchcache by reading from the file using the line
        iterator passedr   r   r   s   oczinvalid branch state   cN)r   r   ri   r   tolocalstripr   r|   
setdefaultappendrz   rK   )r   r&   r   linerC   stater   s          r   r   zbranchcache.load?  s      	,D;;u%D!%D!!4D%E! !788$$U[[]3Et9DMM$$UB/66t<}!!%%d+	,r!   c                 D    d}| j                   rd|| j                   fz  }|S )z7name of a branchcache file for a given repo or repoviews   branch2s   %s-%srl   )r&   filenames     r   r   zbranchcache._filenameO  s(     ??8T__"==Hr!   c                     	 |j                   j                  | j                        }| j                  |k7  ryt        j                  || j                  d      }| j                  |k(  S # t        $ r Y yw xY w)zcheck that cache contents are valid for (a subset of) this repo

        - False when the order of changesets changed or if we detect a strip.
        - True when cache is up-to-date for the current repo or its subset.FTneedobsolete)r0   rC   r*   
IndexErrorrp   r   rq   )r   r&   rC   tiphashs       r   r2   zbranchcache.validforW  ss    
	>>&&t{{3D
 <<4 &&tT[[tL   G++  	 	s   %A( (	A43A4c                 f    |d   }d}t        |      D ]  }|| j                  vs|}d} ||fS  ||fS )zhReturn tuple with last open head in heads and false,
        otherwise return last closed head and true.TF)reversedrz   )r   headstiprQ   rS   s        r   
_branchtipzbranchcache._branchtipl  sV     Bi% 	A)))F{	
 F{r!   c                 0    | j                  | |         d   S )zReturn the tipmost open head on branch head, otherwise return the
        tipmost closed head on branch.
        Raise KeyError for unknown branch.r   )r   )r   r   s     r   	branchtipzbranchcache.branchtipx  s     tF|,Q//r!   c                       fd|D        S )Nc              3   @   K   | ]  }|j                   vs|  y wr   )rz   )r+   r   r   s     r   r.   z'branchcache.iteropen.<locals>.<genexpr>  s     ?aAT->->$>?s   rg   )r   nodess   ` r   iteropenzbranchcache.iteropen~  s    ?5??r!   c                 ~    | j                  |       | j                  |   }|st        | j                  |            }|S r   )r   r|   listr   )r   r   rQ   r   s       r   branchheadszbranchcache.branchheads  s9    6"f%u-.Er!   c              #   l   K   | j                         D ]  \  }}||f| j                  |      z     y wr   )r_   r   )r   bnr   s      r   iterbrancheszbranchcache.iterbranches  s7      	7IBu+ 666	7s   24c                 V    | j                          | j                  j                         S )zreturns all the heads)r   r|   rJ   r   s    r   	iterheadszbranchcache.iterheads  s    }}##%%r!   c                      t        |       | j                  | j                  | j                  | j                  | j
                  | j                        S )z-return an deep copy of the branchcache object)typerx   r|   rp   r*   rq   rz   r   s    r   r7   zbranchcache.copy  sC    tDzJJMMLLKK
 	
r!   c           	         |j                         }t        |dd      sd| _        y 	 | j                  |      }|j	                  |dd      5 }t        | j                        d| j                  z  g}| j                  $|j                  t        | j                               |j                  dj                  |      dz          d}t        | j                  j                               D ]Y  \  }}t        j                   |      }|D ]:  }	|d	z  }|	| j"                  v rd
}
nd}
|j                  dt        |	      |
|fz         < [ 	 d d d        |j$                  j'                  ddt)        |      t+        | j                               d| _        y # 1 sw Y   LxY w# t,        t.        t0        j2                  f$ r;}|j$                  j5                  dt7        j8                  |      z         Y d }~y d }~ww xY w)N	finalizedT   w)
atomictemps   %dr   r   r   r   r      os	   %s %s %s
   branchcaches%   wrote %s with %d labels and %d nodes
Fs    couldn't write branch cache: %s
)currenttransactiongetattrr`   r   r   r   rp   r*   rq   r   rM   joinsortedr|   r_   r   	fromlocalrz   r   logrm   r   r   r   r   Abortr   r   r   )r   r&   trr   r   r   	nodecountr   r   rC   r   r   s               r   rM   zbranchcache.write  s   $$&r;- !DM	~~d+Hx$? K1-ut{{/BC$$0OOC(9(9$:;		(+e34	$*4==+>+>+@$A KLE5$..u5E % K!Q	4#4#44$(E$(ETE50I IJKKK GGKK9 &DMM" "DM-K K. %++. 	GGMM4))$/0 	s1   %F C9FA
F FF G6;1G11G6c                 j   t        j                         }|j                  i }|j                         j                  }|D ][  } ||      \  }}|j                  |g       j                  |       |s2| j                  j                  j                  |             ] | j                  }	d}
|j                         j                  j                  }t        j                  |d      }|j                         D ]  \  }}| j                   j#                  |g       }|D ch c]  }j%                  |       }}t'               }t)        |      D ]  }||v r	|s|j                  |        ||      D cg c]  }|t*        k7  s| }}t'               }t'               }t'               }|D ]M  }||v r|j                  |       ||v s ||      d   |k(  r|j                  |       =|j                  |       O t-        |      t-        |      cxk(  rdk(  s$n |j/                  |       |j/                  |       |j1                  |       |j                  |        |rY|
t'        j3                               }
||
z
  r9t5        |      }|t7        |      k  r t'        j9                  ||            }||z  }|r+t)        |      D cg c]  }j                  |       c}| |<   t7        |      }||	kD  s|}	 |	| j                  kD  r|	| _
        j                  |	      | _        | j=                  |      su|j>                  | _        t*        | _
        | jA                         D ]F  }|st7        fd|D              }|| j                  kD  s*j                  |      | _        || _
        H tC        jD                  || j                  d      | _"        t        j                         |z
  }|jF                  jI                  dd	tK        |      |       | jM                  |       yc c}w c c}w c c}w )
zGiven a branchhead cache, self, that may have extra nodes or be
        missing heads, and a generator of nodes that are strictly a superset of
        heads missing, this function updates self to be correct.
        Ns   obsoleter   r   c              3   @   K   | ]  }j                  |        y wr   )rG   )r+   rC   r<   s     r   r.   z%branchcache.update.<locals>.<genexpr>H  s     <dRVVD\<rE   Tr   r   s   updated %s in %.4f seconds
)'r   timerr0   revbranchcacherH   r   r   rz   rK   rC   r*   r^   
parentrevsr   getrevsr_   r|   r1   rG   rI   r   r   r   r;   difference_updateheadrevsminrL   	ancestorsrp   r2   ry   r   r   rq   r   r   rm   rM   )r   r&   revgen	starttimenewbranchesgetbranchinfor,   r   closesbranchntiprev	topoheadsr   obsrevsnewheadrevsrR   rC   bheadset	uncertainnewrevpparents
samebranchotherbranch
obsparentsfloorrevr   rG   r*   r   durationr<   s                                 @r   r;   zbranchcache.update  s   
 JJL	^^++-88 	2A#0#3 FL""62.55a8!!%%bggaj1		2 ++ 	 __&00;;
 ""45#.#4#4#6 S	!FKH ]]&&vr2F178t8H8I - %W$ LL(&0&8IAL1II U
!e U
  +AG| #q)h-*:1*=*G"q)#*+ HZ=A=$$[1$$Z0**:6V$=%@ $ #BKKM 2Ii'"8}H3y>1$'Y(I$J	 I-8>x8HIIV%F gS	!j T[[ !DK777+DL}}T"  ;;DL!DK) 	) <e<<DKK'#%776?DL"(DK	) $00$++D
 ::<)++T"		
 	

4a 9 J>  Js   P&3P+P+P0)F)*rc   rd   re   rf   r   r   r   bytesr
   r	   r   r   rB   r   r   boolr    r   r   r   r   r   r'   r   r   r_   r   classmethodr4   r   staticmethodr   r2   r   r   r   r   r   r   r7   rM   r;   rg   r!   r   r3   r3      sY   4 #' '(,,059%+)%+ U#$huUDK5G/H&II
%+ %%+ %+ uo%+ c%j)%+ (E7D=12%+ 
%+N$+"##"$
 E&
 * *X,   ,*
0@7&
	
&PTr!   r3   c                       e Zd ZdZd Zy)remotebranchcachez@Branchmap info for a remote connection, should not write locallyc                      y r   rg   r%   s     r   rM   zremotebranchcache.write^  s    r!   N)rc   rd   re   rf   rM   rg   r!   r   r  r  [  s
    Jr!   r  s   -v1s	   rbc-namess   rbc-revss   >4sI@      il        c                   @    e Zd ZdZd Zd Zd Zd Zd Zd Z	d Z
d	 Zy
)rbcrevszLa byte string consisting of an immutable prefix followed by a mutable suffixc                 0    || _         t               | _        y r   )_prefix	bytearray_rest)r   r:   s     r   r    zrbcrevs.__init__s  s    [
r!   c                 X    t        | j                        t        | j                        z   S r   )r   r  r  r   s    r   __len__zrbcrevs.__len__w  s    4<< 3tzz?22r!   c                    |t        | j                        k  r.t        t        t	        j
                  | j                        |      S t        t        t	        j
                  | j                        |t        | j                        z
        S r   )r   r  unpack_from
_rbcrecfmtr   bufferr  )r   	rbcrevidxs     r   unpack_recordzrbcrevs.unpack_recordz  s`    s4<<((z4;;t||+DiPPDJJ'C-- r!   c                     t        | j                        dkD  rKt               }| j                  |d d  |j                  | j                         || _        t               | _        y y )Nr   r   r  r  r9   r  )r   entiretys     r   make_mutablezrbcrevs.make_mutable  sL    t||q  {H,,HQKOODJJ'!DJ$;DL !r!   c                 B    | j                          | j                  |d = y r   )r#  r  )r   poss     r   truncatezrbcrevs.truncate  s    JJstr!   c                 >   |t        | j                        k  r| j                          | j                  }|t        | j                        z
  }|t        z   }t        | j                        |k  r| j                  j                  d|z         t        t        ||||      S )N    )r   r  r#  r  _rbcrecsizer9   	pack_intor  )r   r  rC   	branchidxbufstart_offset
end_offsets          r   r*  zrbcrevs.pack_into  s    s4<<((jj 3t||#44!K/
tzz?Z' JJej01
 	
r!   c                 8    | j                   j                  |      S r   )r  r9   )r   	extensions     r   r9   zrbcrevs.extend  s    zz  ++r!   c                 x   |t        | j                        k  rht               }| j                  || |d d  |j                  | j                  |t        | j                        z
  |t        | j                        z
          |S | j                  |t        | j                        z
  |t        | j                        z
   S r   r!  )r   beginendaccs       r   slicezrbcrevs.slice  s    3t||$$+C\\%,CFJJ

53t||#44sS=N7NO Jzz%#dll"33cC<M6MNNr!   N)rc   rd   re   rf   r    r  r  r#  r&  r*  r9   r5  rg   r!   r   r  r  p  s.    V!3'
,,Or!   r  c                   n    e Zd ZdZddZd Zej                  d        Zd Z	d Z
d Zd Zdd
Zd Zd Zy	)r   a  Persistent cache, mapping from revision number to branch name and close.
    This is a low level cache, independent of filtering.

    Branch names are stored in rbc-names in internal encoding separated by 0.
    rbc-names is append-only, and each branch name is only stored once and will
    thus have a unique index.

    The branch info for each revision is stored in rbc-revs as constant size
    records. The whole file is read into memory, but it is only 'parsed' on
    demand. The file is usually append-only but will be truncated if repo
    modification is detected.
    The record for each revision contains the first 4 bytes of the
    corresponding node hash, and the record is only used if it still matches.
    Even a completely trashed rbc-revs fill thus still give the right result
    while converging towards full recovery ... assuming no incorrectly matching
    node hashes.
    The record also contains 4 bytes where 31 bits contains the index of the
    branch and the last bit indicate that it is a branch close commit.
    The usage pattern for rbc-revs is thus somewhat similar to 00changelog.i
    and will grow with it but be 1/8th of its size.
    c                 t   |j                   J || _        g | _        t        t	                     | _        d| _        	 |j                  j                  t              }t        |      | _        |r7|j                  d      D cg c]  }t        j                  |       c}| _        | j                  r	 |j$                  j'                  dd      rG|j                  t
              5 }t)        j*                  t)        j,                  |            }d d d        n|j                  j                  t
              }t              | _        t5        t        | j
                        t6        z  t        |j8                              | _        | j:                  dk(  rg | _        t        | j                        | _        y c c}w # t        t        f$ r |r| j                   | _        Y 8w xY w# 1 sw Y   xY w# t        t        f$ r;}|j$                  j/                  dt1        j2                  |      z         Y d }~d }~ww xY w)Nr   r(  s   storages   revbranchcache.mmaps(   couldn't read revision branch cache: %s
)r$   rx   _namesr  r  _rbcrevs_rbcsnameslenr   read	_rbcnamesr   r   r   r   r   r   _branchinforH   r   
configboolr   r  mmapreadr   r   r   r   r)  r0   _rbcrevslen_rbcnamescount)r   r&   readonlybndatar   fpdatar   s           r   r    zrevbranchcache.__init__  s   &&&
	,
	3]]''	2F!$VD39<<3F-/H$$R( ;;77%%j2HIx0 >B#{{4==+<=> >  ==--h7D ' +-s4>>/B
 q DK!$++.7 ! 	3"&"2"2	3> >
 W% @ --d34 sT   AF8 F3F8 21G- #)G!8G- 3F8 8"GG!G*&G- -H7<1H22H7c                    d| _         | j                  d d = d| _        t        | j                  j
                        | _        t        t        | j                  t        z              | _
        t        j                  | d       y )Nr   s   _namesreverse)r:  r8  rA  r   rx   r0   r@  r  r  r)  r9  r   clearcachedpropertyr   s    r   _clearzrevbranchcache._clear  sa    KKNtzz334	$*:*:[*H IJ  '78r!   c                 `    t        | j                        D ci c]  \  }}||
 c}}S c c}}w r   )	enumerater8  )r   r,   rT   s      r   _namesreversezrevbranchcache._namesreverse  s'    !*4;;!78A1888s   *c                 ,   | j                   j                  }|t        z  }|t        k(  r|j	                  |      S t        | j                        |t        z   k  r| j                  |      S |j                  |      dt         }| j                  j                  |      \  }}t        |t        z        }|r	|t        z  }|dk(  rn~||k(  r	 | j                  |   |fS | j                   j                   j#                  d|z         |t        z   }| j                  j'                  |       t)        | j*                  |      | _        | j                  |      S # t        $ r8 | j                   j                   j#                  d       | j%                          Y Qw xY w)zWReturn branch name and close flag for rev, using and updating
        persistent cache.Ns       sR   referenced branch names not found - rebuilding revision branch cache from scratch
sP   history modification detected - truncating revision branch cache to revision %d
)rx   r0   r)  r   rH   r   r9  r=  rC   _rbcnodelenr  r
  _rbccloseflag_rbcbranchidxmaskr8  r   r   r   rH  r&  r   r@  )	r   rG   r0   r  reponode	cachenoder+  r   r&  s	            r   rH   zrevbranchcache.branchinfo  s}    JJ((	+%	 '>'',, t}}	K 77##C(( >>#&|4#}}::9E	9Y./**I#("{{9-u44 JJMM:<?@ !;.HMM""8,"4#3#3X>D $$%  

##J s   	E >FFc                 v   | j                   j                  }|j                  |      \  }}|| j                  v r| j                  |   }n?t	        | j
                        }| j
                  j                  |       || j                  |<   |j                  |      }|r	|t        z  }| j                  |||       ||fS )z7Retrieve branch info from changelog and update _rbcrevs)
rx   r0   rH   rK  r   r8  r   rC   rN  _setcachedata)r   rG   r0   rT   r   r+  rP  s          r   r=  zrevbranchcache._branchinfo,  s    JJ((	'',5"""**1-IDKK(IKKq!$-Dq!>>#&&I3)4%xr!   c                 ~   |j                   \  }}|| j                  v r| j                  |   }n?t        | j                        }| j                  j	                  |       || j                  |<   |r	|t
        z  }| j                  || j                  j                  j                  |      |       dt        |       v r| ` yy)z%add new data information to the cacherH   N)rH   rK  r   r8  r   rN  rS  rx   r0   rC   vars)r   rG   changelogrevisionr   r   r+  s         r   setdatazrevbranchcache.setdata<  s    )44T'''**62IDKK(IKKv&)2Dv&&I3

 4 4 9 9# >	J 4:% &r!   c                    |t         k(  ry|t        z  }| j                  j                  |||       t	        | j
                  |      | _        | j                  j                         }|r|j                  d| j                         yy)z:Writes the node's branch data to the in-memory cache data.Ns   write-revbranchcache)
r   r)  r9  r*  r   r@  rx   r   addfinalizerM   )r   rG   rC   r+  r  r   s         r   rS  zrevbranchcache._setcachedataR  sn    '>+%		4;t//5ZZ**,NN2DJJ? r!   Nc                    | j                   }d}d}	 | j                  t        | j                        k  r%|j	                  d      }d}| j                  |       | j                  t        z  }|t        | j                        k7  r(d}||j	                  d      }| j                  ||       ||j'                          yy# t        t        t        j                  t        j                  f$ r=}|j                  j!                  d|t#        j$                  |      fz         Y d}~yd}~ww xY w# ||j'                          w w xY w)z!Save branch cache if it is dirty.Nr!   F)waits    namess+   couldn't write revision branch cache%s: %s
)rx   rA  r   r8  wlock_writenamesr@  r)  r9  
_writerevsr   r   r   r   	LockErrorr   r   r   r   release)r   r   r&   r\  stepr/   r   s          r   rM   zrevbranchcache.write^  s   zz	 ""S%55


.   & $${2EDMM**= JJEJ2Ee,   ! %++u? 	GGMM?00678 	   !s*   BC   -D*-3D% D- %D**D- -Ec                    | j                   dk7  r|j                  j                  t        d      }|j	                         | j
                  k(  r|j                  d       n@|j                          |j                  j                  dt        z         d| _         d| _
        | j                   dk(  rA|j                  j                  t        d       |j                  j                  t        d      }j                  dj                  d | j                  | j                   d	 D                     |j	                         | _        |j                          t        | j                        | _         y	)
z,write the new branch names to revbranchcacher      abr(  s   %s changed - rewriting it
T)ignoremissings   wbc              3   F   K   | ]  }t        j                  |        y wr   )r   r   )r+   rT   s     r   r.   z-revbranchcache._writenames.<locals>.<genexpr>  s#       ""1%s   !N)rA  r   openr<  tellr:  rM   r   r   r   r@  
unlinkpathr9  r   r8  r   )r   r&   r   s      r   r]  zrevbranchcache._writenames{  s   !#""9e4Avvx4---	<yHI&'##$ !#MM$$XT$B""9e4A	JJ T%8%8%:; 	
 VVX		!$++.r!   c                 `   t        t        |j                        t        | j                        t        z        }|j
                  j                  t        d      5 }|j                         |k7  rk|j                  j                  dt        |fz         |j                  |       |j                         |k7  rd}|j                  |       |j                          |t        z  }|j                  | j                  j                  ||             ddd       || _        y# 1 sw Y   || _        yxY w)z$write the new revs to revbranchcacherc  s   truncating cache/%s to %d
r   N)r   r   r0   r9  r)  r   rf  rg  r   r   seekr&  rM   r5  r@  )r   r&   r/   r:   r   r3  s         r   r^  zrevbranchcache._writerevs  s    3t~~&DMM(:k(IJ]]%0 	5Avvx5 2h5FF u668u$EFF5M

$CGGDMM''s34	5  	5  s   B3DD-)Tr   )rc   rd   re   rf   r    rH  r   propertycacherK  rH   r=  rW  rS  rM   r]  r^  rg   r!   r   r   r     sQ    ,%/P9 
9 9+%Z  ,
@ :/2 r!   r   )-structrC   r   r   r   typingr   r   r	   r
   r   r   r   r   r    r   r   r   r   r   utilsr   r   r   r5   calcsizer*  r  r   rj   rm   r3   r  _rbcversionr<  r9  r  r)  
_rbcminincrM  rO  rN  r  r   rg   r!   r   <module>rs     s     
 
 
 
 ;;&&??	  \" \"~<
w wt  ;&	$
z"+
 AO AOHo  o r!   