# HG changeset patch # User Antoine Cezar # Date 1600440728 -7200 # Node ID 4f11a67a12fbe0ca95c586bc9ccfe10306519f92 # Parent 89ac95bd49932aeccc45673836bd88fcec2570ac hg-core: add `Revlog.get_node_rev` Find the revision of a node given its full hash. Differential Revision: https://phab.mercurial-scm.org/D9012 diff -r 89ac95bd4993 -r 4f11a67a12fb rust/hg-core/src/revlog/revlog.rs --- a/rust/hg-core/src/revlog/revlog.rs Wed Sep 09 14:50:58 2020 +0200 +++ b/rust/hg-core/src/revlog/revlog.rs Fri Sep 18 16:52:08 2020 +0200 @@ -79,6 +79,32 @@ }) } + /// Return number of entries of the `Revlog`. + pub fn len(&self) -> usize { + self.index().len() + } + + /// Returns `true` if the `Revlog` has zero `entries`. + pub fn is_empty(&self) -> bool { + self.index().is_empty() + } + + /// Return the full data associated to a node. + #[timed] + pub fn get_node_rev(&self, node: &[u8]) -> Result { + let index = self.index(); + // This is brute force. But it is fast enough for now. + // Optimization will come later. + for rev in (0..self.len() as Revision).rev() { + let index_entry = + index.get_entry(rev).ok_or(RevlogError::Corrupted)?; + if node == index_entry.hash() { + return Ok(rev); + } + } + Err(RevlogError::InvalidRevision) + } + /// Return the full data associated to a revision. /// /// All entries required to build the final data out of deltas will be