Debian Codegraph @

cli/src/commands/debug/index.rs

raw
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
15 use std::fmt::Debug;
16 use std::io::Write as _;
17
18 use jj_lib::default_index::DefaultReadonlyIndex;
19
20 use crate::cli_util::CommandHelper;
21 use crate::command_error::CommandError;
22 use crate::command_error::internal_error;
23 use crate::command_error::user_error;
24 use crate::ui::Ui;
25
26 /// Show commit index stats
27 #[derive(clap::Args, Clone, Debug)]
28 pub struct DebugIndexArgs {}
29
30 pub async fn cmd_debug_index(
31 ui: &mut Ui,
32 command: &CommandHelper,
33 _args: &DebugIndexArgs,
34 ) -> Result<(), CommandError> {
35 // Resolve the operation without loading the repo, so this command won't
36 // update the index.
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 let index = index_store
42 .get_index_at_op(&op, repo_loader.store())
43 .await
44 .map_err(internal_error)?;
45 if let Some(default_index) = index.downcast_ref::<DefaultReadonlyIndex>() {
46 let stats = default_index.stats();
47 writeln!(ui.stdout(), "=== Commits ===")?;
48 writeln!(ui.stdout(), "Number of commits: {}", stats.num_commits)?;
49 writeln!(ui.stdout(), "Number of merges: {}", stats.num_merges)?;
50 writeln!(
51 ui.stdout(),
52 "Max generation number: {}",
53 stats.max_generation_number
54 )?;
55 writeln!(ui.stdout(), "Number of heads: {}", stats.num_heads)?;
56 writeln!(ui.stdout(), "Number of changes: {}", stats.num_changes)?;
57 writeln!(ui.stdout(), "Stats per level:")?;
58 for (i, level) in stats.commit_levels.iter().enumerate() {
59 writeln!(ui.stdout(), " Level {i}:")?;
60 writeln!(ui.stdout(), " Number of commits: {}", level.num_commits)?;
61 writeln!(ui.stdout(), " Name: {}", level.name)?;
62 }
63
64 writeln!(ui.stdout(), "=== Changed paths ===")?;
65 if let Some(range) = &stats.changed_path_commits_range {
66 writeln!(ui.stdout(), "Indexed commits: {range:?}")?;
67 } else {
68 writeln!(ui.stdout(), "Indexed commits: none")?;
69 }
70 writeln!(ui.stdout(), "Stats per level:")?;
71 for (i, level) in stats.changed_path_levels.iter().enumerate() {
72 writeln!(ui.stdout(), " Level {i}:")?;
73 writeln!(ui.stdout(), " Number of commits: {}", level.num_commits)?;
74 writeln!(
75 ui.stdout(),
76 " Number of changed paths: {}",
77 level.num_changed_paths
78 )?;
79 writeln!(ui.stdout(), " Number of paths: {}", level.num_paths)?;
80 writeln!(ui.stdout(), " Name: {}", level.name)?;
81 }
82 } else {
83 return Err(user_error(format!(
84 "Cannot get stats for indexes of type '{}'",
85 index_store.name()
86 )));
87 }
88 Ok(())
89 }