Worktrunk is a CLI for Git worktree management, designed for parallel AI agent workflows
fix: run post-remove hooks at primary worktree, not cwd (#1645)
Maximilian Roos115d7e32d74b
diff --git a/src/commands/repository_ext.rs b/src/commands/repository_ext.rsindex a13611bcc..1807b83a7 100644--- a/src/commands/repository_ext.rs+++ b/src/commands/repository_ext.rs@@ -80,9 +80,9 @@ impl RepositoryCliExt for Repository {) -> anyhow::Result<RemoveResult> {let current_path = self.current_worktree().root()?.to_path_buf();let worktrees = self.list_worktrees()?;- // Home worktree: prefer default branch's worktree, fall back to first worktree,- // then repo base for bare repos with no worktrees.- let home_worktree_path = self.home_path()?;+ // Primary worktree path: prefer default branch's worktree, fall back to first+ // worktree, then repo base for bare repos with no worktrees.+ let primary_path = self.home_path()?;// Phase 1: Resolve target to branch name and worktree disposition.// BranchOnly variants don't early-return — they go through shared validation below.@@ -226,11 +226,17 @@ impl RepositoryCliExt for Repository {target_wt.ensure_clean("remove worktree", branch_name.as_deref(), true)?;}- // Compute main_path and changed_directory based on whether we're removing current- let (main_path, changed_directory) = if is_current {- (home_worktree_path, true)+ // main_path: where post-remove hooks run from and background removal+ // executes. Prefer the primary worktree for stability (the removed worktree+ // is gone, and cwd may itself be a removal candidate during prune).+ // Fall back to cwd when the primary worktree IS the one being removed+ // (bare repo only — normal repos guard this in Phase 2 above).+ // changed_directory: whether the user needs to cd away from cwd.+ let changed_directory = is_current;+ let main_path = if worktree_path == primary_path {+ current_path} else {- (current_path, false)+ primary_path};// Resolve target branch for integration reason displaydiff --git a/src/commands/worktree/types.rs b/src/commands/worktree/types.rsindex eeb8fe22b..e1a5dc5e4 100644--- a/src/commands/worktree/types.rs+++ b/src/commands/worktree/types.rs@@ -176,8 +176,13 @@ impl BranchDeletionMode {/// Result of a worktree remove operationpub enum RemoveResult {- /// Removed worktree and returned to main (if needed)+ /// Removed worktree and changed directory (if needed)RemovedWorktree {+ /// Stable working directory for post-removal execution: hooks run here,+ /// background removal spawns from here, and `cd` directs the shell here.+ /// Usually the primary worktree; falls back to cwd when removing the+ /// primary worktree itself (bare repo edge case), or the target branch's+ /// worktree in `wt merge`.main_path: PathBuf,worktree_path: PathBuf,changed_directory: bool,diff --git a/tests/integration_tests/user_hooks.rs b/tests/integration_tests/user_hooks.rsindex 49eadd83f..951a1dd2b 100644--- a/tests/integration_tests/user_hooks.rs+++ b/tests/integration_tests/user_hooks.rs@@ -613,6 +613,29 @@ cleanup = "echo 'USER_POST_REMOVE_RAN' > ../user_postremove_marker.txt");}+/// Post-remove hooks run at the primary worktree, not cwd. When removing a+/// non-current worktree from a linked worktree, the output should show `@ [path]`+/// pointing to the primary worktree where hooks execute.+#[rstest]+fn test_post_remove_hooks_run_at_primary_worktree(mut repo: TestRepo) {+ let _feature_wt = repo.add_worktree("feature");+ let other_wt = repo.add_worktree("other");++ repo.write_test_config(+ r#"[post-remove]+cleanup = "echo done"+"#,+ );++ // Remove feature from the "other" worktree (not primary)+ snapshot_remove(+ "post_remove_runs_at_primary",+ &repo,+ &["feature", "--force-delete"],+ Some(&other_wt),+ );+}+/// Verify that post-remove hook template variables reference the removed worktree,/// not the worktree where the hook executes from.#[rstest]diff --git a/tests/snapshots/integration__integration_tests__user_hooks__post_remove_runs_at_primary.snap b/tests/snapshots/integration__integration_tests__user_hooks__post_remove_runs_at_primary.snapnew file mode 100644index 000000000..eff79ee4a--- /dev/null+++ b/tests/snapshots/integration__integration_tests__user_hooks__post_remove_runs_at_primary.snap@@ -0,0 +1,47 @@+---+source: tests/integration_tests/user_hooks.rs+info:+ program: wt+ args:+ - remove+ - feature+ - "--force-delete"+ env:+ APPDATA: "[TEST_CONFIG_HOME]"+ CLICOLOR_FORCE: "1"+ COLUMNS: "500"+ GIT_AUTHOR_DATE: "2025-01-01T00:00:00Z"+ 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+ LC_ALL: C+ MOCK_CONFIG_DIR: "[MOCK_CONFIG_DIR]"+ NO_COLOR: ""+ PATH: "[PATH]"+ PSModulePath: ""+ RUST_LOG: warn+ SHELL: ""+ TERM: alacritty+ USERPROFILE: "[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]"+---+success: true+exit_code: 0+----- stdout -----++----- stderr -----+[36m◎[39m [36mRemoving [1mfeature[22m worktree & branch in background (--force-delete)[39m+[36m◎[39m [36mRunning post-remove: [1muser:cleanup[22m @ [1m_REPO_[22m[39m