Skip to content

Commit

Permalink
cli_util: support multiple cli arguments for ui.default-command
Browse files Browse the repository at this point in the history
  • Loading branch information
torquestomp committed Mar 2, 2024
1 parent 2f7b15b commit 46616b3
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 6 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
* `jj branch list` now supports a `--tracked/-t` option which can be used to
show tracked branches only. Omits local Git-tracking branches by default.

* `ui.default-command` now accepts multiple string arguments, for more complex
default `jj` commands.

### Fixed bugs

* On Windows, symlinks in the repo are now materialized as regular files in the
Expand Down
30 changes: 25 additions & 5 deletions cli/src/cli_util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2176,6 +2176,22 @@ impl ValueParserFactory for RevisionArg {
}
}

fn get_string_or_array(
config: &config::Config,
key: &str,
) -> Result<Vec<String>, config::ConfigError> {
config
.get_string(key)
.map(|string| vec![string])
.or_else(|_| {
config
.get_array(key)?
.into_iter()
.map(|value| value.into_string())
.try_collect()
})
}

fn resolve_default_command(
ui: &Ui,
config: &config::Config,
Expand All @@ -2199,7 +2215,8 @@ fn resolve_default_command(

if let Some(matches) = matches {
if matches.subcommand_name().is_none() {
if config.get_string("ui.default-command").is_err() {
let args = get_string_or_array(config, "ui.default-command");
if args.is_err() {
writeln!(
ui.hint(),
"Hint: Use `jj -h` for a list of available commands."
Expand All @@ -2209,11 +2226,14 @@ fn resolve_default_command(
"Run `jj config set --user ui.default-command log` to disable this message."
)?;
}
let default_command = config
.get_string("ui.default-command")
.unwrap_or_else(|_| "log".to_string());
let default_command = args.unwrap_or_else(|_| vec!["log".to_string()]);

// Insert the default command directly after the path to the binary.
string_args.insert(1, default_command);
let orig_args = string_args.clone();
string_args.clear();
string_args.push(orig_args[0].clone());
string_args.extend(default_command);
string_args.extend(orig_args.into_iter().skip(1));
}
}
Ok(string_args)
Expand Down
5 changes: 4 additions & 1 deletion cli/tests/test_global_opts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,10 @@ fn test_no_subcommand() {
│ (empty) (no description set)
~
"###);
insta::assert_snapshot!(test_env.jj_cmd_success(&repo_path, &["-r", "show"]), @r###"

// Multiple default command strings work.
test_env.add_config(r#"ui.default-command=["log", "-r", "show"]"#);
insta::assert_snapshot!(test_env.jj_cmd_success(&repo_path, &[]), @r###"
@ qpvuntsm [email protected] 2001-02-03 04:05:07.000 +07:00 help log show 230dd059
│ (empty) (no description set)
~
Expand Down

0 comments on commit 46616b3

Please sign in to comment.