Patchwork D10491: dirstate-tree: Add add_file, remove_file, and drop_file

login
register
mail settings
Submitter phabricator
Date April 20, 2021, 2:18 p.m.
Message ID <differential-rev-PHID-DREV-bl3b5s2yxfrhgvnkvaor-req@mercurial-scm.org>
Download mbox | patch
Permalink /patch/48806/
State Superseded
Headers show

Comments

phabricator - April 20, 2021, 2:18 p.m.
SimonSapin created this revision.
Herald added a reviewer: hg-reviewers.
Herald added a subscriber: mercurial-patches.

REVISION SUMMARY
  Again, various counters need to be kept up to date.

REPOSITORY
  rHG Mercurial

BRANCH
  default

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

AFFECTED FILES
  rust/hg-core/src/dirstate_tree/dirstate_map.rs

CHANGE DETAILS




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

Patch

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
@@ -331,28 +331,61 @@ 
 
     fn add_file(
         &mut self,
-        _filename: &HgPath,
+        filename: &HgPath,
         _old_state: EntryState,
-        _entry: DirstateEntry,
+        entry: DirstateEntry,
     ) -> Result<(), DirstateMapError> {
-        todo!()
+        self.add_file_node(filename, entry, None);
+        Ok(())
     }
 
     fn remove_file(
         &mut self,
-        _filename: &HgPath,
+        filename: &HgPath,
         _old_state: EntryState,
-        _size: i32,
+        size: i32,
     ) -> Result<(), DirstateMapError> {
-        todo!()
+        let entry = DirstateEntry {
+            state: EntryState::Removed,
+            mode: 0,
+            size,
+            mtime: 0,
+        };
+        self.add_file_node(filename, entry, None);
+        Ok(())
     }
 
     fn drop_file(
         &mut self,
-        _filename: &HgPath,
+        filename: &HgPath,
         _old_state: EntryState,
     ) -> Result<bool, DirstateMapError> {
-        todo!()
+        if let Some(node) = Self::get_node_mut(&mut self.root, filename) {
+            let was_tracked = node.is_tracked_file();
+            let had_entry = node.entry.is_some();
+            let had_copy_source = node.copy_source.is_some();
+
+            // TODO: this leaves in the tree a "non-file" node. Should we
+            // remove the node instead, together with ancestor nodes for
+            // directories that become empty?
+            node.entry = None;
+            node.copy_source = None;
+
+            if had_entry {
+                self.nodes_with_entry_count -= 1
+            }
+            if had_copy_source {
+                self.nodes_with_copy_source_count -= 1
+            }
+            if was_tracked {
+                self.for_each_ancestor_node(filename, |node| {
+                    node.tracked_descendants_count -= 1
+                })
+            }
+            Ok(had_entry)
+        } else {
+            Ok(false)
+        }
     }
 
     fn clear_ambiguous_times(&mut self, filenames: Vec<HgPathBuf>, now: i32) {