Worktrunk is a CLI for Git worktree management, designed for parallel AI agent workflows
fix(plugin): reopen an existing worktree from the WorktreeCreate hook (#4487)
Worktrunk Bot56890db5a509
diff --git a/plugins/worktrunk/hooks/hooks.json b/plugins/worktrunk/hooks/hooks.jsonindex 8e146145a..56a5d36be 100644--- a/plugins/worktrunk/hooks/hooks.json+++ b/plugins/worktrunk/hooks/hooks.json@@ -69,7 +69,7 @@"hooks": [{"type": "command",- "command": "bash -c 'set -o pipefail; name=$(jq -er .name) || exit 1; cd \"${CLAUDE_PROJECT_DIR:-.}\" || exit 1; bash \"$CLAUDE_PLUGIN_ROOT/hooks/wt.sh\" switch --create \"$name\" --no-cd --format=json | jq -er .path'"+ "command": "bash -c 'set -o pipefail; name=$(jq -er .name) || exit 1; cd \"${CLAUDE_PROJECT_DIR:-.}\" || exit 1; create=--create; git rev-parse --verify --quiet \"refs/heads/$name\" >/dev/null 2>&1 && create=; bash \"$CLAUDE_PLUGIN_ROOT/hooks/wt.sh\" switch $create \"$name\" --no-cd --format=json | jq -er .path'"}]}diff --git a/src/commands/config/plugins.rs b/src/commands/config/plugins.rsindex ddb21bf3b..ad671c34b 100644--- a/src/commands/config/plugins.rs+++ b/src/commands/config/plugins.rs@@ -152,8 +152,10 @@ struct PermissionRequest {/// session 💬 while the dialog waits. One hook does both because Claude Code/// runs every matching hook in parallel./// - `WorktreeCreate` runs `wt switch --create` and prints the new worktree's-/// path, which Claude Code reads as the hook's answer. A failure exits-/// nonzero with nothing on stdout (#3545).+/// path, which Claude Code reads as the hook's answer. A name whose branch+/// already exists switches to it without `--create`, since Claude Code fires+/// the hook again when `claude --worktree <name>` reopens a kept worktree+/// (#4486). A failure exits nonzero with nothing on stdout (#3545)./// - `WorktreeRemove` resolves against the worktree path Claude Code hands/// over, never `CLAUDE_PROJECT_DIR`: the `claude agents` view spans/// repositories, so no single project dir is right for every session@@ -185,11 +187,17 @@ pub fn handle_claude_hook() -> anyhow::Result<()> {set_marker(&project_dir, &["set", "💬"])}ClaudeHook::WorktreeCreate { name } => {- let stdout = run_wt(- Cmd::new(wt_binary()?)- .args(["switch", "--create", &name, "--no-cd", "--format=json"])- .current_dir(&project_dir),- )?;+ // A directory no repository resolves from falls through to+ // `--create`, which reports wt's own error.+ let exists = Repository::at(&project_dir)+ .and_then(|repo| repo.branch(&name).exists_locally())+ .unwrap_or(false);+ let mut args = vec!["switch"];+ if !exists {+ args.push("--create");+ }+ args.extend([name.as_str(), "--no-cd", "--format=json"]);+ let stdout = run_wt(Cmd::new(wt_binary()?).args(args).current_dir(&project_dir))?;let created: serde_json::Value = serde_json::from_slice(&stdout).context("Failed to parse `wt switch --format=json` output")?;let path = created["path"]diff --git a/tests/integration_tests/claude_hook.rs b/tests/integration_tests/claude_hook.rsindex 798d30fcb..c87817bc6 100644--- a/tests/integration_tests/claude_hook.rs+++ b/tests/integration_tests/claude_hook.rs@@ -143,7 +143,8 @@ fn test_claude_hook_permission_request(mut repo: TestRepo) {/// `WorktreeCreate` prints the new worktree's path; a failed create exits/// nonzero with nothing on stdout, so Claude Code reports wt's error rather/// than a successful hook with no path (#3545). Without `CLAUDE_PROJECT_DIR`-/// the hook creates from its own cwd.+/// the hook creates from its own cwd. A name whose branch already exists+/// reopens it, as `claude --worktree <name>` does for a kept worktree (#4486).#[rstest]fn test_claude_hook_worktree_create(repo: TestRepo) {let root = repo.root_path().to_path_buf();@@ -156,9 +157,14 @@ fn test_claude_hook_worktree_create(repo: TestRepo) {assert!(path.join(".git").exists(), "no worktree at {path:?}");assert!(path.ends_with("repo.agent-task"), "{path:?}");- let collision = run_hook(&repo, &root, &root, payload);- assert!(!collision.status.success());- assert_eq!(stdout(&collision), "");+ let reopened = run_hook(&repo, &root, &root, payload);+ assert!(reopened.status.success(), "{reopened:?}");+ assert_eq!(Path::new(stdout(&reopened).trim_end()), path);++ let outside = repo.home_path().to_path_buf();+ let failed = run_hook(&repo, &outside, &outside, payload);+ assert!(!failed.status.success());+ assert_eq!(stdout(&failed), "");}/// `WorktreeRemove` resolves against the path Claude Code hands it, even when