Worktrunk is a CLI for Git worktree management, designed for parallel AI agent workflows
feat: Add user config defaults and override flags for merge
Maximilian Roos8a881a263f53
diff --git a/dev/config.example.toml b/dev/config.example.tomlindex 540cce9a9..18e38b0b5 100644--- a/dev/config.example.toml+++ b/dev/config.example.toml@@ -154,6 +154,16 @@ worktree-path = "../{{ main_worktree }}.{{ branch }}"# [list]# full = false # Show CI, conflicts, diffs by default# branches = false # Include branches without worktrees by default+# remotes = false # Include remote branches by default++# Merge Command Defaults+# Configure default behavior for `wt merge`+# [merge]+# squash = true # Squash commits when merging+# commit = true # Create commit after merge+# remove = true # Remove worktree after merge+# verify = true # Run project hooks+# tracked-only = false # Stage tracked files only# Approved Commands# Commands approved for automatic execution after switching worktreesdiff --git a/src/cli.rs b/src/cli.rsindex f64b71725..a73166a61 100644--- a/src/cli.rs+++ b/src/cli.rs@@ -739,28 +739,48 @@ wt merge --no-verify#[arg(add = crate::completion::branch_value_completer())]target: Option<String>,- /// Skip commit squashing- #[arg(long = "no-squash", action = clap::ArgAction::SetFalse, default_value_t = true)]+ /// Force commit squashing+ #[arg(long, overrides_with = "no_squash", hide = true)]squash: bool,- /// Skip commit, squash, and rebase- #[arg(long = "no-commit", action = clap::ArgAction::SetFalse, default_value_t = true)]+ /// Skip commit squashing+ #[arg(long = "no-squash", overrides_with = "squash")]+ no_squash: bool,++ /// Force commit, squash, and rebase+ #[arg(long, overrides_with = "no_commit", hide = true)]commit: bool,- /// Keep worktree after merge- #[arg(long = "no-remove", action = clap::ArgAction::SetFalse, default_value_t = true)]+ /// Skip commit, squash, and rebase+ #[arg(long = "no-commit", overrides_with = "commit")]+ no_commit: bool,++ /// Force worktree removal after merge+ #[arg(long, overrides_with = "no_remove", hide = true)]remove: bool,- /// Skip all project hooks- #[arg(long = "no-verify", action = clap::ArgAction::SetFalse, default_value_t = true)]+ /// Keep worktree after merge+ #[arg(long = "no-remove", overrides_with = "remove")]+ no_remove: bool,++ /// Force running project hooks+ #[arg(long, overrides_with = "no_verify", hide = true)]verify: bool,+ /// Skip all project hooks+ #[arg(long = "no-verify", overrides_with = "verify")]+ no_verify: bool,+/// Skip approval prompts#[arg(short, long)]force: bool,/// Stage tracked files only- #[arg(long)]+ #[arg(long, overrides_with = "no_tracked_only")]tracked_only: bool,++ /// Stage all files (tracked and untracked)+ #[arg(long = "no-tracked-only", overrides_with = "tracked_only", hide = true)]+ no_tracked_only: bool,},}diff --git a/src/commands/merge.rs b/src/commands/merge.rsindex 119f0f475..e50499f52 100644--- a/src/commands/merge.rs+++ b/src/commands/merge.rs@@ -61,10 +61,10 @@ impl<'a> MergeCommandCollector<'a> {pub fn handle_merge(target: Option<&str>,- no_squash: bool,- no_commit: bool,- no_remove: bool,- no_verify: bool,+ squash: bool,+ commit: bool,+ remove: bool,+ verify: bool,force: bool,tracked_only: bool,) -> anyhow::Result<()> {@@ -74,19 +74,19 @@ pub fn handle_merge(let current_branch = env.branch.clone();// Validate --no-commit: requires clean working tree- if no_commit && repo.is_dirty()? {+ if !commit && repo.is_dirty()? {return Err(anyhow::anyhow!("{}", worktrunk::git::uncommitted_changes()));}// Validate --no-commit flag compatibility- if no_commit && !no_remove {+ if !commit && remove {return Err(anyhow::anyhow!(format!("{ERROR_EMOJI} {ERROR}--no-commit requires --no-remove{ERROR:#}\n\n{HINT_EMOJI} {HINT}Cannot remove active worktree when skipping commit/rebase{HINT:#}")));}// --no-commit implies --no-squash (validation above ensures --no-remove is already set)- let squash_enabled = !no_squash && !no_commit;+ let squash_enabled = squash && commit;// Get target branch (default to default branch if not provided)let target_branch = repo.resolve_target_branch(target)?;@@ -94,15 +94,15 @@ pub fn handle_merge(// Worktree for target is optional: if present we use it for safety checks and as destination.let target_worktree_path = repo.worktree_for_branch(&target_branch)?;- // When current == target or we're in the main worktree, force --no-remove (can't remove it)+ // When current == target or we're in the main worktree, disable remove (can't remove it)let in_main = !repo.is_in_worktree().unwrap_or(false);- let no_remove_effective = no_remove || current_branch == target_branch || in_main;+ let remove_effective = remove && current_branch != target_branch && !in_main;// Collect and approve all commands upfront for batch permission requestlet (all_commands, project_id) = MergeCommandCollector {repo,- no_commit,- no_verify,+ no_commit: !commit,+ no_verify: !verify,}.collect()?;@@ -116,14 +116,14 @@ pub fn handle_merge(}// Handle uncommitted changes (skip if --no-commit) - track whether commit occurred- let committed = if !no_commit && repo.is_dirty()? {+ let committed = if commit && repo.is_dirty()? {if squash_enabled {false // Squash path handles staging and committing} else {let ctx = env.context(force);let mut options = CommitOptions::new(&ctx);options.target_branch = Some(&target_branch);- options.no_verify = no_verify;+ options.no_verify = !verify;options.tracked_only = tracked_only;options.auto_trust = true;options.warn_about_untracked = !tracked_only;@@ -141,7 +141,7 @@ pub fn handle_merge(super::standalone::handle_squash(Some(&target_branch),force,- no_verify,+ !verify,true,tracked_only,!tracked_only,@@ -151,7 +151,7 @@ pub fn handle_merge(};// Rebase onto target (skip if --no-commit) - track whether rebasing occurred- let rebased = if !no_commit {+ let rebased = if commit {super::standalone::handle_rebase(Some(&target_branch))?} else {false@@ -159,7 +159,7 @@ pub fn handle_merge(// Run pre-merge checks unless --no-verify was specified// Do this after commit/squash/rebase to validate the final state that will be pushed- if !no_verify && let Some(project_config) = repo.load_project_config()? {+ if verify && let Some(project_config) = repo.load_project_config()? {let ctx = env.context(force);run_pre_merge_commands(&project_config, &ctx, &target_branch, true)?;}@@ -182,7 +182,7 @@ pub fn handle_merge(.unwrap_or_else(|| worktrees.main().path.clone());// Finish worktree unless --no-remove was specified- if !no_remove_effective {+ if remove_effective {// STEP 1: Check for uncommitted changes before attempting cleanup// This prevents showing "Cleaning up worktree..." before failingrepo.ensure_clean_working_tree()?;@@ -204,7 +204,7 @@ pub fn handle_merge(handle_merge_summary_output(None)?;}- if !no_verify {+ if verify {// Execute post-merge commands in the destination worktree// This runs after cleanup so the context is clear to the user// Create a fresh Repository instance at the destination (the old repo may be invalid)diff --git a/src/config/mod.rs b/src/config/mod.rsindex 96a1cb2a1..55d40ea7b 100644--- a/src/config/mod.rs+++ b/src/config/mod.rs@@ -79,6 +79,7 @@ mod tests {commit_generation: CommitGenerationConfig::default(),projects: std::collections::BTreeMap::new(),list: None,+ merge: None,};assert_eq!(config.format_path("myproject", "feature-x").unwrap(),@@ -93,6 +94,7 @@ mod tests {commit_generation: CommitGenerationConfig::default(),projects: std::collections::BTreeMap::new(),list: None,+ merge: None,};assert_eq!(config.format_path("myproject", "feature-x").unwrap(),@@ -107,6 +109,7 @@ mod tests {commit_generation: CommitGenerationConfig::default(),projects: std::collections::BTreeMap::new(),list: None,+ merge: None,};assert_eq!(config.format_path("myproject", "feature-x").unwrap(),@@ -122,6 +125,7 @@ mod tests {commit_generation: CommitGenerationConfig::default(),projects: std::collections::BTreeMap::new(),list: None,+ merge: None,};assert_eq!(config.format_path("myproject", "feature/foo").unwrap(),@@ -136,6 +140,7 @@ mod tests {commit_generation: CommitGenerationConfig::default(),projects: std::collections::BTreeMap::new(),list: None,+ merge: None,};assert_eq!(config.format_path("myproject", "feature/sub/task").unwrap(),@@ -151,6 +156,7 @@ mod tests {commit_generation: CommitGenerationConfig::default(),projects: std::collections::BTreeMap::new(),list: None,+ merge: None,};assert_eq!(config.format_path("myproject", "feature\\foo").unwrap(),diff --git a/src/config/user.rs b/src/config/user.rsindex 7a3e50d04..7216791df 100644--- a/src/config/user.rs+++ b/src/config/user.rs@@ -75,6 +75,10 @@ pub struct WorktrunkConfig {/// Configuration for the `wt list` command#[serde(default, skip_serializing_if = "Option::is_none")]pub list: Option<ListConfig>,++ /// Configuration for the `wt merge` command+ #[serde(default, skip_serializing_if = "Option::is_none")]+ pub merge: Option<MergeConfig>,}/// Configuration for commit message generation@@ -162,6 +166,30 @@ pub struct ListConfig {pub remotes: Option<bool>,}+/// Configuration for the `wt merge` command+#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]+pub struct MergeConfig {+ /// Squash commits when merging (default: true)+ #[serde(skip_serializing_if = "Option::is_none")]+ pub squash: Option<bool>,++ /// Create commit after merge (default: true)+ #[serde(skip_serializing_if = "Option::is_none")]+ pub commit: Option<bool>,++ /// Remove worktree after merge (default: true)+ #[serde(skip_serializing_if = "Option::is_none")]+ pub remove: Option<bool>,++ /// Run project hooks (default: true)+ #[serde(skip_serializing_if = "Option::is_none")]+ pub verify: Option<bool>,++ /// Stage tracked files only (default: false)+ #[serde(rename = "tracked-only", skip_serializing_if = "Option::is_none")]+ pub tracked_only: Option<bool>,+}+impl Default for WorktrunkConfig {fn default() -> Self {Self {@@ -169,6 +197,7 @@ impl Default for WorktrunkConfig {commit_generation: CommitGenerationConfig::default(),projects: std::collections::BTreeMap::new(),list: None,+ merge: None,}}}diff --git a/src/main.rs b/src/main.rsindex dee023a3f..b00035453 100644--- a/src/main.rs+++ b/src/main.rs@@ -440,20 +440,55 @@ fn main() {Commands::Merge {target,squash,+ no_squash,commit,+ no_commit,remove,+ no_remove,verify,+ no_verify,force,tracked_only,- } => handle_merge(- target.as_deref(),- !squash,- !commit,- !remove,- !verify,- force,- tracked_only,- ),+ no_tracked_only,+ } => WorktrunkConfig::load()+ .context("Failed to load config")+ .and_then(|config| {+ // Convert paired flags to Option<bool>+ fn flag_pair(positive: bool, negative: bool) -> Option<bool> {+ match (positive, negative) {+ (true, _) => Some(true),+ (_, true) => Some(false),+ _ => None,+ }+ }++ // Get config defaults (positive form: true = do it)+ let merge_config = config.merge.as_ref();+ let squash_default = merge_config.and_then(|m| m.squash).unwrap_or(true);+ let commit_default = merge_config.and_then(|m| m.commit).unwrap_or(true);+ let remove_default = merge_config.and_then(|m| m.remove).unwrap_or(true);+ let verify_default = merge_config.and_then(|m| m.verify).unwrap_or(true);+ let tracked_only_default =+ merge_config.and_then(|m| m.tracked_only).unwrap_or(false);++ // CLI flags override config, config overrides defaults+ let squash_final = flag_pair(squash, no_squash).unwrap_or(squash_default);+ let commit_final = flag_pair(commit, no_commit).unwrap_or(commit_default);+ let remove_final = flag_pair(remove, no_remove).unwrap_or(remove_default);+ let verify_final = flag_pair(verify, no_verify).unwrap_or(verify_default);+ let tracked_only_final =+ flag_pair(tracked_only, no_tracked_only).unwrap_or(tracked_only_default);++ handle_merge(+ target.as_deref(),+ squash_final,+ commit_final,+ remove_final,+ verify_final,+ force,+ tracked_only_final,+ )+ }),};if let Err(e) = result {diff --git a/tests/snapshots/integration__integration_tests__init__init_bash.snap b/tests/snapshots/integration__integration_tests__init__init_bash.snapindex 82eda0893..f39db5501 100644--- a/tests/snapshots/integration__integration_tests__init__init_bash.snap+++ b/tests/snapshots/integration__integration_tests__init__init_bash.snap@@ -12,13 +12,13 @@ info:COLUMNS: "150"GIT_AUTHOR_DATE: "2025-01-01T00:00:00Z"GIT_COMMITTER_DATE: "2025-01-01T00:00:00Z"- GIT_CONFIG_GLOBAL: /var/folders/wf/s6ycxvvs4ln8qsdbfx40hnc40000gn/T/.tmpX880pk/test-gitconfig+ GIT_CONFIG_GLOBAL: /var/folders/wf/s6ycxvvs4ln8qsdbfx40hnc40000gn/T/.tmpb2n8Kq/test-gitconfigGIT_CONFIG_SYSTEM: /dev/nullGIT_EDITOR: ""LANG: CLC_ALL: CSOURCE_DATE_EPOCH: "1761609600"- WORKTRUNK_CONFIG_PATH: /var/folders/wf/s6ycxvvs4ln8qsdbfx40hnc40000gn/T/.tmpX880pk/test-config.toml+ WORKTRUNK_CONFIG_PATH: /var/folders/wf/s6ycxvvs4ln8qsdbfx40hnc40000gn/T/.tmpb2n8Kq/test-config.toml---success: trueexit_code: 0@@ -761,7 +761,7 @@ _wt() {return 0;;wt__merge)- opts="-f -C -v -h --no-squash --no-commit --no-remove --no-verify --force --tracked-only --config --verbose --help [TARGET]"+ opts="-f -C -v -h --squash --no-squash --commit --no-commit --remove --no-remove --verify --no-verify --force --tracked-only --no-tracked-only --config --verbose --help [TARGET]"if [[ ${cur} == -* || ${COMP_CWORD} -eq 2 ]] ; thenCOMPREPLY=( $(compgen -W "${opts}" -- "${cur}") )return 0diff --git a/tests/snapshots/integration__integration_tests__init__init_fish.snap b/tests/snapshots/integration__integration_tests__init__init_fish.snapindex 76aa55429..8bb73e396 100644--- a/tests/snapshots/integration__integration_tests__init__init_fish.snap+++ b/tests/snapshots/integration__integration_tests__init__init_fish.snap@@ -12,13 +12,13 @@ info:COLUMNS: "150"GIT_AUTHOR_DATE: "2025-01-01T00:00:00Z"GIT_COMMITTER_DATE: "2025-01-01T00:00:00Z"- GIT_CONFIG_GLOBAL: /var/folders/wf/s6ycxvvs4ln8qsdbfx40hnc40000gn/T/.tmpPPjZB2/test-gitconfig+ GIT_CONFIG_GLOBAL: /var/folders/wf/s6ycxvvs4ln8qsdbfx40hnc40000gn/T/.tmprnIggk/test-gitconfigGIT_CONFIG_SYSTEM: /dev/nullGIT_EDITOR: ""LANG: CLC_ALL: CSOURCE_DATE_EPOCH: "1761609600"- WORKTRUNK_CONFIG_PATH: /var/folders/wf/s6ycxvvs4ln8qsdbfx40hnc40000gn/T/.tmpPPjZB2/test-config.toml+ WORKTRUNK_CONFIG_PATH: /var/folders/wf/s6ycxvvs4ln8qsdbfx40hnc40000gn/T/.tmprnIggk/test-config.toml---success: trueexit_code: 0@@ -306,12 +306,17 @@ complete -c wt -n "__fish_wt_using_subcommand remove" -s v -l verbose -d 'Show ccomplete -c wt -n "__fish_wt_using_subcommand remove" -s h -l help -d 'Print help (see more with \'--help\')'complete -c wt -n "__fish_wt_using_subcommand merge" -s C -d 'Change working directory' -r -Fcomplete -c wt -n "__fish_wt_using_subcommand merge" -l config -d 'User config file path' -r -F+complete -c wt -n "__fish_wt_using_subcommand merge" -l squash -d 'Force commit squashing'complete -c wt -n "__fish_wt_using_subcommand merge" -l no-squash -d 'Skip commit squashing'+complete -c wt -n "__fish_wt_using_subcommand merge" -l commit -d 'Force commit, squash, and rebase'complete -c wt -n "__fish_wt_using_subcommand merge" -l no-commit -d 'Skip commit, squash, and rebase'+complete -c wt -n "__fish_wt_using_subcommand merge" -l remove -d 'Force worktree removal after merge'complete -c wt -n "__fish_wt_using_subcommand merge" -l no-remove -d 'Keep worktree after merge'+complete -c wt -n "__fish_wt_using_subcommand merge" -l verify -d 'Force running project hooks'complete -c wt -n "__fish_wt_using_subcommand merge" -l no-verify -d 'Skip all project hooks'complete -c wt -n "__fish_wt_using_subcommand merge" -s f -l force -d 'Skip approval prompts'complete -c wt -n "__fish_wt_using_subcommand merge" -l tracked-only -d 'Stage tracked files only'+complete -c wt -n "__fish_wt_using_subcommand merge" -l no-tracked-only -d 'Stage all files (tracked and untracked)'complete -c wt -n "__fish_wt_using_subcommand merge" -s v -l verbose -d 'Show commands and debug info'complete -c wt -n "__fish_wt_using_subcommand merge" -s h -l help -d 'Print help (see more with \'--help\')'diff --git a/tests/snapshots/integration__integration_tests__init__init_zsh.snap b/tests/snapshots/integration__integration_tests__init__init_zsh.snapindex db5d59602..d9be428e8 100644--- a/tests/snapshots/integration__integration_tests__init__init_zsh.snap+++ b/tests/snapshots/integration__integration_tests__init__init_zsh.snap@@ -12,13 +12,13 @@ info:COLUMNS: "150"GIT_AUTHOR_DATE: "2025-01-01T00:00:00Z"GIT_COMMITTER_DATE: "2025-01-01T00:00:00Z"- GIT_CONFIG_GLOBAL: /var/folders/wf/s6ycxvvs4ln8qsdbfx40hnc40000gn/T/.tmps6u1c7/test-gitconfig+ GIT_CONFIG_GLOBAL: /var/folders/wf/s6ycxvvs4ln8qsdbfx40hnc40000gn/T/.tmpyIHHFh/test-gitconfigGIT_CONFIG_SYSTEM: /dev/nullGIT_EDITOR: ""LANG: CLC_ALL: CSOURCE_DATE_EPOCH: "1761609600"- WORKTRUNK_CONFIG_PATH: /var/folders/wf/s6ycxvvs4ln8qsdbfx40hnc40000gn/T/.tmps6u1c7/test-config.toml+ WORKTRUNK_CONFIG_PATH: /var/folders/wf/s6ycxvvs4ln8qsdbfx40hnc40000gn/T/.tmpyIHHFh/test-config.toml---success: trueexit_code: 0@@ -543,13 +543,18 @@ _arguments "${_arguments_options[@]}" : \_arguments "${_arguments_options[@]}" : \'-C+[Change working directory]:path:_files' \'--config=[User config file path]:path:_files' \+'--squash[Force commit squashing]' \'--no-squash[Skip commit squashing]' \+'--commit[Force commit, squash, and rebase]' \'--no-commit[Skip commit, squash, and rebase]' \+'--remove[Force worktree removal after merge]' \'--no-remove[Keep worktree after merge]' \+'--verify[Force running project hooks]' \'--no-verify[Skip all project hooks]' \'-f[Skip approval prompts]' \'--force[Skip approval prompts]' \'--tracked-only[Stage tracked files only]' \+'--no-tracked-only[Stage all files (tracked and untracked)]' \'-v[Show commands and debug info]' \'--verbose[Show commands and debug info]' \'-h[Print help (see more with '\''--help'\'')]' \