##// 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 }
@@ -1,64 +1,65 b''
1 // Copyright 2018-2020 Georges Racinet <georges.racinet@octobus.net>
1 // Copyright 2018-2020 Georges Racinet <georges.racinet@octobus.net>
2 // and Mercurial contributors
2 // and Mercurial contributors
3 //
3 //
4 // This software may be used and distributed according to the terms of the
4 // This software may be used and distributed according to the terms of the
5 // GNU General Public License version 2 or any later version.
5 // GNU General Public License version 2 or any later version.
6 //! Mercurial concepts for handling revision history
6 //! Mercurial concepts for handling revision history
7
7
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;
14
15
15 /// Mercurial revision numbers
16 /// Mercurial revision numbers
16 ///
17 ///
17 /// As noted in revlog.c, revision numbers are actually encoded in
18 /// As noted in revlog.c, revision numbers are actually encoded in
18 /// 4 bytes, and are liberally converted to ints, whence the i32
19 /// 4 bytes, and are liberally converted to ints, whence the i32
19 pub type Revision = i32;
20 pub type Revision = i32;
20
21
21 /// Marker expressing the absence of a parent
22 /// Marker expressing the absence of a parent
22 ///
23 ///
23 /// Independently of the actual representation, `NULL_REVISION` is guaranteed
24 /// Independently of the actual representation, `NULL_REVISION` is guaranteed
24 /// to be smaller than all existing revisions.
25 /// to be smaller than all existing revisions.
25 pub const NULL_REVISION: Revision = -1;
26 pub const NULL_REVISION: Revision = -1;
26
27
27 /// Same as `mercurial.node.wdirrev`
28 /// Same as `mercurial.node.wdirrev`
28 ///
29 ///
29 /// This is also equal to `i32::max_value()`, but it's better to spell
30 /// This is also equal to `i32::max_value()`, but it's better to spell
30 /// it out explicitely, same as in `mercurial.node`
31 /// it out explicitely, same as in `mercurial.node`
31 #[allow(clippy::unreadable_literal)]
32 #[allow(clippy::unreadable_literal)]
32 pub const WORKING_DIRECTORY_REVISION: Revision = 0x7fffffff;
33 pub const WORKING_DIRECTORY_REVISION: Revision = 0x7fffffff;
33
34
34 /// The simplest expression of what we need of Mercurial DAGs.
35 /// The simplest expression of what we need of Mercurial DAGs.
35 pub trait Graph {
36 pub trait Graph {
36 /// Return the two parents of the given `Revision`.
37 /// Return the two parents of the given `Revision`.
37 ///
38 ///
38 /// Each of the parents can be independently `NULL_REVISION`
39 /// Each of the parents can be independently `NULL_REVISION`
39 fn parents(&self, rev: Revision) -> Result<[Revision; 2], GraphError>;
40 fn parents(&self, rev: Revision) -> Result<[Revision; 2], GraphError>;
40 }
41 }
41
42
42 #[derive(Clone, Debug, PartialEq)]
43 #[derive(Clone, Debug, PartialEq)]
43 pub enum GraphError {
44 pub enum GraphError {
44 ParentOutOfRange(Revision),
45 ParentOutOfRange(Revision),
45 WorkingDirectoryUnsupported,
46 WorkingDirectoryUnsupported,
46 }
47 }
47
48
48 /// The Mercurial Revlog Index
49 /// The Mercurial Revlog Index
49 ///
50 ///
50 /// This is currently limited to the minimal interface that is needed for
51 /// This is currently limited to the minimal interface that is needed for
51 /// the [`nodemap`](nodemap/index.html) module
52 /// the [`nodemap`](nodemap/index.html) module
52 pub trait RevlogIndex {
53 pub trait RevlogIndex {
53 /// Total number of Revisions referenced in this index
54 /// Total number of Revisions referenced in this index
54 fn len(&self) -> usize;
55 fn len(&self) -> usize;
55
56
56 fn is_empty(&self) -> bool {
57 fn is_empty(&self) -> bool {
57 self.len() == 0
58 self.len() == 0
58 }
59 }
59
60
60 /// Return a reference to the Node or `None` if rev is out of bounds
61 /// Return a reference to the Node or `None` if rev is out of bounds
61 ///
62 ///
62 /// `NULL_REVISION` is not considered to be out of bounds.
63 /// `NULL_REVISION` is not considered to be out of bounds.
63 fn node(&self, rev: Revision) -> Option<&Node>;
64 fn node(&self, rev: Revision) -> Option<&Node>;
64 }
65 }
General Comments 0
You need to be logged in to leave comments. Login now