
    6h;                       S SK Jr  S SKrS SKrSSKJr  \R                  (       a  S SKrSSKJrJ	r	  \	R                  " S5      rSrSr\R                  " S5      r\R                  " S	5      r\\R"                  :X  d   e\\R"                  :X  d   eS
rS rS rS r\R,                   " S S5      5       rS rS rS rS rS rg)    )annotationsN   )attr)errorpolicyparsers,   z>LLLLL4s20sz>LHHLHLLLLHllli    c                X    [         R                  U5      u  nnnnnn	n
[        XX$U5        g)a&  parse a full v2-dirstate from a binary data into dictionaries:

- map: a {path: entry} mapping that will be filled
- copy_map: a {path: copy-source} mapping that will be filled
- data: a binary blob contains v2 nodes data
- tree_metadata:: a binary blob of the top level node (from the docket)
N)TREE_METADATAunpackparse_nodes)mapcopy_mapdatatree_metadataroot_nodes_startroot_nodes_len_nodes_with_entry_count_nodes_with_copy_source_count_unreachable_bytes_unused_ignore_patterns_hashs              </usr/lib/python3/dist-packages/mercurial/dirstateutils/v2.pyparse_dirstater   F   s8      	]+%t~F    c                |   [        U5       H  nU[        U-  -   n[        X&[        5      n[        R	                  U5      u  nn	n
nnnnnnnnnn[        XX-U5        [        R                  R                  UUUU5      nUR                  (       d  M  [        X(U	5      nUU U'   U(       d  M  [        X+U5      UU'   M     g)a	  parse <len> nodes from <data> starting at offset <start>

This is used by parse_dirstate to recursively fill `map` and `copy_map`.

All directory specific information is ignored and do not need any
processing (DIRECTORY, ALL_UNKNOWN_RECORDED, ALL_IGNORED_RECORDED)
N)
range	NODE_SIZEslice_with_lenNODEr   r   r   DirstateItemfrom_v2_dataany_tracked)r   r   r   startleni
node_start
node_bytes
path_startpath_len_basename_startcopy_source_startcopy_source_lenchildren_startchildren_count_descendants_with_entry_count_tracked_descendants_countflagssizemtime_smtime_nsitempaths                          r   r   r   Z   s     3ZY]*
#Di@
 KK
#	
)& 	C4H##00gxPd9D	+HTN9 r   c                    XX-    $ N )r   r$   r%   s      r   r   r      s    $$r   c                      \ rS rSr\R
                  " 5       r\R
                  " 5       r\R
                  " SS9r\R
                  " SS9r	\R
                  " SS9r
\R
                  " SS9r\R
                  " SS9rS rSrg)Node   N)defaultr   c                   U R                   nUR                  U5      nU R                  nUn[        U5      nUR	                  S5      S-   nUb  U[        U5      -   n	[        U5      n
OSn	Sn
Ub  UR                  5       u  ppO[        nSnSnSn[        R                  UUUU	U
U R                  U R                  U R                  U R                  UUUU5      $ )N   /   r   )r7   getentryr%   rfindv2_dataDIRSTATE_V2_DIRECTORYr    packchildren_offsetr/   descendants_with_entrytracked_descendants)selfr   paths_offsetr7   copyrC   r)   r*   basename_startr,   r-   r2   r3   r4   r5   s                  r   rG   	Node.pack   s    yy||D!

!
t9D)A- ,s4y 8!$iO !O-2]]_*E( *EDGHyy  ''$$
 	
r   r:   )__name__
__module____qualname____firstlineno__r   ibr7   rC   parentr/   rH   rI   rJ   rG   __static_attributes__r:   r   r   r<   r<      se    779DGGIEWWT"FWWQ'Ngga(O!WWQ/''!,$
r   r<   c           	        [        5       nSnSnSnSnSnSnSn	[        U 5      S:X  a  [        R                  UUUUUUU	5      n
X*4$ [	        U R                  5       S S9n/ n[        SS5      nUR                  U5        [        US5       GHh  u  nu  nnUS-  nX;   a  US-  n[        U5      n[        UX5      nU=R                  S-  sl        UR                  (       a  U=R                  S-  sl        U=R                  S-  sl        UR                  [        UUU5      5        S	nSnU[        U5      :  a  X   S   n[        UU5      (       + nU(       d  M  [!        XX,5        U(       d  M  UR"                  S:w  d  M  UR$                  nUSL=(       a"    [        U[        US
   R"                  5      5      nUb  U(       a  GM=  [!        UXU5        UnU(       d  GMV  UR"                  S:w  a  Mq  GMk     UR'                  5       nUR"                  S:X  d   UR"                  5       e[        U5      S:X  d   [        U5      5       e[        R                  UR(                  UR                  UUUUU	5      n
X*4$ )aA  
Pack `map` and `copy_map` into the dirstate v2 binary format and return
the tuple of (data, metadata) bytearrays.

The on-disk format expects a tree-like structure where the leaves are
written first (and sorted per-directory), going up levels until the root
node and writing that one to the docket. See more details on the on-disk
format in `mercurial/helptext/internals/dirstate-v2`.

Since both `map` and `copy_map` are flat dicts we need to figure out the
hierarchy. This algorithm does so without having to build the entire tree
in-memory: it only keeps the minimum number of nodes around to satisfy the
format.

# Algorithm explanation

This explanation does not talk about the different counters for tracked
descendants and storing the copies, but that work is pretty simple once this
algorithm is in place.

## Building a subtree

First, sort `map`: this makes it so the leaves of the tree are contiguous
per directory (i.e. a/b/c and a/b/d will be next to each other in the list),
and enables us to use the ordering of folders to have a "cursor" of the
current folder we're in without ever going twice in the same branch of the
tree. The cursor is a node that remembers its parent and any information
relevant to the format (see the `Node` class), building the relevant part
of the tree lazily.
Then, for each file in `map`, move the cursor into the tree to the
corresponding folder of the file: for example, if the very first file
is "a/b/c", we start from `Node[""]`, create `Node["a"]` which points to
its parent `Node[""]`, then create `Node["a/b"]`, which points to its parent
`Node["a"]`. These nodes are kept around in a stack.
If the next file in `map` is in the same subtree ("a/b/d" or "a/b/e/f"), we
add it to the stack and keep looping with the same logic of creating the
tree nodes as needed. If however the next file in `map` is *not* in the same
subtree ("a/other", if we're still in the "a/b" folder), then we know that
the subtree we're in is complete.

## Writing the subtree

We have the entire subtree in the stack, so we start writing it to disk
folder by folder. The way we write a folder is to pop the stack into a list
until the folder changes, revert this list of direct children (to satisfy
the format requirement that children be sorted). This process repeats until
we hit the "other" subtree.

An example:
    a
    dir1/b
    dir1/c
    dir2/dir3/d
    dir2/dir3/e
    dir2/f

Would have us:
    - add to the stack until "dir2/dir3/e"
    - realize that "dir2/f" is in a different subtree
    - pop "dir2/dir3/e", "dir2/dir3/d", reverse them so they're sorted and
      pack them since the next entry is "dir2/dir3"
    - go back up to "dir2"
    - add "dir2/f" to the stack
    - realize we're done with the map
    - pop "dir2/f", "dir2/dir3" from the stack, reverse and pack them
    - go up to the root node, do the same to write "a", "dir1" and "dir2" in
      that order

## Special case for the root node

The root node is not serialized in the format, but its information is
written to the docket. Again, see more details on the on-disk format in
`mercurial/helptext/internals/dirstate-v2`.
r   s       s                       c                *    U S   R                  S5      $ )Nr   r@   )split)xs    r   <lambda>pack_dirstate.<locals>.<lambda>  s    1Q4::d3Cr   )keyr   NrA   T)	bytearrayr%   r   rG   sorteditemsr<   append	enumerate
get_foldermove_to_correct_node_in_treer/   trackedrJ   rI   is_ancestorpack_directory_childrenr7   rU   poprH   )r   r   r   r   r   nodes_with_entry_countnodes_with_copy_source_countunreachable_bytesunusedignore_patterns_hashr   
sorted_mapstackcurrent_nodeindexr7   rC   current_foldershould_pack	next_pathrU   in_ancestor_of_next_paths                         r   pack_dirstaterw      s   V ;DN#$  F'
3x1}%**"( 
 ""		)CDJ ET?L	LL )*a 8}e!#(A-(#D)3L
 	##q(#==,,1,++q0+T$|45	3z?" #)!,I))^DDK;#LDH%L--4 &,,+4D+@ ,	:eBinn+EF ) >%='F% %L--443 !9N 99;L#6\%6%66#u:?&CJ&?!&&$$##$M r   c                <    SU ;   a  U R                  SS5      S   $ S$ )zM
Return the folder of the path that's given, an empty string for root paths.
r@   rA   r   r   )rsplit)r7   s    r   rd   rd   [  s%     '+dl4;;tQ";;r   c                    US:X  a  gX::  a  gU R                  S5      nUR                  S5      n[        S [        X#5       5       5      $ )zReturns whether `maybe_ancestor` is an ancestor of `path`.

>>> is_ancestor(b"a", b"")
True
>>> is_ancestor(b"a/b/c", b"a/b/c")
False
>>> is_ancestor(b"hgext3rd/__init__.py", b"hgext")
False
>>> is_ancestor(b"hgext3rd/__init__.py", b"hgext3rd")
True
r   TFr@   c              3  .   #    U  H  u  pX:H  v   M     g 7fr9   r:   ).0cos      r   	<genexpr>is_ancestor.<locals>.<genexpr>t  s     L"K$!qv"Ks   )rY   allzip)r7   maybe_ancestorpath_componentsancestor_componentss       r   rg   rg   b  sM     jj&O(..t4L#o"KLLLr   c                   XR                   :w  a  [        XR                   5      (       a  U [        UR                   5      S R                  S5      nUR	                  SS5      S   nUR                   (       a  UR                   S-   U-   nOUnUS   nUR                   U :X  a  UnO@U=R
                  S-  sl        [        USU5      nUR                  U5        OUR                  nXR                   :w  a  M  U$ )z
Move inside the dirstate node tree to the node corresponding to
`target_folder`, creating the missing nodes along the way if needed.
Nr@   rA   r   r^   )	r7   rg   r%   lstriprY   r/   r<   rb   rU   )target_folderrq   rp   prefixsubfolder_namesubfolder_path	next_nodes          r   re   re   w  s    
 ,,
,}&7&788"3|'8'8#9#;<CCDIF#\\$215N  !-!2!2T!9N!J!/b	I~~.  )++q0+#ND,G\* (..L) ,,
,* r   c                f   / nUS   R                   S:w  a  [        US   R                   5      U R                   :X  aZ  UR                  UR                  5       5        US   R                   S:w  a(  [        US   R                   5      U R                   :X  a  MZ  U(       d#  [        R
                  " SU R                   -  5      eUR                  5         [        5       nU H  nUR                  U[        U5      S9nUR                  U5        UR                  UR                   5        UR                  UR                  UR                   S5      5        U =R                  UR                  -  sl        U =R                  UR                  -  sl        M     [        U5      U l        UR                  U5        g)zS
Write the binary representation of the direct sorted children of `node` to
`data`
r^   r   s   no direct children for %r)r   rL   N)r7   rd   rb   ri   r   ProgrammingErrorreverser_   rG   r%   extendrB   rJ   rI   rH   )noder   r   rp   direct_childrenpacked_childrenchildpackeds           r   rh   rh     sC   
 O
)..C
JuRy~~$>$))$Kuyy{+ )..C
JuRy~~$>$))$K$$%ADII%MNN kO XCIFv&EJJHLLS12  E$=$== ##u'C'CC# ! t9DKK r   )
__future__r   structtyping
thirdpartyr   TYPE_CHECKING r   r   	importmodr   TREE_METADATA_SIZEr   Structr   r    r3   rF   r   r   r   sr<   rw   rd   rg   re   rh   r:   r   r   <module>r      s    #    
 


9
%
  	 m,& }}%& ]/// //DII    G(&R% -
 -
 -
``F<M*:!r   