rust-dirstatemap: use `get_node_mut` instead or `get_or_insert_node`
authorRaphaël Gomès <rgomes@octobus.net>
Fri, 08 Apr 2022 17:22:39 +0200
changeset 49132 7276a6007573
parent 49131 fcf6f943a142
child 49133 23a5659125c8
rust-dirstatemap: use `get_node_mut` instead or `get_or_insert_node` This (along with the docstring), makes it more obvious that we're not expecting to insert a node here. This is less prone to bugs in later refactorings. Differential Revision: https://phab.mercurial-scm.org/D12530
rust/hg-core/src/dirstate_tree/dirstate_map.rs
--- a/rust/hg-core/src/dirstate_tree/dirstate_map.rs	Fri Apr 08 16:56:52 2022 +0200
+++ b/rust/hg-core/src/dirstate_tree/dirstate_map.rs	Fri Apr 08 17:22:39 2022 +0200
@@ -733,25 +733,45 @@
         Ok(new)
     }
 
-    /// It is the responsibility of the caller to know that there was an entry
-    /// there before. Does not handle the removal of copy source
+    /// Set a node as untracked in the dirstate.
+    ///
+    /// It is the responsibility of the caller to remove the copy source and/or
+    /// the entry itself if appropriate.
+    ///
+    /// # Panics
+    ///
+    /// Panics if the node does not exist.
     fn set_untracked(
         &mut self,
         filename: &HgPath,
         old_entry: DirstateEntry,
     ) -> Result<(), DirstateV2ParseError> {
-        let node = self.get_or_insert_node(filename, |ancestor| {
-            ancestor.tracked_descendants_count = ancestor
-                .tracked_descendants_count
-                .checked_sub(1)
-                .expect("tracked_descendants_count should be >= 0");
-        })?;
+        let node = DirstateMap::get_node_mut(
+            self.on_disk,
+            &mut self.unreachable_bytes,
+            &mut self.root,
+            filename,
+            |ancestor| {
+                ancestor.tracked_descendants_count = ancestor
+                    .tracked_descendants_count
+                    .checked_sub(1)
+                    .expect("tracked_descendants_count should be >= 0");
+            },
+        )?
+        .expect("node should exist");
         let mut new_entry = old_entry.clone();
         new_entry.set_untracked();
         node.data = NodeData::Entry(new_entry);
         Ok(())
     }
 
+    /// Set a node as clean in the dirstate.
+    ///
+    /// It is the responsibility of the caller to remove the copy source.
+    ///
+    /// # Panics
+    ///
+    /// Panics if the node does not exist.
     fn set_clean(
         &mut self,
         filename: &HgPath,
@@ -760,22 +780,41 @@
         size: u32,
         mtime: TruncatedTimestamp,
     ) -> Result<(), DirstateError> {
-        let node = self.get_or_insert_node(filename, |ancestor| {
-            if !old_entry.tracked() {
-                ancestor.tracked_descendants_count += 1;
-            }
-        })?;
+        let node = DirstateMap::get_node_mut(
+            self.on_disk,
+            &mut self.unreachable_bytes,
+            &mut self.root,
+            filename,
+            |ancestor| {
+                if !old_entry.tracked() {
+                    ancestor.tracked_descendants_count += 1;
+                }
+            },
+        )?
+        .expect("node should exist");
         let mut new_entry = old_entry.clone();
         new_entry.set_clean(mode, size, mtime);
         node.data = NodeData::Entry(new_entry);
         Ok(())
     }
 
+    /// Set a node as possibly dirty in the dirstate.
+    ///
+    /// # Panics
+    ///
+    /// Panics if the node does not exist.
     fn set_possibly_dirty(
         &mut self,
         filename: &HgPath,
     ) -> Result<(), DirstateError> {
-        let node = self.get_or_insert_node(filename, |_ancestor| {})?;
+        let node = DirstateMap::get_node_mut(
+            self.on_disk,
+            &mut self.unreachable_bytes,
+            &mut self.root,
+            filename,
+            |_ancestor| {},
+        )?
+        .expect("node should exist");
         let entry = node.data.as_entry_mut().expect("entry should exist");
         entry.set_possibly_dirty();
         node.data = NodeData::Entry(*entry);