Mercurial > hg
annotate rust/hg-core/src/operations/debugdata.rs @ 46167:8a4914397d02
rust: introduce Repo and Vfs types for filesystem abstraction
This is similar to the corresponding Python classes.
Repo represents a repository and knows the path to the `.hg` directory,
the `store` directory, and the working directory.
Separating these will enable supporting the share extension.
A Vfs is created from a Repo for one of these three directories.
It has filesystem access APIs that take a relative std::path::Path
as a parameter.
Differential Revision: https://phab.mercurial-scm.org/D9596
author | Simon Sapin <simon.sapin@octobus.net> |
---|---|
date | Mon, 14 Dec 2020 16:33:15 +0100 |
parents | dca9cb99971c |
children | 645ee7225fab |
rev | line source |
---|---|
45527
b56df13a0450
hg-core: define a `DebugData` `Operation`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
1 // debugdata.rs |
b56df13a0450
hg-core: define a `DebugData` `Operation`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
2 // |
b56df13a0450
hg-core: define a `DebugData` `Operation`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
3 // Copyright 2020 Antoine Cezar <antoine.cezar@octobus.net> |
b56df13a0450
hg-core: define a `DebugData` `Operation`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
4 // |
b56df13a0450
hg-core: define a `DebugData` `Operation`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
5 // This software may be used and distributed according to the terms of the |
b56df13a0450
hg-core: define a `DebugData` `Operation`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
6 // GNU General Public License version 2 or any later version. |
b56df13a0450
hg-core: define a `DebugData` `Operation`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
7 |
46167
8a4914397d02
rust: introduce Repo and Vfs types for filesystem abstraction
Simon Sapin <simon.sapin@octobus.net>
parents:
46135
diff
changeset
|
8 use crate::repo::Repo; |
45527
b56df13a0450
hg-core: define a `DebugData` `Operation`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
9 use crate::revlog::revlog::{Revlog, RevlogError}; |
46033
88e741bf2d93
rust: use NodePrefix::from_hex instead of hex::decode directly
Simon Sapin <simon-commits@exyr.org>
parents:
46032
diff
changeset
|
10 use crate::revlog::NodePrefix; |
45527
b56df13a0450
hg-core: define a `DebugData` `Operation`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
11 use crate::revlog::Revision; |
b56df13a0450
hg-core: define a `DebugData` `Operation`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
12 |
b56df13a0450
hg-core: define a `DebugData` `Operation`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
13 /// Kind of data to debug |
b56df13a0450
hg-core: define a `DebugData` `Operation`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
14 #[derive(Debug, Copy, Clone)] |
b56df13a0450
hg-core: define a `DebugData` `Operation`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
15 pub enum DebugDataKind { |
b56df13a0450
hg-core: define a `DebugData` `Operation`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
16 Changelog, |
b56df13a0450
hg-core: define a `DebugData` `Operation`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
17 Manifest, |
b56df13a0450
hg-core: define a `DebugData` `Operation`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
18 } |
b56df13a0450
hg-core: define a `DebugData` `Operation`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
19 |
b56df13a0450
hg-core: define a `DebugData` `Operation`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
20 /// Kind of error encountered by DebugData |
b56df13a0450
hg-core: define a `DebugData` `Operation`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
21 #[derive(Debug)] |
b56df13a0450
hg-core: define a `DebugData` `Operation`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
22 pub enum DebugDataErrorKind { |
b56df13a0450
hg-core: define a `DebugData` `Operation`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
23 /// Error when reading a `revlog` file. |
b56df13a0450
hg-core: define a `DebugData` `Operation`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
24 IoError(std::io::Error), |
b56df13a0450
hg-core: define a `DebugData` `Operation`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
25 /// The revision has not been found. |
b56df13a0450
hg-core: define a `DebugData` `Operation`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
26 InvalidRevision, |
46032
8d6164098782
rhg: allow specifying a changeset ID prefix
Simon Sapin <simon-commits@exyr.org>
parents:
45813
diff
changeset
|
27 /// Found more than one revision whose ID match the requested prefix |
8d6164098782
rhg: allow specifying a changeset ID prefix
Simon Sapin <simon-commits@exyr.org>
parents:
45813
diff
changeset
|
28 AmbiguousPrefix, |
45527
b56df13a0450
hg-core: define a `DebugData` `Operation`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
29 /// A `revlog` file is corrupted. |
b56df13a0450
hg-core: define a `DebugData` `Operation`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
30 CorruptedRevlog, |
b56df13a0450
hg-core: define a `DebugData` `Operation`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
31 /// The `revlog` format version is not supported. |
b56df13a0450
hg-core: define a `DebugData` `Operation`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
32 UnsuportedRevlogVersion(u16), |
b56df13a0450
hg-core: define a `DebugData` `Operation`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
33 /// The `revlog` data format is not supported. |
b56df13a0450
hg-core: define a `DebugData` `Operation`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
34 UnknowRevlogDataFormat(u8), |
b56df13a0450
hg-core: define a `DebugData` `Operation`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
35 } |
b56df13a0450
hg-core: define a `DebugData` `Operation`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
36 |
b56df13a0450
hg-core: define a `DebugData` `Operation`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
37 /// A DebugData error |
b56df13a0450
hg-core: define a `DebugData` `Operation`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
38 #[derive(Debug)] |
b56df13a0450
hg-core: define a `DebugData` `Operation`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
39 pub struct DebugDataError { |
b56df13a0450
hg-core: define a `DebugData` `Operation`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
40 /// Kind of error encountered by DebugData |
b56df13a0450
hg-core: define a `DebugData` `Operation`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
41 pub kind: DebugDataErrorKind, |
b56df13a0450
hg-core: define a `DebugData` `Operation`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
42 } |
b56df13a0450
hg-core: define a `DebugData` `Operation`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
43 |
b56df13a0450
hg-core: define a `DebugData` `Operation`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
44 impl From<DebugDataErrorKind> for DebugDataError { |
b56df13a0450
hg-core: define a `DebugData` `Operation`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
45 fn from(kind: DebugDataErrorKind) -> Self { |
b56df13a0450
hg-core: define a `DebugData` `Operation`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
46 DebugDataError { kind } |
b56df13a0450
hg-core: define a `DebugData` `Operation`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
47 } |
b56df13a0450
hg-core: define a `DebugData` `Operation`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
48 } |
b56df13a0450
hg-core: define a `DebugData` `Operation`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
49 |
b56df13a0450
hg-core: define a `DebugData` `Operation`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
50 impl From<std::io::Error> for DebugDataError { |
b56df13a0450
hg-core: define a `DebugData` `Operation`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
51 fn from(err: std::io::Error) -> Self { |
b56df13a0450
hg-core: define a `DebugData` `Operation`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
52 let kind = DebugDataErrorKind::IoError(err); |
b56df13a0450
hg-core: define a `DebugData` `Operation`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
53 DebugDataError { kind } |
b56df13a0450
hg-core: define a `DebugData` `Operation`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
54 } |
b56df13a0450
hg-core: define a `DebugData` `Operation`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
55 } |
b56df13a0450
hg-core: define a `DebugData` `Operation`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
56 |
b56df13a0450
hg-core: define a `DebugData` `Operation`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
57 impl From<RevlogError> for DebugDataError { |
b56df13a0450
hg-core: define a `DebugData` `Operation`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
58 fn from(err: RevlogError) -> Self { |
b56df13a0450
hg-core: define a `DebugData` `Operation`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
59 match err { |
b56df13a0450
hg-core: define a `DebugData` `Operation`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
60 RevlogError::IoError(err) => DebugDataErrorKind::IoError(err), |
b56df13a0450
hg-core: define a `DebugData` `Operation`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
61 RevlogError::UnsuportedVersion(version) => { |
b56df13a0450
hg-core: define a `DebugData` `Operation`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
62 DebugDataErrorKind::UnsuportedRevlogVersion(version) |
b56df13a0450
hg-core: define a `DebugData` `Operation`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
63 } |
b56df13a0450
hg-core: define a `DebugData` `Operation`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
64 RevlogError::InvalidRevision => { |
b56df13a0450
hg-core: define a `DebugData` `Operation`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
65 DebugDataErrorKind::InvalidRevision |
b56df13a0450
hg-core: define a `DebugData` `Operation`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
66 } |
46032
8d6164098782
rhg: allow specifying a changeset ID prefix
Simon Sapin <simon-commits@exyr.org>
parents:
45813
diff
changeset
|
67 RevlogError::AmbiguousPrefix => { |
8d6164098782
rhg: allow specifying a changeset ID prefix
Simon Sapin <simon-commits@exyr.org>
parents:
45813
diff
changeset
|
68 DebugDataErrorKind::AmbiguousPrefix |
8d6164098782
rhg: allow specifying a changeset ID prefix
Simon Sapin <simon-commits@exyr.org>
parents:
45813
diff
changeset
|
69 } |
45527
b56df13a0450
hg-core: define a `DebugData` `Operation`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
70 RevlogError::Corrupted => DebugDataErrorKind::CorruptedRevlog, |
b56df13a0450
hg-core: define a `DebugData` `Operation`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
71 RevlogError::UnknowDataFormat(format) => { |
b56df13a0450
hg-core: define a `DebugData` `Operation`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
72 DebugDataErrorKind::UnknowRevlogDataFormat(format) |
b56df13a0450
hg-core: define a `DebugData` `Operation`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
73 } |
b56df13a0450
hg-core: define a `DebugData` `Operation`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
74 } |
b56df13a0450
hg-core: define a `DebugData` `Operation`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
75 .into() |
b56df13a0450
hg-core: define a `DebugData` `Operation`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
76 } |
b56df13a0450
hg-core: define a `DebugData` `Operation`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
77 } |
b56df13a0450
hg-core: define a `DebugData` `Operation`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
78 |
b56df13a0450
hg-core: define a `DebugData` `Operation`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
79 /// Dump the contents data of a revision. |
46135
dca9cb99971c
rust: replace most "operation" structs with functions
Simon Sapin <simon.sapin@octobus.net>
parents:
46033
diff
changeset
|
80 pub fn debug_data( |
46167
8a4914397d02
rust: introduce Repo and Vfs types for filesystem abstraction
Simon Sapin <simon.sapin@octobus.net>
parents:
46135
diff
changeset
|
81 repo: &Repo, |
46135
dca9cb99971c
rust: replace most "operation" structs with functions
Simon Sapin <simon.sapin@octobus.net>
parents:
46033
diff
changeset
|
82 rev: &str, |
45527
b56df13a0450
hg-core: define a `DebugData` `Operation`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
83 kind: DebugDataKind, |
46135
dca9cb99971c
rust: replace most "operation" structs with functions
Simon Sapin <simon.sapin@octobus.net>
parents:
46033
diff
changeset
|
84 ) -> Result<Vec<u8>, DebugDataError> { |
dca9cb99971c
rust: replace most "operation" structs with functions
Simon Sapin <simon.sapin@octobus.net>
parents:
46033
diff
changeset
|
85 let index_file = match kind { |
46167
8a4914397d02
rust: introduce Repo and Vfs types for filesystem abstraction
Simon Sapin <simon.sapin@octobus.net>
parents:
46135
diff
changeset
|
86 DebugDataKind::Changelog => "00changelog.i", |
8a4914397d02
rust: introduce Repo and Vfs types for filesystem abstraction
Simon Sapin <simon.sapin@octobus.net>
parents:
46135
diff
changeset
|
87 DebugDataKind::Manifest => "00manifest.i", |
46135
dca9cb99971c
rust: replace most "operation" structs with functions
Simon Sapin <simon.sapin@octobus.net>
parents:
46033
diff
changeset
|
88 }; |
46167
8a4914397d02
rust: introduce Repo and Vfs types for filesystem abstraction
Simon Sapin <simon.sapin@octobus.net>
parents:
46135
diff
changeset
|
89 let revlog = Revlog::open(repo, index_file, None)?; |
45527
b56df13a0450
hg-core: define a `DebugData` `Operation`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
90 |
46135
dca9cb99971c
rust: replace most "operation" structs with functions
Simon Sapin <simon.sapin@octobus.net>
parents:
46033
diff
changeset
|
91 let data = match rev.parse::<Revision>() { |
dca9cb99971c
rust: replace most "operation" structs with functions
Simon Sapin <simon.sapin@octobus.net>
parents:
46033
diff
changeset
|
92 Ok(rev) => revlog.get_rev_data(rev)?, |
dca9cb99971c
rust: replace most "operation" structs with functions
Simon Sapin <simon.sapin@octobus.net>
parents:
46033
diff
changeset
|
93 _ => { |
dca9cb99971c
rust: replace most "operation" structs with functions
Simon Sapin <simon.sapin@octobus.net>
parents:
46033
diff
changeset
|
94 let node = NodePrefix::from_hex(&rev) |
dca9cb99971c
rust: replace most "operation" structs with functions
Simon Sapin <simon.sapin@octobus.net>
parents:
46033
diff
changeset
|
95 .map_err(|_| DebugDataErrorKind::InvalidRevision)?; |
dca9cb99971c
rust: replace most "operation" structs with functions
Simon Sapin <simon.sapin@octobus.net>
parents:
46033
diff
changeset
|
96 let rev = revlog.get_node_rev(node.borrow())?; |
dca9cb99971c
rust: replace most "operation" structs with functions
Simon Sapin <simon.sapin@octobus.net>
parents:
46033
diff
changeset
|
97 revlog.get_rev_data(rev)? |
dca9cb99971c
rust: replace most "operation" structs with functions
Simon Sapin <simon.sapin@octobus.net>
parents:
46033
diff
changeset
|
98 } |
dca9cb99971c
rust: replace most "operation" structs with functions
Simon Sapin <simon.sapin@octobus.net>
parents:
46033
diff
changeset
|
99 }; |
45813
57dc78861196
rhg: add full node id support for `debugdata` command
Antoine cezar<acezar@chwitlabs.fr>
parents:
45806
diff
changeset
|
100 |
46135
dca9cb99971c
rust: replace most "operation" structs with functions
Simon Sapin <simon.sapin@octobus.net>
parents:
46033
diff
changeset
|
101 Ok(data) |
45527
b56df13a0450
hg-core: define a `DebugData` `Operation`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
102 } |