Repository navigation
refactor(hooks): route post-commit and post-switch through HookAnnouncer - #2482
Conversation
Migrates `wt switch --create` (post-switch + post-start) and `wt merge --squash` (post-commit + post-remove + post-switch + post-merge) to share one `◎ Running …` announce line instead of emitting one per phase. `CommitOptions::commit` and `handle_squash` now accept an optional `&mut HookAnnouncer`; `wt merge` constructs one early and threads it through both. Standalone `wt commit` / `wt step squash` pass `None` and self-announce as before. Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
worktrunk-bot
left a comment
There was a problem hiding this comment.
One nit on docstring placement — the announcer parameter description is attached to the impl block, so rustdoc and IDE hover on commit() won't pick it up. Move the doc onto the method.
|
CI follow-up:
|
Adds `test_merge_auto_commit_combines_post_commit_post_remove_post_switch_post_merge` exercising `wt merge --no-squash` with uncommitted changes — the path that threads the announcer through `CommitOptions::commit`'s `Some` arm (`commit.rs:243`), which the squash sibling test didn't reach. Also moves the `commit()` doc comment from the impl block onto the method so rustdoc and IDE hover surface the `announcer` parameter description. Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
# Conflicts: # src/commands/commit.rs # src/commands/step_commands.rs
worktrunk-bot
left a comment
There was a problem hiding this comment.
CI follow-up — correcting my earlier diagnosis.
The Windows failure on commit 2766001 was not a cargo-install flake; I misread the log. The actual failure was a real compile error during the test build, and it has reproduced on the merge commit (job 73688190160):
error: function `spawn_background_hooks` is never used
--> src\commands\hooks.rs:779:15
Root cause: this PR replaces the commit.rs and step_commands.rs callers of spawn_background_hooks with the prepare_background_pipelines + announcer-dispatch pattern. The only remaining caller is src/commands/picker/mod.rs:234, and picker is gated on #[cfg(unix)] in src/commands/mod.rs:18. On Windows the function ends up with zero callers, and -D dead-code (implied by -D warnings) trips the build.
Suggested fix — match the function's reachability to its sole caller by cfg-gating it the same way:
// src/commands/hooks.rs
/// Spawn background hooks for a single hook type (post-commit, post-merge, …).
/// […]
#[cfg(unix)]
pub(crate) fn spawn_background_hooks(
ctx: &CommandContext,
hook_type: HookType,
extra_vars: &[(&str, &str)],
display_path: Option<&Path>,
) -> anyhow::Result<()> {
…
}That preserves the "one-shot single-phase convenience" docstring intent while making the cfg footprint match the actual caller. Happy to push this as a follow-up commit if you'd like — it's a one-line change and shouldn't affect Linux/macOS at all (and Path stays imported because other Path-using items in hooks.rs are unconditional).
Dismissing my previous approval since the failure is PR-related rather than infrastructure.
Windows CI fails to compile — function spawn_background_hooks becomes dead code on Windows after this PR removes its commit.rs/step_commands.rs callers (sole remaining caller is picker, which is cfg(unix)). See follow-up review for diagnosis and suggested fix.
After this PR migrated commit.rs and step_commands.rs to the prepare_background_pipelines + extend/run_hooks_background pattern, the sole remaining caller of spawn_background_hooks was picker::do_removal, which is gated #[cfg(unix)]. Under -D warnings (-> -D dead-code) the function fails to compile on Windows. Drop the wrapper and inline the two-line pattern in picker, matching the call-site shape used elsewhere (commit.rs, step_commands.rs, output/handlers.rs::spawn_hooks_after_remove). Reported by worktrunk-bot in the #2482 review. Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
|
Thanks @worktrunk-bot — fixed in b35d639. I went one step further than the suggested
|
Builds on #2477 and #2482. Drops the `Option<&mut HookAnnouncer<'_>>` plumbing through `commit.rs::commit`, `step_commands::handle_squash`, `output/handlers.rs::handle_remove_output` (and its four sub-handlers), and the multi-target prune loop in `step_commands::step_prune`. Each function now takes `&mut HookAnnouncer<'_>` unconditionally — the if/else around \"share with caller's announcer or self-announce\" is gone. Standalone callers (`wt commit`, `wt step squash`, `wt remove`, `wt step prune`, `wt switch` picker post-remove) construct a local announcer and flush right after; multi-phase callers (`wt merge`) keep their existing single-announcer-per-command pattern. `run_hooks_background` is now a module-private helper of `HookAnnouncer::flush` (was `pub(crate)`). `hook_commands::run_post_hook` also routes through `HookAnnouncer`, so `flush` is the only path that reaches the announce/spawn primitive. `RemovedWorktreeOutputContext` loses its now-redundant `show_branch_in_hooks` field — callers configure the announcer directly when constructing it. No behavior change: combined announce lines, log paths, `show_branch` semantics for batch contexts (prune, multi-target remove), and exit codes are all unchanged. Existing snapshots and the full integration suite (3399 tests) pass; `cargo run -- hook pre-merge --yes` is green. > _This was written by Claude Code on behalf of @max-sixty_ Co-authored-by: Claude <[email protected]>
Extends the
HookAnnouncercoordinator (introduced in #2457, refined in #2477) to more hot paths so multi-phase background hooks share a single◎ Running …line.Migrated sites:
wt switch --create— post-switch + post-start (spawn_switch_background_hooksinhandle_switch.rs).wt merge— auto-commit (when dirty) + post-remove + post-switch + post-merge.wt merge --squash— post-commit (from squash) + post-remove + post-switch + post-merge.Plumbing:
CommitOptions::commitandhandle_squashgained an optional&mut HookAnnouncer<'_>parameter.handle_mergeconstructs one announcer early and threads it through the commit, squash, and remove paths;flush()runs once at the end. Standalonewt commit/wt step squashpassNoneand self-announce as before.No new abstractions — the
Somearm callsextend, theNonearm callsrun_hooks_background, mirroring the existing precedent inoutput/handlers.rs::spawn_hooks_after_remove. As a follow-on,spawn_background_hooks(whose only caller after migration waspicker::do_removal, gated#[cfg(unix)]) is dropped and the picker's call site is inlined to match — fixing a Windows dead-code build error and unifying all four single-shot post-hook sites on the same shape.Test coverage: new
test_merge_squash_combines_post_commit_post_remove_post_switch_post_mergeandtest_merge_auto_commit_combines_post_commit_post_remove_post_switch_post_mergesnapshot the combined four-phase announce line for both squash and non-squash auto-commit paths. Existingtest_merge_combines_post_remove_post_switch_post_merge,test_merge_drops_pending_hooks_when_post_merge_fails, andtest_switch_combined_post_switch_and_post_start_hooksremain green.🤖 Generated with Claude Code