##// END OF EJS Templates
hg-core: add `Changlog` a specialized `Revlog`...
Antoine Cezar -
r46103:c2317b76 default
parent child Browse files
Show More
@@ -0,0 +1,58 b''
1 use crate::revlog::revlog::{Revlog, RevlogError};
2 use crate::revlog::Revision;
3 use std::path::PathBuf;
4
5 /// A specialized `Revlog` to work with `changelog` data format.
6 pub struct Changelog {
7 /// The generic `revlog` format.
8 revlog: Revlog,
9 }
10
11 impl Changelog {
12 /// Open the `changelog` of a repository given by its root.
13 pub fn open(root: &PathBuf) -> Result<Self, RevlogError> {
14 let index_file = root.join(".hg/store/00changelog.i");
15 let revlog = Revlog::open(&index_file)?;
16 Ok(Self { revlog })
17 }
18
19 /// Return the `ChangelogEntry` a given node id.
20 pub fn get_node(
21 &self,
22 node: &[u8],
23 ) -> Result<ChangelogEntry, RevlogError> {
24 let rev = self.revlog.get_node_rev(node)?;
25 self.get_rev(rev)
26 }
27
28 /// Return the `ChangelogEntry` of a given node revision.
29 pub fn get_rev(
30 &self,
31 rev: Revision,
32 ) -> Result<ChangelogEntry, RevlogError> {
33 let bytes = self.revlog.get_rev_data(rev)?;
34 Ok(ChangelogEntry { bytes })
35 }
36 }
37
38 /// `Changelog` entry which knows how to interpret the `changelog` data bytes.
39 #[derive(Debug)]
40 pub struct ChangelogEntry {
41 /// The data bytes of the `changelog` entry.
42 bytes: Vec<u8>,
43 }
44
45 impl ChangelogEntry {
46 /// Return an iterator over the lines of the entry.
47 pub fn lines(&self) -> impl Iterator<Item = &[u8]> {
48 self.bytes
49 .split(|b| b == &b'\n')
50 .filter(|line| !line.is_empty())
51 }
52
53 /// Return the node id of the `manifest` referenced by this `changelog`
54 /// entry.
55 pub fn manifest_node(&self) -> Result<&[u8], RevlogError> {
56 self.lines().next().ok_or(RevlogError::Corrupted)
57 }
58 }
@@ -8,6 +8,7 b''
8 pub mod node;
8 pub mod node;
9 pub mod nodemap;
9 pub mod nodemap;
10 pub use node::{Node, NodeError, NodePrefix, NodePrefixRef};
10 pub use node::{Node, NodeError, NodePrefix, NodePrefixRef};
11 pub mod changelog;
11 pub mod index;
12 pub mod index;
12 pub mod patch;
13 pub mod patch;
13 pub mod revlog;
14 pub mod revlog;
General Comments 0
You need to be logged in to leave comments. Login now