Skip to content

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

refactor: unify overridable config into shared OverridableConfig struct (#829)

Maximilian Roos5617986528b1
diff --git a/src/commands/list/mod.rs b/src/commands/list/mod.rs
index 251d3f644..85f78a864 100644
--- a/src/commands/list/mod.rs
+++ b/src/commands/list/mod.rs
@@ -188,6 +188,7 @@ pub fn handle_list(
None // --full disables timeout for complete data collection
} else {
config
+ .overrides
.list
.as_ref()
.and_then(|l| l.timeout_ms)
diff --git a/src/commands/select/pager.rs b/src/commands/select/pager.rs
index 903f4e259..919635ea1 100644
--- a/src/commands/select/pager.rs
+++ b/src/commands/select/pager.rs
@@ -36,7 +36,7 @@ pub(super) fn get_diff_pager() -> Option<&'static String> {
// Check user config first for explicit pager override
// When set, use exactly as specified (no auto-detection)
if let Ok(config) = UserConfig::load()
- && let Some(select_config) = config.select
+ && let Some(select_config) = config.overrides.select
&& let Some(pager) = select_config.pager
&& !pager.trim().is_empty()
{
@@ -81,7 +81,7 @@ pub(super) fn pager_needs_paging_disabled(pager_cmd: &str) -> bool {
pub(super) fn has_explicit_pager_config() -> bool {
UserConfig::load()
.ok()
- .and_then(|config| config.select)
+ .and_then(|config| config.overrides.select)
.and_then(|select| select.pager)
.is_some_and(|p| !p.trim().is_empty())
}
diff --git a/src/config/mod.rs b/src/config/mod.rs
index e04c776a3..bb563b7e3 100644
--- a/src/config/mod.rs
+++ b/src/config/mod.rs
@@ -91,9 +91,9 @@ pub use project::{
find_unknown_keys as find_unknown_project_keys,
};
pub use user::{
- CommitConfig, CommitGenerationConfig, ListConfig, MergeConfig, StageMode, UserConfig,
- UserProjectOverrides, find_unknown_keys as find_unknown_user_keys, get_config_path,
- set_config_path,
+ CommitConfig, CommitGenerationConfig, ListConfig, MergeConfig, OverridableConfig, SelectConfig,
+ StageMode, UserConfig, UserProjectOverrides, find_unknown_keys as find_unknown_user_keys,
+ get_config_path, set_config_path,
};
#[cfg(test)]
@@ -138,7 +138,10 @@ mod tests {
#[test]
fn test_config_serialization_with_worktree_path() {
let config = UserConfig {
- worktree_path: Some("custom/{{ branch }}".to_string()),
+ overrides: OverridableConfig {
+ worktree_path: Some("custom/{{ branch }}".to_string()),
+ ..Default::default()
+ },
..Default::default()
};
let toml = toml::to_string(&config).unwrap();
@@ -150,7 +153,7 @@ mod tests {
fn test_default_config() {
let config = UserConfig::default();
// worktree_path is None by default, but the getter returns the default
- assert!(config.worktree_path.is_none());
+ assert!(config.overrides.worktree_path.is_none());
assert_eq!(
config.worktree_path(),
"../{{ repo }}.{{ branch | sanitize }}"
@@ -164,7 +167,10 @@ mod tests {
fn test_format_worktree_path() {
let test = test_repo();
let config = UserConfig {
- worktree_path: Some("{{ main_worktree }}.{{ branch }}".to_string()),
+ overrides: OverridableConfig {
+ worktree_path: Some("{{ main_worktree }}.{{ branch }}".to_string()),
+ ..Default::default()
+ },
..Default::default()
};
assert_eq!(
@@ -179,7 +185,10 @@ mod tests {
fn test_format_worktree_path_custom_template() {
let test = test_repo();
let config = UserConfig {
- worktree_path: Some("{{ main_worktree }}-{{ branch }}".to_string()),
+ overrides: OverridableConfig {
+ worktree_path: Some("{{ main_worktree }}-{{ branch }}".to_string()),
+ ..Default::default()
+ },
..Default::default()
};
assert_eq!(
@@ -194,7 +203,10 @@ mod tests {
fn test_format_worktree_path_only_branch() {
let test = test_repo();
let config = UserConfig {
- worktree_path: Some(".worktrees/{{ main_worktree }}/{{ branch }}".to_string()),
+ overrides: OverridableConfig {
+ worktree_path: Some(".worktrees/{{ main_worktree }}/{{ branch }}".to_string()),
+ ..Default::default()
+ },
..Default::default()
};
assert_eq!(
@@ -210,7 +222,10 @@ mod tests {
let test = test_repo();
// Use {{ branch | sanitize }} to replace slashes with dashes
let config = UserConfig {
- worktree_path: Some("{{ main_worktree }}.{{ branch | sanitize }}".to_string()),
+ overrides: OverridableConfig {
+ worktree_path: Some("{{ main_worktree }}.{{ branch | sanitize }}".to_string()),
+ ..Default::default()
+ },
..Default::default()
};
assert_eq!(
@@ -225,9 +240,12 @@ mod tests {
fn test_format_worktree_path_with_multiple_slashes() {
let test = test_repo();
let config = UserConfig {
- worktree_path: Some(
- ".worktrees/{{ main_worktree }}/{{ branch | sanitize }}".to_string(),
- ),
+ overrides: OverridableConfig {
+ worktree_path: Some(
+ ".worktrees/{{ main_worktree }}/{{ branch | sanitize }}".to_string(),
+ ),
+ ..Default::default()
+ },
..Default::default()
};
assert_eq!(
@@ -243,9 +261,12 @@ mod tests {
let test = test_repo();
// Windows-style path separators should also be sanitized
let config = UserConfig {
- worktree_path: Some(
- ".worktrees/{{ main_worktree }}/{{ branch | sanitize }}".to_string(),
- ),
+ overrides: OverridableConfig {
+ worktree_path: Some(
+ ".worktrees/{{ main_worktree }}/{{ branch | sanitize }}".to_string(),
+ ),
+ ..Default::default()
+ },
..Default::default()
};
assert_eq!(
@@ -261,7 +282,10 @@ mod tests {
let test = test_repo();
// {{ branch }} without filter gives raw branch name
let config = UserConfig {
- worktree_path: Some("{{ main_worktree }}.{{ branch }}".to_string()),
+ overrides: OverridableConfig {
+ worktree_path: Some("{{ main_worktree }}.{{ branch }}".to_string()),
+ ..Default::default()
+ },
..Default::default()
};
assert_eq!(
@@ -738,7 +762,11 @@ template-file = "~/file.txt"
// The deserialization should succeed, but validation in load() would fail
// Since we can't easily test load() without env vars, we verify the fields deserialize
if let Ok(config) = config_result {
- let generation = config.commit.as_ref().and_then(|c| c.generation.as_ref());
+ let generation = config
+ .overrides
+ .commit
+ .as_ref()
+ .and_then(|c| c.generation.as_ref());
// Verify validation logic: both fields should not be Some
let has_both = generation
.map(|g| g.template.is_some() && g.template_file.is_some())
@@ -768,7 +796,11 @@ squash-template-file = "~/file.txt"
// The deserialization should succeed, but validation in load() would fail
// Since we can't easily test load() without env vars, we verify the fields deserialize
if let Ok(config) = config_result {
- let generation = config.commit.as_ref().and_then(|c| c.generation.as_ref());
+ let generation = config
+ .overrides
+ .commit
+ .as_ref()
+ .and_then(|c| c.generation.as_ref());
// Verify validation logic: both fields should not be Some
let has_both = generation
.map(|g| g.squash_template.is_some() && g.squash_template_file.is_some())
diff --git a/src/config/user.rs b/src/config/user.rs
index c793418b7..a892b56f3 100644
--- a/src/config/user.rs
+++ b/src/config/user.rs
@@ -139,13 +139,6 @@ pub enum StageMode {
/// `__` separator for nested fields (e.g., `WORKTRUNK_COMMIT__GENERATION__COMMAND`).
#[derive(Debug, Default, Serialize, Deserialize)]
pub struct UserConfig {
- #[serde(
- rename = "worktree-path",
- default,
- skip_serializing_if = "Option::is_none"
- )]
- pub(crate) worktree_path: Option<String>,
-
/// **DEPRECATED**: Use `[commit.generation]` instead.
///
/// This field is kept for backward compatibility. When both are set,
@@ -162,21 +155,9 @@ pub struct UserConfig {
#[serde(default)]
pub projects: std::collections::BTreeMap<String, UserProjectOverrides>,
- /// Configuration for the `wt list` command
- #[serde(default, skip_serializing_if = "Option::is_none")]
- pub list: Option<ListConfig>,
-
- /// Configuration for the `wt step commit` command (also used by merge)
- #[serde(default, skip_serializing_if = "Option::is_none")]
- pub commit: Option<CommitConfig>,
-
- /// Configuration for the `wt merge` command
- #[serde(default, skip_serializing_if = "Option::is_none")]
- pub merge: Option<MergeConfig>,
-
- /// Configuration for the `wt select` command
- #[serde(default, skip_serializing_if = "Option::is_none")]
- pub select: Option<SelectConfig>,
+ /// Settings that can be overridden per-project (worktree-path, list, commit, merge, select)
+ #[serde(flatten, default)]
+ pub overrides: OverridableConfig,
// =========================================================================
// User-level hooks (same syntax as project hooks, run before project hooks)
@@ -300,14 +281,6 @@ impl Merge for CommitGenerationConfig {
/// ```
#[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Default)]
pub struct UserProjectOverrides {
- /// Worktree path template for this project (overrides global worktree-path)
- #[serde(
- rename = "worktree-path",
- default,
- skip_serializing_if = "Option::is_none"
- )]
- pub worktree_path: Option<String>,
-
/// Commands that have been approved for automatic execution in this project
#[serde(
default,
@@ -326,17 +299,9 @@ pub struct UserProjectOverrides {
)]
pub commit_generation: Option<CommitGenerationConfig>,
- /// Per-project list settings (overrides global `[list]`)
- #[serde(default, skip_serializing_if = "Option::is_none")]
- pub list: Option<ListConfig>,
-
- /// Per-project commit settings (overrides global `[commit]`)
- #[serde(default, skip_serializing_if = "Option::is_none")]
- pub commit: Option<CommitConfig>,
-
- /// Per-project merge settings (overrides global `[merge]`)
- #[serde(default, skip_serializing_if = "Option::is_none")]
- pub merge: Option<MergeConfig>,
+ /// Per-project overrides (worktree-path, list, commit, merge, select)
+ #[serde(flatten, default)]
+ pub overrides: OverridableConfig,
}
impl UserProjectOverrides {
@@ -345,12 +310,9 @@ impl UserProjectOverrides {
/// Used to determine if a project entry can be removed from config after
/// clearing approvals.
pub fn is_empty(&self) -> bool {
- self.worktree_path.is_none()
- && self.approved_commands.is_empty()
+ self.approved_commands.is_empty()
&& self.commit_generation.is_none()
- && self.list.is_none()
- && self.commit.is_none()
- && self.merge.is_none()
+ && self.overrides.is_empty()
}
}
@@ -460,7 +422,7 @@ impl Merge for MergeConfig {
}
/// Configuration for the `wt select` command
-#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
+#[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Default)]
pub struct SelectConfig {
/// Pager command with flags for diff preview
///
@@ -472,6 +434,72 @@ pub struct SelectConfig {
pub pager: Option<String>,
}
+impl Merge for SelectConfig {
+ fn merge_with(&self, other: &Self) -> Self {
+ Self {
+ pager: other.pager.clone().or_else(|| self.pager.clone()),
+ }
+ }
+}
+
+/// Settings that can be set globally or per-project.
+///
+/// This struct is flattened into both `UserConfig` (global) and `UserProjectOverrides`
+/// (per-project), ensuring new settings are automatically available in both
+/// contexts without manual synchronization.
+#[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Default)]
+pub struct OverridableConfig {
+ /// Worktree path template
+ #[serde(
+ rename = "worktree-path",
+ default,
+ skip_serializing_if = "Option::is_none"
+ )]
+ pub worktree_path: Option<String>,
+
+ /// Configuration for the `wt list` command
+ #[serde(default, skip_serializing_if = "Option::is_none")]
+ pub list: Option<ListConfig>,
+
+ /// Configuration for the `wt step commit` command (also used by merge)
+ #[serde(default, skip_serializing_if = "Option::is_none")]
+ pub commit: Option<CommitConfig>,
+
+ /// Configuration for the `wt merge` command
+ #[serde(default, skip_serializing_if = "Option::is_none")]
+ pub merge: Option<MergeConfig>,
+
+ /// Configuration for the `wt select` command
+ #[serde(default, skip_serializing_if = "Option::is_none")]
+ pub select: Option<SelectConfig>,
+}
+
+impl OverridableConfig {
+ /// Returns true if all settings are None.
+ pub fn is_empty(&self) -> bool {
+ self.worktree_path.is_none()
+ && self.list.is_none()
+ && self.commit.is_none()
+ && self.merge.is_none()
+ && self.select.is_none()
+ }
+}
+
+impl Merge for OverridableConfig {
+ fn merge_with(&self, other: &Self) -> Self {
+ Self {
+ worktree_path: other
+ .worktree_path
+ .clone()
+ .or_else(|| self.worktree_path.clone()),
+ list: merge_optional(self.list.as_ref(), other.list.as_ref()),
+ commit: merge_optional(self.commit.as_ref(), other.commit.as_ref()),
+ merge: merge_optional(self.merge.as_ref(), other.merge.as_ref()),
+ select: merge_optional(self.select.as_ref(), other.select.as_ref()),
+ }
+ }
+}
+
/// Default worktree path template
fn default_worktree_path() -> String {
"../{{ repo }}.{{ branch | sanitize }}".to_string()
@@ -480,14 +508,15 @@ fn default_worktree_path() -> String {
impl UserConfig {
/// Returns the worktree path template, falling back to the default if not set.
pub fn worktree_path(&self) -> String {
- self.worktree_path
+ self.overrides
+ .worktree_path
.clone()
.unwrap_or_else(default_worktree_path)
}
/// Returns true if the user has explicitly set a custom worktree-path.
pub fn has_custom_worktree_path(&self) -> bool {
- self.worktree_path.is_some()
+ self.overrides.worktree_path.is_some()
}
/// Returns the worktree path template for a specific project.
@@ -497,7 +526,7 @@ impl UserConfig {
pub fn worktree_path_for_project(&self, project: &str) -> String {
self.projects
.get(project)
- .and_then(|p| p.worktree_path.clone())
+ .and_then(|p| p.overrides.worktree_path.clone())
.unwrap_or_else(|| self.worktree_path())
}
@@ -513,6 +542,7 @@ impl UserConfig {
pub fn commit_generation(&self, project: Option<&str>) -> CommitGenerationConfig {
// Get global config: prefer new location, fall back to deprecated
let global = self
+ .overrides
.commit
.as_ref()
.and_then(|c| c.generation.as_ref())
@@ -522,7 +552,8 @@ impl UserConfig {
// Get project override (also checks both locations)
let project_config = project.and_then(|p| self.projects.get(p)).and_then(|c| {
- c.commit
+ c.overrides
+ .commit
.as_ref()
.and_then(|cc| cc.generation.as_ref())
.or(c.commit_generation.as_ref())
@@ -541,8 +572,8 @@ impl UserConfig {
pub fn list(&self, project: Option<&str>) -> Option<ListConfig> {
let project_config = project
.and_then(|p| self.projects.get(p))
- .and_then(|c| c.list.as_ref());
- merge_optional(self.list.as_ref(), project_config)
+ .and_then(|c| c.overrides.list.as_ref());
+ merge_optional(self.overrides.list.as_ref(), project_config)
}
/// Returns the commit config for a specific project.
@@ -552,8 +583,8 @@ impl UserConfig {
pub fn commit(&self, project: Option<&str>) -> Option<CommitConfig> {
let project_config = project
.and_then(|p| self.projects.get(p))
- .and_then(|c| c.commit.as_ref());
- merge_optional(self.commit.as_ref(), project_config)
+ .and_then(|c| c.overrides.commit.as_ref());
+ merge_optional(self.overrides.commit.as_ref(), project_config)
}
/// Returns the merge config for a specific project.
@@ -563,8 +594,19 @@ impl UserConfig {
pub fn merge(&self, project: Option<&str>) -> Option<MergeConfig> {
let project_config = project
.and_then(|p| self.projects.get(p))
- .and_then(|c| c.merge.as_ref());
- merge_optional(self.merge.as_ref(), project_config)
+ .and_then(|c| c.overrides.merge.as_ref());
+ merge_optional(self.overrides.merge.as_ref(), project_config)
+ }
+
+ /// Returns the select config for a specific project.
+ ///
+ /// Merges project-specific settings with global settings, where project
+ /// settings take precedence for fields that are set.
+ pub fn select(&self, project: Option<&str>) -> Option<SelectConfig> {
+ let project_config = project
+ .and_then(|p| self.projects.get(p))
+ .and_then(|c| c.overrides.select.as_ref());
+ merge_optional(self.overrides.select.as_ref(), project_config)
}
/// Load configuration from config file and environment variables.
@@ -639,7 +681,7 @@ impl UserConfig {
/// Validate configuration values.
fn validate(&self) -> Result<(), ConfigError> {
// Validate worktree path (only if explicitly set - default is always valid)
- if let Some(ref path) = self.worktree_path {
+ if let Some(ref path) = self.overrides.worktree_path {
if path.is_empty() {
return Err(ConfigError::Message("worktree-path cannot be empty".into()));
}
@@ -653,7 +695,7 @@ impl UserConfig {
// Validate per-project configs
for (project, project_config) in &self.projects {
// Validate worktree path
- if let Some(ref path) = project_config.worktree_path {
+ if let Some(ref path) = project_config.overrides.worktree_path {
if path.is_empty() {
return Err(ConfigError::Message(format!(
"projects.{project}.worktree-path cannot be empty"
@@ -681,7 +723,7 @@ impl UserConfig {
}
}
// New: [projects."...".commit.generation]
- if let Some(ref commit) = project_config.commit
+ if let Some(ref commit) = project_config.overrides.commit
&& let Some(ref cg) = commit.generation
{
if cg.template.is_some() && cg.template_file.is_some() {
@@ -1070,7 +1112,7 @@ impl UserConfig {
}
// worktree-path (only if set)
- if let Some(ref path) = project_config.worktree_path {
+ if let Some(ref path) = project_config.overrides.worktree_path {
projects[project_id]["worktree-path"] = toml_edit::value(path);
} else if let Some(table) = projects[project_id].as_table_mut() {
table.remove("worktree-path");
@@ -1092,19 +1134,25 @@ impl UserConfig {
projects,
project_id,
"list",
- project_config.list.as_ref(),
+ project_config.overrides.list.as_ref(),
);
Self::serialize_project_config_section(
projects,
project_id,
"commit",
- project_config.commit.as_ref(),
+ project_config.overrides.commit.as_ref(),
);
Self::serialize_project_config_section(
projects,
project_id,
"merge",
- project_config.merge.as_ref(),
+ project_config.overrides.merge.as_ref(),
+ );
+ Self::serialize_project_config_section(
+ projects,
+ project_id,
+ "select",
+ project_config.overrides.select.as_ref(),
);
}
}
@@ -1335,14 +1383,17 @@ rename-tab = "echo 'switched'"
#[test]
fn test_user_project_config_default() {
let config = UserProjectOverrides::default();
- assert!(config.worktree_path.is_none());
+ assert!(config.overrides.worktree_path.is_none());
assert!(config.approved_commands.is_empty());
}
#[test]
fn test_user_project_config_with_worktree_path_serde() {
let config = UserProjectOverrides {
- worktree_path: Some(".worktrees/{{ branch | sanitize }}".to_string()),
+ overrides: OverridableConfig {
+ worktree_path: Some(".worktrees/{{ branch | sanitize }}".to_string()),
+ ..Default::default()
+ },
approved_commands: vec!["npm install".to_string()],
..Default::default()
};
@@ -1352,7 +1403,7 @@ rename-tab = "echo 'switched'"
let parsed: UserProjectOverrides = toml::from_str(&toml).unwrap();
assert_eq!(
- parsed.worktree_path,
+ parsed.overrides.worktree_path,
Some(".worktrees/{{ branch | sanitize }}".to_string())
);
assert_eq!(parsed.approved_commands, vec!["npm install".to_string()]);
@@ -1364,7 +1415,10 @@ rename-tab = "echo 'switched'"
config.projects.insert(
"github.com/user/repo".to_string(),
UserProjectOverrides {
- worktree_path: Some(".worktrees/{{ branch | sanitize }}".to_string()),
+ overrides: OverridableConfig {
+ worktree_path: Some(".worktrees/{{ branch | sanitize }}".to_string()),
+ ..Default::default()
+ },
approved_commands: vec![],
..Default::default()
},
@@ -1380,13 +1434,19 @@ rename-tab = "echo 'switched'"
#[test]
fn test_worktree_path_for_project_falls_back_to_global() {
let mut config = UserConfig {
- worktree_path: Some("../{{ repo }}-{{ branch | sanitize }}".to_string()),
+ overrides: OverridableConfig {
+ worktree_path: Some("../{{ repo }}-{{ branch | sanitize }}".to_string()),
+ ..Default::default()
+ },
..Default::default()
};
config.projects.insert(
"github.com/user/repo".to_string(),
UserProjectOverrides {
- worktree_path: None, // No project-specific path
+ overrides: OverridableConfig {
+ worktree_path: None, // No project-specific path
+ ..Default::default()
+ },
approved_commands: vec!["npm install".to_string()],
..Default::default()
},
@@ -1414,13 +1474,19 @@ rename-tab = "echo 'switched'"
fn test_format_path_with_project_override() {
let test = test_repo();
let mut config = UserConfig {
- worktree_path: Some("../{{ repo }}.{{ branch | sanitize }}".to_string()),
+ overrides: OverridableConfig {
+ worktree_path: Some("../{{ repo }}.{{ branch | sanitize }}".to_string()),
+ ..Default::default()
+ },
..Default::default()
};
config.projects.insert(
"github.com/user/repo".to_string(),
UserProjectOverrides {
- worktree_path: Some(".worktrees/{{ branch | sanitize }}".to_string()),
+ overrides: OverridableConfig {
+ worktree_path: Some(".worktrees/{{ branch | sanitize }}".to_string()),
+ ..Default::default()
+ },
approved_commands: vec![],
..Default::default()
},
@@ -1470,15 +1536,15 @@ rename-tab = "echo 'switched'"
fn test_worktrunk_config_default() {
let config = UserConfig::default();
// worktree_path is None by default, but the getter returns the default
- assert!(config.worktree_path.is_none());
+ assert!(config.overrides.worktree_path.is_none());
assert_eq!(
config.worktree_path(),
"../{{ repo }}.{{ branch | sanitize }}"
);
assert!(config.projects.is_empty());
- assert!(config.list.is_none());
- assert!(config.commit.is_none());
- assert!(config.merge.is_none());
+ assert!(config.overrides.list.is_none());
+ assert!(config.overrides.commit.is_none());
+ assert!(config.overrides.merge.is_none());
assert!(config.commit_generation.is_none());
assert!(!config.skip_shell_integration_prompt);
}
@@ -1593,7 +1659,10 @@ rename-tab = "echo 'switched'"
fn test_worktrunk_config_format_path_custom_template() {
let test = test_repo();
let config = UserConfig {
- worktree_path: Some(".worktrees/{{ branch }}".to_string()),
+ overrides: OverridableConfig {
+ worktree_path: Some(".worktrees/{{ branch }}".to_string()),
+ ..Default::default()
+ },
..Default::default()
};
let path = config
@@ -1911,13 +1980,16 @@ worktree-path = "../{{ main_worktree }}.{{ branch }}"
#[test]
fn test_effective_commit_generation_no_project() {
let config = UserConfig {
- commit: Some(CommitConfig {
- stage: None,
- generation: Some(CommitGenerationConfig {
- command: Some("global-llm".to_string()),
- ..Default::default()
+ overrides: OverridableConfig {
+ commit: Some(CommitConfig {
+ stage: None,
+ generation: Some(CommitGenerationConfig {
+ command: Some("global-llm".to_string()),
+ ..Default::default()
+ }),
}),
- }),
+ ..Default::default()
+ },
..Default::default()
};
@@ -1928,26 +2000,32 @@ worktree-path = "../{{ main_worktree }}.{{ branch }}"
#[test]
fn test_effective_commit_generation_with_project_override() {
let mut config = UserConfig {
- commit: Some(CommitConfig {
- stage: None,
- generation: Some(CommitGenerationConfig {
- command: Some("global-llm".to_string()),
- ..Default::default()
+ overrides: OverridableConfig {
+ commit: Some(CommitConfig {
+ stage: None,
+ generation: Some(CommitGenerationConfig {
+ command: Some("global-llm".to_string()),
+ ..Default::default()
+ }),
}),
- }),
+ ..Default::default()
+ },
..Default::default()
};
config.projects.insert(
"github.com/user/repo".to_string(),
UserProjectOverrides {
- commit: Some(CommitConfig {
- stage: None,
- generation: Some(CommitGenerationConfig {
- command: Some("project-llm".to_string()),
- ..Default::default()
+ overrides: OverridableConfig {
+ commit: Some(CommitConfig {
+ stage: None,
+ generation: Some(CommitGenerationConfig {
+ command: Some("project-llm".to_string()),
+ ..Default::default()
+ }),
}),
- }),
+ ..Default::default()
+ },
..Default::default()
},
);
@@ -1967,26 +2045,32 @@ worktree-path = "../{{ main_worktree }}.{{ branch }}"
#[test]
fn test_effective_merge_with_partial_override() {
let mut config = UserConfig {
- merge: Some(MergeConfig {
- squash: Some(true),
- commit: Some(true),
- rebase: Some(true),
- remove: Some(true),
- verify: Some(true),
- }),
+ overrides: OverridableConfig {
+ merge: Some(MergeConfig {
+ squash: Some(true),
+ commit: Some(true),
+ rebase: Some(true),
+ remove: Some(true),
+ verify: Some(true),
+ }),
+ ..Default::default()
+ },
..Default::default()
};
config.projects.insert(
"github.com/user/repo".to_string(),
UserProjectOverrides {
- merge: Some(MergeConfig {
- squash: Some(false), // Only override squash
- commit: None,
- rebase: None,
- remove: None,
- verify: None,
- }),
+ overrides: OverridableConfig {
+ merge: Some(MergeConfig {
+ squash: Some(false), // Only override squash
+ commit: None,
+ rebase: None,
+ remove: None,
+ verify: None,
+ }),
+ ..Default::default()
+ },
..Default::default()
},
);
@@ -2001,17 +2085,20 @@ worktree-path = "../{{ main_worktree }}.{{ branch }}"
fn test_effective_list_project_only() {
// No global list config, only project config
let mut config = UserConfig::default();
- assert!(config.list.is_none());
+ assert!(config.overrides.list.is_none());
config.projects.insert(
"github.com/user/repo".to_string(),
UserProjectOverrides {
- list: Some(ListConfig {
- full: Some(true),
- branches: None,
- remotes: None,
- timeout_ms: None,
- }),
+ overrides: OverridableConfig {
+ list: Some(ListConfig {
+ full: Some(true),
+ branches: None,
+ remotes: None,
+ timeout_ms: None,
+ }),
+ ..Default::default()
+ },
..Default::default()
},
);
@@ -2024,14 +2111,56 @@ worktree-path = "../{{ main_worktree }}.{{ branch }}"
assert!(config.list(Some("github.com/other/repo")).is_none());
}
+ #[test]
+ fn test_effective_select_with_project_override() {
+ // Test that OverridableConfig merge works correctly for select
+ let mut config = UserConfig {
+ overrides: OverridableConfig {
+ select: Some(SelectConfig {
+ pager: Some("delta".to_string()),
+ }),
+ ..Default::default()
+ },
+ ..Default::default()
+ };
+
+ config.projects.insert(
+ "github.com/user/repo".to_string(),
+ UserProjectOverrides {
+ overrides: OverridableConfig {
+ select: Some(SelectConfig {
+ pager: Some("bat".to_string()),
+ }),
+ ..Default::default()
+ },
+ ..Default::default()
+ },
+ );
+
+ // Project override takes precedence
+ let effective = config.select(Some("github.com/user/repo")).unwrap();
+ assert_eq!(effective.pager, Some("bat".to_string()));
+
+ // No project override = use global
+ let effective = config.select(Some("github.com/other/repo")).unwrap();
+ assert_eq!(effective.pager, Some("delta".to_string()));
+
+ // No project = use global
+ let effective = config.select(None).unwrap();
+ assert_eq!(effective.pager, Some("delta".to_string()));
+ }
+
#[test]
fn test_effective_commit_global_only() {
// Only global config, no project config
let config = UserConfig {
- commit: Some(CommitConfig {
- stage: Some(StageMode::Tracked),
- generation: None,
- }),
+ overrides: OverridableConfig {
+ commit: Some(CommitConfig {
+ stage: Some(StageMode::Tracked),
+ generation: None,
+ }),
+ ..Default::default()
+ },
..Default::default()
};
@@ -2046,35 +2175,39 @@ worktree-path = "../{{ main_worktree }}.{{ branch }}"
#[test]
fn test_user_project_config_with_nested_configs_serde() {
let config = UserProjectOverrides {
- worktree_path: Some(".worktrees/{{ branch }}".to_string()),
approved_commands: vec!["npm install".to_string()],
commit_generation: None, // Deprecated field, use commit.generation instead
- list: Some(ListConfig {
- full: Some(true),
- ..Default::default()
- }),
- commit: Some(CommitConfig {
- stage: Some(StageMode::Tracked),
- generation: Some(CommitGenerationConfig {
- command: Some("llm -m gpt-4".to_string()),
+ overrides: OverridableConfig {
+ worktree_path: Some(".worktrees/{{ branch }}".to_string()),
+ list: Some(ListConfig {
+ full: Some(true),
+ ..Default::default()
+ }),
+ commit: Some(CommitConfig {
+ stage: Some(StageMode::Tracked),
+ generation: Some(CommitGenerationConfig {
+ command: Some("llm -m gpt-4".to_string()),
+ ..Default::default()
+ }),
+ }),
+ merge: Some(MergeConfig {
+ squash: Some(false),
..Default::default()
}),
- }),
- merge: Some(MergeConfig {
- squash: Some(false),
..Default::default()
- }),
+ },
};
let toml = toml::to_string(&config).unwrap();
let parsed: UserProjectOverrides = toml::from_str(&toml).unwrap();
assert_eq!(
- parsed.worktree_path,
+ parsed.overrides.worktree_path,
Some(".worktrees/{{ branch }}".to_string())
);
assert_eq!(
parsed
+ .overrides
.commit
.as_ref()
.unwrap()
@@ -2084,12 +2217,12 @@ worktree-path = "../{{ main_worktree }}.{{ branch }}"
.command,
Some("llm -m gpt-4".to_string())
);
- assert_eq!(parsed.list.as_ref().unwrap().full, Some(true));
+ assert_eq!(parsed.overrides.list.as_ref().unwrap().full, Some(true));
assert_eq!(
- parsed.commit.as_ref().unwrap().stage,
+ parsed.overrides.commit.as_ref().unwrap().stage,
Some(StageMode::Tracked)
);
- assert_eq!(parsed.merge.as_ref().unwrap().squash, Some(false));
+ assert_eq!(parsed.overrides.merge.as_ref().unwrap().squash, Some(false));
}
#[test]
@@ -2119,11 +2252,12 @@ squash = false
// Global config
assert_eq!(
- config.worktree_path,
+ config.overrides.worktree_path,
Some("../{{ repo }}.{{ branch | sanitize }}".to_string())
);
assert_eq!(
config
+ .overrides
.commit
.as_ref()
.unwrap()
@@ -2137,11 +2271,12 @@ squash = false
// Project config
let project = config.projects.get("github.com/user/repo").unwrap();
assert_eq!(
- project.worktree_path,
+ project.overrides.worktree_path,
Some(".worktrees/{{ branch | sanitize }}".to_string())
);
assert_eq!(
project
+ .overrides
.commit
.as_ref()
.unwrap()
@@ -2151,8 +2286,11 @@ squash = false
.command,
Some("claude -p --model opus".to_string())
);
- assert_eq!(project.list.as_ref().unwrap().full, Some(true));
- assert_eq!(project.merge.as_ref().unwrap().squash, Some(false));
+ assert_eq!(project.overrides.list.as_ref().unwrap().full, Some(true));
+ assert_eq!(
+ project.overrides.merge.as_ref().unwrap().squash,
+ Some(false)
+ );
// Effective config for project
let effective_cg = config.commit_generation(Some("github.com/user/repo"));
@@ -2399,13 +2537,16 @@ squash-template-file = "path/to/file"
let config_path = dir.path().join("config.toml");
let config = UserConfig {
- commit: Some(CommitConfig {
- stage: None,
- generation: Some(CommitGenerationConfig {
- command: Some("llm -m haiku".to_string()),
- ..Default::default()
+ overrides: OverridableConfig {
+ commit: Some(CommitConfig {
+ stage: None,
+ generation: Some(CommitGenerationConfig {
+ command: Some("llm -m haiku".to_string()),
+ ..Default::default()
+ }),
}),
- }),
+ ..Default::default()
+ },
..Default::default()
};
@@ -2477,7 +2618,10 @@ squash-template-file = "path/to/file"
let config_path = dir.path().join("config.toml");
let config = UserConfig {
- worktree_path: Some("../{{ repo }}.{{ branch }}".to_string()),
+ overrides: OverridableConfig {
+ worktree_path: Some("../{{ repo }}.{{ branch }}".to_string()),
+ ..Default::default()
+ },
..Default::default()
};
diff --git a/tests/integration_tests/select_config.rs b/tests/integration_tests/select_config.rs
index bbeae0827..0edd2de5f 100644
--- a/tests/integration_tests/select_config.rs
+++ b/tests/integration_tests/select_config.rs
@@ -10,8 +10,8 @@ pager = "test-pager --custom-flag"
let config: UserConfig = toml::from_str(config_content).unwrap();
- assert!(config.select.is_some());
- let select = config.select.unwrap();
+ assert!(config.overrides.select.is_some());
+ let select = config.overrides.select.unwrap();
assert_eq!(select.pager, Some("test-pager --custom-flag".to_string()));
}
@@ -25,8 +25,8 @@ pager = ""
let config: UserConfig = toml::from_str(config_content).unwrap();
- assert!(config.select.is_some());
- let select = config.select.unwrap();
+ assert!(config.overrides.select.is_some());
+ let select = config.overrides.select.unwrap();
assert_eq!(select.pager, Some("".to_string()));
}
@@ -39,5 +39,5 @@ full = true
"#;
let config: UserConfig = toml::from_str(config_content).unwrap();
- assert!(config.select.is_none());
+ assert!(config.overrides.select.is_none());
}
diff --git a/tests/integration_tests/shell_integration_prompt.rs b/tests/integration_tests/shell_integration_prompt.rs
index 3a6ecc95f..c4cc75258 100644
--- a/tests/integration_tests/shell_integration_prompt.rs
+++ b/tests/integration_tests/shell_integration_prompt.rs
@@ -64,8 +64,10 @@ fn test_switch_with_active_shell_integration_no_prompt(repo: TestRepo) {
fn test_switch_with_skip_prompt_flag(repo: TestRepo) {
// Set the skip flag in config
let config_path = repo.test_config_path();
- let mut config = UserConfig::default();
- config.skip_shell_integration_prompt = true;
+ let config = UserConfig {
+ skip_shell_integration_prompt: true,
+ ..Default::default()
+ };
config.save_to(config_path).unwrap();
let output = repo