Repository navigation
refactor(hooks): unify dispatch and replace CommandOrigin with closures - #2472
Merged
Merged
Conversation
…osures Collapse `hooks.rs` to two public entry points — `run_hooks_foreground` and `run_hooks_background` — replacing `execute_hook`, `run_hook_with_filter`, `spawn_background_hooks`, `spawn_hook_pipeline`, and `announce_and_spawn_background_hooks`. `prepare_background_hooks` is gone too; its source-grouping is now `into_source_groups` over the flat `prepare_sourced_steps` result. Replace `CommandOrigin` in `command_executor.rs` with explicit per-step fields on `ForegroundStep` (`AnnouncePolicy`, `pipe_stdin`, `error_wrapper` closure) and per-command labels (`label`, `log_label`) on `PreparedCommand` set at prep time. `handle_command_error` becomes a single branch instead of a 4-variant case-split, resolving the TODO at command_executor.rs:308-316. Add `HookType::is_pre()` plus `FailureStrategy::default_for(hook_type)` so `run_hook`'s pre/post dispatch is one `if`. Extract `approve_or_skip` for the "approve hooks → fall through if declined" pattern repeated across `step_commands.rs`, `main.rs`, and `handle_switch.rs`. Tighten `spawn_hook_pipeline_quiet`'s base-context extraction (caller invariants guarantee non-empty steps). `add_hook_skip_hint` placement is preserved: operation-driven callers (merge, switch, commit, …) tag failures with the `--no-hooks` reminder via `run_hooks_foreground_for_type` or explicit `.map_err`; `wt hook <type>` leaves errors unwrapped because the user already asked for the hooks. `test_standalone_hook_failure_omits_skip_hint` locks the invariant. 3377 tests pass (+2 vs main: warn-unnamed coverage, skip-hint regression test). Net -125 LOC across 14 source files. Co-Authored-By: Claude <[email protected]>
Integrates `HookAnnouncer` (#2457) with the unified hook entry points: - `HookAnnouncer::register` now calls `prepare_background_pipelines` (this branch's helper) instead of the deleted `prepare_background_hooks`. - `HookAnnouncer::flush` calls `run_hooks_background` (this branch's unified entry) instead of the private `announce_and_spawn_pipelines` helper. The empty-filter is folded into `run_hooks_background` itself. - `output/handlers::spawn_hooks_after_remove` keeps the `Option<&mut HookAnnouncer<'_>>` parameter and falls back to `run_hooks_background` when no announcer is provided. - Also pulls in `PushKind` (#2468); no integration changes needed. Co-Authored-By: Claude <[email protected]>
Brings in `TemplateVars` (#XXXX) helper for building hook template vars. The conflict in handle_switch.rs was just imports — keep both this branch's hook helpers (prepare_background_pipelines, run_hooks_background, run_hooks_foreground_for_type) and main's TemplateVars. Co-Authored-By: Claude <[email protected]>
The previous refactor collapsed `hooks.rs` to two public entry points (`run_hooks_foreground`, `run_hooks_background`) and pushed config-lookup boilerplate onto every operation-driven caller. This iteration walks back that part — the call sites are what readers actually touch, and they should be one-liners. Restore two `pub(crate)` shortcuts: - `execute_hook(ctx, hook_type, extra_vars, strategy, display_path)` — auto-lookup foreground hooks, with the `--no-hooks` skip-hint applied centrally in one place. Replaces the previous `run_hooks_foreground_for_type` (renamed for familiarity) and absorbs `commit.rs` / `step_commands.rs`'s bare-spec + manual `.map_err(add_hook_skip_hint)` calls. - `spawn_background_hooks(ctx, hook_type, extra_vars, display_path)` — symmetric one-liner for single-type background hooks. `run_hooks_foreground` becomes `pub(crate)`; only `wt hook <type>` now calls it directly, where the no-hint behaviour is intentional. Multi-type background callers (`handle_switch::spawn_switch_background_hooks` for PostSwitch + PostStart, `output::handlers::spawn_hooks_after_remove` for PostRemove + PostSwitch) keep using `prepare_background_pipelines` + `run_hooks_background` so all types share one announce line. `merge.rs` keeps using `HookAnnouncer` for the same reason across phases. Caller diffs vs main shrink dramatically: - picker: 29 → 1 line changed - merge: 17 → 7 - worktree/hooks: 5 → 1 - commit / step_commands: pre-commit hook execution is now a single `execute_hook(...)` call instead of building a `HookCommandSpec`. The skip-hint policy is now centralized in `execute_hook` (operation-driven path always adds it). The regression test `test_standalone_hook_failure_omits_skip_hint` continues to lock in that `wt hook <type>` does not. 3391 tests pass. Co-Authored-By: Claude <[email protected]>
worktrunk-bot
left a comment
Collaborator
There was a problem hiding this comment.
Two minor suggestions inline. Otherwise the refactor reads cleanly: the CommandOrigin → explicit-fields split resolves the per-origin branching genuinely (single error_wrapper callsite, no enum match in handle_command_error), and execute_hook / spawn_background_hooks shave the operation-driven callsites down to one-liners as advertised.
- HookType::is_pre uses an exhaustive match instead of `matches!` so a future variant trips a compile-time nudge. - post_hook_display_path doc example uses the spawn_background_hooks one-liner (the recommended path for single-type background hooks) instead of the two-line prepare+run form (which is for multi-type). Co-Authored-By: Claude <[email protected]>
worktrunk-bot
approved these changes
Apr 29, 2026
max-sixty
added a commit
that referenced
this pull request
Apr 29, 2026
Follow-up to #2472. Four sites still built hook extra-vars by hand: `commit.rs`, `step_commands::handle_squash`, `picker::do_removal`, and `hook_commands::build_manual_hook_extra_vars`. They now use `TemplateVars` like the rest of the hook call sites — owning the strings and centralizing the `worktree_path`/`worktree` alias. `build_manual_hook_extra_vars` is renamed to `build_manual_hook_template_vars` and returns `TemplateVars` so the two callers (`run_hook`, `expand_command_template`) can append CLI bindings or `{{ args }}` before materializing. Drops two redundant `to_posix_path` calls along the way. Module docstring on `template_vars.rs` updated to reflect that the struct now covers all hook call sites, not just switch/merge. > _This was written by Claude Code on behalf of @max-sixty_ --------- Co-authored-by: Claude <[email protected]>
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.
Summary
CommandOriginenum incommand_executor.rswith explicit fields onForegroundStep(AnnouncePolicy,pipe_stdin: bool,error_wrapper: ErrorWrapper) and per-commandlabel/log_labelset at prep time.handle_command_errorcollapses from a 4-variant case-split to a single branch (FailFastcalls the closure,Warnprints). Resolves the pre-existing TODO atcommand_executor.rs:308-316.hooks.rsaround two pub entries (run_hooks_foreground,run_hooks_background) pluspub(crate)shortcuts (execute_hook,spawn_background_hooks) that absorb the auto-config-lookup boilerplate.execute_hookis the canonical operation-driven path and appliesadd_hook_skip_hintcentrally;wt hook <type>callsrun_hooks_foregrounddirectly so failures don't carry the misleading--no-hooksreminder.HookType::is_pre()andFailureStrategy::default_for(hook_type)sorun_hook's pre/post dispatch is oneif. Addsapprove_or_skipfor the "approve hooks → fall through if declined" pattern (used at 4 sites).run_post_hook's filter/no-filter dispatch to a singleis_empty()branch. Eliminatesprepare_background_hooksduplication viainto_source_groupsover the flatprepare_sourced_stepsresult. Tightensspawn_hook_pipeline_quiet's base-context extraction (caller invariants guarantee non-empty steps).HookAnnouncer(hooks: combine background hook announces per command #2457):registercallsprepare_background_pipelines,flushcallsrun_hooks_background.Net: 500 insertions, 593 deletions across 16 files. 3391 tests pass; lints and snapshots clean. New regression test
test_standalone_hook_failure_omits_skip_hintlocks in thewt hook <type>skip-hint behaviour.Test plan
🤖 Generated with Claude Code