Repository navigation
feat(hooks): post-remove reads the removed worktree's config (snapshot before removal) - #2736
Merged
Merged
Conversation
…hot-before-removal); collapse the prep-pipeline shim `pre-remove` already reads the worktree-being-removed's `.config/wt.toml`, but `post-remove` read the *post-removal* working directory's config (the primary worktree, or the merge destination) — so a `post-remove` defined in a feature branch's branch-local config never fired, and the `pre-remove`/`post-remove` pair didn't read the same file. The only reason for the asymmetry was that the worktree ceases to exist between the two hooks. This snapshots the removed worktree's `ProjectConfig` before deletion (stashed on `RemoveResult::RemovedWorktree::removed_project_config`) and threads it to the `post-remove` executor. The rule the spec docstring now states up front: **every hook is anchored to one worktree — the one it's *about* — and reads that worktree's `.config/wt.toml`; when the worktree isn't on disk at read time, read a snapshot.** Two hooks need that: `post-remove` (worktree just deleted) and `wt switch --create`'s post-switch hooks at approval time (worktree not created yet — reads the base ref via `git show`). Same role, two physical sources; the spec's new "Snapshot paths" section frames them together. `pre-remove` and `post-remove` collapse into one table row. ### What changed - **`RemoveResult::RemovedWorktree`** gains `removed_project_config: Option<Box<ProjectConfig>>` (boxed — `ProjectConfig` is ~600 bytes and would otherwise dominate the variant; clippy `large_enum_variant`). `prepare_worktree_removal` and `finish_after_merge` (for `wt merge`) populate it before any deletion. A parse failure there is *not* propagated — the executor's own `load_project_config()` re-reads from disk and surfaces the parse error with full chain, and `pre-remove` aborts before `post-remove` fires, so the `None` is invisible. (Routing the parse error through `prepare_worktree_removal` would land it in `main.rs`'s `record_error`, which renders only the top message — a pre-existing display gap orthogonal to this work.) - **`output::handlers::spawn_hooks_after_remove`** registers `post-remove` via the new `HookAnnouncer::register_with_project_config` using that snapshot. `post-switch` after a removal keeps loading from `ctx.repo` (the post-removal cwd — that's the worktree it's *about*). - **`commands::picker::do_removal`** (the TUI in-place removal path) was the last hook-execution site reading the primary's config — `pre-remove` was rooted at `main_path`, `post-remove` likewise. Now `pre-remove` is rooted at the worktree being removed and `post-remove` uses the snapshot, matching `wt remove`. - **`collect_remove_hook_commands`** (the shared approval helper for `wt remove` / `wt merge` / `wt step prune`) collects `post-remove` per-worktree (same loop as `pre-remove`); `post-switch` keeps coming from `primary_repo`. - **`prepare_background_pipelines`** absorbs its single-use loading shim: it now takes a required `Option<&ProjectConfig>`, and `HookAnnouncer::register` (loads from `ctx.repo`) / `register_with_project_config` (explicit snapshot) are the two ergonomic wrappers — mirroring the approval side's `approve_or_skip` / `approve_or_skip_with_config`. - **Spec**: `commands::hooks` module docstring rewritten — "anchor worktree" framing, the `pre-`/`post-remove` row merge, the "Snapshot paths" section. ### Tests - `test_post_remove_hook_reads_removed_worktree_config` (remove.rs) — the mirror of the existing `test_pre_remove_hook_reads_removed_worktree_config`: a `post-remove` in the removed worktree's branch-local `.config/wt.toml` fires correctly *after* the worktree is gone. Verified to fail without the snapshot wiring. - Existing pre-remove / merge-teardown / prune / approval-prompt / picker suites all pass; `wt hook pre-merge --yes` (full suite + lints + doctests) is green. Co-authored-by: Claude Opus 4.7 (1M context) <[email protected]>
worktrunk-bot
approved these changes
May 12, 2026
max-sixty
added a commit
that referenced
this pull request
May 12, 2026
…2746) The picker's `alt-r` removal (`PickerCollector::do_removal`) was a parallel reimplementation of the `wt remove` teardown pipeline — `pre-remove` hook → git worktree removal → `post-remove` (and `post-switch`) hooks — that had to be kept in lockstep with `output::handle_remove_output` by hand. #2736 had to fix the same hook-config bug in both. This routes `do_removal` through `handle_remove_output`, so the duplication is gone, and gates the picker's hooks on approval (which the old path didn't do). ## What changed **`handle_remove_output` gains a `silent` flag** (`src/output/handlers.rs`). When set, a `RemovedWorktree` result is removed with no progress/success messages, no trash-cleanup spinner, and no `cd` directive — just `pre-remove`, the synchronous git worktree removal (`remove_removed_worktree_silently`), and `post-remove` / `post-switch` hook registration. Required because the picker runs `do_removal` from a background thread spawned off skim's event loop, so any `wt`-generated stderr output would corrupt skim's frame. `silent` is the only new knob; it threads through the five `handle_remove_output` call sites as `false` everywhere except the picker. **`do_removal` is now a thin call into `handle_remove_output`** (`src/commands/picker/mod.rs`). The `RemovedWorktree` arm drops its hand-rolled `pre-remove` (`CommandContext` + `execute_hook`) / git removal / `post-remove` (`PostRemoveContext` + `HookAnnouncer::register_with_project_config`) code — net ~80 lines, plus the `removed_project_config` snapshot plumbing #2736 added now lives in one place. The `BranchOnly` arm is unchanged (it's small and `handle_remove_output`'s `BranchOnly` path prints things the picker can't have). **The picker's hooks now run only when already approved.** The old `do_removal` ran project `pre-remove` / `post-remove` / `post-switch` hooks *unconditionally* — it was the one removal/switch path that bypassed the approval gate, so a freshly-cloned repo's `post-switch` hook would fire on `alt-r`. The picker can't show an approval prompt mid-render, so it now consults `Approvals` read-only (`removal_hooks_approved` — the same `collect_remove_hook_commands` set `wt remove` would prompt for, checked against `Approvals::is_command_approved`) and passes the result as `verify`; when it's `false`, `execute_pre_remove_hooks_if_needed` and `spawn_hooks_after_remove` early-return. `CLAUDE.md` gains a "Project Commands Run Only After Approval" section stating the general rule (`src/commands/command_approval.rs` is the gate; never build a path that runs project commands without it; a context that can't prompt skips the unapproved ones). **One behavior delta beyond consolidation:** removing the *current* worktree via `alt-r` now also registers `post-switch` hooks for the home worktree (if approved), matching `wt remove` / `wt merge` / `wt step prune`. The old `do_removal` registered only `post-remove`. ## Testing `cargo run -- hook pre-merge --yes` is green (3690 tests). The `test_do_removal_*` unit tests cover the silent removal path (worktree + branch + detached), and a new `test_do_removal_skips_unapproved_pre_remove_hook` covers the approval gating (`removal_hooks_approved` is fully covered). Not covered: the two lines in `invoke()`'s `alt-r` spawn block that hand the `Approvals` snapshot to the removal thread — `invoke()` is already documented as not unit-testable ("Full invoke() tests require interactive skim"), and the rest of its body is uncovered too; the approval-check logic itself is. No end-to-end test exercises "hook present *and* approved → runs" through the picker (would need seeding `Approvals` against the test repo's path) — the unit test asserts the logic, just not that exact integration. 🤖 Generated with [Claude Code](https://claude.com/claude-code) --------- Co-authored-by: Claude Opus 4.7 (1M context) <[email protected]>
Merged
max-sixty
added a commit
that referenced
this pull request
May 13, 2026
Release v0.50.0. Highlights: - Experimental Azure DevOps support (#1256, thanks @mikeyroush; fixes #1144 from @dlecan) — `wt switch pr:<N>`, `wt list --full`, and `wt config show --full` recognize Azure DevOps via the `az` CLI. - Experimental Gitea CI-status detection (#2702) on top of the Gitea `pr:` shortcut (#1320, thanks @SjB). - Hooks now resolve `.config/wt.toml` from the worktree they act on — the primary-worktree fallback is gone, and `post-remove` reads the removed worktree's config (snapshotted before removal). Approval prompts collect hook commands from the same worktree. Breaking change for setups that relied on the primary-worktree fallback; the changelog entry has the recovery action. (#2690, #2703, #2714, #2717, #2701, #2708, #2727, #2736, #2748) - The `wt switch` picker's `alt-r` removal no longer runs unapproved project hooks (#2746) — the picker's removal path is now routed through `handle_remove_output` and consults the existing approval state read-only. - `wt config alias show` with no name lists every alias's full definition (#2684, #2691); `wt --help` switches to a compact aliases pointer (#2688). - `wt list --branches` warm-run perf: SHA-keyed cache for `main↕` and `Remote⇅` ahead/behind counts; shared push-remote URL and local-branch scan (#2704, #2718, #2673). - Claude Code plugin ships the `wt-switch-create` skill (#2737, thanks @onetom for #2631). See `CHANGELOG.md` for the full list (8 Improved, 5 Fixed, 5 Internal). semver-checks reports breaking library-API changes (new enum variants without `#[non_exhaustive]`, removed `Branch::github_push_url`, new trait method on `RemoteRefProvider`), which mandates at minimum a minor bump pre-1.0.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
pre-removealready reads the worktree-being-removed's.config/wt.toml, butpost-removeread the post-removal working directory's config — the primary worktree forwt remove/wt step prune, the merge destination forwt merge. So apost-removedefined in a feature branch's branch-local config never fired, and a[pre-remove]/[post-remove]pair in the same.config/wt.tomldidn't read the same file. The only reason for the asymmetry: the worktree ceases to exist between the two hooks.This snapshots the removed worktree's
ProjectConfigbefore deletion (stashed onRemoveResult::RemovedWorktree::removed_project_config) and threads it to thepost-removeexecutor. The rule thecommands::hooksspec docstring now states up front: every hook is anchored to one worktree — the one it's about — and reads that worktree's.config/wt.toml; when the worktree isn't on disk at read time, read a snapshot. Two hooks need that:post-remove(worktree just deleted) andwt switch --create's post-switch hooks at approval time (worktree not created yet — reads the base ref viagit show). Same role, two physical sources; the spec's new "Snapshot paths" section frames them together, andpre-remove/post-removecollapse into one table row.What changed
RemoveResult::RemovedWorktreegainsremoved_project_config: Option<Box<ProjectConfig>>(boxed —ProjectConfigis ~600 bytes and would otherwise dominate the variant; clippylarge_enum_variant).prepare_worktree_removalandfinish_after_mergepopulate it before any deletion. A parse failure there is not propagated: the executor's ownload_project_config()re-reads from disk and surfaces the parse error with full chain, andpre-removeaborts beforepost-removefires, so theNoneis invisible. (Routing the parse error throughprepare_worktree_removalwould land it inmain.rs'srecord_error, which renders only the top message — a pre-existing display gap orthogonal to this work.)output::handlers::spawn_hooks_after_removeregisterspost-removevia the newHookAnnouncer::register_with_project_configusing that snapshot.post-switchafter a removal keeps loading fromctx.repo(the post-removal cwd — the worktree it's about).commands::picker::do_removal(the TUI in-place removal path) was the last hook-execution site reading the primary's config —pre-removewas rooted atmain_path,post-removelikewise. Nowpre-removeis rooted at the worktree being removed andpost-removeuses the snapshot, matchingwt remove.collect_remove_hook_commands(the shared approval helper forwt remove/wt merge/wt step prune) collectspost-removeper-worktree (same loop aspre-remove);post-switchkeeps coming fromprimary_repo.prepare_background_pipelinesabsorbs its single-use loading shim: it now takes a requiredOption<&ProjectConfig>, andHookAnnouncer::register(loads fromctx.repo) /register_with_project_config(explicit snapshot) are the two ergonomic wrappers — mirroring the approval side'sapprove_or_skip/approve_or_skip_with_config.commands::hooksmodule docstring rewritten — "anchor worktree" framing, thepre-/post-removerow merge, the "Snapshot paths" section.Tests
test_post_remove_hook_reads_removed_worktree_config(remove.rs) — the mirror of the existingtest_pre_remove_hook_reads_removed_worktree_config: apost-removein the removed worktree's branch-local.config/wt.tomlfires correctly after the worktree is gone. Verified to fail without the snapshot wiring. Existing pre-remove / merge-teardown / prune / approval-prompt / picker suites all pass;wt hook pre-merge --yes(full suite + lints + doctests) is green.Follow-up (not in this PR)
commands::picker::do_removalis still a parallel reimplementation of thewt removepipeline (pre-remove → git removal → post-remove), ~80 lines that shadowhandle_remove_output's flow plus its ownPostRemoveContextconstruction. This PR makes it follow the same config rule; routing it throughhandle_remove_output(which has a quiet/background mode) would delete the duplication outright — a separate, larger refactor with its own picker-test burden.