rust-dirstatemap: add `set_cached_mtime` helper method
authorRaphaël Gomès <rgomes@octobus.net>
Fri, 08 Apr 2022 16:04:17 +0200
changeset 49128 464747faef14
parent 49127 f3e8b0b0a8c2
child 49129 74c996f84958
rust-dirstatemap: add `set_cached_mtime` helper method This will help remove the `get_or_insert` method, which is dangerous because it does not take the `DirstateMap` counters into account Differential Revision: https://phab.mercurial-scm.org/D12526
rust/hg-core/src/dirstate_tree/dirstate_map.rs
rust/hg-core/src/dirstate_tree/status.rs
--- a/rust/hg-core/src/dirstate_tree/dirstate_map.rs	Fri Apr 08 16:03:39 2022 +0200
+++ b/rust/hg-core/src/dirstate_tree/dirstate_map.rs	Fri Apr 08 16:04:17 2022 +0200
@@ -823,6 +823,30 @@
         Ok(())
     }
 
+    /// Sets the cached mtime for the (potential) folder at `path`.
+    pub(super) fn set_cached_mtime(
+        &mut self,
+        path: &HgPath,
+        mtime: TruncatedTimestamp,
+    ) -> Result<(), DirstateV2ParseError> {
+        let node = match DirstateMap::get_node_mut(
+            self.on_disk,
+            &mut self.unreachable_bytes,
+            &mut self.root,
+            path,
+        )? {
+            Some(node) => node,
+            None => return Ok(()),
+        };
+        match &node.data {
+            NodeData::Entry(_) => {} // Don’t overwrite an entry
+            NodeData::CachedDirectory { .. } | NodeData::None => {
+                node.data = NodeData::CachedDirectory { mtime }
+            }
+        }
+        Ok(())
+    }
+
     fn iter_nodes<'tree>(
         &'tree self,
     ) -> impl Iterator<
--- a/rust/hg-core/src/dirstate_tree/status.rs	Fri Apr 08 16:03:39 2022 +0200
+++ b/rust/hg-core/src/dirstate_tree/status.rs	Fri Apr 08 16:04:17 2022 +0200
@@ -4,7 +4,6 @@
 use crate::dirstate_tree::dirstate_map::BorrowedPath;
 use crate::dirstate_tree::dirstate_map::ChildNodesRef;
 use crate::dirstate_tree::dirstate_map::DirstateMap;
-use crate::dirstate_tree::dirstate_map::NodeData;
 use crate::dirstate_tree::dirstate_map::NodeRef;
 use crate::dirstate_tree::on_disk::DirstateV2ParseError;
 use crate::matchers::get_ignore_function;
@@ -143,13 +142,7 @@
         dmap.clear_cached_mtime(path)?;
     }
     for (path, mtime) in &new_cachable {
-        let node = dmap.get_or_insert(path)?;
-        match &node.data {
-            NodeData::Entry(_) => {} // Don’t overwrite an entry
-            NodeData::CachedDirectory { .. } | NodeData::None => {
-                node.data = NodeData::CachedDirectory { mtime: *mtime }
-            }
-        }
+        dmap.set_cached_mtime(path, *mtime)?;
     }
 
     Ok((outcome, warnings))