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