Skip to content

Commit

Permalink
cli: allow overwriting non-scalar with jj config set
Browse files Browse the repository at this point in the history
Before this patch, it was an error to run `jj config set --user foo
'[1]'` twice. But it's only been broken since the previous commit
because '[1]' was interpreted as a string before then.
  • Loading branch information
martinvonz committed Oct 11, 2023
1 parent caf5510 commit 80ef091
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 4 deletions.
11 changes: 7 additions & 4 deletions cli/src/cli_util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2194,14 +2194,17 @@ pub fn write_config_value_to_file(
))
})?;
}
// Error out if overwriting non-scalar value for key (table or array).
// Error out if overwriting non-scalar value for key (table or array) with
// scalar.
match target_table.get(last_key_part) {
None | Some(toml_edit::Item::None) => {}
Some(toml_edit::Item::Value(val)) if !val.is_array() && !val.is_inline_table() => {}
_ => {
return Err(user_error(format!(
"Failed to set {key}: would overwrite entire non-scalar value with scalar"
)));
if !item.is_array() && !item.is_inline_table() {
return Err(user_error(format!(
"Failed to set {key}: would overwrite entire non-scalar value with scalar"
)));
}
}
}
target_table[last_key_part] = item;
Expand Down
10 changes: 10 additions & 0 deletions cli/tests/test_config_command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,16 @@ fn test_config_set_type_mismatch() {
insta::assert_snapshot!(stderr, @r###"
Error: Failed to set test-table: would overwrite entire non-scalar value with scalar
"###);

// But it's fine to overwrite a non-scalar by a non-scalar
test_env.jj_cmd_success(
&repo_path,
&["config", "set", "--user", "test-table.foo", "[1,2,3]"],
);
test_env.jj_cmd_success(
&repo_path,
&["config", "set", "--user", "test-table.foo", "[4,5,6]"],
);
}

#[test]
Expand Down

0 comments on commit 80ef091

Please sign in to comment.