Patchwork D8959: hg-core: define a `DebugData` `Operation`

login
register
mail settings
Submitter phabricator
Date Aug. 26, 2020, 1:24 p.m.
Message ID <differential-rev-PHID-DREV-jlveegzsggftyfyxdkfk-req@mercurial-scm.org>
Download mbox | patch
Permalink /patch/47058/
State Superseded
Headers show

Comments

phabricator - Aug. 26, 2020, 1:24 p.m.
acezar created this revision.
Herald added a reviewer: hg-reviewers.
Herald added a subscriber: mercurial-patches.

REVISION SUMMARY
  Read the content of a revlog at a given revision.

REPOSITORY
  rHG Mercurial

BRANCH
  default

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

AFFECTED FILES
  rust/hg-core/src/operations/debugdata.rs
  rust/hg-core/src/operations/mod.rs

CHANGE DETAILS




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

Patch

diff --git a/rust/hg-core/src/operations/mod.rs b/rust/hg-core/src/operations/mod.rs
--- a/rust/hg-core/src/operations/mod.rs
+++ b/rust/hg-core/src/operations/mod.rs
@@ -2,6 +2,7 @@ 
 //! An operation is what can be done whereas a command is what is exposed by
 //! the cli. A single command can use several operations to achieve its goal.
 
+mod debugdata;
 mod dirstate_status;
 mod find_root;
 mod list_tracked_files;
diff --git a/rust/hg-core/src/operations/debugdata.rs b/rust/hg-core/src/operations/debugdata.rs
new file mode 100644
--- /dev/null
+++ b/rust/hg-core/src/operations/debugdata.rs
@@ -0,0 +1,94 @@ 
+// debugdata.rs
+//
+// Copyright 2020 Antoine Cezar <antoine.cezar@octobus.net>
+//
+// This software may be used and distributed according to the terms of the
+// GNU General Public License version 2 or any later version.
+
+use super::find_root;
+use crate::revlog::revlog::Revlog;
+use crate::revlog::Revision;
+use std::fmt;
+
+/// Kind of data to debug
+#[derive(Debug, Copy, Clone)]
+pub enum DebugDataKind {
+    Changelog,
+    Manifest,
+}
+
+/// Kind of error encoutered by DebugData
+#[derive(Debug)]
+pub enum DebugDataErrorKind {
+    FindRootError(find_root::FindRootError),
+    IoError(std::io::Error),
+    InvalidRevision,
+}
+
+/// A DebugData error
+#[derive(Debug)]
+pub struct DebugDataError {
+    /// Kind of error encoutered by DebugData
+    pub kind: DebugDataErrorKind,
+}
+
+impl std::error::Error for DebugDataError {}
+
+impl fmt::Display for DebugDataError {
+    fn fmt(&self, _f: &mut fmt::Formatter<'_>) -> fmt::Result {
+        unimplemented!()
+    }
+}
+
+impl From<DebugDataErrorKind> for DebugDataError {
+    fn from(kind: DebugDataErrorKind) -> Self {
+        DebugDataError { kind }
+    }
+}
+
+impl From<find_root::FindRootError> for DebugDataError {
+    fn from(err: find_root::FindRootError) -> Self {
+        let kind = DebugDataErrorKind::FindRootError(err);
+        DebugDataError { kind }
+    }
+}
+
+impl From<std::io::Error> for DebugDataError {
+    fn from(err: std::io::Error) -> Self {
+        let kind = DebugDataErrorKind::IoError(err);
+        DebugDataError { kind }
+    }
+}
+
+/// Dump the contents data of a revision.
+pub struct DebugData<'a> {
+    /// Revision or hash of the revision.
+    rev: &'a str,
+    /// Kind of data to debug.
+    kind: DebugDataKind,
+}
+
+impl<'a> DebugData<'a> {
+    pub fn new(rev: &'a str, kind: DebugDataKind) -> Self {
+        DebugData { rev, kind }
+    }
+
+    pub fn run(&mut self) -> Result<Vec<u8>, DebugDataError> {
+        let rev = self
+            .rev
+            .parse::<Revision>()
+            .or(Err(DebugDataErrorKind::InvalidRevision))?;
+
+        let root = find_root::FindRoot::new().run()?;
+        let index_file = match self.kind {
+            DebugDataKind::Changelog => root.join(".hg/store/00changelog.i"),
+            DebugDataKind::Manifest => root.join(".hg/store/00manifest.i"),
+        };
+        let revlog = Revlog::open(&index_file)?;
+        let data = revlog
+            .get_rev_data(rev)
+            .ok_or(DebugDataErrorKind::InvalidRevision)?;
+
+        Ok(data)
+    }
+}