Worktrunk is a CLI for Git worktree management, designed for parallel AI agent workflows
fix: protect default branch worktree from removal in bare repos (#1620)
worktrunk-boteabf4aac2793
diff --git a/docs/content/merge.md b/docs/content/merge.mdindex 64161da9a..9dcbb546d 100644--- a/docs/content/merge.md+++ b/docs/content/merge.md@@ -67,7 +67,7 @@ wt merge --no-commit3. **Pre-merge hooks** — Hooks run after rebase, before merge. Failures abort. See [`wt hook`](@/hook.md).4. **Merge** — Fast-forward merge to the target branch. With `--no-ff`, a merge commit is created instead (semi-linear history: rebased commits plus a merge commit). Non-fast-forward merges are rejected.5. **Pre-remove hooks** — Hooks run before removing worktree. Failures abort.-6. **Cleanup** — Removes the worktree and branch. Use `--no-remove` to keep the worktree. When already on the target branch or in the main worktree, the worktree is preserved.+6. **Cleanup** — Removes the worktree and branch. Use `--no-remove` to keep the worktree. When already on the target branch or in the primary worktree, the worktree is preserved.7. **Post-merge hooks** — Hooks run after cleanup. Failures are logged but don't abort.Use `--no-commit` to skip committing uncommitted changes and squashing; rebase still runs by default and can rewrite commits unless `--no-rebase` is passed. Useful after preparing commits manually with `wt step`. Requires a clean working tree.diff --git a/skills/worktrunk/reference/merge.md b/skills/worktrunk/reference/merge.mdindex fd2540b2c..0a54924b7 100644--- a/skills/worktrunk/reference/merge.md+++ b/skills/worktrunk/reference/merge.md@@ -51,7 +51,7 @@ wt merge --no-commit3. **Pre-merge hooks** — Hooks run after rebase, before merge. Failures abort. See [`wt hook`](https://worktrunk.dev/hook/).4. **Merge** — Fast-forward merge to the target branch. With `--no-ff`, a merge commit is created instead (semi-linear history: rebased commits plus a merge commit). Non-fast-forward merges are rejected.5. **Pre-remove hooks** — Hooks run before removing worktree. Failures abort.-6. **Cleanup** — Removes the worktree and branch. Use `--no-remove` to keep the worktree. When already on the target branch or in the main worktree, the worktree is preserved.+6. **Cleanup** — Removes the worktree and branch. Use `--no-remove` to keep the worktree. When already on the target branch or in the primary worktree, the worktree is preserved.7. **Post-merge hooks** — Hooks run after cleanup. Failures are logged but don't abort.Use `--no-commit` to skip committing uncommitted changes and squashing; rebase still runs by default and can rewrite commits unless `--no-rebase` is passed. Useful after preparing commits manually with `wt step`. Requires a clean working tree.diff --git a/src/cli/mod.rs b/src/cli/mod.rsindex dc30f7c4b..4530b6b55 100644--- a/src/cli/mod.rs+++ b/src/cli/mod.rs@@ -910,7 +910,7 @@ wt merge --no-commit3. **Pre-merge hooks** — Hooks run after rebase, before merge. Failures abort. See [`wt hook`](@/hook.md).4. **Merge** — Fast-forward merge to the target branch. With `--no-ff`, a merge commit is created instead (semi-linear history: rebased commits plus a merge commit). Non-fast-forward merges are rejected.5. **Pre-remove hooks** — Hooks run before removing worktree. Failures abort.-6. **Cleanup** — Removes the worktree and branch. Use `--no-remove` to keep the worktree. When already on the target branch or in the main worktree, the worktree is preserved.+6. **Cleanup** — Removes the worktree and branch. Use `--no-remove` to keep the worktree. When already on the target branch or in the primary worktree, the worktree is preserved.7. **Post-merge hooks** — Hooks run after cleanup. Failures are logged but don't abort.Use `--no-commit` to skip committing uncommitted changes and squashing; rebase still runs by default and can rewrite commits unless `--no-rebase` is passed. Useful after preparing commits manually with `wt step`. Requires a clean working tree.diff --git a/src/commands/merge.rs b/src/commands/merge.rsindex da74c3f95..531ea602a 100644--- a/src/commands/merge.rs+++ b/src/commands/merge.rs@@ -138,10 +138,17 @@ pub fn handle_merge(opts: MergeOptions<'_>) -> anyhow::Result<()> {// 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, disable remove (can't remove it)- let in_main = !current_wt.is_linked().unwrap_or(false);+ // When current == target or we're in the primary worktree, disable remove (can't remove it).+ // In normal repos, the primary worktree is the non-linked main worktree.+ // In bare repos, all worktrees are linked — protect the default branch worktree.+ let in_primary = !current_wt.is_linked().unwrap_or(false)+ || (repo.is_bare()?+ && repo+ .default_branch()+ .as_deref()+ .is_some_and(|db| db == current_branch));let on_target = current_branch == target_branch;- let remove_effective = remove && !on_target && !in_main;+ let remove_effective = remove && !on_target && !in_primary;// Collect and approve all commands upfront for batch permission requestlet (all_commands, project_id) =@@ -279,9 +286,9 @@ pub fn handle_merge(opts: MergeOptions<'_>) -> anyhow::Result<()> {// Approval was handled at the gate (collect_merge_commands)crate::output::handle_remove_output(&remove_result, false, verify, false)?;} else {- // Worktree preserved - show reason (priority: main worktree > on target > --no-remove flag)- let message = if in_main {- "Worktree preserved (main worktree)"+ // Worktree preserved - show reason (priority: primary worktree > on target > --no-remove flag)+ let message = if in_primary {+ "Worktree preserved (primary worktree)"} else if on_target {"Worktree preserved (already on target branch)"} else {@@ -295,7 +302,7 @@ pub fn handle_merge(opts: MergeOptions<'_>) -> anyhow::Result<()> {// This runs after cleanup so the context is clear to the userlet ctx = CommandContext::new(repo, config, Some(¤t_branch), &destination_path, yes);// Show path when user's shell won't be in the destination directory where hooks run.- let display_path = if remove_effective && !in_main && !on_target {+ let display_path = if remove_effective && !in_primary && !on_target {// Worktree removed, user will cd to destinationcrate::output::post_hook_display_path(&destination_path)} else {diff --git a/tests/integration_tests/bare_repository.rs b/tests/integration_tests/bare_repository.rsindex e22ec11d0..581afc5bc 100644--- a/tests/integration_tests/bare_repository.rs+++ b/tests/integration_tests/bare_repository.rs@@ -940,3 +940,49 @@ fn test_clone_bare_repo_list_no_status_errors() {"Should not have git operation failures.\nstderr: {stderr}");}++/// Regression test for #1618: `wt merge` must not remove the default branch+/// worktree in a bare repo. In bare repos all worktrees are linked, so the+/// `is_linked()` check alone can't protect the primary worktree.+#[test]+fn test_bare_repo_merge_preserves_default_branch_worktree() {+ let test = BareRepoTest::new();++ // Create main (default branch) worktree and a feature worktree at the same commit+ let main_worktree = test.create_worktree("main", "main");+ test.commit_in(&main_worktree, "Initial commit on main");++ // Create feature branch at the same commit as main+ let _feature_worktree = test.create_worktree("feature", "feature");++ // Run `wt merge feature` from the main (default branch) worktree.+ // This attempts to merge main into feature — the important thing is that+ // the main worktree must NOT be removed even though is_linked() returns true.+ let (directive_path, _guard) = directive_file();+ let mut cmd = wt_command();+ test.configure_wt_cmd(&mut cmd);+ configure_directive_file(&mut cmd, &directive_path);+ cmd.args([+ "merge",+ "feature", // Target = feature branch+ "--no-squash", // Skip squash to avoid LLM dependency+ "--no-verify", // Skip hooks+ ])+ .current_dir(&main_worktree);++ let output = cmd.output().unwrap();+ let stderr = String::from_utf8_lossy(&output.stderr);++ // The merge itself may succeed or show "already up to date", but the key+ // assertion is that the main worktree is preserved (not removed).+ assert!(+ main_worktree.exists(),+ "Default branch worktree must not be removed.\nstderr: {stderr}"+ );++ // Should show "primary worktree" preservation message+ assert!(+ stderr.contains("primary worktree"),+ "Should show primary worktree preservation message.\nstderr: {stderr}"+ );+}diff --git a/tests/snapshots/integration__integration_tests__help__help_md_merge.snap b/tests/snapshots/integration__integration_tests__help__help_md_merge.snapindex 5ba3f0a4e..de3bc7330 100644--- a/tests/snapshots/integration__integration_tests__help__help_md_merge.snap+++ b/tests/snapshots/integration__integration_tests__help__help_md_merge.snap@@ -1,6 +1,5 @@---source: tests/integration_tests/help.rs-assertion_line: 131info:program: wtargs:@@ -9,6 +8,7 @@ info:env:CLICOLOR_FORCE: "1"COLUMNS: "500"+ GIT_EDITOR: ""LANG: CLC_ALL: CNO_COLOR: ""@@ -135,7 +135,7 @@ wt merge --no-commit3. **Pre-merge hooks** — Hooks run after rebase, before merge. Failures abort. See [`wt hook`](@/hook.md).4. **Merge** — Fast-forward merge to the target branch. With `--no-ff`, a merge commit is created instead (semi-linear history: rebased commits plus a merge commit). Non-fast-forward merges are rejected.5. **Pre-remove hooks** — Hooks run before removing worktree. Failures abort.-6. **Cleanup** — Removes the worktree and branch. Use `--no-remove` to keep the worktree. When already on the target branch or in the main worktree, the worktree is preserved.+6. **Cleanup** — Removes the worktree and branch. Use `--no-remove` to keep the worktree. When already on the target branch or in the primary worktree, the worktree is preserved.7. **Post-merge hooks** — Hooks run after cleanup. Failures are logged but don't abort.Use `--no-commit` to skip committing uncommitted changes and squashing; rebase still runs by default and can rewrite commits unless `--no-rebase` is passed. Useful after preparing commits manually with `wt step`. Requires a clean working tree.diff --git a/tests/snapshots/integration__integration_tests__help__help_merge_long.snap b/tests/snapshots/integration__integration_tests__help__help_merge_long.snapindex 8e696b9d7..05eebeb46 100644--- a/tests/snapshots/integration__integration_tests__help__help_merge_long.snap+++ b/tests/snapshots/integration__integration_tests__help__help_merge_long.snap@@ -1,6 +1,5 @@---source: tests/integration_tests/help.rs-assertion_line: 40info:program: wtargs:@@ -9,6 +8,7 @@ info:env:CLICOLOR_FORCE: "1"COLUMNS: "500"+ GIT_EDITOR: ""LANG: CLC_ALL: CNO_COLOR: ""@@ -124,7 +124,7 @@ Skip committing/squashing (rebase still runs unless --no-rebase):3. [1mPre-merge hooks[0m — Hooks run after rebase, before merge. Failures abort. See [2mwt hook[0m.4. [1mMerge[0m — Fast-forward merge to the target branch. With [2m--no-ff[0m, a merge commit is created instead (semi-linear history: rebased commits plus a merge commit). Non-fast-forward merges are rejected.5. [1mPre-remove hooks[0m — Hooks run before removing worktree. Failures abort.-6. [1mCleanup[0m — Removes the worktree and branch. Use [2m--no-remove[0m to keep the worktree. When already on the target branch or in the main worktree, the worktree is preserved.+6. [1mCleanup[0m — Removes the worktree and branch. Use [2m--no-remove[0m to keep the worktree. When already on the target branch or in the primary worktree, the worktree is preserved.7. [1mPost-merge hooks[0m — Hooks run after cleanup. Failures are logged but don't abort.Use [2m--no-commit[0m to skip committing uncommitted changes and squashing; rebase still runs by default and can rewrite commits unless [2m--no-rebase[0m is passed. Useful after preparing commits manually with [2mwt step[0m. Requires a clean working tree.diff --git a/tests/snapshots/integration__integration_tests__merge__merge_already_on_target.snap b/tests/snapshots/integration__integration_tests__merge__merge_already_on_target.snapindex 284abd206..7d9e4ee16 100644--- a/tests/snapshots/integration__integration_tests__merge__merge_already_on_target.snap+++ b/tests/snapshots/integration__integration_tests__merge__merge_already_on_target.snap@@ -18,16 +18,20 @@ info:LANG: CLC_ALL: CMOCK_CONFIG_DIR: "[MOCK_CONFIG_DIR]"+ NO_COLOR: ""PATH: "[PATH]"PSModulePath: ""RUST_LOG: warnSHELL: ""TERM: alacrittyUSERPROFILE: "[TEST_HOME]"+ WORKTRUNK_APPROVALS_PATH: "[TEST_APPROVALS]"WORKTRUNK_CONFIG_PATH: "[TEST_CONFIG]"+ WORKTRUNK_SYSTEM_CONFIG_PATH: "[TEST_SYSTEM_CONFIG]"WORKTRUNK_TEST_CLAUDE_INSTALLED: "0"WORKTRUNK_TEST_DELAYED_STREAM_MS: "-1"WORKTRUNK_TEST_EPOCH: "1735776000"+ WORKTRUNK_TEST_NUSHELL_ENV: "0"WORKTRUNK_TEST_POWERSHELL_ENV: "0"WORKTRUNK_TEST_SKIP_URL_HEALTH_CHECK: "1"XDG_CONFIG_HOME: "[TEST_CONFIG_HOME]"@@ -38,4 +42,4 @@ exit_code: 0----- stderr -----[2m○[22m Already up to date with [1mmain[22m (no new commits, no rebase needed)-[2m○[22m Worktree preserved (main worktree)+[2m○[22m Worktree preserved (primary worktree)diff --git a/tests/snapshots/integration__integration_tests__merge__merge_no_ff_target_without_worktree.snap b/tests/snapshots/integration__integration_tests__merge__merge_no_ff_target_without_worktree.snapindex 5a11ab208..d28f4b799 100644--- a/tests/snapshots/integration__integration_tests__merge__merge_no_ff_target_without_worktree.snap+++ b/tests/snapshots/integration__integration_tests__merge__merge_no_ff_target_without_worktree.snap@@ -1,6 +1,5 @@---source: tests/integration_tests/merge.rs-assertion_line: 2487info:program: wtargs:@@ -15,6 +14,7 @@ info:GIT_COMMITTER_DATE: "2025-01-01T00:00:00Z"GIT_CONFIG_GLOBAL: "[TEST_GIT_CONFIG]"GIT_CONFIG_SYSTEM: /dev/null+ GIT_EDITOR: ""GIT_TERMINAL_PROMPT: "0"HOME: "[TEST_HOME]"LANG: C@@ -48,4 +48,4 @@ exit_code: 0[107m [0m feature.txt | 1 [32m+[m[107m [0m 1 file changed, 1 insertion(+)[32m✓[39m [32mMerged to [1mmain[22m @ [2m[HASH][22m [90m(1 commit, 1 file, [32m+1[39m, --no-ff[39m[90m)[39m[39m-[2m○[22m Worktree preserved (main worktree)+[2m○[22m Worktree preserved (primary worktree)diff --git a/tests/snapshots/integration__integration_tests__merge__merge_post_merge_runs_from_main_branch.snap b/tests/snapshots/integration__integration_tests__merge__merge_post_merge_runs_from_main_branch.snapindex 7e2a47415..749f23c98 100644--- a/tests/snapshots/integration__integration_tests__merge__merge_post_merge_runs_from_main_branch.snap+++ b/tests/snapshots/integration__integration_tests__merge__merge_post_merge_runs_from_main_branch.snap@@ -19,16 +19,20 @@ info:LANG: CLC_ALL: CMOCK_CONFIG_DIR: "[MOCK_CONFIG_DIR]"+ NO_COLOR: ""PATH: "[PATH]"PSModulePath: ""RUST_LOG: warnSHELL: ""TERM: alacrittyUSERPROFILE: "[TEST_HOME]"+ WORKTRUNK_APPROVALS_PATH: "[TEST_APPROVALS]"WORKTRUNK_CONFIG_PATH: "[TEST_CONFIG]"+ WORKTRUNK_SYSTEM_CONFIG_PATH: "[TEST_SYSTEM_CONFIG]"WORKTRUNK_TEST_CLAUDE_INSTALLED: "0"WORKTRUNK_TEST_DELAYED_STREAM_MS: "-1"WORKTRUNK_TEST_EPOCH: "1735776000"+ WORKTRUNK_TEST_NUSHELL_ENV: "0"WORKTRUNK_TEST_POWERSHELL_ENV: "0"WORKTRUNK_TEST_SKIP_URL_HEALTH_CHECK: "1"XDG_CONFIG_HOME: "[TEST_CONFIG_HOME]"@@ -39,6 +43,6 @@ exit_code: 0----- stderr -----[2m○[22m Already up to date with [1mmain[22m (no new commits, no rebase needed)-[2m○[22m Worktree preserved (main worktree)+[2m○[22m Worktree preserved (primary worktree)[36m◎[39m [36mRunning post-merge project hook[39m[107m [0m [2m[0m[2m[34mecho[0m[2m [0m[2m[32m'post-merge ran from main'[0m[2m [0m[2m[36m>[0m[2m post-merge-ran.txtdiff --git a/tests/snapshots/integration__integration_tests__merge__merge_when_default_branch_missing_worktree.snap b/tests/snapshots/integration__integration_tests__merge__merge_when_default_branch_missing_worktree.snapindex 284abd206..7d9e4ee16 100644--- a/tests/snapshots/integration__integration_tests__merge__merge_when_default_branch_missing_worktree.snap+++ b/tests/snapshots/integration__integration_tests__merge__merge_when_default_branch_missing_worktree.snap@@ -18,16 +18,20 @@ info:LANG: CLC_ALL: CMOCK_CONFIG_DIR: "[MOCK_CONFIG_DIR]"+ NO_COLOR: ""PATH: "[PATH]"PSModulePath: ""RUST_LOG: warnSHELL: ""TERM: alacrittyUSERPROFILE: "[TEST_HOME]"+ WORKTRUNK_APPROVALS_PATH: "[TEST_APPROVALS]"WORKTRUNK_CONFIG_PATH: "[TEST_CONFIG]"+ WORKTRUNK_SYSTEM_CONFIG_PATH: "[TEST_SYSTEM_CONFIG]"WORKTRUNK_TEST_CLAUDE_INSTALLED: "0"WORKTRUNK_TEST_DELAYED_STREAM_MS: "-1"WORKTRUNK_TEST_EPOCH: "1735776000"+ WORKTRUNK_TEST_NUSHELL_ENV: "0"WORKTRUNK_TEST_POWERSHELL_ENV: "0"WORKTRUNK_TEST_SKIP_URL_HEALTH_CHECK: "1"XDG_CONFIG_HOME: "[TEST_CONFIG_HOME]"@@ -38,4 +42,4 @@ exit_code: 0----- stderr -----[2m○[22m Already up to date with [1mmain[22m (no new commits, no rebase needed)-[2m○[22m Worktree preserved (main worktree)+[2m○[22m Worktree preserved (primary worktree)diff --git a/tests/snapshots/integration__integration_tests__shell_wrapper__unix_tests__merge.snap b/tests/snapshots/integration__integration_tests__shell_wrapper__unix_tests__merge.snapindex ba6aa6dab..b02664cb6 100644--- a/tests/snapshots/integration__integration_tests__shell_wrapper__unix_tests__merge.snap+++ b/tests/snapshots/integration__integration_tests__shell_wrapper__unix_tests__merge.snap@@ -4,4 +4,4 @@ expression: "&output.combined"---[2m○[22m Already up to date with [1mmain[22m (no new commits, no rebase needed)-[2m○[22m Worktree preserved (main worktree)+[2m○[22m Worktree preserved (primary worktree)