rust-dirstatemap: add `set_possibly_dirty` method
authorRaphaël Gomès <rgomes@octobus.net>
Mon, 28 Mar 2022 18:09:01 +0200
changeset 49106 c1a3fdedc492
parent 49105 a69ea5a3c5a5
child 49107 079aaf996eca
rust-dirstatemap: add `set_possibly_dirty` method This is the new API that Python has already migrated to. Differential Revision: https://phab.mercurial-scm.org/D12504
rust/hg-core/src/dirstate_tree/dirstate_map.rs
rust/hg-cpython/src/dirstate/dirstate_map.rs
--- a/rust/hg-core/src/dirstate_tree/dirstate_map.rs	Mon Mar 28 18:02:45 2022 +0200
+++ b/rust/hg-core/src/dirstate_tree/dirstate_map.rs	Mon Mar 28 18:09:01 2022 +0200
@@ -427,6 +427,13 @@
             _ => None,
         }
     }
+
+    fn as_entry_mut(&mut self) -> Option<&mut DirstateEntry> {
+        match self {
+            NodeData::Entry(entry) => Some(entry),
+            _ => None,
+        }
+    }
 }
 
 impl<'on_disk> DirstateMap<'on_disk> {
@@ -791,6 +798,24 @@
         Ok(())
     }
 
+    fn set_possibly_dirty(
+        &mut self,
+        filename: &HgPath,
+    ) -> Result<(), DirstateError> {
+        let node = Self::get_or_insert_node(
+            self.on_disk,
+            &mut self.unreachable_bytes,
+            &mut self.root,
+            filename,
+            WithBasename::to_cow_owned,
+            |_ancestor| {},
+        )?;
+        let entry = node.data.as_entry_mut().expect("entry should exist");
+        entry.set_possibly_dirty();
+        node.data = NodeData::Entry(*entry);
+        Ok(())
+    }
+
     fn iter_nodes<'tree>(
         &'tree self,
     ) -> impl Iterator<
@@ -929,6 +954,16 @@
         })
     }
 
+    pub fn set_possibly_dirty(
+        &mut self,
+        filename: &HgPath,
+    ) -> Result<(), DirstateError> {
+        if self.get(filename)?.is_none() {
+            return Err(DirstateMapError::PathNotFound(filename.into()).into());
+        }
+        self.with_dmap_mut(|map| map.set_possibly_dirty(filename))
+    }
+
     pub fn reset_state(
         &mut self,
         filename: &HgPath,
--- a/rust/hg-cpython/src/dirstate/dirstate_map.rs	Mon Mar 28 18:02:45 2022 +0200
+++ b/rust/hg-cpython/src/dirstate/dirstate_map.rs	Mon Mar 28 18:09:01 2022 +0200
@@ -164,6 +164,16 @@
         Ok(PyNone)
     }
 
+    def set_possibly_dirty(&self, f: PyObject) -> PyResult<PyNone> {
+        let bytes = f.extract::<PyBytes>(py)?;
+        let path = HgPath::new(bytes.data(py));
+        let res = self.inner(py).borrow_mut().set_possibly_dirty(path);
+        res.or_else(|_| {
+            Err(PyErr::new::<exc::OSError, _>(py, "Dirstate error".to_string()))
+        })?;
+        Ok(PyNone)
+    }
+
     def reset_state(
         &self,
         f: PyObject,