D10967: dirstate: use a `added` parameter to _addpath

marmoute (Pierre-Yves David) phabricator at mercurial-scm.org
Sun Jul 4 21:55:32 UTC 2021


marmoute created this revision.
Herald added a reviewer: hg-reviewers.
Herald added a subscriber: mercurial-patches.

REVISION SUMMARY
  My next target are the explicit `state` byte, so let start with abstracting
  their initial storage.

REPOSITORY
  rHG Mercurial

BRANCH
  default

REVISION DETAIL
  https://phab.mercurial-scm.org/D10967

AFFECTED FILES
  mercurial/dirstate.py
  mercurial/dirstatemap.py
  rust/hg-core/src/dirstate/dirstate_map.rs
  rust/hg-core/src/dirstate_tree/dirstate_map.rs
  rust/hg-core/src/dirstate_tree/dispatch.rs
  rust/hg-cpython/src/dirstate/dirstate_map.rs
  rust/hg-cpython/src/dirstate/dispatch.rs

CHANGE DETAILS

diff --git a/rust/hg-cpython/src/dirstate/dispatch.rs b/rust/hg-cpython/src/dirstate/dispatch.rs
--- a/rust/hg-cpython/src/dirstate/dispatch.rs
+++ b/rust/hg-cpython/src/dirstate/dispatch.rs
@@ -25,11 +25,17 @@
         &mut self,
         filename: &HgPath,
         entry: DirstateEntry,
+        added: bool,
         from_p2: bool,
         possibly_dirty: bool,
     ) -> Result<(), DirstateError> {
-        self.get_mut()
-            .add_file(filename, entry, from_p2, possibly_dirty)
+        self.get_mut().add_file(
+            filename,
+            entry,
+            added,
+            from_p2,
+            possibly_dirty,
+        )
     }
 
     fn remove_file(
diff --git a/rust/hg-cpython/src/dirstate/dirstate_map.rs b/rust/hg-cpython/src/dirstate/dirstate_map.rs
--- a/rust/hg-cpython/src/dirstate/dirstate_map.rs
+++ b/rust/hg-cpython/src/dirstate/dirstate_map.rs
@@ -112,17 +112,28 @@
         mode: PyObject,
         size: PyObject,
         mtime: PyObject,
+        added: PyObject,
         from_p2: PyObject,
         possibly_dirty: PyObject,
     ) -> PyResult<PyObject> {
         let f = f.extract::<PyBytes>(py)?;
         let filename = HgPath::new(f.data(py));
-        let state = state.extract::<PyBytes>(py)?.data(py)[0]
+        let state = if state.is_none(py) {
+            // Arbitrary default value
+            EntryState::Normal
+        } else {
+            state.extract::<PyBytes>(py)?.data(py)[0]
             .try_into()
             .map_err(|e: HgError| {
                 PyErr::new::<exc::ValueError, _>(py, e.to_string())
-            })?;
-        let mode = mode.extract(py)?;
+            })?
+        };
+        let mode = if mode.is_none(py) {
+            // fallback default value
+            0
+        } else {
+            mode.extract(py)?
+        };
         let size = if size.is_none(py) {
             // fallback default value
             SIZE_NON_NORMAL
@@ -141,11 +152,13 @@
             size: size,
             mtime: mtime,
         };
