mirror of
https://github.com/martinvonz/jj.git
synced 2025-01-18 18:27:38 +00:00
config: extract jj-lib's default values to embedded TOML file
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.
This commit is contained in:
parent
5c12c2fc8b
commit
e4a350fcaa
10 changed files with 47 additions and 18 deletions
|
@ -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());
|
||||
|
|
|
@ -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());
|
||||
|
|
|
@ -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;
|
||||
|
@ -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(
|
||||
|
@ -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;
|
||||
|
|
2
lib/src/config/README.md
Normal file
2
lib/src/config/README.md
Normal file
|
@ -0,0 +1,2 @@
|
|||
These files are compiled into the binary; they are not installed to the user's
|
||||
system.
|
16
lib/src/config/misc.toml
Normal file
16
lib/src/config/misc.toml
Normal file
|
@ -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 = ""
|
|
@ -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()
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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,
|
||||
|
@ -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()?;
|
||||
|
|
|
@ -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,
|
||||
|
@ -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;
|
||||
|
||||
|
@ -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,
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue