Debian Codegraph @

cli/src/commands/debug/tree.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::backend::BackendResult;
19 use jj_lib::backend::MergedTreeValue;
20 use jj_lib::backend::TreeId;
21 use jj_lib::merge::Merge;
22 use jj_lib::repo::Repo as _;
23 use jj_lib::repo_path::RepoPathBuf;
24
25 use crate::cli_util::CommandHelper;
26 use crate::cli_util::RevisionArg;
27 use crate::command_error::CommandError;
28 use crate::command_error::user_error;
29 use crate::ui::Ui;
30
31 /// List the recursive entries of a tree.
32 #[derive(clap::Args, Clone, Debug)]
33 pub struct DebugTreeArgs {
34 #[arg(long, short = 'r', value_name = "REVSET")]
35 revision: Option<RevisionArg>,
36
37 #[arg(long, conflicts_with = "revision")]
38 id: Option<String>,
39
40 #[arg(long, requires = "id")]
41 dir: Option<String>,
42
43 #[arg(value_name = "FILESETS")]
44 paths: Vec<String>,
45 // TODO: Add an option to include trees that are ancestors of the matched paths
46 }
47
48 pub async fn cmd_debug_tree(
49 ui: &mut Ui,
50 command: &CommandHelper,
51 args: &DebugTreeArgs,
52 ) -> Result<(), CommandError> {
53 let workspace_command = command.workspace_helper(ui).await?;
54 let matcher = workspace_command
55 .parse_file_patterns(ui, &args.paths)?
56 .to_matcher();
57 let entries: Box<dyn Iterator<Item = (RepoPathBuf, BackendResult<MergedTreeValue>)>> =
58 if let Some(tree_id_hex) = &args.id {
59 let tree_id =
60 TreeId::try_from_hex(tree_id_hex).ok_or_else(|| user_error("Invalid tree id"))?;
61 let dir = if let Some(dir_str) = &args.dir {
62 workspace_command.parse_file_path(dir_str)?
63 } else {
64 RepoPathBuf::root()
65 };
66 let store = workspace_command.repo().store();
67 let tree = store.get_tree(dir, &tree_id).await?;
68 // We can't use `MergedTree` here, since it only supports iterating from the
69 // root, but we support a `--dir` option to read trees at any path.
70 Box::new(
71 tree.entries_matching(matcher.as_ref())
72 .map(|(path, value)| (path, Ok(Merge::normal(value)))),
73 )
74 } else {
75 let commit = workspace_command
76 .resolve_single_rev(ui, args.revision.as_ref().unwrap_or(&RevisionArg::AT))
77 .await?;
78 let tree = commit.tree();
79 Box::new(tree.entries_matching(matcher.as_ref()))
80 };
81 for (path, value) in entries {
82 let ui_path = workspace_command.format_file_path(&path);
83 writeln!(ui.stdout(), "{ui_path}: {value:?}")?;
84 }
85
86 Ok(())
87 }