Skip to content

Commit

Permalink
config: extract jj-lib's default values to embedded TOML file
Browse files Browse the repository at this point in the history
It's nice that "jj config list --include-defaults" can show these default
values.

I just copied the jj-cli directory structure, but it's unlikely we'll add more
config/*.toml files.
  • Loading branch information
yuja committed Dec 25, 2024
1 parent 5c12c2f commit e4a350f
Show file tree
Hide file tree
Showing 10 changed files with 47 additions and 18 deletions.
2 changes: 1 addition & 1 deletion cli/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -424,7 +424,7 @@ fn config_files_for(
///
/// This function sets up 1, 2, and 6.
pub fn config_from_environment(default_layers: impl IntoIterator<Item = ConfigLayer>) -> RawConfig {
let mut config = StackedConfig::empty();
let mut config = StackedConfig::with_defaults();
config.extend_layers(default_layers);
config.add_layer(env_base_layer());
config.add_layer(env_overrides_layer());
Expand Down
2 changes: 1 addition & 1 deletion cli/src/merge_tools/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,7 @@ mod tests {
use super::*;

fn config_from_string(text: &str) -> StackedConfig {
let mut config = StackedConfig::empty();
let mut config = StackedConfig::with_defaults();
// Load defaults to test the default args lookup
config.extend_layers(crate::config::default_config_layers());
config.add_layer(ConfigLayer::parse(ConfigSource::User, text).unwrap());
Expand Down
14 changes: 14 additions & 0 deletions lib/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ use std::str::FromStr;
use std::sync::Arc;

use itertools::Itertools as _;
use once_cell::sync::Lazy;
use serde::de::IntoDeserializer as _;
use serde::Deserialize;
use thiserror::Error;
Expand Down Expand Up @@ -595,6 +596,14 @@ impl StackedConfig {
StackedConfig { layers: vec![] }
}

/// Creates a stack of configuration layers containing the default variables
/// referred to by `jj-lib`.
pub fn with_defaults() -> Self {
StackedConfig {
layers: DEFAULT_CONFIG_LAYERS.to_vec(),
}
}

/// Loads config file from the specified `path`, inserts it at the position
/// specified by `source`. The file should exist.
pub fn load_file(
Expand Down Expand Up @@ -833,6 +842,11 @@ fn merge_items(lower_item: &mut ConfigItem, upper_item: &ConfigItem) {
}
}

static DEFAULT_CONFIG_LAYERS: Lazy<[Arc<ConfigLayer>; 1]> = Lazy::new(|| {
let parse = |text: &str| Arc::new(ConfigLayer::parse(ConfigSource::Default, text).unwrap());
[parse(include_str!("config/misc.toml"))]
});

#[cfg(test)]
mod tests {
use assert_matches::assert_matches;
Expand Down
2 changes: 2 additions & 0 deletions lib/src/config/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
These files are compiled into the binary; they are not installed to the user's
system.
16 changes: 16 additions & 0 deletions lib/src/config/misc.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
[debug]
# commit-timestamp = <now>
# operation-timestamp = <now>
# randomness-seed = <random>

[git]
abandon-unreachable-commits = true
# auto-local-bookmark = false

[operation]
# hostname = <host>
# username = <user>

[user]
email = ""
name = ""
2 changes: 1 addition & 1 deletion lib/src/git_backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2221,7 +2221,7 @@ mod tests {
// UserSettings type. testutils returns jj_lib (2)'s UserSettings, whereas
// our UserSettings type comes from jj_lib (1).
fn user_settings() -> UserSettings {
let config = StackedConfig::empty();
let config = StackedConfig::with_defaults();
UserSettings::from_config(config).unwrap()
}
}
11 changes: 5 additions & 6 deletions lib/src/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,14 +64,13 @@ pub struct GitSettings {
impl GitSettings {
pub fn from_settings(settings: &UserSettings) -> Result<Self, ConfigGetError> {
let auto_local_bookmark = {
// TODO: Drop support for git.auto-local-branch and move the default
// value to config/*.toml
let opt1 = settings.get_bool("git.auto-local-bookmark").optional()?;
let opt2 = settings.get_bool("git.auto-local-branch").optional()?;
opt1.or(opt2).unwrap_or(false)
};
let abandon_unreachable_commits = settings
.get_bool("git.abandon-unreachable-commits")
.optional()?
.unwrap_or(true);
let abandon_unreachable_commits = settings.get_bool("git.abandon-unreachable-commits")?;
Ok(GitSettings {
auto_local_bookmark,
abandon_unreachable_commits,
Expand Down Expand Up @@ -146,8 +145,8 @@ fn to_timestamp(value: ConfigValue) -> Result<Timestamp, Box<dyn std::error::Err

impl UserSettings {
pub fn from_config(config: StackedConfig) -> Result<Self, ConfigGetError> {
let user_name = config.get("user.name").optional()?.unwrap_or_default();
let user_email = config.get("user.email").optional()?.unwrap_or_default();
let user_name = config.get("user.name")?;
let user_email = config.get("user.email")?;
let commit_timestamp = config
.get_value_with("debug.commit-timestamp", to_timestamp)
.optional()?;
Expand Down
6 changes: 3 additions & 3 deletions lib/tests/test_commit_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ fn test_rewrite(backend: TestRepoBackend) {
],
);

let mut config = StackedConfig::empty();
let mut config = StackedConfig::with_defaults();
config.add_layer(
ConfigLayer::parse(
ConfigSource::User,
Expand Down Expand Up @@ -221,7 +221,7 @@ fn test_rewrite(backend: TestRepoBackend) {
#[test_case(TestRepoBackend::Local ; "local backend")]
#[test_case(TestRepoBackend::Git ; "git backend")]
fn test_rewrite_update_missing_user(backend: TestRepoBackend) {
let missing_user_settings = UserSettings::from_config(StackedConfig::empty()).unwrap();
let missing_user_settings = UserSettings::from_config(StackedConfig::with_defaults()).unwrap();
let test_repo = TestRepo::init_with_backend(backend);
let repo = &test_repo.repo;

Expand All @@ -240,7 +240,7 @@ fn test_rewrite_update_missing_user(backend: TestRepoBackend) {
assert_eq!(initial_commit.committer().name, "");
assert_eq!(initial_commit.committer().email, "");

let mut config = StackedConfig::empty();
let mut config = StackedConfig::with_defaults();
config.add_layer(
ConfigLayer::parse(
ConfigSource::User,
Expand Down
8 changes: 3 additions & 5 deletions lib/tests/test_init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,11 +139,9 @@ fn test_init_external_git() {

#[test_case(TestRepoBackend::Local ; "local backend")]
#[test_case(TestRepoBackend::Git ; "git backend")]
fn test_init_no_config_set(backend: TestRepoBackend) {
// Test that we can create a repo without setting any config
// TODO: Perhaps, StackedConfig::empty() will be replaced with ::default()
// or something that includes the minimal configuration variables.
let settings = UserSettings::from_config(StackedConfig::empty()).unwrap();
fn test_init_with_default_config(backend: TestRepoBackend) {
// Test that we can create a repo without setting any non-default config
let settings = UserSettings::from_config(StackedConfig::with_defaults()).unwrap();
let test_workspace = TestWorkspace::init_with_backend(&settings, backend);
let repo = &test_workspace.repo;
let wc_commit_id = repo
Expand Down
2 changes: 1 addition & 1 deletion lib/testutils/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ pub fn base_user_config() -> StackedConfig {
operation.hostname = "host.example.com"
debug.randomness-seed = 42
"#;
let mut config = StackedConfig::empty();
let mut config = StackedConfig::with_defaults();
config.add_layer(ConfigLayer::parse(ConfigSource::User, config_text).unwrap());
config
}
Expand Down

0 comments on commit e4a350f

Please sign in to comment.