From 43e016a7d12c4f53ec864e5cd18cb3516fa2991e Mon Sep 17 00:00:00 2001 From: Yuya Nishihara Date: Wed, 27 Dec 2023 22:37:13 +0900 Subject: [PATCH] index: add explicit reindexing method that can propagate error --- cli/src/commands/debug.rs | 10 ++++------ lib/src/default_index/store.rs | 13 +++++++++++++ 2 files changed, 17 insertions(+), 6 deletions(-) diff --git a/cli/src/commands/debug.rs b/cli/src/commands/debug.rs index 6ce2d2c9d..b86ed84eb 100644 --- a/cli/src/commands/debug.rs +++ b/cli/src/commands/debug.rs @@ -20,6 +20,7 @@ use clap::Subcommand; use jj_lib::backend::ObjectId; use jj_lib::default_index::{AsCompositeIndex as _, DefaultIndexStore, DefaultReadonlyIndex}; use jj_lib::local_working_copy::LocalWorkingCopy; +use jj_lib::repo::Repo; use jj_lib::revset; use jj_lib::working_copy::WorkingCopy; @@ -240,12 +241,9 @@ fn cmd_debug_reindex( default_index_store .reinit() .map_err(|err| CommandError::InternalError(err.to_string()))?; - let repo = repo.reload_at(repo.operation())?; - let default_index: &DefaultReadonlyIndex = repo - .readonly_index() - .as_any() - .downcast_ref() - .expect("Default index should be a DefaultReadonlyIndex"); + let default_index = default_index_store + .build_index_at_operation(repo.operation(), repo.store()) + .map_err(|err| CommandError::InternalError(err.to_string()))?; writeln!( ui.stderr(), "Finished indexing {:?} commits.", diff --git a/lib/src/default_index/store.rs b/lib/src/default_index/store.rs index 6e9f46263..8560fab33 100644 --- a/lib/src/default_index/store.rs +++ b/lib/src/default_index/store.rs @@ -119,6 +119,19 @@ impl DefaultIndexStore { .map_err(DefaultIndexStoreError::LoadIndex) } + /// Rebuilds index for the given `operation`. + /// + /// The index to be built will be calculated from one of the ancestor + /// operations if exists. Use `reinit()` to rebuild index from scratch. + pub fn build_index_at_operation( + &self, + operation: &Operation, + store: &Arc, + ) -> Result { + let index_segment = self.build_index_segments_at_operation(operation, store)?; + Ok(DefaultReadonlyIndex::from_segment(index_segment)) + } + #[tracing::instrument(skip(self, store))] fn build_index_segments_at_operation( &self,