changeset 49100:38e5bb1425dd

rust-dirstate: introduce intermediate struct for dirstate-v2 data This is passed often as a long tuple that is not easy to know the form of, so we refactor everything in this struct. This also renames `wdir_tracked` to follow the Python `wc_tracked`, even though the on-disk format uses `WDIR_TRACKED`. I think a single naming scheme is better, but we can't easily break the Python impl now because of extensions, so this is low-effort enough and facilitates grepping. Differential Revision: https://phab.mercurial-scm.org/D12498
author Raphaël Gomès <rgomes@octobus.net>
date Wed, 23 Mar 2022 17:38:59 +0100
parents ed3ef8dacf02
children dd0430434ce9
files rust/hg-core/src/dirstate/entry.rs rust/hg-core/src/dirstate_tree/on_disk.rs rust/hg-cpython/src/dirstate/item.rs
diffstat 3 files changed, 52 insertions(+), 46 deletions(-) [+]
line wrap: on
line diff
--- a/rust/hg-core/src/dirstate/entry.rs	Wed Mar 23 16:32:31 2022 +0100
+++ b/rust/hg-core/src/dirstate/entry.rs	Wed Mar 23 17:38:59 2022 +0100
@@ -248,23 +248,35 @@
 /// dirstate v1 format.
 pub const SIZE_NON_NORMAL: i32 = -1;
 
