Skip to main content

jj_cli/commands/
mod.rs

1// Copyright 2020 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
15mod abandon;
16mod absorb;
17mod arrange;
18#[cfg(feature = "bench")]
19mod bench;
20mod bisect;
21mod bookmark;
22mod commit;
23mod config;
24mod converge;
25mod debug;
26mod describe;
27mod diff;
28mod diffedit;
29mod duplicate;
30mod edit;
31mod evolog;
32mod file;
33mod fix;
34#[cfg(feature = "git")]
35mod gerrit;
36#[cfg(feature = "git")]
37mod git;
38mod help;
39mod interdiff;
40mod log;
41mod metaedit;
42mod new;
43mod next;
44mod operation;
45mod parallelize;
46mod prev;
47mod rebase;
48mod redo;
49mod resolve;
50mod restore;
51mod revert;
52mod root;
53mod run;
54mod show;
55mod sign;
56mod simplify_parents;
57mod sparse;
58mod split;
59mod squash;
60mod status;
61mod tag;
62mod undo;
63mod unsign;
64mod util;
65mod version;
66mod workspace;
67
68use std::fmt::Debug;
69
70use clap::CommandFactory as _;
71use clap::FromArgMatches as _;
72use clap::Subcommand as _;
73use clap::builder::Styles;
74use clap::builder::styling::AnsiColor;
75use clap_complete::engine::SubcommandCandidates;
76use tracing::instrument;
77
78use crate::cli_util::Args;
79use crate::cli_util::CommandHelper;
80use crate::command_error::CommandError;
81use crate::complete;
82use crate::ui::Ui;
83
84const STYLES: Styles = Styles::styled()
85    .header(AnsiColor::Yellow.on_default().bold())
86    .usage(AnsiColor::Yellow.on_default().bold())
87    .literal(AnsiColor::Green.on_default().bold())
88    .placeholder(AnsiColor::Green.on_default());
89
90#[derive(clap::Parser, Clone, Debug)]
91#[command(styles = STYLES)]
92#[command(disable_help_subcommand = true)]
93#[command(after_long_help = help::show_keyword_hint_after_help())]
94#[command(add = SubcommandCandidates::new(complete::aliases))]
95// A repeated argument is accepted, and the last occurrence wins. This is a
96// propagated setting, so it also applies to subcommands and to custom commands
97// and global arguments registered by `CliRunner`. Arguments that can be
98// specified multiple times, such as `--config`, keep all of their values.
99#[command(args_override_self = true)]
100enum Command {
101    Abandon(abandon::AbandonArgs),
102    Absorb(absorb::AbsorbArgs),
103    Arrange(arrange::ArrangeArgs),
104    #[cfg(feature = "bench")]
105    #[command(subcommand)]
106    Bench(bench::BenchCommand),
107    #[command(subcommand)]
108    Bisect(bisect::BisectCommand),
109    #[command(subcommand)]
110    Bookmark(bookmark::BookmarkCommand),
111    Commit(commit::CommitArgs),
112    #[command(subcommand)]
113    Config(config::ConfigCommand),
114    Converge(converge::ConvergeArgs),
115    #[command(subcommand)]
116    Debug(debug::DebugCommand),
117    Describe(describe::DescribeArgs),
118    Diff(diff::DiffArgs),
119    Diffedit(diffedit::DiffeditArgs),
120    Duplicate(duplicate::DuplicateArgs),
121    Edit(edit::EditArgs),
122    #[command(alias = "obslog", visible_alias = "evolution-log")]
123    Evolog(evolog::EvologArgs),
124    #[command(subcommand)]
125    File(file::FileCommand),
126    Fix(fix::FixArgs),
127    #[cfg(feature = "git")]
128    #[command(subcommand)]
129    Gerrit(gerrit::GerritCommand),
130    #[cfg(feature = "git")]
131    #[command(subcommand)]
132    Git(git::GitCommand),
133    Help(help::HelpArgs),
134    Interdiff(interdiff::InterdiffArgs),
135    Log(log::LogArgs),
136    Metaedit(metaedit::MetaeditArgs),
137    New(new::NewArgs),
138    Next(next::NextArgs),
139    #[command(subcommand)]
140    #[command(visible_alias = "op")]
141    Operation(operation::OperationCommand),
142    Parallelize(parallelize::ParallelizeArgs),
143    Prev(prev::PrevArgs),
144    Rebase(rebase::RebaseArgs),
145    Redo(redo::RedoArgs),
146    Resolve(resolve::ResolveArgs),
147    Restore(restore::RestoreArgs),
148    Revert(revert::RevertArgs),
149    Root(root::RootArgs),
150    Run(run::RunArgs),
151    Show(show::ShowArgs),
152    Sign(sign::SignArgs),
153    SimplifyParents(simplify_parents::SimplifyParentsArgs),
154    #[command(subcommand)]
155    Sparse(sparse::SparseCommand),
156    Split(split::SplitArgs),
157    Squash(squash::SquashArgs),
158    Status(status::StatusArgs),
159    #[command(subcommand)]
160    Tag(tag::TagCommand),
161    Undo(undo::UndoArgs),
162    Unsign(unsign::UnsignArgs),
163    #[command(subcommand)]
164    Util(util::UtilCommand),
165    Version(version::VersionArgs),
166    #[command(subcommand)]
167    Workspace(workspace::WorkspaceCommand),
168}
169
170pub fn default_app() -> clap::Command {
171    Command::augment_subcommands(Args::command())
172}
173
174#[instrument(skip_all)]
175pub async fn run_command(ui: &mut Ui, command_helper: &CommandHelper) -> Result<(), CommandError> {
176    let subcommand = Command::from_arg_matches(command_helper.matches()).unwrap();
177    match &subcommand {
178        Command::Abandon(args) => abandon::cmd_abandon(ui, command_helper, args).await,
179        Command::Absorb(args) => absorb::cmd_absorb(ui, command_helper, args).await,
180        #[cfg(feature = "bench")]
181        Command::Bench(args) => bench::cmd_bench(ui, command_helper, args).await,
182        Command::Bisect(args) => bisect::cmd_bisect(ui, command_helper, args).await,
183        Command::Bookmark(args) => bookmark::cmd_bookmark(ui, command_helper, args).await,
184        Command::Commit(args) => commit::cmd_commit(ui, command_helper, args).await,
185        Command::Config(args) => config::cmd_config(ui, command_helper, args).await,
186        Command::Converge(args) => converge::cmd_converge(ui, command_helper, args).await,
187        Command::Debug(args) => debug::cmd_debug(ui, command_helper, args).await,
188        Command::Describe(args) => describe::cmd_describe(ui, command_helper, args).await,
189        Command::Diff(args) => diff::cmd_diff(ui, command_helper, args).await,
190        Command::Diffedit(args) => diffedit::cmd_diffedit(ui, command_helper, args).await,
191        Command::Duplicate(args) => duplicate::cmd_duplicate(ui, command_helper, args).await,
192        Command::Edit(args) => edit::cmd_edit(ui, command_helper, args).await,
193        Command::Evolog(args) => evolog::cmd_evolog(ui, command_helper, args).await,
194        Command::File(args) => file::cmd_file(ui, command_helper, args).await,
195        Command::Fix(args) => fix::cmd_fix(ui, command_helper, args).await,
196        #[cfg(feature = "git")]
197        Command::Gerrit(sub_args) => gerrit::cmd_gerrit(ui, command_helper, sub_args).await,
198        #[cfg(feature = "git")]
199        Command::Git(args) => git::cmd_git(ui, command_helper, args).await,
200        Command::Help(args) => help::cmd_help(ui, command_helper, args).await,
201        Command::Arrange(args) => arrange::cmd_arrange(ui, command_helper, args).await,
202        Command::Interdiff(args) => interdiff::cmd_interdiff(ui, command_helper, args).await,
203        Command::Log(args) => log::cmd_log(ui, command_helper, args).await,
204        Command::Metaedit(args) => metaedit::cmd_metaedit(ui, command_helper, args).await,
205        Command::New(args) => new::cmd_new(ui, command_helper, args).await,
206        Command::Next(args) => next::cmd_next(ui, command_helper, args).await,
207        Command::Operation(args) => operation::cmd_operation(ui, command_helper, args).await,
208        Command::Parallelize(args) => parallelize::cmd_parallelize(ui, command_helper, args).await,
209        Command::Prev(args) => prev::cmd_prev(ui, command_helper, args).await,
210        Command::Rebase(args) => rebase::cmd_rebase(ui, command_helper, args).await,
211        Command::Redo(args) => redo::cmd_redo(ui, command_helper, args).await,
212        Command::Resolve(args) => resolve::cmd_resolve(ui, command_helper, args).await,
213        Command::Restore(args) => restore::cmd_restore(ui, command_helper, args).await,
214        Command::Revert(args) => revert::cmd_revert(ui, command_helper, args).await,
215        Command::Root(args) => root::cmd_root(ui, command_helper, args).await,
216        Command::Run(args) => run::cmd_run(ui, command_helper, args).await,
217        Command::SimplifyParents(args) => {
218            simplify_parents::cmd_simplify_parents(ui, command_helper, args).await
219        }
220        Command::Show(args) => show::cmd_show(ui, command_helper, args).await,
221        Command::Sign(args) => sign::cmd_sign(ui, command_helper, args).await,
222        Command::Sparse(args) => sparse::cmd_sparse(ui, command_helper, args).await,
223        Command::Split(args) => split::cmd_split(ui, command_helper, args).await,
224        Command::Squash(args) => squash::cmd_squash(ui, command_helper, args).await,
225        Command::Status(args) => status::cmd_status(ui, command_helper, args).await,
226        Command::Tag(args) => tag::cmd_tag(ui, command_helper, args).await,
227        Command::Undo(args) => undo::cmd_undo(ui, command_helper, args).await,
228        Command::Unsign(args) => unsign::cmd_unsign(ui, command_helper, args).await,
229        Command::Util(args) => util::cmd_util(ui, command_helper, args).await,
230        Command::Version(args) => version::cmd_version(ui, command_helper, args).await,
231        Command::Workspace(args) => workspace::cmd_workspace(ui, command_helper, args).await,
232    }
233}
234
235#[cfg(test)]
236mod tests {
237    use super::*;
238
239    #[test]
240    fn verify_app() {
241        default_app().debug_assert();
242    }
243}