Mercurial > public > mercurial-scm > hg-stable
diff rust/hg-core/src/revlog/revlog.rs @ 46036:8d6164098782
rhg: allow specifying a changeset ID prefix
Differential Revision: https://phab.mercurial-scm.org/D9479
author | Simon Sapin <simon-commits@exyr.org> |
---|---|
date | Mon, 30 Nov 2020 19:34:49 +0100 |
parents | 7252f5237352 |
children | 88e741bf2d93 |
line wrap: on
line diff
--- a/rust/hg-core/src/revlog/revlog.rs Thu Dec 03 13:23:59 2020 -0800 +++ b/rust/hg-core/src/revlog/revlog.rs Mon Nov 30 19:34:49 2020 +0100 @@ -21,6 +21,8 @@ IoError(std::io::Error), UnsuportedVersion(u16), InvalidRevision, + /// Found more than one entry whose ID match the requested prefix + AmbiguousPrefix, Corrupted, UnknowDataFormat(u8), } @@ -93,14 +95,21 @@ pub fn get_node_rev(&self, node: &[u8]) -> Result<Revision, RevlogError> { // This is brute force. But it is fast enough for now. // Optimization will come later. + let mut found_by_prefix = None; for rev in (0..self.len() as Revision).rev() { let index_entry = self.index.get_entry(rev).ok_or(RevlogError::Corrupted)?; - if node == index_entry.hash() { + if index_entry.hash() == node { return Ok(rev); } + if index_entry.hash().starts_with(node) { + if found_by_prefix.is_some() { + return Err(RevlogError::AmbiguousPrefix); + } + found_by_prefix = Some(rev) + } } - Err(RevlogError::InvalidRevision) + found_by_prefix.ok_or(RevlogError::InvalidRevision) } /// Return the full data associated to a revision.