Debian Codegraph @

cli/examples/custom-command/main.rs

raw
1 // Copyright 2022 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::io::Write as _;
16
17 use jj_cli::cli_util::CliRunner;
18 use jj_cli::cli_util::CommandHelper;
19 use jj_cli::cli_util::RevisionArg;
20 use jj_cli::command_error::CommandError;
21 use jj_cli::ui::Ui;
22
23 #[derive(clap::Parser, Clone, Debug)]
24 enum CustomCommand {
25 Frobnicate(FrobnicateArgs),
26 }
27
28 /// Frobnicate a revisions
29 #[derive(clap::Args, Clone, Debug)]
30 struct FrobnicateArgs {
31 /// The revision to frobnicate
32 #[arg(default_value = "@")]
33 revision: RevisionArg,
34 }
35
36 async fn run_custom_command(
37 ui: &mut Ui,
38 command_helper: &CommandHelper,
39 command: CustomCommand,
40 ) -> Result<(), CommandError> {
41 match command {
42 CustomCommand::Frobnicate(args) => {
43 let mut workspace_command = command_helper.workspace_helper(ui).await?;
44 let commit = workspace_command
45 .resolve_single_rev(ui, &args.revision)
46 .await?;
47 let mut tx = workspace_command.start_transaction();
48 let new_commit = tx
49 .repo_mut()
50 .rewrite_commit(&commit)
51 .set_description("Frobnicated!")
52 .write()
53 .await?;
54 tx.finish(ui, "frobnicate").await?;
55 writeln!(
56 ui.status(),
57 "Frobnicated revision: {}",
58 workspace_command.format_commit_summary(&new_commit)
59 )?;
60 Ok(())
61 }
62 }
63 }
64
65 fn main() -> std::process::ExitCode {
66 CliRunner::init()
67 .add_subcommand(run_custom_command)
68 .run()
69 .into()
70 }