annotate rust/hg-core/src/dirstate/dirstate_tree/tree.rs @ 45562:b51167d70f5a

rust: add `dirstate_tree` module Mercurial needs to represent the filesystem hierarchy on which it operates, for example in the dirstate. Its current on-disk representation is an unsorted, flat structure that gets transformed in the current Rust code into a `HashMap`. This loses the hierarchical information of the dirstate, leading to some unfortunate performance and algorithmic compromises. This module adds an implementation of a radix tree that is specialized for representing the dirstate: its unit is the path component. I have made no efforts to optimize either its memory footprint or its insertion speed: they're pretty bad for now. Following will be a few patches that modify the dirstate.status logic to use that new hierarchical information, fixing issue 6335 in the same swing. Differential Revision: https://phab.mercurial-scm.org/D9085
author Raphaël Gomès <rgomes@octobus.net>
date Fri, 25 Sep 2020 17:51:34 +0200
parents
children c35db907363d
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
45562
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1 // tree.rs
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
2 //
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
3 // Copyright 2020, Raphaël Gomès <rgomes@octobus.net>
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
4 //
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
5 // This software may be used and distributed according to the terms of the
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
6 // GNU General Public License version 2 or any later version.
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
7
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
8 use super::iter::Iter;
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
9 use super::node::{Directory, Node, NodeKind};
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
10 use crate::dirstate::dirstate_tree::iter::FsIter;
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
11 use crate::dirstate::dirstate_tree::node::{InsertResult, RemoveResult};
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
12 use crate::utils::hg_path::{HgPath, HgPathBuf};
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
13 use crate::DirstateEntry;
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
14 use std::path::PathBuf;
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
15
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
16 /// A specialized tree to represent the Mercurial dirstate.
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
17 ///
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
18 /// # Advantages over a flat structure
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
19 ///
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
20 /// The dirstate is inherently hierarchical, since it's a representation of the
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
21 /// file structure of the project. The current dirstate format is flat, and
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
22 /// while that affords us potentially great (unordered) iteration speeds, the
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
23 /// need to retrieve a given path is great enough that you need some kind of
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
24 /// hashmap or tree in a lot of cases anyway.
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
25 ///
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
26 /// Going with a tree allows us to be smarter:
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
27 /// - Skipping an ignored directory means we don't visit its entire subtree
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
28 /// - Security auditing does not need to reconstruct paths backwards to check
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
29 /// for symlinked directories, this can be done during the iteration in a
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
30 /// very efficient fashion
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
31 /// - We don't need to build the directory information in another struct,
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
32 /// simplifying the code a lot, reducing the memory footprint and
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
33 /// potentially going faster depending on the implementation.
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
34 /// - We can use it to store a (platform-dependent) caching mechanism [1]
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
35 /// - And probably other types of optimizations.
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
36 ///
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
37 /// Only the first two items in this list are implemented as of this commit.
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
38 ///
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
39 /// [1]: https://www.mercurial-scm.org/wiki/DirsCachePlan
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
40 ///
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
41 ///
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
42 /// # Structure
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
43 ///
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
44 /// It's a prefix (radix) tree with no fixed arity, with a granularity of a
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
45 /// folder, allowing it to mimic a filesystem hierarchy:
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
46 ///
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
47 /// ```text
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
48 /// foo/bar
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
49 /// foo/baz
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
50 /// test
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
51 /// ```
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
52 /// Will be represented (simplified) by:
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
53 ///
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
54 /// ```text
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
55 /// Directory(root):
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
56 /// - File("test")
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
57 /// - Directory("foo"):
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
58 /// - File("bar")
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
59 /// - File("baz")
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
60 /// ```
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
61 ///
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
62 /// Moreover, it is special-cased for storing the dirstate and as such handles
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
63 /// cases that a simple `HashMap` would handle, but while preserving the
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
64 /// hierarchy.
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
65 /// For example:
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
66 ///
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
67 /// ```shell
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
68 /// $ touch foo
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
69 /// $ hg add foo
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
70 /// $ hg commit -m "foo"
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
71 /// $ hg remove foo
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
72 /// $ rm foo
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
73 /// $ mkdir foo
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
74 /// $ touch foo/a
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
75 /// $ hg add foo/a
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
76 /// $ hg status
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
77 /// R foo
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
78 /// A foo/a
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
79 /// ```
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
80 /// To represent this in a tree, one needs to keep track of whether any given
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
81 /// file was a directory and whether any given directory was a file at the last
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
82 /// dirstate update. This tree stores that information, but only in the right
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
83 /// circumstances by respecting the high-level rules that prevent nonsensical
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
84 /// structures to exist:
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
85 /// - a file can only be added as a child of another file if the latter is
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
86 /// marked as `Removed`
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
87 /// - a file cannot replace a folder unless all its descendents are removed
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
88 ///
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
89 /// This second rule is not checked by the tree for performance reasons, and
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
90 /// because high-level logic already prevents that state from happening.
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
91 ///
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
92 /// # Ordering
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
93 ///
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
94 /// It makes no guarantee of ordering for now.
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
95 #[derive(Debug, Default, Clone, PartialEq)]
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
96 pub struct Tree {
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
97 pub root: Node,
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
98 files_count: usize,
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
99 }
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
100
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
101 impl Tree {
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
102 pub fn new() -> Self {
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
103 Self {
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
104 root: Node {
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
105 kind: NodeKind::Directory(Directory {
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
106 was_file: None,
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
107 children: Default::default(),
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
108 }),
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
109 },
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
110 files_count: 0,
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
111 }
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
112 }
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
113
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
114 /// How many files (not directories) are stored in the tree, including ones
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
115 /// marked as `Removed`.
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
116 pub fn len(&self) -> usize {
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
117 self.files_count
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
118 }
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
119
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
120 pub fn is_empty(&self) -> bool {
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
121 self.len() == 0
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
122 }
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
123
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
124 /// Inserts a file in the tree and returns the previous entry if any.
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
125 pub fn insert(
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
126 &mut self,
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
127 path: impl AsRef<HgPath>,
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
128 kind: DirstateEntry,
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
129 ) -> Option<DirstateEntry> {
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
130 let old = self.insert_node(path, kind);
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
131 match old?.kind {
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
132 NodeKind::Directory(_) => None,
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
133 NodeKind::File(f) => Some(f.entry),
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
134 }
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
135 }
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
136
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
137 /// Low-level insertion method that returns the previous node (directories
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
138 /// included).
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
139 fn insert_node(&mut self, path: impl AsRef<HgPath>, kind: DirstateEntry) -> Option<Node> {
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
140 let InsertResult {
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
141 did_insert,
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
142 old_entry,
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
143 } = self.root.insert(path.as_ref().as_bytes(), kind);
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
144 self.files_count += if did_insert { 1 } else { 0 };
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
145 old_entry
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
146 }
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
147
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
148 /// Returns a reference to a node if it exists.
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
149 pub fn get_node(&self, path: impl AsRef<HgPath>) -> Option<&Node> {
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
150 self.root.get(path.as_ref().as_bytes())
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
151 }
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
152
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
153 /// Returns a reference to the entry corresponding to `path` if it exists.
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
154 pub fn get(&self, path: impl AsRef<HgPath>) -> Option<&DirstateEntry> {
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
155 if let Some(node) = self.get_node(&path) {
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
156 return match &node.kind {
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
157 NodeKind::Directory(d) => d.was_file.as_ref().map(|f| &f.entry),
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
158 NodeKind::File(f) => Some(&f.entry),
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
159 };
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
160 }
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
161 None
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
162 }
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
163
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
164 /// Returns `true` if an entry is found for the given `path`.
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
165 pub fn contains_key(&self, path: impl AsRef<HgPath>) -> bool {
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
166 self.get(path).is_some()
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
167 }
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
168
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
169 /// Returns a mutable reference to the entry corresponding to `path` if it
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
170 /// exists.
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
171 pub fn get_mut(&mut self, path: impl AsRef<HgPath>) -> Option<&mut DirstateEntry> {
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
172 if let Some(kind) = self.root.get_mut(path.as_ref().as_bytes()) {
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
173 return match kind {
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
174 NodeKind::Directory(d) => d.was_file.as_mut().map(|f| &mut f.entry),
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
175 NodeKind::File(f) => Some(&mut f.entry),
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
176 };
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
177 }
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
178 None
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
179 }
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
180
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
181 /// Returns an iterator over the paths and corresponding entries in the
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
182 /// tree.
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
183 pub fn iter(&self) -> Iter {
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
184 Iter::new(&self.root)
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
185 }
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
186
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
187 /// Returns an iterator of all entries in the tree, with a special
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
188 /// filesystem handling for the directories containing said entries. See
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
189 /// the documentation of `FsIter` for more.
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
190 pub fn fs_iter(&self, root_dir: PathBuf) -> FsIter {
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
191 FsIter::new(&self.root, root_dir)
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
192 }
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
193
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
194 /// Remove the entry at `path` and returns it, if it exists.
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
195 pub fn remove(&mut self, path: impl AsRef<HgPath>) -> Option<DirstateEntry> {
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
196 let RemoveResult { old_entry, .. } = self.root.remove(path.as_ref().as_bytes());
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
197 self.files_count = self
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
198 .files_count
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
199 .checked_sub(if old_entry.is_some() { 1 } else { 0 })
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
200 .expect("removed too many files");
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
201 old_entry
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
202 }
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
203 }
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
204
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
205 impl<P: AsRef<HgPath>> Extend<(P, DirstateEntry)> for Tree {
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
206 fn extend<T: IntoIterator<Item = (P, DirstateEntry)>>(&mut self, iter: T) {
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
207 for (path, entry) in iter {
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
208 self.insert(path, entry);
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
209 }
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
210 }
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
211 }
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
212
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
213 impl<'a> IntoIterator for &'a Tree {
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
214 type Item = (HgPathBuf, DirstateEntry);
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
215 type IntoIter = Iter<'a>;
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
216
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
217 fn into_iter(self) -> Self::IntoIter {
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
218 self.iter()
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
219 }
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
220 }
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
221
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
222 #[cfg(test)]
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
223 mod tests {
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
224 use super::*;
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
225 use crate::dirstate::dirstate_tree::node::File;
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
226 use crate::{EntryState, FastHashMap};
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
227 use pretty_assertions::assert_eq;
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
228
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
229 impl Node {
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
230 /// Shortcut for getting children of a node in tests.
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
231 fn children(&self) -> Option<&FastHashMap<Vec<u8>, Node>> {
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
232 match &self.kind {
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
233 NodeKind::Directory(d) => Some(&d.children),
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
234 NodeKind::File(_) => None,
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
235 }
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
236 }
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
237 }
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
238
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
239 #[test]
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
240 fn test_dirstate_tree() {
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
241 let mut tree = Tree::new();
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
242
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
243 assert_eq!(
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
244 tree.insert_node(
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
245 HgPath::new(b"we/p"),
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
246 DirstateEntry {
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
247 state: EntryState::Normal,
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
248 mode: 0,
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
249 mtime: 0,
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
250 size: 0
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
251 }
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
252 ),
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
253 None
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
254 );
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
255 dbg!(&tree);
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
256 assert!(tree.get_node(HgPath::new(b"we")).is_some());
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
257 let entry = DirstateEntry {
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
258 state: EntryState::Merged,
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
259 mode: 41,
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
260 mtime: 42,
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
261 size: 43,
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
262 };
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
263 assert_eq!(tree.insert_node(HgPath::new(b"foo/bar"), entry), None);
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
264 assert_eq!(
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
265 tree.get_node(HgPath::new(b"foo/bar")),
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
266 Some(&Node {
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
267 kind: NodeKind::File(File {
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
268 was_directory: None,
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
269 entry
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
270 })
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
271 })
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
272 );
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
273 // We didn't override the first entry we made
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
274 assert!(tree.get_node(HgPath::new(b"we")).is_some(),);
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
275 // Inserting the same key again
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
276 assert_eq!(
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
277 tree.insert_node(HgPath::new(b"foo/bar"), entry),
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
278 Some(Node {
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
279 kind: NodeKind::File(File {
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
280 was_directory: None,
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
281 entry
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
282 }),
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
283 })
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
284 );
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
285 // Inserting the two levels deep
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
286 assert_eq!(tree.insert_node(HgPath::new(b"foo/bar/baz"), entry), None);
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
287 // Getting a file "inside a file" should return `None`
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
288 assert_eq!(tree.get_node(HgPath::new(b"foo/bar/baz/bap"),), None);
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
289
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
290 assert_eq!(
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
291 tree.insert_node(HgPath::new(b"wasdir/subfile"), entry),
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
292 None,
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
293 );
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
294 let removed_entry = DirstateEntry {
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
295 state: EntryState::Removed,
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
296 mode: 0,
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
297 mtime: 0,
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
298 size: 0,
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
299 };
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
300 assert!(tree
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
301 .insert_node(HgPath::new(b"wasdir"), removed_entry)
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
302 .is_some());
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
303
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
304 assert_eq!(
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
305 tree.get_node(HgPath::new(b"wasdir")),
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
306 Some(&Node {
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
307 kind: NodeKind::File(File {
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
308 was_directory: Some(Box::new(Directory {
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
309 was_file: None,
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
310 children: [(
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
311 b"subfile".to_vec(),
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
312 Node {
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
313 kind: NodeKind::File(File {
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
314 was_directory: None,
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
315 entry,
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
316 })
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
317 }
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
318 )]
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
319 .to_vec()
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
320 .into_iter()
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
321 .collect()
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
322 })),
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
323 entry: removed_entry
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
324 })
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
325 })
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
326 );
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
327
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
328 assert!(tree.get(HgPath::new(b"wasdir/subfile")).is_some())
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
329 }
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
330
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
331 #[test]
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
332 fn test_insert_removed() {
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
333 let mut tree = Tree::new();
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
334 let entry = DirstateEntry {
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
335 state: EntryState::Merged,
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
336 mode: 1,
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
337 mtime: 2,
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
338 size: 3,
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
339 };
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
340 let removed_entry = DirstateEntry {
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
341 state: EntryState::Removed,
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
342 mode: 10,
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
343 mtime: 20,
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
344 size: 30,
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
345 };
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
346 assert_eq!(tree.insert_node(HgPath::new(b"foo"), entry), None);
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
347 assert_eq!(tree.insert_node(HgPath::new(b"foo/a"), removed_entry), None);
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
348 // The insert should not turn `foo` into a directory as `foo` is not
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
349 // `Removed`.
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
350 match tree.get_node(HgPath::new(b"foo")).unwrap().kind {
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
351 NodeKind::Directory(_) => panic!("should be a file"),
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
352 NodeKind::File(_) => {}
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
353 }
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
354
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
355 let mut tree = Tree::new();
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
356 let entry = DirstateEntry {
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
357 state: EntryState::Merged,
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
358 mode: 1,
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
359 mtime: 2,
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
360 size: 3,
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
361 };
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
362 let removed_entry = DirstateEntry {
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
363 state: EntryState::Removed,
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
364 mode: 10,
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
365 mtime: 20,
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
366 size: 30,
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
367 };
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
368 // The insert *should* turn `foo` into a directory as it is `Removed`.
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
369 assert_eq!(tree.insert_node(HgPath::new(b"foo"), removed_entry), None);
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
370 assert_eq!(tree.insert_node(HgPath::new(b"foo/a"), entry), None);
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
371 match tree.get_node(HgPath::new(b"foo")).unwrap().kind {
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
372 NodeKind::Directory(_) => {}
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
373 NodeKind::File(_) => panic!("should be a directory"),
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
374 }
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
375 }
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
376
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
377 #[test]
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
378 fn test_get() {
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
379 let mut tree = Tree::new();
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
380 let entry = DirstateEntry {
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
381 state: EntryState::Merged,
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
382 mode: 1,
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
383 mtime: 2,
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
384 size: 3,
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
385 };
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
386 assert_eq!(tree.insert_node(HgPath::new(b"a/b/c"), entry), None);
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
387 assert_eq!(tree.files_count, 1);
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
388 assert_eq!(tree.get(HgPath::new(b"a/b/c")), Some(&entry));
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
389 assert_eq!(tree.get(HgPath::new(b"a/b")), None);
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
390 assert_eq!(tree.get(HgPath::new(b"a")), None);
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
391 assert_eq!(tree.get(HgPath::new(b"a/b/c/d")), None);
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
392 let entry2 = DirstateEntry {
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
393 state: EntryState::Removed,
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
394 mode: 0,
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
395 mtime: 5,
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
396 size: 1,
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
397 };
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
398 // was_directory
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
399 assert_eq!(tree.insert(HgPath::new(b"a/b"), entry2), None);
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
400 assert_eq!(tree.files_count, 2);
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
401 assert_eq!(tree.get(HgPath::new(b"a/b")), Some(&entry2));
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
402 assert_eq!(tree.get(HgPath::new(b"a/b/c")), Some(&entry));
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
403
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
404 let mut tree = Tree::new();
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
405
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
406 // was_file
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
407 assert_eq!(tree.insert_node(HgPath::new(b"a"), entry), None);
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
408 assert_eq!(tree.files_count, 1);
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
409 assert_eq!(tree.insert_node(HgPath::new(b"a/b"), entry2), None);
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
410 assert_eq!(tree.files_count, 2);
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
411 assert_eq!(tree.get(HgPath::new(b"a/b")), Some(&entry2));
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
412 }
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
413
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
414 #[test]
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
415 fn test_get_mut() {
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
416 let mut tree = Tree::new();
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
417 let mut entry = DirstateEntry {
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
418 state: EntryState::Merged,
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
419 mode: 1,
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
420 mtime: 2,
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
421 size: 3,
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
422 };
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
423 assert_eq!(tree.insert_node(HgPath::new(b"a/b/c"), entry), None);
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
424 assert_eq!(tree.files_count, 1);
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
425 assert_eq!(tree.get_mut(HgPath::new(b"a/b/c")), Some(&mut entry));
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
426 assert_eq!(tree.get_mut(HgPath::new(b"a/b")), None);
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
427 assert_eq!(tree.get_mut(HgPath::new(b"a")), None);
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
428 assert_eq!(tree.get_mut(HgPath::new(b"a/b/c/d")), None);
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
429 let mut entry2 = DirstateEntry {
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
430 state: EntryState::Removed,
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
431 mode: 0,
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
432 mtime: 5,
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
433 size: 1,
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
434 };
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
435 // was_directory
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
436 assert_eq!(tree.insert(HgPath::new(b"a/b"), entry2), None);
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
437 assert_eq!(tree.files_count, 2);
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
438 assert_eq!(tree.get_mut(HgPath::new(b"a/b")), Some(&mut entry2));
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
439 assert_eq!(tree.get_mut(HgPath::new(b"a/b/c")), Some(&mut entry));
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
440
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
441 let mut tree = Tree::new();
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
442
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
443 // was_file
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
444 assert_eq!(tree.insert_node(HgPath::new(b"a"), entry), None);
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
445 assert_eq!(tree.files_count, 1);
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
446 assert_eq!(tree.insert_node(HgPath::new(b"a/b"), entry2), None);
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
447 assert_eq!(tree.files_count, 2);
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
448 assert_eq!(tree.get_mut(HgPath::new(b"a/b")), Some(&mut entry2));
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
449 }
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
450
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
451 #[test]
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
452 fn test_remove() {
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
453 let mut tree = Tree::new();
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
454 assert_eq!(tree.files_count, 0);
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
455 assert_eq!(tree.remove(HgPath::new(b"foo")), None);
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
456 assert_eq!(tree.files_count, 0);
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
457
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
458 let entry = DirstateEntry {
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
459 state: EntryState::Normal,
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
460 mode: 0,
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
461 mtime: 0,
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
462 size: 0,
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
463 };
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
464 assert_eq!(tree.insert_node(HgPath::new(b"a/b/c"), entry), None);
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
465 assert_eq!(tree.files_count, 1);
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
466
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
467 assert_eq!(tree.remove(HgPath::new(b"a/b/c")), Some(entry));
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
468 assert_eq!(tree.files_count, 0);
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
469
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
470 assert_eq!(tree.insert_node(HgPath::new(b"a/b/x"), entry), None);
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
471 assert_eq!(tree.insert_node(HgPath::new(b"a/b/y"), entry), None);
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
472 assert_eq!(tree.insert_node(HgPath::new(b"a/b/z"), entry), None);
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
473 assert_eq!(tree.insert_node(HgPath::new(b"x"), entry), None);
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
474 assert_eq!(tree.insert_node(HgPath::new(b"y"), entry), None);
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
475 assert_eq!(tree.files_count, 5);
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
476
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
477 assert_eq!(tree.remove(HgPath::new(b"a/b/x")), Some(entry));
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
478 assert_eq!(tree.files_count, 4);
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
479 assert_eq!(tree.remove(HgPath::new(b"a/b/x")), None);
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
480 assert_eq!(tree.files_count, 4);
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
481 assert_eq!(tree.remove(HgPath::new(b"a/b/y")), Some(entry));
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
482 assert_eq!(tree.files_count, 3);
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
483 assert_eq!(tree.remove(HgPath::new(b"a/b/z")), Some(entry));
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
484 assert_eq!(tree.files_count, 2);
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
485
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
486 assert_eq!(tree.remove(HgPath::new(b"x")), Some(entry));
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
487 assert_eq!(tree.files_count, 1);
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
488 assert_eq!(tree.remove(HgPath::new(b"y")), Some(entry));
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
489 assert_eq!(tree.files_count, 0);
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
490
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
491 // `a` should have been cleaned up, no more files anywhere in its
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
492 // descendents
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
493 assert_eq!(tree.get_node(HgPath::new(b"a")), None);
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
494 assert_eq!(tree.root.children().unwrap().len(), 0);
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
495
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
496 let removed_entry = DirstateEntry {
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
497 state: EntryState::Removed,
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
498 ..entry
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
499 };
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
500 assert_eq!(tree.insert(HgPath::new(b"a"), removed_entry), None);
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
501 assert_eq!(tree.insert_node(HgPath::new(b"a/b/x"), entry), None);
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
502 assert_eq!(tree.files_count, 2);
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
503 dbg!(&tree);
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
504 assert_eq!(tree.remove(HgPath::new(b"a")), Some(removed_entry));
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
505 assert_eq!(tree.files_count, 1);
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
506 dbg!(&tree);
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
507 assert_eq!(tree.remove(HgPath::new(b"a/b/x")), Some(entry));
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
508 assert_eq!(tree.files_count, 0);
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
509
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
510 // The entire tree should have been cleaned up, no more files anywhere
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
511 // in its descendents
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
512 assert_eq!(tree.root.children().unwrap().len(), 0);
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
513
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
514 let removed_entry = DirstateEntry {
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
515 state: EntryState::Removed,
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
516 ..entry
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
517 };
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
518 assert_eq!(tree.insert(HgPath::new(b"a"), entry), None);
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
519 assert_eq!(tree.insert_node(HgPath::new(b"a/b/x"), removed_entry), None);
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
520 assert_eq!(tree.files_count, 2);
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
521 dbg!(&tree);
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
522 assert_eq!(tree.remove(HgPath::new(b"a")), Some(entry));
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
523 assert_eq!(tree.files_count, 1);
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
524 dbg!(&tree);
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
525 assert_eq!(tree.remove(HgPath::new(b"a/b/x")), Some(removed_entry));
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
526 assert_eq!(tree.files_count, 0);
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
527
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
528 dbg!(&tree);
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
529 // The entire tree should have been cleaned up, no more files anywhere
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
530 // in its descendents
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
531 assert_eq!(tree.root.children().unwrap().len(), 0);
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
532
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
533 assert_eq!(tree.insert(HgPath::new(b"d"), entry), None);
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
534 assert_eq!(tree.insert(HgPath::new(b"d/d/d"), entry), None);
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
535 assert_eq!(tree.files_count, 2);
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
536
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
537 // Deleting the nested file should not delete the top directory as it
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
538 // used to be a file
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
539 assert_eq!(tree.remove(HgPath::new(b"d/d/d")), Some(entry));
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
540 assert_eq!(tree.files_count, 1);
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
541 assert!(tree.get_node(HgPath::new(b"d")).is_some());
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
542 assert!(tree.remove(HgPath::new(b"d")).is_some());
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
543 assert_eq!(tree.files_count, 0);
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
544
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
545 // Deleting the nested file should not delete the top file (other way
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
546 // around from the last case)
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
547 assert_eq!(tree.insert(HgPath::new(b"a/a"), entry), None);
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
548 assert_eq!(tree.files_count, 1);
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
549 assert_eq!(tree.insert(HgPath::new(b"a"), entry), None);
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
550 assert_eq!(tree.files_count, 2);
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
551 dbg!(&tree);
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
552 assert_eq!(tree.remove(HgPath::new(b"a/a")), Some(entry));
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
553 assert_eq!(tree.files_count, 1);
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
554 dbg!(&tree);
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
555 assert!(tree.get_node(HgPath::new(b"a")).is_some());
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
556 assert!(tree.get_node(HgPath::new(b"a/a")).is_none());
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
557 }
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
558
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
559 #[test]
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
560 fn test_was_directory() {
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
561 let mut tree = Tree::new();
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
562
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
563 let entry = DirstateEntry {
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
564 state: EntryState::Removed,
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
565 mode: 0,
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
566 mtime: 0,
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
567 size: 0,
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
568 };
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
569 assert_eq!(tree.insert_node(HgPath::new(b"a/b/c"), entry), None);
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
570 assert_eq!(tree.files_count, 1);
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
571
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
572 assert!(tree.insert_node(HgPath::new(b"a"), entry).is_some());
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
573 let new_a = tree.root.children().unwrap().get(&b"a".to_vec()).unwrap();
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
574
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
575 match &new_a.kind {
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
576 NodeKind::Directory(_) => panic!(),
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
577 NodeKind::File(f) => {
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
578 let dir = f.was_directory.clone().unwrap();
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
579 let c = dir
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
580 .children
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
581 .get(&b"b".to_vec())
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
582 .unwrap()
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
583 .children()
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
584 .unwrap()
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
585 .get(&b"c".to_vec())
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
586 .unwrap();
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
587
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
588 assert_eq!(
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
589 match &c.kind {
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
590 NodeKind::Directory(_) => panic!(),
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
591 NodeKind::File(f) => f.entry,
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
592 },
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
593 entry
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
594 );
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
595 }
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
596 }
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
597 assert_eq!(tree.files_count, 2);
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
598 dbg!(&tree);
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
599 assert_eq!(tree.remove(HgPath::new(b"a/b/c")), Some(entry));
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
600 assert_eq!(tree.files_count, 1);
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
601 dbg!(&tree);
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
602 let a = tree.get_node(HgPath::new(b"a")).unwrap();
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
603 match &a.kind {
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
604 NodeKind::Directory(_) => panic!(),
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
605 NodeKind::File(f) => {
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
606 // Directory in `was_directory` was emptied, should be removed
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
607 assert_eq!(f.was_directory, None);
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
608 }
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
609 }
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
610 }
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
611 #[test]
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
612 fn test_extend() {
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
613 let insertions = [
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
614 (
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
615 HgPathBuf::from_bytes(b"d"),
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
616 DirstateEntry {
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
617 state: EntryState::Added,
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
618 mode: 0,
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
619 mtime: -1,
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
620 size: -1,
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
621 },
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
622 ),
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
623 (
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
624 HgPathBuf::from_bytes(b"b"),
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
625 DirstateEntry {
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
626 state: EntryState::Normal,
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
627 mode: 33188,
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
628 mtime: 1599647984,
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
629 size: 2,
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
630 },
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
631 ),
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
632 (
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
633 HgPathBuf::from_bytes(b"a/a"),
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
634 DirstateEntry {
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
635 state: EntryState::Normal,
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
636 mode: 33188,
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
637 mtime: 1599647984,
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
638 size: 2,
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
639 },
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
640 ),
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
641 (
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
642 HgPathBuf::from_bytes(b"d/d/d"),
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
643 DirstateEntry {
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
644 state: EntryState::Removed,
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
645 mode: 0,
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
646 mtime: 0,
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
647 size: 0,
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
648 },
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
649 ),
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
650 ]
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
651 .to_vec();
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
652 let mut tree = Tree::new();
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
653
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
654 tree.extend(insertions.clone().into_iter());
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
655
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
656 for (path, _) in &insertions {
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
657 assert!(tree.contains_key(path), true);
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
658 }
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
659 assert_eq!(tree.files_count, 4);
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
660 }
b51167d70f5a rust: add `dirstate_tree` module
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
661 }