##// END OF EJS Templates
hg-core: define a `DebugData` `Operation`...
Antoine Cezar -
r46098:b56df13a default
parent child Browse files
Show More
@@ -0,0 +1,110 b''
1 // debugdata.rs
2 //
3 // Copyright 2020 Antoine Cezar <antoine.cezar@octobus.net>
4 //
5 // This software may be used and distributed according to the terms of the
6 // GNU General Public License version 2 or any later version.
7
8 use super::find_root;
9 use crate::revlog::revlog::{Revlog, RevlogError};
10 use crate::revlog::Revision;
11
12 /// Kind of data to debug
13 #[derive(Debug, Copy, Clone)]
14 pub enum DebugDataKind {
15 Changelog,
16 Manifest,
17 }
18
19 /// Kind of error encountered by DebugData
20 #[derive(Debug)]
21 pub enum DebugDataErrorKind {
22 FindRootError(find_root::FindRootError),
23 /// Error when reading a `revlog` file.
24 IoError(std::io::Error),
25 /// The revision has not been found.
26 InvalidRevision,
27 /// A `revlog` file is corrupted.
28 CorruptedRevlog,
29 /// The `revlog` format version is not supported.
30 UnsuportedRevlogVersion(u16),
31 /// The `revlog` data format is not supported.
32 UnknowRevlogDataFormat(u8),
33 }
34
35 /// A DebugData error
36 #[derive(Debug)]
37 pub struct DebugDataError {
38 /// Kind of error encountered by DebugData
39 pub kind: DebugDataErrorKind,
40 }
41
42 impl From<DebugDataErrorKind> for DebugDataError {
43 fn from(kind: DebugDataErrorKind) -> Self {
44 DebugDataError { kind }
45 }
46 }
47
48 impl From<find_root::FindRootError> for DebugDataError {
49 fn from(err: find_root::FindRootError) -> Self {
50 let kind = DebugDataErrorKind::FindRootError(err);
51 DebugDataError { kind }
52 }
53 }
54
55 impl From<std::io::Error> for DebugDataError {
56 fn from(err: std::io::Error) -> Self {
57 let kind = DebugDataErrorKind::IoError(err);
58 DebugDataError { kind }
59 }
60 }
61
62 impl From<RevlogError> for DebugDataError {
63 fn from(err: RevlogError) -> Self {
64 match err {
65 RevlogError::IoError(err) => DebugDataErrorKind::IoError(err),
66 RevlogError::UnsuportedVersion(version) => {
67 DebugDataErrorKind::UnsuportedRevlogVersion(version)
68 }
69 RevlogError::InvalidRevision => {
70 DebugDataErrorKind::InvalidRevision
71 }
72 RevlogError::Corrupted => DebugDataErrorKind::CorruptedRevlog,
73 RevlogError::UnknowDataFormat(format) => {
74 DebugDataErrorKind::UnknowRevlogDataFormat(format)
75 }
76 }
77 .into()
78 }
79 }
80
81 /// Dump the contents data of a revision.
82 pub struct DebugData<'a> {
83 /// Revision or hash of the revision.
84 rev: &'a str,
85 /// Kind of data to debug.
86 kind: DebugDataKind,
87 }
88
89 impl<'a> DebugData<'a> {
90 pub fn new(rev: &'a str, kind: DebugDataKind) -> Self {
91 DebugData { rev, kind }
92 }
93
94 pub fn run(&mut self) -> Result<Vec<u8>, DebugDataError> {
95 let rev = self
96 .rev
97 .parse::<Revision>()
98 .or(Err(DebugDataErrorKind::InvalidRevision))?;
99
100 let root = find_root::FindRoot::new().run()?;
101 let index_file = match self.kind {
102 DebugDataKind::Changelog => root.join(".hg/store/00changelog.i"),
103 DebugDataKind::Manifest => root.join(".hg/store/00manifest.i"),
104 };
105 let revlog = Revlog::open(&index_file)?;
106 let data = revlog.get_rev_data(rev)?;
107
108 Ok(data)
109 }
110 }
@@ -2,6 +2,7 b''
2 //! An operation is what can be done whereas a command is what is exposed by
2 //! An operation is what can be done whereas a command is what is exposed by
3 //! the cli. A single command can use several operations to achieve its goal.
3 //! the cli. A single command can use several operations to achieve its goal.
4
4
5 mod debugdata;
5 mod dirstate_status;
6 mod dirstate_status;
6 mod find_root;
7 mod find_root;
7 mod list_tracked_files;
8 mod list_tracked_files;
General Comments 0
You need to be logged in to leave comments. Login now