just playing with tangled
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::backend::TreeId;
19use jj_lib::merged_tree::MergedTree;
20use jj_lib::repo::Repo as _;
21use jj_lib::repo_path::RepoPathBuf;
22
23use crate::cli_util::CommandHelper;
24use crate::cli_util::RevisionArg;
25use crate::command_error::user_error;
26use crate::command_error::CommandError;
27use crate::ui::Ui;
28
29/// List the recursive entries of a tree.
30#[derive(clap::Args, Clone, Debug)]
31pub struct DebugTreeArgs {
32 #[arg(long, short = 'r', value_name = "REVSET")]
33 revision: Option<RevisionArg>,
34 #[arg(long, conflicts_with = "revision")]
35 id: Option<String>,
36 #[arg(long, requires = "id")]
37 dir: Option<String>,
38 #[arg(value_name = "FILESETS")]
39 paths: Vec<String>,
40 // TODO: Add an option to include trees that are ancestors of the matched paths
41}
42
43pub fn cmd_debug_tree(
44 ui: &mut Ui,
45 command: &CommandHelper,
46 args: &DebugTreeArgs,
47) -> Result<(), CommandError> {
48 let workspace_command = command.workspace_helper(ui)?;
49 let tree = if let Some(tree_id_hex) = &args.id {
50 let tree_id =
51 TreeId::try_from_hex(tree_id_hex).map_err(|_| user_error("Invalid tree id"))?;
52 let dir = if let Some(dir_str) = &args.dir {
53 workspace_command.parse_file_path(dir_str)?
54 } else {
55 RepoPathBuf::root()
56 };
57 let store = workspace_command.repo().store();
58 let tree = store.get_tree(dir, &tree_id)?;
59 MergedTree::resolved(tree)
60 } else {
61 let commit = workspace_command
62 .resolve_single_rev(ui, args.revision.as_ref().unwrap_or(&RevisionArg::AT))?;
63 commit.tree()?
64 };
65 let matcher = workspace_command
66 .parse_file_patterns(ui, &args.paths)?
67 .to_matcher();
68 for (path, value) in tree.entries_matching(matcher.as_ref()) {
69 let ui_path = workspace_command.format_file_path(&path);
70 writeln!(ui.stdout(), "{ui_path}: {value:?}")?;
71 }
72
73 Ok(())
74}