
    6hJ                    N   S SK Jr  S SKrS SKJrJr  S SKJr  S SKJ	r	  SSK
JrJrJr  Sr\R                  " \5      rS	rS
r\R                  " \5      rSrSrSrSrSrS rSrSrSrSr " S S\R<                  5      r " S S\R                  5      r  " S S\RB                  5      r"g)    )annotationsN)hexsha1nodeconstants)util)hashutil   )basepack	constantsshallowutils   !20sQQII   s   !20sQs   !20s20s20s20sHR      s   .histidxs	   .histpack      c                  8    \ rS rSr\r\rS rSS jrS rS r	Sr
g)	historypackstore+   c                    [        U5      $ N)historypack)selfpaths     A/usr/lib/python3/dist-packages/hgext/remotefilelog/historypack.pygetpackhistorypackstore.getpack/   s    4      Nc                    U R                    H  n UR                  XUS9s  $    U R                  5        H  n UR                  XUS9s  $    [        X45      e! [         a     MY  f = f! [         a     MB  f = f)Nknown)packsgetancestorsKeyErrorrefresh)r   namenoder   packs        r   r!   historypackstore.getancestors2   s    JJD((5(AA  LLND((5(AA # |$$    s"   AA*
A'&A'*
A87A8c                    U R                    H  n UR                  X5      s  $    U R                  5        H  n UR                  X5      s  $    [        X45      e! [         a     M[  f = f! [         a     MC  f = fr   )r    getnodeinfor"   r#   )r   r$   r%   r&   s       r   r)   historypackstore.getnodeinfoA   s    JJD''33  LLND''33 # |$$    s"   AA,
A)(A),
A:9A:c                4    [        SU[        U5      4-  5      e)Ns&   cannot add to historypackstore (%s:%s)RuntimeErrorr   r   filenamer%   p1p2linknodecopyfroms          r   addhistorypackstore.addP   s     53t98MM
 	
r    r   )__name__
__module____qualname____firstlineno__INDEXSUFFIX
PACKSUFFIXr   r!   r)   r4   __static_attributes__r6   r   r   r   r   +   s     KJ!%%
r   r   c                     ^  \ rS rSr\r\rS/rU 4S jrS rSS jr	S r
SS jrS rS	 rS
 rS rS rSS jrS rS rS rSrU =r$ )r   V   r   c                P   > [         TU ]  U5        [        U l        [        U l        g r   )super__init__INDEXFORMAT2INDEXFORMATINDEXENTRYLENGTH2INDEXENTRYLENGTH)r   r   	__class__s     r   rB   historypack.__init__\   s     ' 1r   c                    / nU H  u  p4 U R                  X45        M     U$ ! [         a    UR                  X445         M<  f = fr   )	_findnoder"   append)r   keysmissingr$   r%   s        r   
getmissinghistorypack.getmissinga   sJ    JD-t*    -|,-s   "AAc                    U(       a  X#;   a  / $ U R                  XUS9n0 nU H  u  pgpn
XxX4XV'   M     U(       d  [        X45      eU$ )znReturns as many ancestors as we're aware of.

return value: {
   node: (p1, p2, linknode, copyfrom),
   ...
}
r   )_getancestorsr"   )r   r$   r%   r   	ancestorsresultsancnoder0   r1   r2   r3   s              r   r!   historypack.getancestorsk   s_     T]I&&t&?	3</Gx ";G 4= D<((r   c                *    U R                  X5      SS  $ )Nr   )rJ   )r   r$   r%   s      r   r)   historypack.getnodeinfo   s     ~~d)!"--r   c              #    #    Uc
  [        5       nU R                  U5      nUu  pVpxn	U1n
SnX:  a  U
(       d  g U R                  Xk-   5      u  pU[        -  nU(       a  U[	        U5      -  nU[
           nX;   a  U
R                  U5        U[           nU[           nU[        R                  :w  a  X;  a  U
R                  U5        U[        R                  :w  a  UU;  a  U
R                  U5        XUU[           U4v   X:  a  M  g g 7fNr   )set_findsection
_readentryPACKENTRYLENGTHlenANC_NODEremove
ANC_P1NODE
ANC_P2NODEr   nullidr4   ANC_LINKNODE)r   r$   r%   r   sectionr/   offsetsizenodeindexoffsetnodeindexsizependingoentryr3   rT   p1nodep2nodes                    r   rQ   historypack._getancestors   s     =EE##D)AH>$&h"oofj9OE AS]"HoG!w'z*z*.555&:MKK'.555&:MKK'l0CXNN% hs   DD
D
c                    U R                   n[        R                  " [        X!U[        -    5      nS nU[
           nUS:w  a  U[        -  nX!X-    nX44$ rY   )_datastructunpack
PACKFORMATr]   ANC_COPYFROM)r   rf   datarl   r3   copyfromlens         r   r\   historypack._readentry   s`    zzf&>?
 L)!o%FV%9:Hr   c                4    [        SU[        U5      4-  5      e)Ns!   cannot add to historypack (%s:%s)r,   r.   s          r   r4   historypack.add   s     0Hc$i3HH
 	
r   c                   U R                   S:X  a+  U R                  X5      nU H  u  pEpgnXB:X  d  M  XEXgU4s  $    OnU R                  U5      n	U	SS  u  pU R                  UU
X-   [        5      nUb9  [
        R                  " [        U5      u  p-U R                  U5      u  pUS S U4-   $ [        SU[        U5      4-  5      e)Nr   r   r   s    unable to find history for %s:%s)VERSIONrQ   r[   _bisectNODEINDEXENTRYLENGTHrr   rs   NODEINDEXFORMATr\   r"   r   )r   r$   r%   rR   rT   rm   rn   r2   r3   re   rh   ri   rl   rf   s                 r   rJ   historypack._findnode   s    <<1**46I?H;8?#VxHH @I ''-G-4QR[*OLL/$	E  %}}_eD"&//&"9 RayH;..:dCI=NNOOr   c                n   U R                   n[        R                  " U5      R                  5       n[        R
                  " UR                  US UR                   5      S   nU R                  nXT   UR                  -   nU R                  n[        US-   UR                  5       H  nXX   UR                  -   n	X:w  d  M    O   Un	U R                  X6XR                  5      n
U
(       d  [        U5      e[        R
                  " U R                   U
5      nUu  ppnU R"                  X[$        R&                  -    n[        R
                  " SU5      S   nU[$        R&                  -  nU R"                  XU-    nUU:w  a  [        SUU4-  5      eUU-  n[        R
                  " SU R(                  X[$        R&                  -    5      S   nU[$        R&                  -  nU R(                  XU-    nUU-  nUU:w  a  [        SUU4-  5      eU[*        -  nUnU[$        R&                  -
  U-
  [*        -
  nUUUUU4$ )Nr   r      !Hs&   found file name %s when looking for %s)paramsr   sha1digestrr   rs   fanoutstructfanoutprefix_fanouttable
indexstart	_indexendrangefanoutcountr}   rF   r"   rD   _indexr
   FILENAMESIZErq   ENTRYCOUNTSIZE)r   r$   r   namehash	fanoutkeyfanoutstartindexendiendrl   rawentryxrf   rg   rh   ri   
rawnamelenactualnamelen
actualnamefilenamelengthnodelistoffsetnodelistsizes                          r   r[   historypack._findsection   sM   ==&--/MM*?F,?,?!@

	 ""!F$5$55>>y1}f&8&89A)f///C| :
 CXc3H3HI4. ==!1!159:B74-[[	0F0FF

 eZ8;9111[[=

 9Z<NN  	=(4::f	0F0F'FG

 	)(((ZZ.)@A
. :9Z<NN 
 	. 9)))N:^K 	 
 	
r   c                t   UnU R                   X"[        -    nU R                   X3[        -    nXa:X  a  U R                   X"U-    $ Xq:X  a  U R                   X3U-    $ X#U-
  :  aX  X#U-
  S-  -   nXU-
  U-  -
  nU R                   X[        -    n	X:X  a  U R                   XU-    $ X:  a  UnOX:  a  UnX#U-
  :  a  MX  g )Nr   )r   
NODELENGTH)
r   r%   r   r   entrylen	origstart	startnodeendnodemidmidnodes
             r   r}   historypack._bisect
  s    	KK
(:;	++c*$45;;ux'788_;;s8^44.(U{q00Io9:++c*,<=?;;s8^<<>E^C .( r   c                >    U  H  u  p4UR                  XU5        M     g r   )markhistoryentry)r   ledgeroptionsr/   r%   s        r   
markledgerhistorypack.markledger!  s    "NH##DD9 #r   c                   UR                   R                  U / 5      n[        U 5      nU Vs1 s H-  oDR                  (       d  M  UR                  UR
                  4iM/     nn[        X5-
  5      S:X  aZ  U R                  UR                  ;  a?  [        R                  " U R                  SS9  [        R                  " U R                  SS9  g g g s  snf )Nr   T)ignoremissing)sourcesgetrZ   historyrepackedr/   r%   r^   r   createdr   
unlinkpath	indexpathpackpath)r   r   entriesallkeyserepackedkeyss         r   cleanuphistorypack.cleanup%  s    ..$$T2.d)*1
*1Q5F5F QZZ ' 	 
 w%&!+yy.dCTB / ,	
s   CCc              #  P   #    U R                  5        H  u  p      nX4v   M     g 7fr   )iterentries)r   fnr   s       r   __iter__historypack.__iter__1  s*      $ 0 0 2A!Q1$J !3s   $&c              #    #    SnXR                   :  Ga;  U R                  n[        R                  " SX!U[        R
                  -    5      S   nU[        R
                  -  nX!X-    nX-  n[        R                  " SX!U[        -    5      S   nU[        -  n[        U5       H  n[        R                  " [        X!U[        -    5      nU[        -  nX!X[           -    nX[           -  nUU[           U[           U[           U[           U4v   U =R                  [        -  sl        M     U R!                  5         XR                   :  a  GM:  g g 7f)Nr   r   r      !I)datasizerq   rr   rs   r
   r   r   r   rt   r]   ru   r_   ra   rb   rd   _pagedin
freememory)	r   rf   rv   filenamelenr/   revcountr   rl   r3   s	            r   r   historypack.iterentries5  sS    }}$::D --tVi.D.D%DEK i,,,FV%9:H!F}}tVn%<=H n$F8_f.F G /)2E)EF-- (O*%*%,'  0% %* OOI }}$$s   E
EE)rF   rD   r   )r7   r8   r9   r:   r;   r<   SUPPORTED_VERSIONSrB   rN   r!   r)   rQ   r\   r4   rJ   r[   r}   r   r   r   r   r=   __classcell__rG   s   @r   r   r   V   se    KJ2
(.
O6


P0@
D.:
C' 'r   r   c                  `   ^  \ rS rSrSr\r\rS/rS
U 4S jjrS r	S r
SU 4S jjrS rS	rU =r$ )mutablehistorypacki_  a  A class for constructing and serializing a histpack file and index.

A history pack is a pair of files that contain the revision history for
various file revisions in Mercurial. It contains only revision history (like
parent pointers and linknodes), not any revision content information.

It consists of two files, with the following format:

.histpack
    The pack itself is a series of file revisions with some basic header
    information on each.

    datapack = <version: 1 byte>
               [<filesection>,...]
    filesection = <filename len: 2 byte unsigned int>
                  <filename>
                  <revision count: 4 byte unsigned int>
                  [<revision>,...]
    revision = <node: 20 byte>
               <p1node: 20 byte>
               <p2node: 20 byte>
               <linknode: 20 byte>
               <copyfromlen: 2 byte>
               <copyfrom>

    The revisions within each filesection are stored in topological order
    (newest first). If a given entry has a parent from another file (a copy)
    then p1node is the node from the other file, and copyfrom is the
    filepath of the other file.

.histidx
    The index file provides a mapping from filename to the file section in
    the histpack. In V1 it also contains sub-indexes for specific nodes
    within each file. It consists of three parts, the fanout, the file index
    and the node indexes.

    The file index is a list of index entries, sorted by filename hash (one
    per file section in the pack). Each entry has:

    - node (The 20 byte hash of the filename)
    - pack entry offset (The location of this file section in the histpack)
    - pack content size (The on-disk length of this file section's pack
                         data)
    - node index offset (The location of the file's node index in the index
                         file) [1]
    - node index size (the on-disk length of this file's node index) [1]

    The fanout is a quick lookup table to reduce the number of steps for
    bisecting the index. It is a series of 4 byte pointers to positions
    within the index. It has 2^16 entries, which corresponds to hash
    prefixes [00, 01, 02,..., FD, FE, FF]. Example: the pointer in slot 4F
    points to the index position of the first revision whose node starts
    with 4F. This saves log(2^16) bisect steps.

    dataidx = <fanouttable>
              <file count: 8 byte unsigned> [1]
              <fileindex>
              <node count: 8 byte unsigned> [1]
              [<nodeindex>,...] [1]
    fanouttable = [<index offset: 4 byte unsigned int>,...] (2^16 entries)

    fileindex = [<file index entry>,...]
    fileindexentry = <node: 20 byte>
                     <pack file section offset: 8 byte unsigned int>
                     <pack file section size: 8 byte unsigned int>
                     <node index offset: 4 byte unsigned int> [1]
                     <node index size: 4 byte unsigned int>   [1]
    nodeindex = <filename>[<node index entry>,...] [1]
    filename = <filename len : 2 byte unsigned int><filename value> [1]
    nodeindexentry = <node: 20 byte> [1]
                     <pack file node offset: 8 byte unsigned int> [1]

[1]: new in version 1.
r   c                   > [         TU ]  XUS9  0 U l        0 U l        0 U l        [
        U l        [        U l        [        U l	        [        U l
        g )N)version)rA   rB   filesentrylocationsfileentriesrC   rD   rE   rF   r   r~   )r   uir   r   rG   s       r   rB   mutablehistorypack.__init__  sL    w7
 ' 1.$8!r   c                    U=(       d    Sn[         R                  " S[        U5      5      nU R                  R	                  U/ 5      R                  X#XEXv45        g )Nr   r   )rr   r&   r^   r   
setdefaultrK   )r   r/   r%   r0   r1   r2   r3   rw   s           r   r4   mutablehistorypack.add  sJ    ?skk%X7##Hb188r[;	
r   c                  ^ [        U R                  5       GH  nU R                  U   nU R                  R                  5       nU Vs0 s H  oDS   U_M
     snmU4S jn[	        [        [        R                  " S U 5       U5      5      5      nU R                  S[        R                  " S[        U5      5      U[        R                  " S[        U5      5      4-  5        [        R                  [        U5      -   S-   n/ nU R                  R                  U0 5      n	X7-   n
U H.  nXU'   STU   -  nUR!                  U5        U
[        U5      -  n
M0     S	R#                  U5      nU[        U5      -  nU R                  U5        X74U R$                  U'   [&        R(                  " U5      R+                  5       nXR,                  U'   GM     g s  snf )
Nr   c                   > TU    u  pn    n/ nU[         R                  :w  a  UR                  U5        U[         R                  :w  a  UR                  U5        U$ r   )r   rc   rK   )r%   r   r0   r1   parentsentrymaps        r   
parentfunc-mutablehistorypack._write.<locals>.parentfunc  sW    %-d^"r1a*111NN2&*111NN2&r   c              3  *   #    U  H	  oS    v   M     g7f)r   Nr6   ).0r   s     r   	<genexpr>,mutablehistorypack._write.<locals>.<genexpr>  s     *AAQ4s   s   %s%s%sr   r   r   s   %s%s%s%s%s%sr   )sortedr   packfptelllistreversedr   	sortnodeswriterawrr   r&   r^   r
   r   r   r   rK   joinr   r   r   r   r   )r   r/   r   sectionstartr   r   sortednodes
sectionlen
rawstrings	locationsrf   r%   rawrawdatar   s                 @r   _writemutablehistorypack._write  s   t//0H&&x0G;;++-L *11A!a1H ))*A*A:NK MMKKs8}5KKs;'78 #//#h-?!CJJ ++66xDI!.F#"($%6!!#&#c("	 $ hhz*G#g,&JMM'" %1#=DJJx ==*113D!%LLk 1
 2s   Gc                b   > U R                   (       a  g U R                  5         [        TU ]  US9$ )N)r   )_closedr   rA   close)r   r   rG   s     r   r   mutablehistorypack.close  s'    <<w}F}++r   c           	        U R                   nU R                  nU R                  nU R                  nS U R                  R                  5        5       n[        U5      n[        R                  " S5      nUU-   [        U5      U-  -   U-   n	/ n
/ nSnU GH  u  pnnU R                  U   n[        U5      U-  n[        R                  " UUUUU	U5      nUR                  [        R                  " [        R                  [        U5      5      U-   5        U	[        R                  [        U5      -   -  n	[        UR                  5       5       H2  u  nnUR                  [        R                  " UUU5      5        US-  nM4     U	[        U5      U-  -  n	U
R                  U5        GM     [        R                  " SU5      nSR!                  U
5      U-   SR!                  U5      -   $ )Nc              3  z   #    U  H1  u  nu  p#[         R                  " U5      R                  5       XU4v   M3     g 7fr   )r   r   r   )r   r/   rf   rg   s       r   r   1mutablehistorypack.createindex.<locals>.<genexpr>	  s7      
,>(.6 ]]8$++-xF,>s   9;s   !Qr   r   r   )rD   rF   r   r~   r   itemsr   rr   calcsizer^   r   r&   rK   r
   FILENAMESTRUCTr   r   )r   nodelocationsindexoffsetfileindexformatfileindexlengthnodeindexformatnodeindexlengthr   indexlensizerh   fileindexentriesnodeindexentries	nodecountr   r/   rf   rg   ri   r   r%   locationnodecountraws                         r   createindexmutablehistorypack.createindex  s   **//..33
,0JJ,<,<,>
 u u-5zO+-  	 	05,H //9M.@M{{H ##I44c(mDxO y55HEEO"()<)<)>"?h ''KKx@ Q		 #@ s=1OCCO##H-9 16< {{5)4HH%&hh'()	
r   )rF   rD   r~   r   r   r   r   )r   r   )r7   r8   r9   r:   __doc__r;   r<   r   rB   r4   r   r   r  r=   r   r   s   @r   r   r   _  s=    IV KJ
9
6&p,;
 ;
r   r   )#
__future__r   rr   mercurial.noder   r   	mercurialr   mercurial.utilsr    r	   r
   r   rC   r   rE   r   r   r~   rt   r]   r   r;   r<   r_   ra   rb   rd   ru   basepackstorer   r   mutablebasepackr   r6   r   r   <module>r     s    "  %  OOL1 
7  



(
x-- (
VF(## FR_
11 _
r   