Skip to content

Commit

Permalink
cli: default to log when no subcommand is provided
Browse files Browse the repository at this point in the history
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] clap-rs/clap#975
[2] https://sapling-scm.com/docs/overview/smartlog
  • Loading branch information
elasticdog committed Apr 14, 2023
1 parent cf402af commit 62e64e9
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 16 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Expand Up @@ -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
Expand Down
22 changes: 16 additions & 6 deletions src/commands/mod.rs
Expand Up @@ -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() |
Expand Down Expand Up @@ -3474,18 +3474,28 @@ 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(
ui: &mut Ui,
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> = 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),
Expand Down
2 changes: 1 addition & 1 deletion src/diff_util.rs
Expand Up @@ -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 {
Expand Down
6 changes: 3 additions & 3 deletions tests/test_alias.rs
Expand Up @@ -69,7 +69,7 @@ fn test_alias_bad_name() {
insta::assert_snapshot!(stderr, @r###"
error: unrecognized subcommand 'foo.'
Usage: jj [OPTIONS] <COMMAND>
Usage: jj [OPTIONS] [COMMAND]
For more information, try '--help'.
"###);
Expand All @@ -86,7 +86,7 @@ fn test_alias_calls_unknown_command() {
insta::assert_snapshot!(stderr, @r###"
error: unrecognized subcommand 'nonexistent'
Usage: jj [OPTIONS] <COMMAND>
Usage: jj [OPTIONS] [COMMAND]
For more information, try '--help'.
"###);
Expand Down Expand Up @@ -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] <COMMAND>
Usage: jj [OPTIONS] [COMMAND]
"###);
}

Expand Down
21 changes: 15 additions & 6 deletions tests/test_global_opts.rs
Expand Up @@ -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(), "?");
Expand All @@ -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]
Expand Down

0 comments on commit 62e64e9

Please sign in to comment.