changeset 50978:27e773aa607d

rust: implement the `Graph` trait for all revlogs This is trivial and makes all the algorithms relying on the trait usable for more use cases.
author Raphaël Gomès <rgomes@octobus.net>
date Thu, 10 Aug 2023 11:01:07 +0200
parents 1928b770e3e7
children 4c5f6e95df84
files rust/hg-core/src/revlog/changelog.rs rust/hg-core/src/revlog/filelog.rs rust/hg-core/src/revlog/index.rs rust/hg-core/src/revlog/manifest.rs rust/hg-core/src/revlog/mod.rs
diffstat 5 files changed, 46 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
--- a/rust/hg-core/src/revlog/changelog.rs	Thu Aug 10 11:00:34 2023 +0200
+++ b/rust/hg-core/src/revlog/changelog.rs	Thu Aug 10 11:01:07 2023 +0200
@@ -4,7 +4,7 @@
 use crate::revlog::{Revlog, RevlogEntry, RevlogError};
 use crate::utils::hg_path::HgPath;
 use crate::vfs::Vfs;
-use crate::UncheckedRevision;
+use crate::{Graph, GraphError, UncheckedRevision};
 use itertools::Itertools;
 use std::ascii::escape_default;
 use std::borrow::Cow;
@@ -76,6 +76,12 @@
     }
 }
 
+impl Graph for Changelog {
+    fn parents(&self, rev: Revision) -> Result<[Revision; 2], GraphError> {
+        self.revlog.parents(rev)
+    }
+}
+
 /// A specialized `RevlogEntry` for `changelog` data format
 ///
 /// This is a `RevlogEntry` with the added semantics that the associated
--- a/rust/hg-core/src/revlog/filelog.rs	Thu Aug 10 11:00:34 2023 +0200
+++ b/rust/hg-core/src/revlog/filelog.rs	Thu Aug 10 11:01:07 2023 +0200
@@ -9,6 +9,8 @@
 use crate::utils::files::get_path_from_bytes;
 use crate::utils::hg_path::HgPath;
 use crate::utils::SliceExt;
+use crate::Graph;
+use crate::GraphError;
 use crate::UncheckedRevision;
 use std::path::PathBuf;
 
@@ -18,6 +20,12 @@
     revlog: Revlog,
 }
 
+impl Graph for Filelog {
+    fn parents(&self, rev: Revision) -> Result<[Revision; 2], GraphError> {
+        self.revlog.parents(rev)
+    }
+}
+
 impl Filelog {
     pub fn open_vfs(
         store_vfs: &crate::vfs::Vfs<'_>,
--- a/rust/hg-core/src/revlog/index.rs	Thu Aug 10 11:00:34 2023 +0200
+++ b/rust/hg-core/src/revlog/index.rs	Thu Aug 10 11:01:07 2023 +0200
@@ -6,7 +6,7 @@
 use crate::errors::HgError;
 use crate::revlog::node::Node;
 use crate::revlog::{Revision, NULL_REVISION};
-use crate::UncheckedRevision;
+use crate::{Graph, GraphError, RevlogIndex, UncheckedRevision};
 
 pub const INDEX_ENTRY_SIZE: usize = 64;
 
@@ -97,6 +97,23 @@
     }
 }
 
+impl Graph for Index {
+    fn parents(&self, rev: Revision) -> Result<[Revision; 2], GraphError> {
+        let err = || GraphError::ParentOutOfRange(rev);
+        match self.get_entry(rev) {
+            Some(entry) => {
+                // The C implementation checks that the parents are valid
+                // before returning
+                Ok([
+                    self.check_revision(entry.p1()).ok_or_else(err)?,
+                    self.check_revision(entry.p2()).ok_or_else(err)?,
+                ])
+            }
+            None => Ok([NULL_REVISION, NULL_REVISION]),
+        }
+    }
+}
+
 impl Index {
     /// Create an index from bytes.
     /// Calculate the start of each entry when is_inline is true.
--- a/rust/hg-core/src/revlog/manifest.rs	Thu Aug 10 11:00:34 2023 +0200
+++ b/rust/hg-core/src/revlog/manifest.rs	Thu Aug 10 11:01:07 2023 +0200
@@ -4,7 +4,7 @@
 use crate::utils::hg_path::HgPath;
 use crate::utils::SliceExt;
 use crate::vfs::Vfs;
-use crate::{Revision, UncheckedRevision};
+use crate::{Graph, GraphError, Revision, UncheckedRevision};
 
 /// A specialized `Revlog` to work with `manifest` data format.
 pub struct Manifestlog {
@@ -12,6 +12,12 @@
     revlog: Revlog,
 }
 
+impl Graph for Manifestlog {
+    fn parents(&self, rev: Revision) -> Result<[Revision; 2], GraphError> {
+        self.revlog.parents(rev)
+    }
+}
+
 impl Manifestlog {
     /// Open the `manifest` of a repository given by its root.
     pub fn open(store_vfs: &Vfs, use_nodemap: bool) -> Result<Self, HgError> {
--- a/rust/hg-core/src/revlog/mod.rs	Thu Aug 10 11:00:34 2023 +0200
+++ b/rust/hg-core/src/revlog/mod.rs	Thu Aug 10 11:01:07 2023 +0200
@@ -182,6 +182,12 @@
     nodemap: Option<nodemap::NodeTree>,
 }
 
+impl Graph for Revlog {
+    fn parents(&self, rev: Revision) -> Result<[Revision; 2], GraphError> {
+        self.index.parents(rev)
+    }
+}
+
 impl Revlog {
     /// Open a revlog index file.
     ///