Debian Codegraph @

cli/src/commands/debug/revset.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 futures::TryStreamExt as _;
19 use jj_lib::object_id::ObjectId as _;
20 use jj_lib::revset;
21 use jj_lib::revset::RevsetDiagnostics;
22
23 use crate::cli_util::CommandHelper;
24 use crate::command_error::CommandError;
25 use crate::command_error::print_parse_diagnostics;
26 use crate::revset_util;
27 use crate::ui::Ui;
28
29 /// Evaluate revset to full commit IDs
30 #[derive(clap::Args, Clone, Debug)]
31 pub struct DebugRevsetArgs {
32 revision: String,
33
34 /// Do not resolve and evaluate expression
35 #[arg(long)]
36 no_resolve: bool,
37
38 /// Do not rewrite expression to optimized form
39 #[arg(long)]
40 no_optimize: bool,
41 }
42
43 pub async fn cmd_debug_revset(
44 ui: &mut Ui,
45 command: &CommandHelper,
46 args: &DebugRevsetArgs,
47 ) -> Result<(), CommandError> {
48 let workspace_command = command.workspace_helper(ui).await?;
49 let workspace_ctx = workspace_command.env().revset_parse_context();
50 let repo = workspace_command.repo().as_ref();
51
52 let mut diagnostics = RevsetDiagnostics::new();
53 let expression = revset::parse(&mut diagnostics, &args.revision, &workspace_ctx)?;
54 print_parse_diagnostics(ui, "In revset expression", &diagnostics)?;
55 writeln!(ui.stdout(), "-- Parsed:")?;
56 writeln!(ui.stdout(), "{expression:#?}")?;
57 writeln!(ui.stdout())?;
58
59 if args.no_resolve && args.no_optimize {
60 return Ok(());
61 } else if args.no_resolve {
62 let expression = revset::optimize(expression);
63 writeln!(ui.stdout(), "-- Optimized:")?;
64 writeln!(ui.stdout(), "{expression:#?}")?;
65 writeln!(ui.stdout())?;
66 return Ok(());
67 }
68
69 let symbol_resolver = revset_util::default_symbol_resolver(
70 repo,
71 command.revset_extensions().symbol_resolvers(),
72 workspace_command.id_prefix_context(),
73 );
74 let mut expression = expression.resolve_user_expression(repo, &symbol_resolver)?;
75 writeln!(ui.stdout(), "-- Resolved:")?;
76 writeln!(ui.stdout(), "{expression:#?}")?;
77 writeln!(ui.stdout())?;
78
79 if !args.no_optimize {
80 expression = revset::optimize(expression);
81 writeln!(ui.stdout(), "-- Optimized:")?;
82 writeln!(ui.stdout(), "{expression:#?}")?;
83 writeln!(ui.stdout())?;
84 }
85
86 let backend_expression = expression.to_backend_expression(repo);
87 writeln!(ui.stdout(), "-- Backend:")?;
88 writeln!(ui.stdout(), "{backend_expression:#?}")?;
89 writeln!(ui.stdout())?;
90
91 let revset = expression.evaluate_unoptimized(repo)?;
92 writeln!(ui.stdout(), "-- Evaluated:")?;
93 writeln!(ui.stdout(), "{revset:#?}")?;
94 writeln!(ui.stdout())?;
95
96 writeln!(ui.stdout(), "-- Commit IDs:")?;
97 let mut commit_ids = revset.stream();
98 while let Some(commit_id) = commit_ids.try_next().await? {
99 writeln!(ui.stdout(), "{}", commit_id.hex())?;
100 }
101 Ok(())
102 }