+        let added = added.extract::<PyBool>(py)?.is_true();
         let from_p2 = from_p2.extract::<PyBool>(py)?.is_true();
         let possibly_dirty = possibly_dirty.extract::<PyBool>(py)?.is_true();
         self.inner(py).borrow_mut().add_file(
             filename,
             entry,
+            added,
             from_p2,
             possibly_dirty
         ).and(Ok(py.None())).or_else(|e: DirstateError| {
diff --git a/rust/hg-core/src/dirstate_tree/dispatch.rs b/rust/hg-core/src/dirstate_tree/dispatch.rs
--- a/rust/hg-core/src/dirstate_tree/dispatch.rs
+++ b/rust/hg-core/src/dirstate_tree/dispatch.rs
@@ -48,6 +48,7 @@
         &mut self,
         filename: &HgPath,
         entry: DirstateEntry,
+        added: bool,
         from_p2: bool,
         possibly_dirty: bool,
     ) -> Result<(), DirstateError>;
@@ -287,10 +288,11 @@
         &mut self,
         filename: &HgPath,
         entry: DirstateEntry,
+        added: bool,
         from_p2: bool,
         possibly_dirty: bool,
     ) -> Result<(), DirstateError> {
-        self.add_file(filename, entry, from_p2, possibly_dirty)
+        self.add_file(filename, entry, added, from_p2, possibly_dirty)
     }
 
     fn remove_file(
diff --git a/rust/hg-core/src/dirstate_tree/dirstate_map.rs b/rust/hg-core/src/dirstate_tree/dirstate_map.rs
--- a/rust/hg-core/src/dirstate_tree/dirstate_map.rs
+++ b/rust/hg-core/src/dirstate_tree/dirstate_map.rs
@@ -722,13 +722,15 @@
         &mut self,
         filename: &HgPath,
         entry: DirstateEntry,
+        added: bool,
         from_p2: bool,
         possibly_dirty: bool,
     ) -> Result<(), DirstateError> {
         let mut entry = entry;
-        if entry.state == EntryState::Added {
+        if added {
             assert!(!possibly_dirty);
             assert!(!from_p2);
+            entry.state = EntryState::Added;
             entry.size = SIZE_NON_NORMAL;
             entry.mtime = MTIME_UNSET;
         } else if from_p2 {
diff --git a/rust/hg-core/src/dirstate/dirstate_map.rs b/rust/hg-core/src/dirstate/dirstate_map.rs
--- a/rust/hg-core/src/dirstate/dirstate_map.rs
+++ b/rust/hg-core/src/dirstate/dirstate_map.rs
@@ -70,13 +70,15 @@
         filename: &HgPath,
         entry: DirstateEntry,
         // XXX once the dust settle this should probably become an enum
+        added: bool,
         from_p2: bool,
         possibly_dirty: bool,
     ) -> Result<(), DirstateError> {
         let mut entry = entry;
-        if entry.state == EntryState::Added {
+        if added {
             assert!(!possibly_dirty);
             assert!(!from_p2);
+            entry.state = EntryState::Added;
             entry.size = SIZE_NON_NORMAL;
             entry.mtime = MTIME_UNSET;
         } else if from_p2 {
@@ -407,6 +409,7 @@
             },
             false,
             false,
+            false,
         )
         .unwrap();
 
diff --git a/mercurial/dirstatemap.py b/mercurial/dirstatemap.py
--- a/mercurial/dirstatemap.py
+++ b/mercurial/dirstatemap.py
@@ -147,17 +147,19 @@
     def addfile(
         self,
         f,
-        state,
-        mode,
+        state=None,
+        mode=0,
         size=None,
         mtime=None,
+        added=False,
         from_p2=False,
         possibly_dirty=False,
     ):
         """Add a tracked file to the dirstate."""
-        if state == b'a':
+        if added:
             assert not possibly_dirty
             assert not from_p2
+            state = b'a'
             size = NONNORMAL
             mtime = AMBIGUOUS_TIME
         elif from_p2:
@@ -168,10 +170,12 @@
             size = NONNORMAL
             mtime = AMBIGUOUS_TIME
         else:
+            assert state != b'a'
             assert size != FROM_P2
             assert size != NONNORMAL
             size = size & rangemask
             mtime = mtime & rangemask
+        assert state is not None
         assert size is not None
         assert mtime is not None
         old_entry = self.get(f)
@@ -461,10 +465,11 @@
         def addfile(
             self,
             f,
-            state,
-            mode,
+            state=None,
+            mode=0,
             size=None,
             mtime=None,
+            added=False,
             from_p2=False,
             possibly_dirty=False,
         ):
@@ -474,6 +479,7 @@
                 mode,
                 size,
                 mtime,
+                added,
                 from_p2,
                 possibly_dirty,
             )
diff --git a/mercurial/dirstate.py b/mercurial/dirstate.py
--- a/mercurial/dirstate.py
+++ b/mercurial/dirstate.py
@@ -443,15 +443,16 @@
     def _addpath(
         self,
         f,
-        state,
-        mode,
+        state=None,
+        mode=0,
         size=None,
         mtime=None,
+        added=False,
         from_p2=False,
         possibly_dirty=False,
     ):
         entry = self._map.get(f)
-        if state == b'a' or entry is not None and entry.removed:
+        if added or entry is not None and entry.removed:
             scmutil.checkfilename(f)
             if self._map.hastrackeddir(f):
                 msg = _(b'directory %r already in dirstate')
@@ -474,6 +475,7 @@
             mode=mode,
             size=size,
             mtime=mtime,
+            added=added,
             from_p2=from_p2,
             possibly_dirty=possibly_dirty,
         )
@@ -544,7 +546,7 @@
 
     def add(self, f):
         '''Mark a file added.'''
-        self._addpath(f, b'a', 0)
+        self._addpath(f, added=True)
         self._map.copymap.pop(f, None)
 
     def remove(self, f):



To: marmoute, #hg-reviewers
Cc: mercurial-patches, mercurial-devel


More information about the Mercurial-devel mailing list