Skip to content

Worktrunk is a CLI for Git worktree management, designed for parallel AI agent workflows

Reuse user config snapshot for removal hooks (#4276)

DDLe1cc1295ef00
diff --git a/src/output/handlers.rs b/src/output/handlers.rs
index 7231d93d9..76fb33208 100644
--- a/src/output/handlers.rs
+++ b/src/output/handlers.rs
@@ -1420,13 +1420,12 @@ fn spawn_hooks_after_remove(
) -> anyhow::Result<()> {
// The worktree is gone (or, on the fallback path, being deleted by the
// detached `git worktree remove` this call follows), so a pipeline anchored
- // on it must not be spawned into it. Recorded before the config load, which
- // returns early on an unreadable user config.
+ // on it must not be spawned into it.
announcer.mark_worktree_removed(ctx.worktree_path);
- let Ok(config) = UserConfig::load() else {
- return Ok(());
- };
+ // The startup snapshot, not a reload: `pre-remove` and the removal itself
+ // may have rewritten the user config since the approval gate read it.
+ let config = repo.user_config();
// When removing the current worktree, user cd's to main_path → use post_hook logic
// (suppresses path if shell integration will cd there).
@@ -1445,7 +1444,7 @@ fn spawn_hooks_after_remove(
// All hooks use remove_ctx for spawning: log files are named after the removed
// branch since both post-remove and post-switch are consequences of that removal.
- let remove_ctx = CommandContext::new(repo, &config, removed_branch, ctx.main_path, false);
+ let remove_ctx = CommandContext::new(repo, config, removed_branch, ctx.main_path, false);
// `post-remove` is *about* the removed worktree (gone by now); it was
// selected and frozen into `hook_plan` at the gate, anchored at the removed
@@ -1465,7 +1464,7 @@ fn spawn_hooks_after_remove(
if ctx.changed_directory {
let dest_branch = repo.worktree_at(ctx.main_path).branch()?;
let switch_ctx =
- CommandContext::new(repo, &config, dest_branch.as_deref(), ctx.main_path, false);
+ CommandContext::new(repo, config, dest_branch.as_deref(), ctx.main_path, false);
register_planned(
announcer,
ctx.hook_plan,
@@ -1753,10 +1752,6 @@ fn execute_pre_remove_hooks_if_needed(
repo: &Repository,
ctx: &WorktreeRemovalContext<'_>,
) -> anyhow::Result<()> {
- let Ok(config) = UserConfig::load() else {
- return Ok(());
- };
-
// `pre-remove` runs in the worktree being removed (still on disk here).
// `pre_remove_repo` roots the *render* context there for template vars;
// the command set is the frozen `hook_plan` selected at the gate, so no
@@ -1764,7 +1759,7 @@ fn execute_pre_remove_hooks_if_needed(
let pre_remove_repo = Repository::at(ctx.worktree_path)?;
let command_ctx = CommandContext::new(
&pre_remove_repo,
- &config,
+ pre_remove_repo.user_config(),
ctx.branch_name,
ctx.worktree_path,
false, // yes=false for CommandContext (not approval-related)
diff --git a/tests/integration_tests/user_hooks.rs b/tests/integration_tests/user_hooks.rs
index 72302e5a3..0534ebf39 100644
--- a/tests/integration_tests/user_hooks.rs
+++ b/tests/integration_tests/user_hooks.rs
@@ -1467,10 +1467,51 @@ notify = "echo switched"
});
}
-// Note: The `return Ok(())` path in spawn_hooks_after_remove when UserConfig::load()
-// fails is defensive code for an extremely rare race condition where config becomes
-// invalid between command startup and hook execution. This is not easily testable
-// without complex timing manipulation.
+/// The approval gate freezes both removal hooks before either runs. If
+/// `pre-remove` makes the user config unreadable, `post-remove` must still run
+/// from that frozen plan rather than silently disappearing on a second load.
+#[rstest]
+fn test_post_remove_uses_config_snapshot_after_pre_remove_mutates_file(mut repo: TestRepo) {
+ let feature_wt = repo.add_worktree("feature");
+ let config_path = repo.test_config_path().to_slash_lossy();
+ let marker = repo
+ .root_path()
+ .parent()
+ .unwrap()
+ .join("post-remove-after-config-change.txt");
+ let marker_path = marker.to_slash_lossy();
+
+ repo.write_test_config(&format!(
+ r#"[pre-remove]
+break-config = "printf 'invalid = [' > '{config_path}'"
+
+[post-remove]
+marker = "printf POST_REMOVE_RAN > '{marker_path}'"
+"#
+ ));
+
+ let output = repo
+ .wt_command()
+ .args(["remove", "--foreground", "--force-delete", "feature"])
+ .output()
+ .unwrap();
+ let stderr = String::from_utf8_lossy(&output.stderr);
+
+ assert!(
+ output.status.success(),
+ "removal should succeed after the planned pre-remove hook mutates config: {stderr}"
+ );
+ assert!(
+ !feature_wt.exists(),
+ "feature worktree should have been removed"
+ );
+ assert!(
+ stderr.contains("Running post-remove"),
+ "the frozen post-remove hook should still be registered: {stderr}"
+ );
+ wait_for_file_content(&marker);
+ assert_eq!(fs::read_to_string(marker).unwrap(), "POST_REMOVE_RAN");
+}
#[rstest]
fn test_standalone_hook_post_remove_invalid_template(repo: TestRepo) {