Skip to content

Worktrunk is a CLI for Git worktree management, designed for parallel AI agent workflows

refactor(config): eliminate Settings types, add accessor methods to Config types (#896)

Maximilian Roos771ad8e80e83
diff --git a/src/commands/context.rs b/src/commands/context.rs
index 6faa8821a..29623bcb0 100644
--- a/src/commands/context.rs
+++ b/src/commands/context.rs
@@ -92,18 +92,8 @@ impl CommandEnv {
self.repo.project_identifier().ok()
}
- /// Get the commit generation config, merging project-specific settings.
- pub fn commit_generation(&self) -> worktrunk::config::CommitGenerationConfig {
- self.config.commit_generation(self.project_id().as_deref())
- }
-
- /// Get the commit config, merging project-specific settings.
- pub fn commit(&self) -> Option<worktrunk::config::CommitConfig> {
- self.config.commit(self.project_id().as_deref())
- }
-
- /// Get the merge config, merging project-specific settings.
- pub fn merge(&self) -> Option<worktrunk::config::MergeConfig> {
- self.config.merge(self.project_id().as_deref())
+ /// Get all resolved config with defaults applied.
+ pub fn resolved(&self) -> worktrunk::config::ResolvedConfig {
+ self.config.resolved(self.project_id().as_deref())
}
}
diff --git a/src/commands/merge.rs b/src/commands/merge.rs
index 889977fb5..5eee90b03 100644
--- a/src/commands/merge.rs
+++ b/src/commands/merge.rs
@@ -101,30 +101,16 @@ pub fn handle_merge(opts: MergeOptions<'_>) -> anyhow::Result<()> {
// Merge requires being on a branch (can't merge from detached HEAD)
let current_branch = env.require_branch("merge")?.to_string();
- // Get effective merge config (project-specific merged with global)
- let merge_config = env.merge();
-
- // Determine final values: CLI > project config > global config > default (true)
- let squash = squash_opt
- .or_else(|| merge_config.as_ref().and_then(|m| m.squash))
- .unwrap_or(true);
- let commit = commit_opt
- .or_else(|| merge_config.as_ref().and_then(|m| m.commit))
- .unwrap_or(true);
- let rebase = rebase_opt
- .or_else(|| merge_config.as_ref().and_then(|m| m.rebase))
- .unwrap_or(true);
- let remove = remove_opt
- .or_else(|| merge_config.as_ref().and_then(|m| m.remove))
- .unwrap_or(true);
- let verify = verify_opt
- .or_else(|| merge_config.as_ref().and_then(|m| m.verify))
- .unwrap_or(true);
-
- // Stage mode: CLI > project commit config > global commit config > default
- let stage_mode = stage
- .or_else(|| env.commit().and_then(|c| c.stage))
- .unwrap_or_default();
+ // Get effective settings (project-specific merged with global, defaults applied)
+ let resolved = env.resolved();
+
+ // CLI flags override config values
+ let squash = squash_opt.unwrap_or(resolved.merge.squash());
+ let commit = commit_opt.unwrap_or(resolved.merge.commit());
+ let rebase = rebase_opt.unwrap_or(resolved.merge.rebase());
+ let remove = remove_opt.unwrap_or(resolved.merge.remove());
+ let verify = verify_opt.unwrap_or(resolved.merge.verify());
+ let stage_mode = stage.unwrap_or(resolved.commit.stage());
// Cache current worktree for multiple queries
let current_wt = repo.current_worktree();
diff --git a/src/commands/step_commands.rs b/src/commands/step_commands.rs
index 935b7c74c..0a79ded90 100644
--- a/src/commands/step_commands.rs
+++ b/src/commands/step_commands.rs
@@ -56,10 +56,8 @@ pub fn step_commit(
let env = CommandEnv::for_action("commit", config)?;
let ctx = env.context(yes);
- // Determine effective stage mode: CLI > project config > global config > default
- let stage_mode = stage
- .or_else(|| env.commit().and_then(|c| c.stage))
- .unwrap_or_default();
+ // CLI flag overrides config value
+ let stage_mode = stage.unwrap_or(env.resolved().commit.stage());
// "Approve at the Gate": approve pre-commit hooks upfront (unless --no-verify)
// Shadow no_verify: if user declines approval, skip hooks but continue commit
@@ -122,13 +120,11 @@ pub fn handle_squash(
// Squash requires being on a branch (can't squash in detached HEAD)
let current_branch = env.require_branch("squash")?.to_string();
let ctx = env.context(yes);
- let effective_config = env.commit_generation();
- let generator = CommitGenerator::new(&effective_config);
+ let resolved = env.resolved();
+ let generator = CommitGenerator::new(&resolved.commit_generation);
- // Determine effective stage mode: CLI > project config > global config > default
- let stage_mode = stage
- .or_else(|| env.commit().and_then(|c| c.stage))
- .unwrap_or_default();
+ // CLI flag overrides config value
+ let stage_mode = stage.unwrap_or(resolved.commit.stage());
// Check if any pre-commit hooks exist (needed for skip message and approval)
let project_config = repo.load_project_config()?;
@@ -300,7 +296,7 @@ pub fn handle_squash(
&subjects,
&current_branch,
repo_name,
- &effective_config,
+ &resolved.commit_generation,
)?;
// Display the generated commit message
diff --git a/src/config/mod.rs b/src/config/mod.rs
index 40581dd4e..cd865c4c1 100644
--- a/src/config/mod.rs
+++ b/src/config/mod.rs
@@ -92,9 +92,9 @@ pub use project::{
find_unknown_keys as find_unknown_project_keys,
};
pub use user::{
- CommitConfig, CommitGenerationConfig, ListConfig, MergeConfig, OverridableConfig, SelectConfig,
- StageMode, UserConfig, UserProjectOverrides, find_unknown_keys as find_unknown_user_keys,
- get_config_path, set_config_path,
+ CommitConfig, CommitGenerationConfig, ListConfig, MergeConfig, OverridableConfig,
+ ResolvedConfig, SelectConfig, StageMode, UserConfig, UserProjectOverrides,
+ find_unknown_keys as find_unknown_user_keys, get_config_path, set_config_path,
};
#[cfg(test)]
diff --git a/src/config/user/accessors.rs b/src/config/user/accessors.rs
index d689c83ca..81521c0f5 100644
--- a/src/config/user/accessors.rs
+++ b/src/config/user/accessors.rs
@@ -138,6 +138,15 @@ impl UserConfig {
}
}
+ // ---- Resolved config (concrete types with defaults applied) ----
+
+ /// Returns all resolved config with defaults applied.
+ ///
+ /// Merges global and per-project settings, applying defaults for any unset fields.
+ pub fn resolved(&self, project: Option<&str>) -> super::resolved::ResolvedConfig {
+ super::resolved::ResolvedConfig::for_project(self, project)
+ }
+
/// Format a worktree path using this configuration's template.
///
/// # Arguments
diff --git a/src/config/user/mod.rs b/src/config/user/mod.rs
index ba90e4f41..35c76fd9b 100644
--- a/src/config/user/mod.rs
+++ b/src/config/user/mod.rs
@@ -7,6 +7,7 @@ mod merge;
mod mutation;
mod path;
mod persistence;
+mod resolved;
mod schema;
mod sections;
#[cfg(test)]
@@ -19,6 +20,7 @@ use serde::{Deserialize, Serialize};
// Re-export public types
pub use merge::Merge;
pub use path::{get_config_path, set_config_path};
+pub use resolved::ResolvedConfig;
pub use schema::{find_unknown_keys, valid_user_config_keys};
pub use sections::{
CommitConfig, CommitGenerationConfig, ListConfig, MergeConfig, OverridableConfig, SelectConfig,
diff --git a/src/config/user/resolved.rs b/src/config/user/resolved.rs
new file mode 100644
index 000000000..5f063d6f9
--- /dev/null
+++ b/src/config/user/resolved.rs
@@ -0,0 +1,46 @@
+//! Resolved configuration with all merging applied.
+//!
+//! `ResolvedConfig` holds the merged configuration for a specific project context.
+//! Config types provide accessor methods that apply defaults, so callers use
+//! `resolved.list.full()` instead of `resolved.list.full.unwrap_or(false)`.
+
+use super::UserConfig;
+use super::sections::{
+ CommitConfig, CommitGenerationConfig, ListConfig, MergeConfig, SelectConfig,
+};
+
+/// All resolved configuration for a specific project context.
+///
+/// Holds merged Config types (global + per-project). Use accessor methods
+/// on each config to get values with defaults applied.
+///
+/// # Example
+/// ```ignore
+/// let resolved = config.resolved(project);
+/// let full = resolved.list.full(); // bool, default applied
+/// let squash = resolved.merge.squash(); // bool, default applied
+/// let stage = resolved.commit.stage(); // StageMode, default applied
+/// let pager = resolved.select.pager(); // Option<&str>
+/// ```
+#[derive(Debug, Clone, PartialEq)]
+pub struct ResolvedConfig {
+ pub list: ListConfig,
+ pub merge: MergeConfig,
+ pub commit: CommitConfig,
+ /// Resolved commit generation config (handles deprecated `[commit-generation]` fallback)
+ pub commit_generation: CommitGenerationConfig,
+ pub select: SelectConfig,
+}
+
+impl ResolvedConfig {
+ /// Resolve all configuration for a project.
+ pub fn for_project(config: &UserConfig, project: Option<&str>) -> Self {
+ Self {
+ list: config.list(project).unwrap_or_default(),
+ merge: config.merge(project).unwrap_or_default(),
+ commit: config.commit(project).unwrap_or_default(),
+ commit_generation: config.commit_generation(project),
+ select: config.select(project).unwrap_or_default(),
+ }
+ }
+}
diff --git a/src/config/user/sections.rs b/src/config/user/sections.rs
index 01ba68f15..a5ed93bd2 100644
--- a/src/config/user/sections.rs
+++ b/src/config/user/sections.rs
@@ -136,6 +136,28 @@ pub struct ListConfig {
pub timeout_ms: Option<u64>,
}
+impl ListConfig {
+ /// Show CI and `main` diffstat by default (default: false)
+ pub fn full(&self) -> bool {
+ self.full.unwrap_or(false)
+ }
+
+ /// Include branches without worktrees by default (default: false)
+ pub fn branches(&self) -> bool {
+ self.branches.unwrap_or(false)
+ }
+
+ /// Include remote branches by default (default: false)
+ pub fn remotes(&self) -> bool {
+ self.remotes.unwrap_or(false)
+ }
+
+ /// Per-task timeout in milliseconds (default: None)
+ pub fn timeout_ms(&self) -> Option<u64> {
+ self.timeout_ms
+ }
+}
+
impl Merge for ListConfig {
fn merge_with(&self, other: &Self) -> Self {
Self {
@@ -164,6 +186,13 @@ pub struct CommitConfig {
pub generation: Option<CommitGenerationConfig>,
}
+impl CommitConfig {
+ /// What to stage before committing (default: All)
+ pub fn stage(&self) -> StageMode {
+ self.stage.unwrap_or_default()
+ }
+}
+
impl Merge for CommitConfig {
fn merge_with(&self, other: &Self) -> Self {
Self {
@@ -206,6 +235,33 @@ pub struct MergeConfig {
pub verify: Option<bool>,
}
+impl MergeConfig {
+ /// Squash commits when merging (default: true)
+ pub fn squash(&self) -> bool {
+ self.squash.unwrap_or(true)
+ }
+
+ /// Commit, squash, and rebase during merge (default: true)
+ pub fn commit(&self) -> bool {
+ self.commit.unwrap_or(true)
+ }
+
+ /// Rebase onto target branch before merging (default: true)
+ pub fn rebase(&self) -> bool {
+ self.rebase.unwrap_or(true)
+ }
+
+ /// Remove worktree after merge (default: true)
+ pub fn remove(&self) -> bool {
+ self.remove.unwrap_or(true)
+ }
+
+ /// Run project hooks (default: true)
+ pub fn verify(&self) -> bool {
+ self.verify.unwrap_or(true)
+ }
+}
+
impl Merge for MergeConfig {
fn merge_with(&self, other: &Self) -> Self {
Self {
@@ -231,6 +287,13 @@ pub struct SelectConfig {
pub pager: Option<String>,
}
+impl SelectConfig {
+ /// Pager command with flags for diff preview (default: None, uses git default)
+ pub fn pager(&self) -> Option<&str> {
+ self.pager.as_deref()
+ }
+}
+
impl Merge for SelectConfig {
fn merge_with(&self, other: &Self) -> Self {
Self {
diff --git a/src/config/user/tests.rs b/src/config/user/tests.rs
index 6fc580225..47cdfc1d4 100644
--- a/src/config/user/tests.rs
+++ b/src/config/user/tests.rs
@@ -932,6 +932,118 @@ fn test_effective_commit_global_only() {
assert_eq!(effective.stage, Some(StageMode::Tracked));
}
+// =========================================================================
+// Config accessor methods and ResolvedConfig tests
+// =========================================================================
+
+#[test]
+fn test_list_config_accessor_methods_defaults() {
+ let config = ListConfig::default();
+ assert!(!config.full());
+ assert!(!config.branches());
+ assert!(!config.remotes());
+ assert!(config.timeout_ms().is_none());
+}
+
+#[test]
+fn test_list_config_accessor_methods_with_values() {
+ let config = ListConfig {
+ full: Some(true),
+ branches: Some(true),
+ remotes: Some(false),
+ timeout_ms: Some(5000),
+ };
+ assert!(config.full());
+ assert!(config.branches());
+ assert!(!config.remotes());
+ assert_eq!(config.timeout_ms(), Some(5000));
+}
+
+#[test]
+fn test_merge_config_accessor_methods_defaults() {
+ let config = MergeConfig::default();
+ // MergeConfig defaults are all true
+ assert!(config.squash());
+ assert!(config.commit());
+ assert!(config.rebase());
+ assert!(config.remove());
+ assert!(config.verify());
+}
+
+#[test]
+fn test_merge_config_accessor_methods_with_values() {
+ let config = MergeConfig {
+ squash: Some(false),
+ commit: Some(false),
+ rebase: Some(false),
+ remove: Some(false),
+ verify: Some(false),
+ };
+ assert!(!config.squash());
+ assert!(!config.commit());
+ assert!(!config.rebase());
+ assert!(!config.remove());
+ assert!(!config.verify());
+}
+
+#[test]
+fn test_commit_config_accessor_methods() {
+ let config = CommitConfig::default();
+ assert_eq!(config.stage(), StageMode::All);
+
+ let config = CommitConfig {
+ stage: Some(StageMode::Tracked),
+ generation: None,
+ };
+ assert_eq!(config.stage(), StageMode::Tracked);
+}
+
+#[test]
+fn test_select_config_accessor_methods() {
+ let config = SelectConfig::default();
+ assert!(config.pager().is_none());
+
+ let config = SelectConfig {
+ pager: Some("delta --paging=never".to_string()),
+ };
+ assert_eq!(config.pager(), Some("delta --paging=never"));
+}
+
+#[test]
+fn test_resolved_config_for_project() {
+ let config = UserConfig {
+ configs: OverridableConfig {
+ list: Some(ListConfig {
+ full: Some(true),
+ ..Default::default()
+ }),
+ merge: Some(MergeConfig {
+ squash: Some(false),
+ ..Default::default()
+ }),
+ commit: Some(CommitConfig {
+ stage: Some(StageMode::None),
+ ..Default::default()
+ }),
+ select: Some(SelectConfig {
+ pager: Some("less".to_string()),
+ }),
+ ..Default::default()
+ },
+ ..Default::default()
+ };
+
+ let resolved = config.resolved(None);
+
+ // Test that accessor methods work through ResolvedConfig
+ assert!(resolved.list.full());
+ assert!(!resolved.list.branches()); // Default
+ assert!(!resolved.merge.squash()); // Overridden to false
+ assert!(resolved.merge.commit()); // Default true
+ assert_eq!(resolved.commit.stage(), StageMode::None);
+ assert_eq!(resolved.select.pager(), Some("less"));
+}
+
// =========================================================================
// Per-project config serde tests
// =========================================================================
diff --git a/src/main.rs b/src/main.rs
index 1731ff12f..f9f51c981 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -646,20 +646,15 @@ fn main() {
UserConfig::load()
.context("Failed to load config")
.and_then(|config| {
- // Get project ID for per-project config lookup
+ // Get effective settings (project-specific merged with global, defaults applied)
let project_id = Repository::current()
.ok()
.and_then(|r| r.project_identifier().ok());
-
- // Get effective list config (merges project-specific with global)
- let (show_branches_config, show_remotes_config) = config
- .list(project_id.as_deref())
- .map(|l| (l.branches.unwrap_or(false), l.remotes.unwrap_or(false)))
- .unwrap_or((false, false));
+ let resolved = config.resolved(project_id.as_deref());
// CLI flags override config
- let show_branches = branches || show_branches_config;
- let show_remotes = remotes || show_remotes_config;
+ let show_branches = branches || resolved.list.branches();
+ let show_remotes = remotes || resolved.list.remotes();
handle_select(show_branches, show_remotes, &config)
})
@@ -702,27 +697,16 @@ fn main() {
UserConfig::load()
.context("Failed to load config")
.and_then(|config| {
- // Get project ID for per-project config lookup
+ // Get resolved config (project-specific merged with global, defaults applied)
let project_id = Repository::current()
.ok()
.and_then(|r| r.project_identifier().ok());
-
- // Get effective list config (merges project-specific with global)
- let (show_branches_config, show_remotes_config, show_full_config) = config
- .list(project_id.as_deref())
- .map(|l| {
- (
- l.branches.unwrap_or(false),
- l.remotes.unwrap_or(false),
- l.full.unwrap_or(false),
- )
- })
- .unwrap_or((false, false, false));
+ let resolved = config.resolved(project_id.as_deref());
// CLI flags override config
- let show_branches = branches || show_branches_config;
- let show_remotes = remotes || show_remotes_config;
- let show_full = full || show_full_config;
+ let show_branches = branches || resolved.list.branches();
+ let show_remotes = remotes || resolved.list.remotes();
+ let show_full = full || resolved.list.full();
// Convert two bools to Option<bool>: Some(true), Some(false), or None
let progressive_opt = match (progressive, no_progressive) {