rust-changelog: remove special parsing of empty changelog data for null rev
For the null revision, `Revlog::get_rev_data()` will return an empty
string (of bytes). We currently handle that case in
`ChangelogRevisionData::manifest_node()`. However, it's going to be
ugly to have special handling for the null revision for each future
method on `ChangelogRevisionData`. This patch therefore restructures
the code so we instead initialize the struct with valid data for the
null revision.
Differential Revision: https://phab.mercurial-scm.org/D12438
--- a/rust/hg-core/src/revlog/changelog.rs Thu Mar 31 22:06:26 2022 -0700
+++ b/rust/hg-core/src/revlog/changelog.rs Mon Apr 04 23:27:16 2022 -0700
@@ -1,6 +1,5 @@
use crate::errors::HgError;
use crate::repo::Repo;
-use crate::revlog::node::NULL_NODE;
use crate::revlog::revlog::{Revlog, RevlogError};
use crate::revlog::Revision;
use crate::revlog::{Node, NodePrefix};
@@ -33,7 +32,11 @@
rev: Revision,
) -> Result<ChangelogRevisionData, RevlogError> {
let bytes = self.revlog.get_rev_data(rev)?.into_owned();
- Ok(ChangelogRevisionData { bytes })
+ if bytes.is_empty() {
+ Ok(ChangelogRevisionData::null())
+ } else {
+ Ok(ChangelogRevisionData::new(bytes))
+ }
}
pub fn node_from_rev(&self, rev: Revision) -> Option<&Node> {
@@ -49,6 +52,16 @@
}
impl ChangelogRevisionData {
+ fn new(bytes: Vec<u8>) -> Self {
+ Self { bytes }
+ }
+
+ fn null() -> Self {
+ Self::new(
+ b"0000000000000000000000000000000000000000\n\n0 0\n\n".to_vec(),
+ )
+ }
+
/// Return an iterator over the lines of the entry.
pub fn lines(&self) -> impl Iterator<Item = &[u8]> {
self.bytes.split(|b| b == &b'\n')
@@ -59,10 +72,6 @@
pub fn manifest_node(&self) -> Result<Node, HgError> {
let manifest_node_hex =
self.lines().next().expect("Empty iterator from split()?");
- if manifest_node_hex.is_empty() {
- Ok(NULL_NODE)
- } else {
- Node::from_hex_for_repo(manifest_node_hex)
- }
+ Node::from_hex_for_repo(manifest_node_hex)
}
}