Patchwork D12564: rust-revlog: make `IndexEntryBuilder` build a whole entry

login
register
mail settings
Submitter phabricator
Date April 16, 2022, 5:47 a.m.
Message ID <differential-rev-PHID-DREV-wt3qs5m6zckpgut2nqka-req@mercurial-scm.org>
Download mbox | patch
Permalink /patch/50912/
State New
Headers show

Comments

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

REVISION SUMMARY
  The `IndexEntryBuilder` we have in tests only built part of a revlog
  entry before this patch. It's more useful if it can build a full
  entry. As part of fixing that, I also added a (non-test)
  `IndexEntry::link_revision()`.

REPOSITORY
  rHG Mercurial

BRANCH
  default

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

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

CHANGE DETAILS




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

Patch

diff --git a/rust/hg-core/src/revlog/index.rs b/rust/hg-core/src/revlog/index.rs
--- a/rust/hg-core/src/revlog/index.rs
+++ b/rust/hg-core/src/revlog/index.rs
@@ -282,6 +282,10 @@ 
         BigEndian::read_i32(&self.bytes[16..])
     }
 
+    pub fn link_revision(&self) -> Revision {
+        BigEndian::read_i32(&self.bytes[20..])
+    }
+
     pub fn p1(&self) -> Revision {
         BigEndian::read_i32(&self.bytes[24..])
     }
@@ -302,6 +306,7 @@ 
 #[cfg(test)]
 mod tests {
     use super::*;
+    use crate::node::NULL_NODE;
 
     #[cfg(test)]
     #[derive(Debug, Copy, Clone)]
@@ -314,6 +319,10 @@ 
         compressed_len: usize,
         uncompressed_len: usize,
         base_revision_or_base_of_delta_chain: Revision,
+        link_revision: Revision,
+        p1: Revision,
+        p2: Revision,
+        node: Node,
     }
 
     #[cfg(test)]
@@ -328,6 +337,10 @@ 
                 compressed_len: 0,
                 uncompressed_len: 0,
                 base_revision_or_base_of_delta_chain: 0,
+                link_revision: 0,
+                p1: NULL_REVISION,
+                p2: NULL_REVISION,
+                node: NULL_NODE,
             }
         }
 
@@ -374,6 +387,26 @@ 
             self
         }
 
+        pub fn with_link_revision(&mut self, value: Revision) -> &mut Self {
+            self.link_revision = value;
+            self
+        }
+
+        pub fn with_p1(&mut self, value: Revision) -> &mut Self {
+            self.p1 = value;
+            self
+        }
+
+        pub fn with_p2(&mut self, value: Revision) -> &mut Self {
+            self.p2 = value;
+            self
+        }
+
+        pub fn with_node(&mut self, value: Node) -> &mut Self {
+            self.node = value;
+            self
+        }
+
         pub fn build(&self) -> Vec<u8> {
             let mut bytes = Vec::with_capacity(INDEX_ENTRY_SIZE);
             if self.is_first {
@@ -396,6 +429,11 @@ 
             bytes.extend(
                 &self.base_revision_or_base_of_delta_chain.to_be_bytes(),
             );
+            bytes.extend(&self.link_revision.to_be_bytes());
+            bytes.extend(&self.p1.to_be_bytes());
+            bytes.extend(&self.p2.to_be_bytes());
+            bytes.extend(self.node.as_bytes());
+            bytes.extend(vec![0u8; 12]);
             bytes
         }
     }
@@ -514,6 +552,56 @@ 
     }
 
     #[test]
+    fn link_revision_test() {
+        let bytes = IndexEntryBuilder::new().with_link_revision(123).build();
+
+        let entry = IndexEntry {
+            bytes: &bytes,
+            offset_override: None,
+        };
+
+        assert_eq!(entry.link_revision(), 123);
+    }
+
+    #[test]
+    fn p1_test() {
+        let bytes = IndexEntryBuilder::new().with_p1(123).build();
+
+        let entry = IndexEntry {
+            bytes: &bytes,
+            offset_override: None,
+        };
+
+        assert_eq!(entry.p1(), 123);
+    }
+
+    #[test]
+    fn p2_test() {
+        let bytes = IndexEntryBuilder::new().with_p2(123).build();
+
+        let entry = IndexEntry {
+            bytes: &bytes,
+            offset_override: None,
+        };
+
+        assert_eq!(entry.p2(), 123);
+    }
+
+    #[test]
+    fn node_test() {
+        let node = Node::from_hex("0123456789012345678901234567890123456789")
+            .unwrap();
+        let bytes = IndexEntryBuilder::new().with_node(node).build();
+
+        let entry = IndexEntry {
+            bytes: &bytes,
+            offset_override: None,
+        };
+
+        assert_eq!(*entry.hash(), node);
+    }
+
+    #[test]
     fn version_test() {
         let bytes = IndexEntryBuilder::new()
             .is_first(true)