##// END OF EJS Templates
rust: format with rustfmt...
Raphaël Gomès -
r46162:c35db907 default
parent child Browse files
Show More
@@ -13,8 +13,8 b' use crate::{'
13 13 files::normalize_case,
14 14 hg_path::{HgPath, HgPathBuf},
15 15 },
16 CopyMap, DirsMultiset, DirstateEntry, DirstateError, DirstateMapError, DirstateParents,
17 DirstateParseError, FastHashMap, StateMap,
16 CopyMap, DirsMultiset, DirstateEntry, DirstateError, DirstateMapError,
17 DirstateParents, DirstateParseError, FastHashMap, StateMap,
18 18 };
19 19 use core::borrow::Borrow;
20 20 use micro_timer::timed;
@@ -51,7 +51,9 b' impl Deref for DirstateMap {'
51 51 }
52 52
53 53 impl FromIterator<(HgPathBuf, DirstateEntry)> for DirstateMap {
54 fn from_iter<I: IntoIterator<Item = (HgPathBuf, DirstateEntry)>>(iter: I) -> Self {
54 fn from_iter<I: IntoIterator<Item = (HgPathBuf, DirstateEntry)>>(
55 iter: I,
56 ) -> Self {
55 57 Self {
56 58 state_map: iter.into_iter().collect(),
57 59 ..Self::default()
@@ -83,7 +85,8 b' impl DirstateMap {'
83 85 old_state: EntryState,
84 86 entry: DirstateEntry,
85 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 90 if let Some(ref mut dirs) = self.dirs {
88 91 dirs.add_path(filename)?;
89 92 }
@@ -120,7 +123,8 b' impl DirstateMap {'
120 123 old_state: EntryState,
121 124 size: i32,
122 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 128 if let Some(ref mut dirs) = self.dirs {
125 129 dirs.delete_path(filename)?;
126 130 }
@@ -178,13 +182,18 b' impl DirstateMap {'
178 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 190 for filename in filenames {
183 191 let mut changed = false;
184 192 self.state_map
185 193 .entry(filename.to_owned())
186 194 .and_modify(|entry| {
187 if entry.state == EntryState::Normal && entry.mtime == now {
195 if entry.state == EntryState::Normal && entry.mtime == now
196 {
188 197 changed = true;
189 198 *entry = DirstateEntry {
190 199 mtime: MTIME_UNSET,
@@ -200,12 +209,18 b' impl DirstateMap {'
200 209 }
201 210 }
202 211
203 pub fn non_normal_entries_remove(&mut self, key: impl AsRef<HgPath>) -> bool {
212 pub fn non_normal_entries_remove(
213 &mut self,
214 key: impl AsRef<HgPath>,
215 ) -> bool {
204 216 self.get_non_normal_other_parent_entries()
205 217 .0
206 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 224 self.get_non_normal_other_parent_entries()
210 225 .0
211 226 .union(&other)
@@ -243,7 +258,10 b' impl DirstateMap {'
243 258 }
244 259
245 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 265 return;
248 266 }
249 267 let mut non_normal = HashSet::new();
@@ -259,7 +277,8 b' impl DirstateMap {'
259 277 if *state != EntryState::Normal || *mtime == MTIME_UNSET {
260 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 282 other_parent.insert(filename.to_owned());
264 283 }
265 284 }
@@ -273,7 +292,8 b' impl DirstateMap {'
273 292 /// good idea.
274 293 pub fn set_all_dirs(&mut self) -> Result<(), DirstateMapError> {
275 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 298 Ok(())
279 299 }
@@ -288,17 +308,26 b' impl DirstateMap {'
288 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 315 self.set_dirs()?;
293 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 323 self.set_all_dirs()?;
298 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 331 if let Some(ref parents) = self.parents {
303 332 return Ok(parents);
304 333 }
@@ -329,7 +358,10 b' impl DirstateMap {'
329 358 }
330 359
331 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 365 if file_contents.is_empty() {
334 366 return Ok(None);
335 367 }
@@ -358,7 +390,8 b' impl DirstateMap {'
358 390 parents: DirstateParents,
359 391 now: Duration,
360 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 396 self.dirty_parents = false;
364 397
@@ -371,9 +404,11 b' impl DirstateMap {'
371 404 return file_fold_map;
372 405 }
373 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 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 414 self.file_fold_map = Some(new_file_fold_map);
@@ -462,6 +497,9 b' mod tests {'
462 497 other_parent.insert(HgPathBuf::from_bytes(b"f4"));
463 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 17 use std::path::PathBuf;
18 18
19 19 impl FromIterator<(HgPathBuf, DirstateEntry)> for Tree {
20 fn from_iter<T: IntoIterator<Item = (HgPathBuf, DirstateEntry)>>(iter: T) -> Self {
20 fn from_iter<T: IntoIterator<Item = (HgPathBuf, DirstateEntry)>>(
21 iter: T,
22 ) -> Self {
21 23 let mut tree = Self::new();
22 24 for (path, entry) in iter {
23 25 tree.insert(path, entry);
@@ -48,16 +50,30 b" impl<'a> Iterator for Iter<'a> {"
48 50 while let Some((base_path, node)) = self.to_visit.pop_front() {
49 51 match &node.kind {
50 52 NodeKind::Directory(dir) => {
51 add_children_to_visit(&mut self.to_visit, &base_path, &dir);
53 add_children_to_visit(
54 &mut self.to_visit,
55 &base_path,
56 &dir,
57 );
52 58 if let Some(file) = &dir.was_file {
53 return Some((HgPathBuf::from_bytes(&base_path), file.entry));
59 return Some((
60 HgPathBuf::from_bytes(&base_path),
61 file.entry,
62 ));
54 63 }
55 64 }
56 65 NodeKind::File(file) => {
57 66 if let Some(dir) = &file.was_directory {
58 add_children_to_visit(&mut self.to_visit, &base_path, &dir);
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 124 /// We need to dispatch the new symlink as `Unknown` and all the
109 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 131 let path = path.as_ref();
112 self.shortcuts
113 .push_back((path.to_owned(), StatusShortcut::Dispatch(Dispatch::Unknown)));
132 self.shortcuts.push_back((
133 path.to_owned(),
134 StatusShortcut::Dispatch(Dispatch::Unknown),
135 ));
114 136 for (file, _) in node.iter() {
115 137 self.shortcuts.push_back((
116 138 path.join(&file),
@@ -171,10 +193,17 b" impl<'a> Iterator for FsIter<'a> {"
171 193 if self.directory_became_symlink(canonical_path) {
172 194 // Potential security issue, don't do a normal
173 195 // traversal, force the results.
174 self.dispatch_symlinked_directory(canonical_path, &node);
196 self.dispatch_symlinked_directory(
197 canonical_path,
198 &node,
199 );
175 200 continue;
176 201 }
177 add_children_to_visit(&mut self.to_visit, &base_path, &dir);
202 add_children_to_visit(
203 &mut self.to_visit,
204 &base_path,
205 &dir,
206 );
178 207 if let Some(file) = &dir.was_file {
179 208 return Some((
180 209 HgPathBuf::from_bytes(&base_path),
@@ -184,7 +213,11 b" impl<'a> Iterator for FsIter<'a> {"
184 213 }
185 214 NodeKind::File(file) => {
186 215 if let Some(dir) = &file.was_directory {
187 add_children_to_visit(&mut self.to_visit, &base_path, &dir);
216 add_children_to_visit(
217 &mut self.to_visit,
218 &base_path,
219 &dir,
220 );
188 221 }
189 222 return Some((
190 223 HgPathBuf::from_bytes(&base_path),
@@ -286,7 +319,8 b' mod tests {'
286 319
287 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 324 dbg!(&results);
291 325 assert!(results.contains(HgPath::new(b"foo2")));
292 326 assert!(results.contains(HgPath::new(b"foo/bar")));
@@ -48,7 +48,11 b' impl Default for NodeKind {'
48 48 }
49 49
50 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 56 let mut split = path.splitn(2, |&c| c == b'/');
53 57 let head = split.next().unwrap_or(b"");
54 58 let tail = split.next().unwrap_or(b"");
@@ -76,16 +80,24 b' impl Node {'
76 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 92 match &mut self.kind {
85 93 NodeKind::Directory(directory) => {
86 return Node::insert_in_directory(directory, new_entry, head, tail);
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 116 was_file: None,
105 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 121 file.was_directory = Some(Box::new(dir));
109 122 res
110 123 }
@@ -130,7 +143,9 b' impl Node {'
130 143 entry: new_entry,
131 144 }),
132 145 };
133 let old_entry = directory.children.insert(head.to_owned(), becomes_file);
146 let old_entry = directory
147 .children
148 .insert(head.to_owned(), becomes_file);
134 149 return InsertResult {
135 150 did_insert: true,
136 151 old_entry,
@@ -192,7 +207,8 b' impl Node {'
192 207 }
193 208 NodeKind::File(f) => {
194 209 if let Some(d) = &mut f.was_directory {
195 let RemoveResult { old_entry, .. } = Node::remove_from_directory(head, d);
210 let RemoveResult { old_entry, .. } =
211 Node::remove_from_directory(head, d);
196 212 return RemoveResult {
197 213 cleanup: false,
198 214 old_entry,
@@ -210,7 +226,8 b' impl Node {'
210 226 if res.cleanup {
211 227 d.children.remove(head);
212 228 }
213 res.cleanup = d.children.len() == 0 && d.was_file.is_none();
229 res.cleanup =
230 d.children.len() == 0 && d.was_file.is_none();
214 231 res
215 232 } else {
216 233 empty_result
@@ -219,7 +236,8 b' impl Node {'
219 236 NodeKind::File(f) => {
220 237 if let Some(d) = &mut f.was_directory {
221 238 if let Some(child) = d.children.get_mut(head) {
222 let RemoveResult { cleanup, old_entry } = child.remove(tail);
239 let RemoveResult { cleanup, old_entry } =
240 child.remove(tail);
223 241 if cleanup {
224 242 d.children.remove(head);
225 243 }
@@ -136,7 +136,11 b' impl Tree {'
136 136
137 137 /// Low-level insertion method that returns the previous node (directories
138 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 144 let InsertResult {
141 145 did_insert,
142 146 old_entry,
@@ -154,7 +158,9 b' impl Tree {'
154 158 pub fn get(&self, path: impl AsRef<HgPath>) -> Option<&DirstateEntry> {
155 159 if let Some(node) = self.get_node(&path) {
156 160 return match &node.kind {
157 NodeKind::Directory(d) => d.was_file.as_ref().map(|f| &f.entry),
161 NodeKind::Directory(d) => {
162 d.was_file.as_ref().map(|f| &f.entry)
163 }
158 164 NodeKind::File(f) => Some(&f.entry),
159 165 };
160 166 }
@@ -168,10 +174,15 b' impl Tree {'
168 174
169 175 /// Returns a mutable reference to the entry corresponding to `path` if it
170 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 181 if let Some(kind) = self.root.get_mut(path.as_ref().as_bytes()) {
173 182 return match kind {
174 NodeKind::Directory(d) => d.was_file.as_mut().map(|f| &mut f.entry),
183 NodeKind::Directory(d) => {
184 d.was_file.as_mut().map(|f| &mut f.entry)
185 }
175 186 NodeKind::File(f) => Some(&mut f.entry),
176 187 };
177 188 }
@@ -192,8 +203,12 b' impl Tree {'
192 203 }
193 204
194 205 /// Remove the entry at `path` and returns it, if it exists.
195 pub fn remove(&mut self, path: impl AsRef<HgPath>) -> Option<DirstateEntry> {
196 let RemoveResult { old_entry, .. } = self.root.remove(path.as_ref().as_bytes());
206 pub fn remove(
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 212 self.files_count = self
198 213 .files_count
199 214 .checked_sub(if old_entry.is_some() { 1 } else { 0 })
@@ -344,7 +359,10 b' mod tests {'
344 359 size: 30,
345 360 };
346 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 366 // The insert should not turn `foo` into a directory as `foo` is not
349 367 // `Removed`.
350 368 match tree.get_node(HgPath::new(b"foo")).unwrap().kind {
@@ -516,7 +534,10 b' mod tests {'
516 534 ..entry
517 535 };
518 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 541 assert_eq!(tree.files_count, 2);
521 542 dbg!(&tree);
522 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