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