Skip to main content

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;
19
20use crate::cli_util::CommandHelper;
21use crate::command_error::CommandError;
22use crate::command_error::internal_error;
23use crate::command_error::user_error;
24use crate::ui::Ui;
25
26/// Rebuild commit index
27#[derive(clap::Args, Clone, Debug)]
28pub struct DebugReindexArgs {}
29
30pub async fn cmd_debug_reindex(
31    ui: &mut Ui,
32    command: &CommandHelper,
33    _args: &DebugReindexArgs,
34) -> Result<(), CommandError> {
35    // Resolve the operation without loading the repo. The index might have to
36    // be rebuilt while loading the repo.
37    let workspace = command.load_workspace()?;
38    let repo_loader = workspace.repo_loader();
39    let op = command.resolve_operation(ui, repo_loader, workspace.workspace_name())?;
40    let index_store = repo_loader.index_store();
41    if let Some(default_index_store) = index_store.downcast_ref::<DefaultIndexStore>() {
42        default_index_store.reinit().map_err(internal_error)?;
43        let default_index = default_index_store
44            .build_index_at_operation(&op, repo_loader.store())
45            .await
46            .map_err(internal_error)?;
47        writeln!(
48            ui.status(),
49            "Finished indexing {} commits.",
50            default_index.num_commits()
51        )?;
52    } else {
53        return Err(user_error(format!(
54            "Cannot reindex indexes of type '{}'",
55            index_store.name()
56        )));
57    }
58    Ok(())
59}