dirstatemap: move `set_tracked` out of common methods and plug in Rust
authorRaphaël Gomès <rgomes@octobus.net>
Wed, 23 Mar 2022 15:19:43 +0100
changeset 49098 55c158a33fa5
parent 49097 791430b0b2d2
child 49099 ed3ef8dacf02
dirstatemap: move `set_tracked` out of common methods and plug in Rust We now have a Rust-specific implementation of this method, it is no longer shared between both implementations. Differential Revision: https://phab.mercurial-scm.org/D12496
mercurial/dirstatemap.py
rust/hg-cpython/src/dirstate/dirstate_map.rs
--- a/mercurial/dirstatemap.py	Wed Mar 23 15:18:12 2022 +0100
+++ b/mercurial/dirstatemap.py	Wed Mar 23 15:19:43 2022 +0100
@@ -130,30 +130,6 @@
         self._refresh_entry(filename, entry)
         self.copymap.pop(filename, None)
 
-    def set_tracked(self, filename):
-        new = False
-        entry = self.get(filename)
-        if entry is None:
-            self._dirs_incr(filename)
-            entry = DirstateItem(
-                wc_tracked=True,
-            )
-
-            self._insert_entry(filename, entry)
-            new = True
-        elif not entry.tracked:
-            self._dirs_incr(filename, entry)
-            entry.set_tracked()
-            self._refresh_entry(filename, entry)
-            new = True
-        else:
-            # XXX This is probably overkill for more case, but we need this to
-            # fully replace the `normallookup` call with `set_tracked` one.
-            # Consider smoothing this in the future.
-            entry.set_possibly_dirty()
-            self._refresh_entry(filename, entry)
-        return new
-
     def set_untracked(self, f):
         """Mark a file as no longer tracked in the dirstate map"""
         entry = self.get(f)
@@ -538,6 +514,30 @@
 
     ### code related to manipulation of entries and copy-sources
 
+    def set_tracked(self, filename):
+        new = False
+        entry = self.get(filename)
+        if entry is None:
+            self._dirs_incr(filename)
+            entry = DirstateItem(
+                wc_tracked=True,
+            )
+
+            self._insert_entry(filename, entry)
+            new = True
+        elif not entry.tracked:
+            self._dirs_incr(filename, entry)
+            entry.set_tracked()
+            self._refresh_entry(filename, entry)
+            new = True
+        else:
+            # XXX This is probably overkill for more case, but we need this to
+            # fully replace the `normallookup` call with `set_tracked` one.
+            # Consider smoothing this in the future.
+            entry.set_possibly_dirty()
+            self._refresh_entry(filename, entry)
+        return new
+
     def _refresh_entry(self, f, entry):
         if not entry.any_tracked:
             self._map.pop(f, None)
@@ -722,6 +722,9 @@
         def _insert_entry(self, f, entry):
             self._map.addfile(f, entry)
 
+        def set_tracked(self, f):
+            return self._map.set_tracked(f)
+
         def _drop_entry(self, f):
             self._map.drop_item_and_copy_source(f)
 
--- a/rust/hg-cpython/src/dirstate/dirstate_map.rs	Wed Mar 23 15:18:12 2022 +0100
+++ b/rust/hg-cpython/src/dirstate/dirstate_map.rs	Wed Mar 23 15:19:43 2022 +0100
@@ -131,6 +131,16 @@
         Ok(PyNone)
     }
 
+    def set_tracked(&self, f: PyObject) -> PyResult<PyBool> {
+        let bytes = f.extract::<PyBytes>(py)?;
+        let path = HgPath::new(bytes.data(py));
+        let res = self.inner(py).borrow_mut().set_tracked(path);
+        let was_tracked = res.or_else(|_| {
+            Err(PyErr::new::<exc::OSError, _>(py, "Dirstate error".to_string()))
+        })?;
+        Ok(was_tracked.to_py_object(py))
+    }
+
     def removefile(
         &self,
         f: PyObject,