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 7, 2024
1 parent bf76080 commit a851474
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 6 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,9 @@ No code changes (fixing Rust `Cargo.toml` stuff).
defined template names when no argument is given, assisting the user in making
a selection.

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

### Fixed bugs

* On Windows, symlinks in the repo are now supported when Developer Mode is enabled.
Expand Down
20 changes: 15 additions & 5 deletions cli/src/cli_util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2171,6 +2171,16 @@ impl ValueParserFactory for RevisionArg {
}
}

fn get_string_or_array(
config: &config::Config,
key: &str,
) -> Result<Vec<String>, config::ConfigError> {
config
.get(key)
.map(|string| vec![string])
.or_else(|_| config.get::<Vec<String>>(key))
}

fn resolve_default_command(
ui: &Ui,
config: &config::Config,
Expand All @@ -2194,7 +2204,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 @@ -2204,11 +2215,10 @@ 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);
string_args.splice(1..1, default_command);
}
}
Ok(string_args)
Expand Down
13 changes: 12 additions & 1 deletion cli/src/config-schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,18 @@
"default-command": {
"type": "string",
"description": "Default command to run when no explicit command is given",
"default": "log"
"default": "log",
"oneOf": [
{
"type": "string"
},
{
"type": "array",
"items": {
"type": "string"
}
}
]
},
"default-description": {
"type": "string",
Expand Down
11 changes: 11 additions & 0 deletions cli/tests/test_global_opts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,17 @@ fn test_no_subcommand() {
│ (empty) (no description set)
~
"###);

// Multiple default command strings work.
test_env.add_config(r#"ui.default-command=["commit", "-m", "foo"]"#);
test_env.jj_cmd_ok(&repo_path, &["new"]);
std::fs::write(repo_path.join("file.txt"), "file").unwrap();
let (stdout, stderr) = test_env.jj_cmd_ok(&repo_path, &[]);
assert_eq!(stdout, "");
insta::assert_snapshot!(stderr, @r###"
Working copy now at: kxryzmor 70ac3df3 (empty) (no description set)
Parent commit : lylxulpl 9dbbb452 foo
"###);
}

#[test]
Expand Down

0 comments on commit a851474

Please sign in to comment.