Skip to content

Commit 5bd669e

Browse files
committed
settings: propagate configuration error of commit and operation parameters
Note that infallible version of whoami::username() would return "Unknown" on error. I just made it error out, but it's also an option to fall back to an empty string.
1 parent 4a69d01 commit 5bd669e

File tree

2 files changed

+24
-6
lines changed

2 files changed

+24
-6
lines changed

CHANGELOG.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,10 @@ to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
1818
* The following configuration variables are now parsed strictly:
1919
`colors.<labels>`, `git.abandon-unreachable-commits`,
2020
`git.auto-local-bookmark`, `git.push-bookmark-prefix`, `revsets.log`,
21-
`revsets.short-prefixes` `signing.backend`, `ui.allow-init-native`,
22-
`ui.color`, `ui.default-description`, `ui.progress-indicator`, `ui.quiet`
21+
`revsets.short-prefixes` `signing.backend`, `operation.hostname`,
22+
`operation.username`, `ui.allow-init-native`, `ui.color`,
23+
`ui.default-description`, `ui.progress-indicator`, `ui.quiet`, `user.email`,
24+
`user.name`
2325

2426
* `jj config list` now prints inline tables `{ key = value, .. }` literally.
2527
Inner items of inline tables are no longer merged across configuration files.

lib/src/settings.rs

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -146,20 +146,36 @@ fn to_timestamp(value: ConfigValue) -> Result<Timestamp, Box<dyn std::error::Err
146146

147147
impl UserSettings {
148148
pub fn from_config(config: StackedConfig) -> Result<Self, ConfigGetError> {
149-
let user_name = config.get("user.name").unwrap_or_default();
150-
let user_email = config.get("user.email").unwrap_or_default();
149+
let user_name = config.get("user.name").optional()?.unwrap_or_default();
150+
let user_email = config.get("user.email").optional()?.unwrap_or_default();
151151
let commit_timestamp = config
152152
.get_value_with("debug.commit-timestamp", to_timestamp)
153153
.optional()?;
154154
let operation_timestamp = config
155155
.get_value_with("debug.operation-timestamp", to_timestamp)
156156
.optional()?;
157+
// whoami::fallible::*() failure isn't a ConfigGetError, but user would
158+
// have to set the corresponding config keys if these parameter can't be
159+
// obtained from the system. Instead of handling environment data here,
160+
// it might be better to load them by CLI and insert as a config layer.
157161
let operation_hostname = config
158162
.get("operation.hostname")
159-
.unwrap_or_else(|_| whoami::fallible::hostname().expect("valid hostname"));
163+
.optional()?
164+
.map_or_else(whoami::fallible::hostname, Ok)
165+
.map_err(|err| ConfigGetError::Type {
166+
name: "operation.hostname".to_owned(),
167+
error: err.into(),
168+
source_path: None,
169+
})?;
160170
let operation_username = config
161171
.get("operation.username")
162-
.unwrap_or_else(|_| whoami::username());
172+
.optional()?
173+
.map_or_else(whoami::fallible::username, Ok)
174+
.map_err(|err| ConfigGetError::Type {
175+
name: "operation.username".to_owned(),
176+
error: err.into(),
177+
source_path: None,
178+
})?;
163179
let rng_seed = config.get::<u64>("debug.randomness-seed").optional()?;
164180
Ok(UserSettings {
165181
config,

0 commit comments

Comments
 (0)