From 3d41237efe9b5a7c234c4894c1b2966a62ad995f Mon Sep 17 00:00:00 2001 From: Yuya Nishihara Date: Mon, 30 Sep 2024 12:28:26 +0900 Subject: [PATCH] id_prefix: add empty disambiguation index for convenience Callers might want to fall back to an empty index if context.populate() failed. --- lib/src/id_prefix.rs | 5 +++++ lib/src/revset.rs | 19 +++++++++---------- 2 files changed, 14 insertions(+), 10 deletions(-) diff --git a/lib/src/id_prefix.rs b/lib/src/id_prefix.rs index 44c4e7a1f..cd13782c4 100644 --- a/lib/src/id_prefix.rs +++ b/lib/src/id_prefix.rs @@ -145,6 +145,11 @@ pub struct IdPrefixIndex<'a> { } impl IdPrefixIndex<'_> { + /// Returns an empty index that just falls back to a provided `repo`. + pub const fn empty() -> IdPrefixIndex<'static> { + IdPrefixIndex { indexes: None } + } + /// Resolve an unambiguous commit ID prefix. pub fn resolve_commit_prefix( &self, diff --git a/lib/src/revset.rs b/lib/src/revset.rs index 73b36704a..5d9693882 100644 --- a/lib/src/revset.rs +++ b/lib/src/revset.rs @@ -41,6 +41,7 @@ use crate::fileset::FilesetExpression; use crate::graph::GraphEdge; use crate::hex_util::to_forward_hex; use crate::id_prefix::IdPrefixContext; +use crate::id_prefix::IdPrefixIndex; use crate::object_id::HexPrefix; use crate::object_id::PrefixResolution; use crate::op_store::RemoteRefState; @@ -1624,11 +1625,10 @@ impl PartialSymbolResolver for CommitPrefixResolver<'_> { symbol: &str, ) -> Result>, RevsetResolutionError> { if let Some(prefix) = HexPrefix::new(symbol) { - let resolution = match self.context.map(|ctx| ctx.populate(repo)) { - Some(index) => index.resolve_commit_prefix(repo, &prefix), - None => repo.index().resolve_commit_id_prefix(&prefix), - }; - match resolution { + let index = self + .context + .map_or(IdPrefixIndex::empty(), |ctx| ctx.populate(repo)); + match index.resolve_commit_prefix(repo, &prefix) { PrefixResolution::AmbiguousMatch => Err( RevsetResolutionError::AmbiguousCommitIdPrefix(symbol.to_owned()), ), @@ -1653,11 +1653,10 @@ impl PartialSymbolResolver for ChangePrefixResolver<'_> { symbol: &str, ) -> Result>, RevsetResolutionError> { if let Some(prefix) = to_forward_hex(symbol).as_deref().and_then(HexPrefix::new) { - let resolution = match self.context.map(|ctx| ctx.populate(repo)) { - Some(index) => index.resolve_change_prefix(repo, &prefix), - None => repo.resolve_change_id_prefix(&prefix), - }; - match resolution { + let index = self + .context + .map_or(IdPrefixIndex::empty(), |ctx| ctx.populate(repo)); + match index.resolve_change_prefix(repo, &prefix) { PrefixResolution::AmbiguousMatch => Err( RevsetResolutionError::AmbiguousChangeIdPrefix(symbol.to_owned()), ),