##// END OF EJS Templates
rust-changelog: don't skip empty lines when iterating over changeset lines...
Martin von Zweigbergk -
r49936:fb82b5cb default
parent child Browse files
Show More
@@ -1,67 +1,68 b''
1 use crate::errors::HgError;
1 use crate::errors::HgError;
2 use crate::repo::Repo;
2 use crate::repo::Repo;
3 use crate::revlog::node::NULL_NODE;
3 use crate::revlog::node::NULL_NODE;
4 use crate::revlog::revlog::{Revlog, RevlogError};
4 use crate::revlog::revlog::{Revlog, RevlogError};
5 use crate::revlog::Revision;
5 use crate::revlog::Revision;
6 use crate::revlog::{Node, NodePrefix};
6 use crate::revlog::{Node, NodePrefix};
7
7
8 /// A specialized `Revlog` to work with `changelog` data format.
8 /// A specialized `Revlog` to work with `changelog` data format.
9 pub struct Changelog {
9 pub struct Changelog {
10 /// The generic `revlog` format.
10 /// The generic `revlog` format.
11 pub(crate) revlog: Revlog,
11 pub(crate) revlog: Revlog,
12 }
12 }
13
13
14 impl Changelog {
14 impl Changelog {
15 /// Open the `changelog` of a repository given by its root.
15 /// Open the `changelog` of a repository given by its root.
16 pub fn open(repo: &Repo) -> Result<Self, HgError> {
16 pub fn open(repo: &Repo) -> Result<Self, HgError> {
17 let revlog = Revlog::open(repo, "00changelog.i", None)?;
17 let revlog = Revlog::open(repo, "00changelog.i", None)?;
18 Ok(Self { revlog })
18 Ok(Self { revlog })
19 }
19 }
20
20
21 /// Return the `ChangelogEntry` for the given node ID.
21 /// Return the `ChangelogEntry` for the given node ID.
22 pub fn data_for_node(
22 pub fn data_for_node(
23 &self,
23 &self,
24 node: NodePrefix,
24 node: NodePrefix,
25 ) -> Result<ChangelogRevisionData, RevlogError> {
25 ) -> Result<ChangelogRevisionData, RevlogError> {
26 let rev = self.revlog.rev_from_node(node)?;
26 let rev = self.revlog.rev_from_node(node)?;
27 self.data_for_rev(rev)
27 self.data_for_rev(rev)
28 }
28 }
29
29
30 /// Return the `ChangelogEntry` of the given revision number.
30 /// Return the `ChangelogEntry` of the given revision number.
31 pub fn data_for_rev(
31 pub fn data_for_rev(
32 &self,
32 &self,
33 rev: Revision,
33 rev: Revision,
34 ) -> Result<ChangelogRevisionData, RevlogError> {
34 ) -> Result<ChangelogRevisionData, RevlogError> {
35 let bytes = self.revlog.get_rev_data(rev)?.into_owned();
35 let bytes = self.revlog.get_rev_data(rev)?.into_owned();
36 Ok(ChangelogRevisionData { bytes })
36 Ok(ChangelogRevisionData { bytes })
37 }
37 }
38
38
39 pub fn node_from_rev(&self, rev: Revision) -> Option<&Node> {
39 pub fn node_from_rev(&self, rev: Revision) -> Option<&Node> {
40 self.revlog.node_from_rev(rev)
40 self.revlog.node_from_rev(rev)
41 }
41 }
42 }
42 }
43
43
44 /// `Changelog` entry which knows how to interpret the `changelog` data bytes.
44 /// `Changelog` entry which knows how to interpret the `changelog` data bytes.
45 #[derive(Debug)]
45 #[derive(Debug)]
46 pub struct ChangelogRevisionData {
46 pub struct ChangelogRevisionData {
47 /// The data bytes of the `changelog` entry.
47 /// The data bytes of the `changelog` entry.
48 bytes: Vec<u8>,
48 bytes: Vec<u8>,
49 }
49 }
50
50
51 impl ChangelogRevisionData {
51 impl ChangelogRevisionData {
52 /// Return an iterator over the lines of the entry.
52 /// Return an iterator over the lines of the entry.
53 pub fn lines(&self) -> impl Iterator<Item = &[u8]> {
53 pub fn lines(&self) -> impl Iterator<Item = &[u8]> {
54 self.bytes
54 self.bytes.split(|b| b == &b'\n')
55 .split(|b| b == &b'\n')
56 .filter(|line| !line.is_empty())
57 }
55 }
58
56
59 /// Return the node id of the `manifest` referenced by this `changelog`
57 /// Return the node id of the `manifest` referenced by this `changelog`
60 /// entry.
58 /// entry.
61 pub fn manifest_node(&self) -> Result<Node, HgError> {
59 pub fn manifest_node(&self) -> Result<Node, HgError> {
62 match self.lines().next() {
60 let manifest_node_hex =
63 None => Ok(NULL_NODE),
61 self.lines().next().expect("Empty iterator from split()?");
64 Some(x) => Node::from_hex_for_repo(x),
62 if manifest_node_hex.is_empty() {
63 Ok(NULL_NODE)
64 } else {
65 Node::from_hex_for_repo(manifest_node_hex)
65 }
66 }
66 }
67 }
67 }
68 }
General Comments 0
You need to be logged in to leave comments. Login now