jj_cli/commands/debug/
reindex.rs

1// Copyright 2023 The Jujutsu Authors
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// https://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15use std::fmt::Debug;
16use std::io::Write as _;
17
18use jj_lib::default_index::DefaultIndexStore;
19use pollster::FutureExt as _;
20
21use crate::cli_util::CommandHelper;
22use crate::command_error::CommandError;
23use crate::command_error::internal_error;
24use crate::command_error::user_error;
25use crate::ui::Ui;
26
27/// Rebuild commit index
28#[derive(clap::Args, Clone, Debug)]
29pub struct DebugReindexArgs {}
30
31pub fn cmd_debug_reindex(
32    ui: &mut Ui,
33    command: &CommandHelper,
34    _args: &DebugReindexArgs,
35) -> Result<(), CommandError> {
36    // Resolve the operation without loading the repo. The index might have to
37    // be rebuilt while loading the repo.
38    let workspace = command.load_workspace()?;
39    let repo_loader = workspace.repo_loader();
40    let op = command.resolve_operation(ui, repo_loader)?;
41    let index_store = repo_loader.index_store();
42    if let Some(default_index_store) = index_store.downcast_ref::<DefaultIndexStore>() {
43        default_index_store.reinit().map_err(internal_error)?;
44        let default_index = default_index_store
45            .build_index_at_operation(&op, repo_loader.store())
46            .block_on()
47            .map_err(internal_error)?;
48        writeln!(
49            ui.status(),
50            "Finished indexing {} commits.",
51            default_index.num_commits()
52        )?;
53    } else {
54        return Err(user_error(format!(
55            "Cannot reindex indexes of type '{}'",
56            index_store.name()
57        )));
58    }
59    Ok(())
60}