+#[derive(Debug, Default, Copy, Clone)]
+pub struct DirstateV2Data {
+    pub wc_tracked: bool,
+    pub p1_tracked: bool,
+    pub p2_info: bool,
+    pub mode_size: Option<(u32, u32)>,
+    pub mtime: Option<TruncatedTimestamp>,
+    pub fallback_exec: Option<bool>,
+    pub fallback_symlink: Option<bool>,
+}
+
 impl DirstateEntry {
-    pub fn from_v2_data(
-        wdir_tracked: bool,
-        p1_tracked: bool,
-        p2_info: bool,
-        mode_size: Option<(u32, u32)>,
-        mtime: Option<TruncatedTimestamp>,
-        fallback_exec: Option<bool>,
-        fallback_symlink: Option<bool>,
-    ) -> Self {
+    pub fn from_v2_data(v2_data: DirstateV2Data) -> Self {
+        let DirstateV2Data {
+            wc_tracked,
+            p1_tracked,
+            p2_info,
+            mode_size,
+            mtime,
+            fallback_exec,
+            fallback_symlink,
+        } = v2_data;
         if let Some((mode, size)) = mode_size {
             // TODO: return an error for out of range values?
             assert!(mode & !RANGE_MASK_31BIT == 0);
             assert!(size & !RANGE_MASK_31BIT == 0);
         }
         let mut flags = Flags::empty();
-        flags.set(Flags::WDIR_TRACKED, wdir_tracked);
+        flags.set(Flags::WDIR_TRACKED, wc_tracked);
         flags.set(Flags::P1_TRACKED, p1_tracked);
         flags.set(Flags::P2_INFO, p2_info);
         if let Some(exec) = fallback_exec {
@@ -368,7 +380,11 @@
     }
 
     pub fn new_tracked() -> Self {
-        Self::from_v2_data(true, false, false, None, None, None, None)
+        let data = DirstateV2Data {
+            wc_tracked: true,
+            ..Default::default()
+        };
+        Self::from_v2_data(data)
     }
 
     pub fn tracked(&self) -> bool {
@@ -413,36 +429,25 @@
         )
     }
 
-    /// Returns `(wdir_tracked, p1_tracked, p2_info, mode_size, mtime)`
-    pub(crate) fn v2_data(
-        &self,
-    ) -> (
-        bool,
-        bool,
-        bool,
-        Option<(u32, u32)>,
-        Option<TruncatedTimestamp>,
-        Option<bool>,
-        Option<bool>,
-    ) {
+    pub(crate) fn v2_data(&self) -> DirstateV2Data {
         if !self.any_tracked() {
             // TODO: return an Option instead?
             panic!("Accessing v2_data of an untracked DirstateEntry")
         }
-        let wdir_tracked = self.flags.contains(Flags::WDIR_TRACKED);
+        let wc_tracked = self.flags.contains(Flags::WDIR_TRACKED);
         let p1_tracked = self.flags.contains(Flags::P1_TRACKED);
         let p2_info = self.flags.contains(Flags::P2_INFO);
         let mode_size = self.mode_size;
         let mtime = self.mtime;
-        (
-            wdir_tracked,
+        DirstateV2Data {
+            wc_tracked,
             p1_tracked,
             p2_info,
             mode_size,
             mtime,
-            self.get_fallback_exec(),
-            self.get_fallback_symlink(),
-        )
+            fallback_exec: self.get_fallback_exec(),
+            fallback_symlink: self.get_fallback_symlink(),
+        }
     }
 
     fn v1_state(&self) -> EntryState {
--- a/rust/hg-core/src/dirstate_tree/on_disk.rs	Wed Mar 23 16:32:31 2022 +0100
+++ b/rust/hg-core/src/dirstate_tree/on_disk.rs	Wed Mar 23 17:38:59 2022 +0100
@@ -2,7 +2,7 @@
 //!
 //! See `mercurial/helptext/internals/dirstate-v2.txt`
 
-use crate::dirstate::TruncatedTimestamp;
+use crate::dirstate::{DirstateV2Data, TruncatedTimestamp};
 use crate::dirstate_tree::dirstate_map::{self, DirstateMap, NodeRef};
 use crate::dirstate_tree::path_with_basename::WithBasename;
 use crate::errors::HgError;
@@ -412,7 +412,7 @@
 
     fn assume_entry(&self) -> Result<DirstateEntry, DirstateV2ParseError> {
         // TODO: convert through raw bits instead?
-        let wdir_tracked = self.flags().contains(Flags::WDIR_TRACKED);
+        let wc_tracked = self.flags().contains(Flags::WDIR_TRACKED);
         let p1_tracked = self.flags().contains(Flags::P1_TRACKED);
         let p2_info = self.flags().contains(Flags::P2_INFO);
         let mode_size = if self.flags().contains(Flags::HAS_MODE_AND_SIZE)
@@ -442,15 +442,15 @@
             } else {
                 None
             };
-        Ok(DirstateEntry::from_v2_data(
-            wdir_tracked,
+        Ok(DirstateEntry::from_v2_data(DirstateV2Data {
+            wc_tracked,
             p1_tracked,
             p2_info,
             mode_size,
             mtime,
             fallback_exec,
             fallback_symlink,
-        ))
+        }))
     }
 
     pub(super) fn entry(
@@ -490,18 +490,18 @@
     fn from_dirstate_entry(
         entry: &DirstateEntry,
     ) -> (Flags, U32Be, PackedTruncatedTimestamp) {
-        let (
-            wdir_tracked,
+        let DirstateV2Data {
+            wc_tracked,
             p1_tracked,
             p2_info,
-            mode_size_opt,
-            mtime_opt,
+            mode_size: mode_size_opt,
+            mtime: mtime_opt,
             fallback_exec,
             fallback_symlink,
-        ) = entry.v2_data();
-        // TODO: convert throug raw flag bits instead?
+        } = entry.v2_data();
+        // TODO: convert through raw flag bits instead?
         let mut flags = Flags::empty();
-        flags.set(Flags::WDIR_TRACKED, wdir_tracked);
+        flags.set(Flags::WDIR_TRACKED, wc_tracked);
         flags.set(Flags::P1_TRACKED, p1_tracked);
         flags.set(Flags::P2_INFO, p2_info);
         let size = if let Some((m, s)) = mode_size_opt {
--- a/rust/hg-cpython/src/dirstate/item.rs	Wed Mar 23 16:32:31 2022 +0100
+++ b/rust/hg-cpython/src/dirstate/item.rs	Wed Mar 23 17:38:59 2022 +0100
@@ -8,6 +8,7 @@
 use cpython::Python;
 use cpython::PythonObject;
 use hg::dirstate::DirstateEntry;
+use hg::dirstate::DirstateV2Data;
 use hg::dirstate::TruncatedTimestamp;
 use std::cell::Cell;
 
@@ -38,15 +39,15 @@
                 }
             }
         }
-        let entry = DirstateEntry::from_v2_data(
-            wc_tracked,
+        let entry = DirstateEntry::from_v2_data(DirstateV2Data {
+            wc_tracked: wc_tracked,
             p1_tracked,
             p2_info,
-            mode_size_opt,
-            mtime_opt,
+            mode_size: mode_size_opt,
+            mtime: mtime_opt,
             fallback_exec,
             fallback_symlink,
-        );
+        });
         DirstateItem::create_instance(py, Cell::new(entry))
     }