Debian Codegraph @

cli/src/commands/debug/object.rs

raw
1 // Copyright 2025 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 clap::Subcommand;
19 use futures::AsyncReadExt as _;
20 use jj_lib::backend::CommitId;
21 use jj_lib::backend::FileId;
22 use jj_lib::backend::MergedTreeValue;
23 use jj_lib::backend::SymlinkId;
24 use jj_lib::backend::TreeId;
25 use jj_lib::backend::TreeValue;
26 use jj_lib::op_store::OperationId;
27 use jj_lib::op_store::ViewId;
28 use jj_lib::repo_path::RepoPath;
29 use jj_lib::repo_path::RepoPathBuf;
30
31 use crate::cli_util::CommandHelper;
32 use crate::cli_util::RevisionArg;
33 use crate::command_error::CommandError;
34 use crate::command_error::user_error;
35 use crate::ui::Ui;
36
37 /// Show information about an operation and its view
38 #[derive(Subcommand, Clone, Debug)]
39 pub enum DebugObjectArgs {
40 Commit(DebugObjectCommitArgs),
41 File(DebugObjectFileArgs),
42 Operation(DebugObjectOperationArgs),
43 Symlink(DebugObjectSymlinkArgs),
44 Tree(DebugObjectTreeArgs),
45 View(DebugObjectViewArgs),
46 }
47
48 #[derive(clap::Args, Clone, Debug)]
49 pub struct DebugObjectCommitArgs {
50 id: String,
51 }
52
53 #[derive(clap::Args, Clone, Debug)]
54 #[command(group(clap::ArgGroup::new("target").required(true)))]
55 pub struct DebugObjectFileArgs {
56 #[arg(value_hint = clap::ValueHint::FilePath)]
57 path: String,
58
59 #[arg(group = "target")]
60 id: Option<String>,
61
62 #[arg(long, short, group = "target")]
63 revision: Option<RevisionArg>,
64 }
65
66 #[derive(clap::Args, Clone, Debug)]
67 pub struct DebugObjectOperationArgs {
68 id: String,
69 }
70
71 #[derive(clap::Args, Clone, Debug)]
72 pub struct DebugObjectSymlinkArgs {
73 #[arg(value_hint = clap::ValueHint::FilePath)]
74 path: String,
75
76 #[arg(group = "target")]
77 id: Option<String>,
78
79 #[arg(long, short, group = "target")]
80 revision: Option<RevisionArg>,
81 }
82
83 #[derive(clap::Args, Clone, Debug)]
84 #[command(group(clap::ArgGroup::new("target").required(true)))]
85 pub struct DebugObjectTreeArgs {
86 #[arg(value_hint = clap::ValueHint::DirPath)]
87 dir: String,
88
89 #[arg(group = "target")]
90 id: Option<String>,
91
92 #[arg(long, short, group = "target")]
93 revision: Option<RevisionArg>,
94 }
95
96 #[derive(clap::Args, Clone, Debug)]
97 #[command(group(clap::ArgGroup::new("target").required(true)))]
98 pub struct DebugObjectViewArgs {
99 #[arg(group = "target")]
100 id: Option<String>,
101
102 #[arg(long, group = "target")]
103 op: Option<String>,
104 }
105
106 pub async fn cmd_debug_object(
107 ui: &mut Ui,
108 command: &CommandHelper,
109 args: &DebugObjectArgs,
110 ) -> Result<(), CommandError> {
111 // Resolve the operation without loading the repo, so this command can be used
112 // even if e.g. the view object is broken.
113 let workspace = command.load_workspace()?;
114 let repo_loader = workspace.repo_loader();
115
116 match args {
117 DebugObjectArgs::Commit(args) => {
118 let id = CommitId::try_from_hex(&args.id)
119 .ok_or_else(|| user_error(format!(r#"Invalid hex commit id: "{}""#, args.id)))?;
120 let commit = repo_loader.store().get_commit_async(&id).await?;
121 writeln!(ui.stdout(), "{:#?}", commit.store_commit())?;
122 }
123 DebugObjectArgs::File(args) => {
124 let path = RepoPathBuf::from_internal_string(&args.path).map_err(user_error)?;
125 let id = if let Some(rev) = &args.revision {
126 if let Some(Some(TreeValue::File { id, .. })) =
127 get_tree_value(ui, command, rev, &path).await?.as_resolved()
128 {
129 id.clone()
130 } else {
131 return Err(user_error("The path is not a single file in the commit"));
132 }
133 } else {
134 let file_id = args.id.as_ref().unwrap();
135 FileId::try_from_hex(file_id)
136 .ok_or_else(|| user_error(format!(r#"Invalid hex file id: "{file_id}""#)))?
137 };
138 let mut contents = repo_loader.store().read_file(&path, &id).await?;
139 let mut buf = vec![];
140 contents.read_to_end(&mut buf).await?;
141 ui.stdout().write_all(&buf)?;
142 }
143 DebugObjectArgs::Operation(args) => {
144 let id = OperationId::try_from_hex(&args.id)
145 .ok_or_else(|| user_error(format!(r#"Invalid hex operation id: "{}""#, args.id)))?;
146 let operation = repo_loader.op_store().read_operation(&id).await?;
147 writeln!(ui.stdout(), "{operation:#?}")?;
148 }
149 DebugObjectArgs::Symlink(args) => {
150 let path = RepoPathBuf::from_internal_string(&args.path).map_err(user_error)?;
151 let id = if let Some(rev) = &args.revision {
152 if let Some(Some(TreeValue::Symlink(id))) =
153 get_tree_value(ui, command, rev, &path).await?.as_resolved()
154 {
155 id.clone()
156 } else {
157 return Err(user_error("The path is not a single symlink in the commit"));
158 }
159 } else {
160 let symlink_id = args.id.as_ref().unwrap();
161 SymlinkId::try_from_hex(symlink_id).ok_or_else(|| {
162 user_error(format!(r#"Invalid hex symlink id: "{symlink_id}""#))
163 })?
164 };
165 let target = repo_loader.store().read_symlink(&path, &id).await?;
166 writeln!(ui.stdout(), "{target}")?;
167 }
168 DebugObjectArgs::Tree(args) => {
169 let dir = RepoPathBuf::from_internal_string(&args.dir).map_err(user_error)?;
170 let id = if let Some(rev) = &args.revision {
171 if let Some(Some(TreeValue::Tree(id))) =
172 get_tree_value(ui, command, rev, &dir).await?.as_resolved()
173 {
174 id.clone()
175 } else {
176 return Err(user_error("The path is not a single tree in the commit"));
177 }
178 } else {
179 let tree_id = args.id.as_ref().unwrap();
180 TreeId::try_from_hex(tree_id)
181 .ok_or_else(|| user_error(format!(r#"Invalid hex tree id: "{tree_id}""#)))?
182 };
183 let tree = repo_loader.store().get_tree(dir, &id).await?;
184 writeln!(ui.stdout(), "{:#?}", tree.data())?;
185 }
186 DebugObjectArgs::View(args) => {
187 let id = if let Some(op_string) = &args.op {
188 let workspace_command = command.workspace_helper_no_snapshot(ui).await?;
189 let op = workspace_command.resolve_single_op(op_string)?;
190 op.view_id().clone()
191 } else {
192 let view_id = args.id.as_ref().unwrap();
193 ViewId::try_from_hex(view_id)
194 .ok_or_else(|| user_error(format!(r#"Invalid hex view id: "{view_id}""#)))?
195 };
196 let view = repo_loader.op_store().read_view(&id).await?;
197 writeln!(ui.stdout(), "{view:#?}")?;
198 }
199 }
200
201 Ok(())
202 }
203
204 async fn get_tree_value(
205 ui: &mut Ui,
206 command: &CommandHelper,
207 rev: &RevisionArg,
208 path: &RepoPath,
209 ) -> Result<MergedTreeValue, CommandError> {
210 let workspace_command = command.workspace_helper_no_snapshot(ui).await?;
211 let commit = workspace_command.resolve_single_rev(ui, rev).await?;
212 let tree_value = commit.tree().path_value(path).await?;
213 Ok(tree_value)
214 }