From 62e64e9662e2f36bb0b1458c6a802bf9d35e50de Mon Sep 17 00:00:00 2001 From: Aaron Bull Schaefer Date: Thu, 13 Apr 2023 18:49:35 -0700 Subject: [PATCH] cli: default to log when no subcommand is provided This is a convenience optimization to improve the default user experience, since `jj log` is a frequently run command. Accessing the help information explicitly still follows normal CLI conventions, and instructions are displayed appropriately if the user happens to make a mistake. Discoverability should not be adversely harmed. Since clap does not natively support setting a default subcommand [1], it will only parse global options when invoking `jj` in this way. This limitation is also why we have to create the LogArgs struct literal (with derived default values) to pass through to the cmd_log function. Note that this behavior (and limitation) mirrors what Sapling does [2], where `sl` will display the smartlog by default. [1] https://github.com/clap-rs/clap/issues/975 [2] https://sapling-scm.com/docs/overview/smartlog --- CHANGELOG.md | 3 +++ src/commands/mod.rs | 22 ++++++++++++++++------ src/diff_util.rs | 2 +- tests/test_alias.rs | 6 +++--- tests/test_global_opts.rs | 21 +++++++++++++++------ 5 files changed, 38 insertions(+), 16 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0884171967..c56de7d253 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -79,6 +79,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 * `jj git fetch` and `jj git push` will now use the single defined remote even if it is not named "origin". +* `jj` with no subcommand now invokes `jj log` with the default options, instead + of showing help. + ### Fixed bugs * Modify/delete conflicts now include context lines diff --git a/src/commands/mod.rs b/src/commands/mod.rs index e13a540c36..ab05e68412 100644 --- a/src/commands/mod.rs +++ b/src/commands/mod.rs @@ -325,7 +325,7 @@ struct ShowArgs { struct StatusArgs {} /// Show commit history -#[derive(clap::Args, Clone, Debug)] +#[derive(clap::Args, Clone, Debug, Default)] struct LogArgs { /// Which revisions to show. Defaults to the `ui.default-revset` setting, /// or `@ | (remote_branches() | tags()).. | ((remote_branches() | @@ -3474,9 +3474,7 @@ fn cmd_sparse(ui: &mut Ui, command: &CommandHelper, args: &SparseArgs) -> Result pub fn default_app() -> Command { let app: Command = Commands::augment_subcommands(Args::command()); - app.arg_required_else_help(true) - .subcommand_required(true) - .version(env!("JJ_VERSION")) + app.version(env!("JJ_VERSION")) } pub fn run_command( @@ -3484,8 +3482,20 @@ pub fn run_command( command_helper: &CommandHelper, matches: &ArgMatches, ) -> Result<(), CommandError> { - let derived_subcommands: Commands = Commands::from_arg_matches(matches).unwrap(); - match &derived_subcommands { + let derived_subcommands: Option = Commands::from_arg_matches(matches).ok(); + + let subcommand = match derived_subcommands { + Some(command) => command, + None => { + // TODO: Make the default subcommand configurable. + let default_sub_args = LogArgs { + ..Default::default() + }; + Commands::Log(default_sub_args) + } + }; + + match &subcommand { Commands::Version(sub_args) => cmd_version(ui, command_helper, sub_args), Commands::Init(sub_args) => cmd_init(ui, command_helper, sub_args), Commands::Config(sub_args) => cmd_config(ui, command_helper, sub_args), diff --git a/src/diff_util.rs b/src/diff_util.rs index 55e3991e60..b61916418b 100644 --- a/src/diff_util.rs +++ b/src/diff_util.rs @@ -32,7 +32,7 @@ use jujutsu_lib::{conflicts, diff, files, rewrite, tree}; use crate::cli_util::{CommandError, WorkspaceCommandHelper}; use crate::formatter::Formatter; -#[derive(clap::Args, Clone, Debug)] +#[derive(clap::Args, Clone, Debug, Default)] #[command(group(clap::ArgGroup::new("short-format").args(&["summary", "types"])))] #[command(group(clap::ArgGroup::new("long-format").args(&["git", "color_words"])))] pub struct DiffFormatArgs { diff --git a/tests/test_alias.rs b/tests/test_alias.rs index 546dc64c15..86bdac4030 100644 --- a/tests/test_alias.rs +++ b/tests/test_alias.rs @@ -69,7 +69,7 @@ fn test_alias_bad_name() { insta::assert_snapshot!(stderr, @r###" error: unrecognized subcommand 'foo.' - Usage: jj [OPTIONS] + Usage: jj [OPTIONS] [COMMAND] For more information, try '--help'. "###); @@ -86,7 +86,7 @@ fn test_alias_calls_unknown_command() { insta::assert_snapshot!(stderr, @r###" error: unrecognized subcommand 'nonexistent' - Usage: jj [OPTIONS] + Usage: jj [OPTIONS] [COMMAND] For more information, try '--help'. "###); @@ -123,7 +123,7 @@ fn test_alias_calls_help() { To get started, see the tutorial at https://github.com/martinvonz/jj/blob/main/docs/tutorial.md. - Usage: jj [OPTIONS] + Usage: jj [OPTIONS] [COMMAND] "###); } diff --git a/tests/test_global_opts.rs b/tests/test_global_opts.rs index fca8012696..205a252b56 100644 --- a/tests/test_global_opts.rs +++ b/tests/test_global_opts.rs @@ -47,12 +47,17 @@ fn test_non_utf8_arg() { #[test] fn test_no_subcommand() { let test_env = TestEnvironment::default(); + test_env.jj_cmd_success(test_env.env_root(), &["init", "repo", "--git"]); + let repo_path = test_env.env_root().join("repo"); - let stderr = test_env.jj_cmd_cli_error(test_env.env_root(), &[]); - insta::assert_snapshot!(stderr.lines().next().unwrap(), @"Jujutsu (An experimental VCS)"); + // Outside of a repo. + let stderr = test_env.jj_cmd_failure(test_env.env_root(), &[]); + insta::assert_snapshot!(stderr, @r###" + Error: There is no jj repo in "." + "###); - let stderr = test_env.jj_cmd_cli_error(test_env.env_root(), &["-R."]); - insta::assert_snapshot!(stderr.lines().next().unwrap(), @"error: 'jj' requires a subcommand but one was not provided"); + let stdout = test_env.jj_cmd_success(test_env.env_root(), &["--help"]); + insta::assert_snapshot!(stdout.lines().next().unwrap(), @"Jujutsu (An experimental VCS)"); let stdout = test_env.jj_cmd_success(test_env.env_root(), &["--version"]); let sanitized = stdout.replace(|c: char| c.is_ascii_hexdigit(), "?"); @@ -62,8 +67,12 @@ fn test_no_subcommand() { "{sanitized}" ); - let stdout = test_env.jj_cmd_success(test_env.env_root(), &["--help"]); - insta::assert_snapshot!(stdout.lines().next().unwrap(), @"Jujutsu (An experimental VCS)"); + let stdout = test_env.jj_cmd_success(test_env.env_root(), &["-R", "repo"]); + assert_eq!(stdout, test_env.jj_cmd_success(&repo_path, &["log"])); + + // Inside of a repo. + let stdout = test_env.jj_cmd_success(&repo_path, &[]); + assert_eq!(stdout, test_env.jj_cmd_success(&repo_path, &["log"])); } #[test]