Patchwork D12438: rhg: remove special parsing of empty changelog data for null rev

login
register
mail settings
Submitter phabricator
Date April 5, 2022, 6:11 p.m.
Message ID <differential-rev-PHID-DREV-bt5wzzboc2r3fpjtavzp-req@mercurial-scm.org>
Download mbox | patch
Permalink /patch/50784/
State New
Headers show

Comments

phabricator - April 5, 2022, 6:11 p.m.
martinvonz created this revision.
Herald added a reviewer: hg-reviewers.
Herald added a subscriber: mercurial-patches.

REVISION SUMMARY
  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.

REPOSITORY
  rHG Mercurial

BRANCH
  default

REVISION DETAIL
  https://phab.mercurial-scm.org/D12438

AFFECTED FILES
  rust/hg-core/src/revlog/changelog.rs

CHANGE DETAILS




To: martinvonz, #hg-reviewers
Cc: mercurial-patches, mercurial-devel

Patch

diff --git a/rust/hg-core/src/revlog/changelog.rs b/rust/hg-core/src/revlog/changelog.rs
--- a/rust/hg-core/src/revlog/changelog.rs
+++ b/rust/hg-core/src/revlog/changelog.rs
@@ -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')
@@ -58,10 +71,6 @@ 
     /// entry.
     pub fn manifest_node(&self) -> Result<Node, HgError> {
         let manifest_node_hex = self.lines().next().unwrap();
-        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)
     }
 }