Show More
@@ -13,8 +13,8 b' use crate::{' | |||||
13 | files::normalize_case, |
|
13 | files::normalize_case, | |
14 | hg_path::{HgPath, HgPathBuf}, |
|
14 | hg_path::{HgPath, HgPathBuf}, | |
15 | }, |
|
15 | }, | |
16 |
CopyMap, DirsMultiset, DirstateEntry, DirstateError, DirstateMapError, |
|
16 | CopyMap, DirsMultiset, DirstateEntry, DirstateError, DirstateMapError, | |
17 | DirstateParseError, FastHashMap, StateMap, |
|
17 | DirstateParents, DirstateParseError, FastHashMap, StateMap, | |
18 | }; |
|
18 | }; | |
19 | use core::borrow::Borrow; |
|
19 | use core::borrow::Borrow; | |
20 | use micro_timer::timed; |
|
20 | use micro_timer::timed; | |
@@ -51,7 +51,9 b' impl Deref for DirstateMap {' | |||||
51 | } |
|
51 | } | |
52 |
|
52 | |||
53 | impl FromIterator<(HgPathBuf, DirstateEntry)> for DirstateMap { |
|
53 | impl FromIterator<(HgPathBuf, DirstateEntry)> for DirstateMap { | |
54 |
fn from_iter<I: IntoIterator<Item = (HgPathBuf, DirstateEntry)>>( |
|
54 | fn from_iter<I: IntoIterator<Item = (HgPathBuf, DirstateEntry)>>( | |
|
55 | iter: I, | |||
|
56 | ) -> Self { | |||
55 | Self { |
|
57 | Self { | |
56 | state_map: iter.into_iter().collect(), |
|
58 | state_map: iter.into_iter().collect(), | |
57 | ..Self::default() |
|
59 | ..Self::default() | |
@@ -83,7 +85,8 b' impl DirstateMap {' | |||||
83 | old_state: EntryState, |
|
85 | old_state: EntryState, | |
84 | entry: DirstateEntry, |
|
86 | entry: DirstateEntry, | |
85 | ) -> Result<(), DirstateMapError> { |
|
87 | ) -> Result<(), DirstateMapError> { | |
86 |
if old_state == EntryState::Unknown || old_state == EntryState::Removed |
|
88 | if old_state == EntryState::Unknown || old_state == EntryState::Removed | |
|
89 | { | |||
87 | if let Some(ref mut dirs) = self.dirs { |
|
90 | if let Some(ref mut dirs) = self.dirs { | |
88 | dirs.add_path(filename)?; |
|
91 | dirs.add_path(filename)?; | |
89 | } |
|
92 | } | |
@@ -120,7 +123,8 b' impl DirstateMap {' | |||||
120 | old_state: EntryState, |
|
123 | old_state: EntryState, | |
121 | size: i32, |
|
124 | size: i32, | |
122 | ) -> Result<(), DirstateMapError> { |
|
125 | ) -> Result<(), DirstateMapError> { | |
123 |
if old_state != EntryState::Unknown && old_state != EntryState::Removed |
|
126 | if old_state != EntryState::Unknown && old_state != EntryState::Removed | |
|
127 | { | |||
124 | if let Some(ref mut dirs) = self.dirs { |
|
128 | if let Some(ref mut dirs) = self.dirs { | |
125 | dirs.delete_path(filename)?; |
|
129 | dirs.delete_path(filename)?; | |
126 | } |
|
130 | } | |
@@ -178,13 +182,18 b' impl DirstateMap {' | |||||
178 | Ok(exists) |
|
182 | Ok(exists) | |
179 | } |
|
183 | } | |
180 |
|
184 | |||
181 | pub fn clear_ambiguous_times(&mut self, filenames: Vec<HgPathBuf>, now: i32) { |
|
185 | pub fn clear_ambiguous_times( | |
|
186 | &mut self, | |||
|
187 | filenames: Vec<HgPathBuf>, | |||
|
188 | now: i32, | |||
|
189 | ) { | |||
182 | for filename in filenames { |
|
190 | for filename in filenames { | |
183 | let mut changed = false; |
|
191 | let mut changed = false; | |
184 | self.state_map |
|
192 | self.state_map | |
185 | .entry(filename.to_owned()) |
|
193 | .entry(filename.to_owned()) | |
186 | .and_modify(|entry| { |
|
194 | .and_modify(|entry| { | |
187 |
if entry.state == EntryState::Normal && entry.mtime == now |
|
195 | if entry.state == EntryState::Normal && entry.mtime == now | |
|
196 | { | |||
188 | changed = true; |
|
197 | changed = true; | |
189 | *entry = DirstateEntry { |
|
198 | *entry = DirstateEntry { | |
190 | mtime: MTIME_UNSET, |
|
199 | mtime: MTIME_UNSET, | |
@@ -200,12 +209,18 b' impl DirstateMap {' | |||||
200 | } |
|
209 | } | |
201 | } |
|
210 | } | |
202 |
|
211 | |||
203 |
pub fn non_normal_entries_remove( |
|
212 | pub fn non_normal_entries_remove( | |
|
213 | &mut self, | |||
|
214 | key: impl AsRef<HgPath>, | |||
|
215 | ) -> bool { | |||
204 | self.get_non_normal_other_parent_entries() |
|
216 | self.get_non_normal_other_parent_entries() | |
205 | .0 |
|
217 | .0 | |
206 | .remove(key.as_ref()) |
|
218 | .remove(key.as_ref()) | |
207 | } |
|
219 | } | |
208 | pub fn non_normal_entries_union(&mut self, other: HashSet<HgPathBuf>) -> Vec<HgPathBuf> { |
|
220 | pub fn non_normal_entries_union( | |
|
221 | &mut self, | |||
|
222 | other: HashSet<HgPathBuf>, | |||
|
223 | ) -> Vec<HgPathBuf> { | |||
209 | self.get_non_normal_other_parent_entries() |
|
224 | self.get_non_normal_other_parent_entries() | |
210 | .0 |
|
225 | .0 | |
211 | .union(&other) |
|
226 | .union(&other) | |
@@ -243,7 +258,10 b' impl DirstateMap {' | |||||
243 | } |
|
258 | } | |
244 |
|
259 | |||
245 | pub fn set_non_normal_other_parent_entries(&mut self, force: bool) { |
|
260 | pub fn set_non_normal_other_parent_entries(&mut self, force: bool) { | |
246 | if !force && self.non_normal_set.is_some() && self.other_parent_set.is_some() { |
|
261 | if !force | |
|
262 | && self.non_normal_set.is_some() | |||
|
263 | && self.other_parent_set.is_some() | |||
|
264 | { | |||
247 | return; |
|
265 | return; | |
248 | } |
|
266 | } | |
249 | let mut non_normal = HashSet::new(); |
|
267 | let mut non_normal = HashSet::new(); | |
@@ -259,7 +277,8 b' impl DirstateMap {' | |||||
259 | if *state != EntryState::Normal || *mtime == MTIME_UNSET { |
|
277 | if *state != EntryState::Normal || *mtime == MTIME_UNSET { | |
260 | non_normal.insert(filename.to_owned()); |
|
278 | non_normal.insert(filename.to_owned()); | |
261 | } |
|
279 | } | |
262 |
if *state == EntryState::Normal && *size == SIZE_FROM_OTHER_PARENT |
|
280 | if *state == EntryState::Normal && *size == SIZE_FROM_OTHER_PARENT | |
|
281 | { | |||
263 | other_parent.insert(filename.to_owned()); |
|
282 | other_parent.insert(filename.to_owned()); | |
264 | } |
|
283 | } | |
265 | } |
|
284 | } | |
@@ -273,7 +292,8 b' impl DirstateMap {' | |||||
273 | /// good idea. |
|
292 | /// good idea. | |
274 | pub fn set_all_dirs(&mut self) -> Result<(), DirstateMapError> { |
|
293 | pub fn set_all_dirs(&mut self) -> Result<(), DirstateMapError> { | |
275 | if self.all_dirs.is_none() { |
|
294 | if self.all_dirs.is_none() { | |
276 | self.all_dirs = Some(DirsMultiset::from_dirstate(&self.state_map, None)?); |
|
295 | self.all_dirs = | |
|
296 | Some(DirsMultiset::from_dirstate(&self.state_map, None)?); | |||
277 | } |
|
297 | } | |
278 | Ok(()) |
|
298 | Ok(()) | |
279 | } |
|
299 | } | |
@@ -288,17 +308,26 b' impl DirstateMap {' | |||||
288 | Ok(()) |
|
308 | Ok(()) | |
289 | } |
|
309 | } | |
290 |
|
310 | |||
291 | pub fn has_tracked_dir(&mut self, directory: &HgPath) -> Result<bool, DirstateMapError> { |
|
311 | pub fn has_tracked_dir( | |
|
312 | &mut self, | |||
|
313 | directory: &HgPath, | |||
|
314 | ) -> Result<bool, DirstateMapError> { | |||
292 | self.set_dirs()?; |
|
315 | self.set_dirs()?; | |
293 | Ok(self.dirs.as_ref().unwrap().contains(directory)) |
|
316 | Ok(self.dirs.as_ref().unwrap().contains(directory)) | |
294 | } |
|
317 | } | |
295 |
|
318 | |||
296 | pub fn has_dir(&mut self, directory: &HgPath) -> Result<bool, DirstateMapError> { |
|
319 | pub fn has_dir( | |
|
320 | &mut self, | |||
|
321 | directory: &HgPath, | |||
|
322 | ) -> Result<bool, DirstateMapError> { | |||
297 | self.set_all_dirs()?; |
|
323 | self.set_all_dirs()?; | |
298 | Ok(self.all_dirs.as_ref().unwrap().contains(directory)) |
|
324 | Ok(self.all_dirs.as_ref().unwrap().contains(directory)) | |
299 | } |
|
325 | } | |
300 |
|
326 | |||
301 | pub fn parents(&mut self, file_contents: &[u8]) -> Result<&DirstateParents, DirstateError> { |
|
327 | pub fn parents( | |
|
328 | &mut self, | |||
|
329 | file_contents: &[u8], | |||
|
330 | ) -> Result<&DirstateParents, DirstateError> { | |||
302 | if let Some(ref parents) = self.parents { |
|
331 | if let Some(ref parents) = self.parents { | |
303 | return Ok(parents); |
|
332 | return Ok(parents); | |
304 | } |
|
333 | } | |
@@ -329,7 +358,10 b' impl DirstateMap {' | |||||
329 | } |
|
358 | } | |
330 |
|
359 | |||
331 | #[timed] |
|
360 | #[timed] | |
332 | pub fn read(&mut self, file_contents: &[u8]) -> Result<Option<DirstateParents>, DirstateError> { |
|
361 | pub fn read( | |
|
362 | &mut self, | |||
|
363 | file_contents: &[u8], | |||
|
364 | ) -> Result<Option<DirstateParents>, DirstateError> { | |||
333 | if file_contents.is_empty() { |
|
365 | if file_contents.is_empty() { | |
334 | return Ok(None); |
|
366 | return Ok(None); | |
335 | } |
|
367 | } | |
@@ -358,7 +390,8 b' impl DirstateMap {' | |||||
358 | parents: DirstateParents, |
|
390 | parents: DirstateParents, | |
359 | now: Duration, |
|
391 | now: Duration, | |
360 | ) -> Result<Vec<u8>, DirstateError> { |
|
392 | ) -> Result<Vec<u8>, DirstateError> { | |
361 | let packed = pack_dirstate(&mut self.state_map, &self.copy_map, parents, now)?; |
|
393 | let packed = | |
|
394 | pack_dirstate(&mut self.state_map, &self.copy_map, parents, now)?; | |||
362 |
|
395 | |||
363 | self.dirty_parents = false; |
|
396 | self.dirty_parents = false; | |
364 |
|
397 | |||
@@ -371,9 +404,11 b' impl DirstateMap {' | |||||
371 | return file_fold_map; |
|
404 | return file_fold_map; | |
372 | } |
|
405 | } | |
373 | let mut new_file_fold_map = FileFoldMap::default(); |
|
406 | let mut new_file_fold_map = FileFoldMap::default(); | |
374 |
for (filename, DirstateEntry { state, .. }) in self.state_map.borrow() |
|
407 | for (filename, DirstateEntry { state, .. }) in self.state_map.borrow() | |
|
408 | { | |||
375 | if *state == EntryState::Removed { |
|
409 | if *state == EntryState::Removed { | |
376 | new_file_fold_map.insert(normalize_case(filename), filename.to_owned()); |
|
410 | new_file_fold_map | |
|
411 | .insert(normalize_case(filename), filename.to_owned()); | |||
377 | } |
|
412 | } | |
378 | } |
|
413 | } | |
379 | self.file_fold_map = Some(new_file_fold_map); |
|
414 | self.file_fold_map = Some(new_file_fold_map); | |
@@ -462,6 +497,9 b' mod tests {' | |||||
462 | other_parent.insert(HgPathBuf::from_bytes(b"f4")); |
|
497 | other_parent.insert(HgPathBuf::from_bytes(b"f4")); | |
463 | let entries = map.get_non_normal_other_parent_entries(); |
|
498 | let entries = map.get_non_normal_other_parent_entries(); | |
464 |
|
499 | |||
465 | assert_eq!((&mut non_normal, &mut other_parent), (entries.0, entries.1)); |
|
500 | assert_eq!( | |
|
501 | (&mut non_normal, &mut other_parent), | |||
|
502 | (entries.0, entries.1) | |||
|
503 | ); | |||
466 | } |
|
504 | } | |
467 | } |
|
505 | } |
@@ -17,7 +17,9 b' use std::iter::{FromIterator, FusedItera' | |||||
17 | use std::path::PathBuf; |
|
17 | use std::path::PathBuf; | |
18 |
|
18 | |||
19 | impl FromIterator<(HgPathBuf, DirstateEntry)> for Tree { |
|
19 | impl FromIterator<(HgPathBuf, DirstateEntry)> for Tree { | |
20 |
fn from_iter<T: IntoIterator<Item = (HgPathBuf, DirstateEntry)>>( |
|
20 | fn from_iter<T: IntoIterator<Item = (HgPathBuf, DirstateEntry)>>( | |
|
21 | iter: T, | |||
|
22 | ) -> Self { | |||
21 | let mut tree = Self::new(); |
|
23 | let mut tree = Self::new(); | |
22 | for (path, entry) in iter { |
|
24 | for (path, entry) in iter { | |
23 | tree.insert(path, entry); |
|
25 | tree.insert(path, entry); | |
@@ -48,16 +50,30 b" impl<'a> Iterator for Iter<'a> {" | |||||
48 | while let Some((base_path, node)) = self.to_visit.pop_front() { |
|
50 | while let Some((base_path, node)) = self.to_visit.pop_front() { | |
49 | match &node.kind { |
|
51 | match &node.kind { | |
50 | NodeKind::Directory(dir) => { |
|
52 | NodeKind::Directory(dir) => { | |
51 |
add_children_to_visit( |
|
53 | add_children_to_visit( | |
|
54 | &mut self.to_visit, | |||
|
55 | &base_path, | |||
|
56 | &dir, | |||
|
57 | ); | |||
52 | if let Some(file) = &dir.was_file { |
|
58 | if let Some(file) = &dir.was_file { | |
53 |
return Some(( |
|
59 | return Some(( | |
|
60 | HgPathBuf::from_bytes(&base_path), | |||
|
61 | file.entry, | |||
|
62 | )); | |||
54 | } |
|
63 | } | |
55 | } |
|
64 | } | |
56 | NodeKind::File(file) => { |
|
65 | NodeKind::File(file) => { | |
57 | if let Some(dir) = &file.was_directory { |
|
66 | if let Some(dir) = &file.was_directory { | |
58 |
add_children_to_visit( |
|
67 | add_children_to_visit( | |
|
68 | &mut self.to_visit, | |||
|
69 | &base_path, | |||
|
70 | &dir, | |||
|
71 | ); | |||
59 | } |
|
72 | } | |
60 | return Some((HgPathBuf::from_bytes(&base_path), file.entry)); |
|
73 | return Some(( | |
|
74 | HgPathBuf::from_bytes(&base_path), | |||
|
75 | file.entry, | |||
|
76 | )); | |||
61 | } |
|
77 | } | |
62 | } |
|
78 | } | |
63 | } |
|
79 | } | |
@@ -107,10 +123,16 b" impl<'a> FsIter<'a> {" | |||||
107 | /// ``` |
|
123 | /// ``` | |
108 | /// We need to dispatch the new symlink as `Unknown` and all the |
|
124 | /// We need to dispatch the new symlink as `Unknown` and all the | |
109 | /// descendents of the directory it replace as `Deleted`. |
|
125 | /// descendents of the directory it replace as `Deleted`. | |
110 | fn dispatch_symlinked_directory(&mut self, path: impl AsRef<HgPath>, node: &Node) { |
|
126 | fn dispatch_symlinked_directory( | |
|
127 | &mut self, | |||
|
128 | path: impl AsRef<HgPath>, | |||
|
129 | node: &Node, | |||
|
130 | ) { | |||
111 | let path = path.as_ref(); |
|
131 | let path = path.as_ref(); | |
112 | self.shortcuts |
|
132 | self.shortcuts.push_back(( | |
113 | .push_back((path.to_owned(), StatusShortcut::Dispatch(Dispatch::Unknown))); |
|
133 | path.to_owned(), | |
|
134 | StatusShortcut::Dispatch(Dispatch::Unknown), | |||
|
135 | )); | |||
114 | for (file, _) in node.iter() { |
|
136 | for (file, _) in node.iter() { | |
115 | self.shortcuts.push_back(( |
|
137 | self.shortcuts.push_back(( | |
116 | path.join(&file), |
|
138 | path.join(&file), | |
@@ -171,10 +193,17 b" impl<'a> Iterator for FsIter<'a> {" | |||||
171 | if self.directory_became_symlink(canonical_path) { |
|
193 | if self.directory_became_symlink(canonical_path) { | |
172 | // Potential security issue, don't do a normal |
|
194 | // Potential security issue, don't do a normal | |
173 | // traversal, force the results. |
|
195 | // traversal, force the results. | |
174 |
self.dispatch_symlinked_directory( |
|
196 | self.dispatch_symlinked_directory( | |
|
197 | canonical_path, | |||
|
198 | &node, | |||
|
199 | ); | |||
175 | continue; |
|
200 | continue; | |
176 | } |
|
201 | } | |
177 |
add_children_to_visit( |
|
202 | add_children_to_visit( | |
|
203 | &mut self.to_visit, | |||
|
204 | &base_path, | |||
|
205 | &dir, | |||
|
206 | ); | |||
178 | if let Some(file) = &dir.was_file { |
|
207 | if let Some(file) = &dir.was_file { | |
179 | return Some(( |
|
208 | return Some(( | |
180 | HgPathBuf::from_bytes(&base_path), |
|
209 | HgPathBuf::from_bytes(&base_path), | |
@@ -184,7 +213,11 b" impl<'a> Iterator for FsIter<'a> {" | |||||
184 | } |
|
213 | } | |
185 | NodeKind::File(file) => { |
|
214 | NodeKind::File(file) => { | |
186 | if let Some(dir) = &file.was_directory { |
|
215 | if let Some(dir) = &file.was_directory { | |
187 |
add_children_to_visit( |
|
216 | add_children_to_visit( | |
|
217 | &mut self.to_visit, | |||
|
218 | &base_path, | |||
|
219 | &dir, | |||
|
220 | ); | |||
188 | } |
|
221 | } | |
189 | return Some(( |
|
222 | return Some(( | |
190 | HgPathBuf::from_bytes(&base_path), |
|
223 | HgPathBuf::from_bytes(&base_path), | |
@@ -286,7 +319,8 b' mod tests {' | |||||
286 |
|
319 | |||
287 | assert_eq!(tree.len(), 4); |
|
320 | assert_eq!(tree.len(), 4); | |
288 |
|
321 | |||
289 | let results: HashSet<_> = tree.iter().map(|(c, _)| c.to_owned()).collect(); |
|
322 | let results: HashSet<_> = | |
|
323 | tree.iter().map(|(c, _)| c.to_owned()).collect(); | |||
290 | dbg!(&results); |
|
324 | dbg!(&results); | |
291 | assert!(results.contains(HgPath::new(b"foo2"))); |
|
325 | assert!(results.contains(HgPath::new(b"foo2"))); | |
292 | assert!(results.contains(HgPath::new(b"foo/bar"))); |
|
326 | assert!(results.contains(HgPath::new(b"foo/bar"))); |
@@ -48,7 +48,11 b' impl Default for NodeKind {' | |||||
48 | } |
|
48 | } | |
49 |
|
49 | |||
50 | impl Node { |
|
50 | impl Node { | |
51 | pub fn insert(&mut self, path: &[u8], new_entry: DirstateEntry) -> InsertResult { |
|
51 | pub fn insert( | |
|
52 | &mut self, | |||
|
53 | path: &[u8], | |||
|
54 | new_entry: DirstateEntry, | |||
|
55 | ) -> InsertResult { | |||
52 | let mut split = path.splitn(2, |&c| c == b'/'); |
|
56 | let mut split = path.splitn(2, |&c| c == b'/'); | |
53 | let head = split.next().unwrap_or(b""); |
|
57 | let head = split.next().unwrap_or(b""); | |
54 | let tail = split.next().unwrap_or(b""); |
|
58 | let tail = split.next().unwrap_or(b""); | |
@@ -76,16 +80,24 b' impl Node {' | |||||
76 | children: Default::default(), |
|
80 | children: Default::default(), | |
77 | }) |
|
81 | }) | |
78 | } |
|
82 | } | |
79 | _ => return Node::insert_in_file(file, new_entry, head, tail), |
|
83 | _ => { | |
|
84 | return Node::insert_in_file( | |||
|
85 | file, new_entry, head, tail, | |||
|
86 | ) | |||
|
87 | } | |||
80 | } |
|
88 | } | |
81 | } |
|
89 | } | |
82 | } |
|
90 | } | |
83 |
|
91 | |||
84 | match &mut self.kind { |
|
92 | match &mut self.kind { | |
85 | NodeKind::Directory(directory) => { |
|
93 | NodeKind::Directory(directory) => { | |
86 |
return Node::insert_in_directory( |
|
94 | return Node::insert_in_directory( | |
|
95 | directory, new_entry, head, tail, | |||
|
96 | ); | |||
87 | } |
|
97 | } | |
88 | NodeKind::File(_) => unreachable!("The file case has already been handled"), |
|
98 | NodeKind::File(_) => { | |
|
99 | unreachable!("The file case has already been handled") | |||
|
100 | } | |||
89 | } |
|
101 | } | |
90 | } |
|
102 | } | |
91 |
|
103 | |||
@@ -104,7 +116,8 b' impl Node {' | |||||
104 | was_file: None, |
|
116 | was_file: None, | |
105 | children: FastHashMap::default(), |
|
117 | children: FastHashMap::default(), | |
106 | }; |
|
118 | }; | |
107 | let res = Node::insert_in_directory(&mut dir, new_entry, head, tail); |
|
119 | let res = | |
|
120 | Node::insert_in_directory(&mut dir, new_entry, head, tail); | |||
108 | file.was_directory = Some(Box::new(dir)); |
|
121 | file.was_directory = Some(Box::new(dir)); | |
109 | res |
|
122 | res | |
110 | } |
|
123 | } | |
@@ -130,7 +143,9 b' impl Node {' | |||||
130 | entry: new_entry, |
|
143 | entry: new_entry, | |
131 | }), |
|
144 | }), | |
132 | }; |
|
145 | }; | |
133 |
let old_entry = directory |
|
146 | let old_entry = directory | |
|
147 | .children | |||
|
148 | .insert(head.to_owned(), becomes_file); | |||
134 | return InsertResult { |
|
149 | return InsertResult { | |
135 | did_insert: true, |
|
150 | did_insert: true, | |
136 | old_entry, |
|
151 | old_entry, | |
@@ -192,7 +207,8 b' impl Node {' | |||||
192 | } |
|
207 | } | |
193 | NodeKind::File(f) => { |
|
208 | NodeKind::File(f) => { | |
194 | if let Some(d) = &mut f.was_directory { |
|
209 | if let Some(d) = &mut f.was_directory { | |
195 |
let RemoveResult { old_entry, .. } = |
|
210 | let RemoveResult { old_entry, .. } = | |
|
211 | Node::remove_from_directory(head, d); | |||
196 | return RemoveResult { |
|
212 | return RemoveResult { | |
197 | cleanup: false, |
|
213 | cleanup: false, | |
198 | old_entry, |
|
214 | old_entry, | |
@@ -210,7 +226,8 b' impl Node {' | |||||
210 | if res.cleanup { |
|
226 | if res.cleanup { | |
211 | d.children.remove(head); |
|
227 | d.children.remove(head); | |
212 | } |
|
228 | } | |
213 |
res.cleanup = |
|
229 | res.cleanup = | |
|
230 | d.children.len() == 0 && d.was_file.is_none(); | |||
214 | res |
|
231 | res | |
215 | } else { |
|
232 | } else { | |
216 | empty_result |
|
233 | empty_result | |
@@ -219,7 +236,8 b' impl Node {' | |||||
219 | NodeKind::File(f) => { |
|
236 | NodeKind::File(f) => { | |
220 | if let Some(d) = &mut f.was_directory { |
|
237 | if let Some(d) = &mut f.was_directory { | |
221 | if let Some(child) = d.children.get_mut(head) { |
|
238 | if let Some(child) = d.children.get_mut(head) { | |
222 |
let RemoveResult { cleanup, old_entry } = |
|
239 | let RemoveResult { cleanup, old_entry } = | |
|
240 | child.remove(tail); | |||
223 | if cleanup { |
|
241 | if cleanup { | |
224 | d.children.remove(head); |
|
242 | d.children.remove(head); | |
225 | } |
|
243 | } |
@@ -136,7 +136,11 b' impl Tree {' | |||||
136 |
|
136 | |||
137 | /// Low-level insertion method that returns the previous node (directories |
|
137 | /// Low-level insertion method that returns the previous node (directories | |
138 | /// included). |
|
138 | /// included). | |
139 | fn insert_node(&mut self, path: impl AsRef<HgPath>, kind: DirstateEntry) -> Option<Node> { |
|
139 | fn insert_node( | |
|
140 | &mut self, | |||
|
141 | path: impl AsRef<HgPath>, | |||
|
142 | kind: DirstateEntry, | |||
|
143 | ) -> Option<Node> { | |||
140 | let InsertResult { |
|
144 | let InsertResult { | |
141 | did_insert, |
|
145 | did_insert, | |
142 | old_entry, |
|
146 | old_entry, | |
@@ -154,7 +158,9 b' impl Tree {' | |||||
154 | pub fn get(&self, path: impl AsRef<HgPath>) -> Option<&DirstateEntry> { |
|
158 | pub fn get(&self, path: impl AsRef<HgPath>) -> Option<&DirstateEntry> { | |
155 | if let Some(node) = self.get_node(&path) { |
|
159 | if let Some(node) = self.get_node(&path) { | |
156 | return match &node.kind { |
|
160 | return match &node.kind { | |
157 |
NodeKind::Directory(d) => |
|
161 | NodeKind::Directory(d) => { | |
|
162 | d.was_file.as_ref().map(|f| &f.entry) | |||
|
163 | } | |||
158 | NodeKind::File(f) => Some(&f.entry), |
|
164 | NodeKind::File(f) => Some(&f.entry), | |
159 | }; |
|
165 | }; | |
160 | } |
|
166 | } | |
@@ -168,10 +174,15 b' impl Tree {' | |||||
168 |
|
174 | |||
169 | /// Returns a mutable reference to the entry corresponding to `path` if it |
|
175 | /// Returns a mutable reference to the entry corresponding to `path` if it | |
170 | /// exists. |
|
176 | /// exists. | |
171 | pub fn get_mut(&mut self, path: impl AsRef<HgPath>) -> Option<&mut DirstateEntry> { |
|
177 | pub fn get_mut( | |
|
178 | &mut self, | |||
|
179 | path: impl AsRef<HgPath>, | |||
|
180 | ) -> Option<&mut DirstateEntry> { | |||
172 | if let Some(kind) = self.root.get_mut(path.as_ref().as_bytes()) { |
|
181 | if let Some(kind) = self.root.get_mut(path.as_ref().as_bytes()) { | |
173 | return match kind { |
|
182 | return match kind { | |
174 |
NodeKind::Directory(d) => |
|
183 | NodeKind::Directory(d) => { | |
|
184 | d.was_file.as_mut().map(|f| &mut f.entry) | |||
|
185 | } | |||
175 | NodeKind::File(f) => Some(&mut f.entry), |
|
186 | NodeKind::File(f) => Some(&mut f.entry), | |
176 | }; |
|
187 | }; | |
177 | } |
|
188 | } | |
@@ -192,8 +203,12 b' impl Tree {' | |||||
192 | } |
|
203 | } | |
193 |
|
204 | |||
194 | /// Remove the entry at `path` and returns it, if it exists. |
|
205 | /// Remove the entry at `path` and returns it, if it exists. | |
195 | pub fn remove(&mut self, path: impl AsRef<HgPath>) -> Option<DirstateEntry> { |
|
206 | pub fn remove( | |
196 | let RemoveResult { old_entry, .. } = self.root.remove(path.as_ref().as_bytes()); |
|
207 | &mut self, | |
|
208 | path: impl AsRef<HgPath>, | |||
|
209 | ) -> Option<DirstateEntry> { | |||
|
210 | let RemoveResult { old_entry, .. } = | |||
|
211 | self.root.remove(path.as_ref().as_bytes()); | |||
197 | self.files_count = self |
|
212 | self.files_count = self | |
198 | .files_count |
|
213 | .files_count | |
199 | .checked_sub(if old_entry.is_some() { 1 } else { 0 }) |
|
214 | .checked_sub(if old_entry.is_some() { 1 } else { 0 }) | |
@@ -344,7 +359,10 b' mod tests {' | |||||
344 | size: 30, |
|
359 | size: 30, | |
345 | }; |
|
360 | }; | |
346 | assert_eq!(tree.insert_node(HgPath::new(b"foo"), entry), None); |
|
361 | assert_eq!(tree.insert_node(HgPath::new(b"foo"), entry), None); | |
347 | assert_eq!(tree.insert_node(HgPath::new(b"foo/a"), removed_entry), None); |
|
362 | assert_eq!( | |
|
363 | tree.insert_node(HgPath::new(b"foo/a"), removed_entry), | |||
|
364 | None | |||
|
365 | ); | |||
348 | // The insert should not turn `foo` into a directory as `foo` is not |
|
366 | // The insert should not turn `foo` into a directory as `foo` is not | |
349 | // `Removed`. |
|
367 | // `Removed`. | |
350 | match tree.get_node(HgPath::new(b"foo")).unwrap().kind { |
|
368 | match tree.get_node(HgPath::new(b"foo")).unwrap().kind { | |
@@ -516,7 +534,10 b' mod tests {' | |||||
516 | ..entry |
|
534 | ..entry | |
517 | }; |
|
535 | }; | |
518 | assert_eq!(tree.insert(HgPath::new(b"a"), entry), None); |
|
536 | assert_eq!(tree.insert(HgPath::new(b"a"), entry), None); | |
519 | assert_eq!(tree.insert_node(HgPath::new(b"a/b/x"), removed_entry), None); |
|
537 | assert_eq!( | |
|
538 | tree.insert_node(HgPath::new(b"a/b/x"), removed_entry), | |||
|
539 | None | |||
|
540 | ); | |||
520 | assert_eq!(tree.files_count, 2); |
|
541 | assert_eq!(tree.files_count, 2); | |
521 | dbg!(&tree); |
|
542 | dbg!(&tree); | |
522 | assert_eq!(tree.remove(HgPath::new(b"a")), Some(entry)); |
|
543 | assert_eq!(tree.remove(HgPath::new(b"a")), Some(entry)); |
General Comments 0
You need to be logged in to leave comments.
Login now