o ]Lb8;@sddlmZddlZddlmZddlmZmZedZ dZ dZ e dZ e d Ze e jks2Je ejks9Jd Zd d Zd dZddZejGdddeZddZddZddZddZddZdS))absolute_importN)attr)errorpolicyparsers,z >LLLLL4s20sz>LHHLHLLLLHlll c Cs,t|\}}}}}} } t|||||dS)a;parse a full v2-dirstate from a binary data into dictionnaries: - 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_METADATAunpack parse_nodes) mapcopy_mapdata tree_metadataroot_nodes_startroot_nodes_len_nodes_with_entry_count_nodes_with_copy_source_count_unreachable_bytes_unused_ignore_patterns_hashr nodes from starting at offset 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_SIZEslice_with_lenNODEr r r DirstateItem from_v2_data any_tracked)r rrstartleni node_start node_bytes path_startpath_len_basename_startcopy_source_startcopy_source_lenchildren_startchildren_count_descendants_with_entry_count_tracked_descendants_countflagssizemtime_smtime_nsitempathrrrr Ss<    r cCs||||SNr)rr"r#rrrr|src@s`eZdZeZeZejddZejddZejddZ ejddZ ejddZ ddZ dS)NodeN)defaultrcCs|j}||}|j}|}t|}|dd}|dur'|t|} t|} nd} d} |dur8|\} } } }nt} d} d} d}t|||| | |j |j |j |j | | | | S)N/r) r5getentryr#rfindv2_dataDIRSTATE_V2_DIRECTORYrpackchildren_offsetr-descendants_with_entrytracked_descendants)selfr paths_offsetr5copyr<r'r(basename_startr*r+r0r1r2r3rrrr@s@   z Node.pack) __name__ __module__ __qualname__ribr5r<parentr-rArBrCr@rrrrr7s      r7c Cst}d}d}d}d}d}d}d} t|dkr&t||||||| } || fSt|ddd} g} tdd} | | t| d D]\}\}}|d 7}||vrS|d 7}t |}t || | } | j d 7_ |j rn| j d 7_ | jd 7_| t||| d }d}|t| kr| |d}t|| }|rt| ||| | r| jdkr| j}|duot|t | d j}|dus|rnt|||| |} | r| jdksqA| } | jdksJ| jt| dksJt| t| j| j ||||| } || fS) a Pack `map` and `copy_map` into the dirstate v2 binary format and return the bytearray. 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 descendents 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`. rsscSs|dS)Nrr)xrrrszpack_dirstate..)keyNr:T) bytearrayr#r r@sorteditemsr7append enumerate get_foldermove_to_correct_node_in_treer-trackedrCrB is_ancestorpack_directory_childrenr5rLpoprA)r rrrrnodes_with_entry_countnodes_with_copy_source_countunreachable_bytesunusedignore_patterns_hashr sorted_mapstack current_nodeindexr5r<current_folder should_pack next_pathrLin_ancestor_of_next_pathrrr pack_dirstatesK         rjcCsd|vr |dddSdS)zU Return the folder of the path that's given, an empty string for root paths. r9r:rrP)rsplit)r5rrrrWTsrWcCsD|dkrdS||kr dS|d}|d}tddt||DS)aReturns 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 rPTFr9css|] \}}||kVqdSr6r).0corrr mszis_ancestor..)splitallzip)r5maybe_ancestorpath_componentsancestor_componentsrrrrZ[s   rZcCs||jkrSt||jrK|t|jdd}|ddd}|jr*|jd|}n|}|d}|j|kr8|}n|jd7_t|d|}||n|j}||jks|S)z Move inside the dirstate node tree to the node corresponding to `target_folder`, creating the missing nodes along the way if needed. Nr9r:rrQ) r5rZr#lstriprpr-r7rUrL) target_folderrdrcprefixsubfolder_namesubfolder_path next_noderrrrXps      rXcCsg}|djdkr+t|dj|jkr+|||djdkr+t|dj|jks|s5td|j|t}|D]0}|j|t |d}| || |j| | |jd|j |j 7_ |j |j 7_ q>t ||_| |dS)z_ Write the binary representation of the direct sorted children of `node` to `data` rQrPsno direct children for %r)rrEN)r5rWrUr\rProgrammingErrorreverserRr@r#extendr;rCrBrA)noderrrcdirect_childrenpacked_childrenchildpackedrrrr[s"""   r[) __future__rstruct thirdpartyrrr importmodrTREE_METADATA_SIZErStructr rr1r?rr rsobjectr7rjrWrZrXr[rrrrs.    )0$