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    // TODO: Remove in jj 0.28+
99    #[command(subcommand, hide = true)]
100    Branch(bookmark::BookmarkCommand),
101    Commit(commit::CommitArgs),
102    #[command(subcommand)]
103    Config(config::ConfigCommand),
104    #[command(subcommand)]
105    Debug(debug::DebugCommand),
106    Describe(describe::DescribeArgs),
107    Diff(diff::DiffArgs),
108    Diffedit(diffedit::DiffeditArgs),
109    Duplicate(duplicate::DuplicateArgs),
110    Edit(edit::EditArgs),
111    #[command(alias = "obslog", visible_alias = "evolution-log")]
112    Evolog(evolog::EvologArgs),
113    #[command(subcommand)]
114    File(file::FileCommand),
115    Fix(fix::FixArgs),
116    #[cfg(feature = "git")]
117    #[command(subcommand)]
118    Git(git::GitCommand),
119    Help(help::HelpArgs),
120    Interdiff(interdiff::InterdiffArgs),
121    Log(log::LogArgs),
122    New(new::NewArgs),
123    Next(next::NextArgs),
124    #[command(subcommand)]
125    #[command(visible_alias = "op")]
126    Operation(operation::OperationCommand),
127    Parallelize(parallelize::ParallelizeArgs),
128    Prev(prev::PrevArgs),
129    Rebase(rebase::RebaseArgs),
130    Resolve(resolve::ResolveArgs),
131    Restore(restore::RestoreArgs),
132    Revert(revert::RevertArgs),
133    Root(root::RootArgs),
134    #[command(hide = true)]
135    // TODO: Flesh out.
136    Run(run::RunArgs),
137    Show(show::ShowArgs),
138    Sign(sign::SignArgs),
139    SimplifyParents(simplify_parents::SimplifyParentsArgs),
140    #[command(subcommand)]
141    Sparse(sparse::SparseCommand),
142    Split(split::SplitArgs),
143    Squash(squash::SquashArgs),
144    Status(status::StatusArgs),
145    #[command(subcommand)]
146    Tag(tag::TagCommand),
147    #[command(subcommand)]
148    Util(util::UtilCommand),
149    /// Undo an operation (shortcut for `jj op undo`)
150    Undo(operation::undo::OperationUndoArgs),
151    Unsign(unsign::UnsignArgs),
152    Version(version::VersionArgs),
153    #[command(subcommand)]
154    Workspace(workspace::WorkspaceCommand),
155}
156
157pub fn default_app() -> clap::Command {
158    Command::augment_subcommands(Args::command())
159}
160
161#[instrument(skip_all)]
162pub fn run_command(ui: &mut Ui, command_helper: &CommandHelper) -> Result<(), CommandError> {
163    let subcommand = Command::from_arg_matches(command_helper.matches()).unwrap();
164    match &subcommand {
165        Command::Abandon(args) => abandon::cmd_abandon(ui, command_helper, args),
166        Command::Absorb(args) => absorb::cmd_absorb(ui, command_helper, args),
167        Command::Backout(args) => backout::cmd_backout(ui, command_helper, args),
168        #[cfg(feature = "bench")]
169        Command::Bench(args) => bench::cmd_bench(ui, command_helper, args),
170        Command::Bookmark(args) => bookmark::cmd_bookmark(ui, command_helper, args),
171        Command::Branch(args) => {
172            let cmd = renamed_cmd("branch", "bookmark", bookmark::cmd_bookmark);
173            cmd(ui, command_helper, args)
174        }
175        Command::Commit(args) => commit::cmd_commit(ui, command_helper, args),
176        Command::Config(args) => config::cmd_config(ui, command_helper, args),
177        Command::Debug(args) => debug::cmd_debug(ui, command_helper, args),
178        Command::Describe(args) => describe::cmd_describe(ui, command_helper, args),
179        Command::Diff(args) => diff::cmd_diff(ui, command_helper, args),
180        Command::Diffedit(args) => diffedit::cmd_diffedit(ui, command_helper, args),
181        Command::Duplicate(args) => duplicate::cmd_duplicate(ui, command_helper, args),
182        Command::Edit(args) => edit::cmd_edit(ui, command_helper, args),
183        Command::File(args) => file::cmd_file(ui, command_helper, args),
184        Command::Fix(args) => fix::cmd_fix(ui, command_helper, args),
185        #[cfg(feature = "git")]
186        Command::Git(args) => git::cmd_git(ui, command_helper, args),
187        Command::Help(args) => help::cmd_help(ui, command_helper, args),
188        Command::Interdiff(args) => interdiff::cmd_interdiff(ui, command_helper, args),
189        Command::Log(args) => log::cmd_log(ui, command_helper, args),
190        Command::New(args) => new::cmd_new(ui, command_helper, args),
191        Command::Next(args) => next::cmd_next(ui, command_helper, args),
192        Command::Evolog(args) => evolog::cmd_evolog(ui, command_helper, args),
193        Command::Operation(args) => operation::cmd_operation(ui, command_helper, args),
194        Command::Parallelize(args) => parallelize::cmd_parallelize(ui, command_helper, args),
195        Command::Prev(args) => prev::cmd_prev(ui, command_helper, args),
196        Command::Rebase(args) => rebase::cmd_rebase(ui, command_helper, args),
197        Command::Resolve(args) => resolve::cmd_resolve(ui, command_helper, args),
198        Command::Restore(args) => restore::cmd_restore(ui, command_helper, args),
199        Command::Revert(args) => revert::cmd_revert(ui, command_helper, args),
200        Command::Root(args) => root::cmd_root(ui, command_helper, args),
201        Command::Run(args) => run::cmd_run(ui, command_helper, args),
202        Command::SimplifyParents(args) => {
203            simplify_parents::cmd_simplify_parents(ui, command_helper, args)
204        }
205        Command::Show(args) => show::cmd_show(ui, command_helper, args),
206        Command::Sign(args) => sign::cmd_sign(ui, command_helper, args),
207        Command::Sparse(args) => sparse::cmd_sparse(ui, command_helper, args),
208        Command::Split(args) => split::cmd_split(ui, command_helper, args),
209        Command::Squash(args) => squash::cmd_squash(ui, command_helper, args),
210        Command::Status(args) => status::cmd_status(ui, command_helper, args),
211        Command::Tag(args) => tag::cmd_tag(ui, command_helper, args),
212        Command::Undo(args) => operation::undo::cmd_op_undo(ui, command_helper, args),
213        Command::Unsign(args) => unsign::cmd_unsign(ui, command_helper, args),
214        Command::Util(args) => util::cmd_util(ui, command_helper, args),
215        Command::Version(args) => version::cmd_version(ui, command_helper, args),
216        Command::Workspace(args) => workspace::cmd_workspace(ui, command_helper, args),
217    }
218}
219
220/// Wraps deprecated command of `old_name` which has been renamed to `new_name`.
221pub(crate) fn renamed_cmd<Args>(
222    old_name: &'static str,
223    new_name: &'static str,
224    cmd: impl Fn(&mut Ui, &CommandHelper, &Args) -> Result<(), CommandError>,
225) -> impl Fn(&mut Ui, &CommandHelper, &Args) -> Result<(), CommandError> {
226    move |ui: &mut Ui, command: &CommandHelper, args: &Args| -> Result<(), CommandError> {
227        writeln!(
228            ui.warning_default(),
229            "`jj {old_name}` is deprecated; use `jj {new_name}` instead, which is equivalent"
230        )?;
231        writeln!(
232            ui.warning_default(),
233            "`jj {old_name}` will be removed in a future version, and this will be a hard error"
234        )?;
235        cmd(ui, command, args)
236    }
237}
238
239#[cfg(test)]
240mod tests {
241    use super::*;
242
243    #[test]
244    fn verify_app() {
245        default_app().debug_assert();
246    }
247}