Skip to content

Commit 94f7f8b

Browse files
committed
fix(remove): scope the detached-worktree guard to removals that delete a ref
The guard fired ahead of `prepare_worktree_removal`'s branch-existence check, so a name that is not a local branch but whose templated path holds a detached worktree reported `Branch <name> has no worktree` instead of `No branch named <name>` (or `RemoteOnlyBranch`). Check `exists_locally` first, failing closed on a lookup error so the #3769 case is never skipped. It also fired under `--no-delete-branch` / `[remove] delete-branch = false`, where the branch-only arm deletes nothing — nothing was going to strand the worktree, so the guard turned a no-op exit 0 into a hard failure. Gate on `deletion_mode.should_keep()`.
1 parent 80e796b commit 94f7f8b

4 files changed

Lines changed: 174 additions & 2 deletions

‎src/commands/remove.rs‎

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,12 @@ fn detached_worktree_for<'a>(
8686
branch: &str,
8787
worktrees: &'a [worktrunk::git::WorktreeInfo],
8888
) -> Option<&'a Path> {
89+
// Downstream, `prepare_worktree_removal` is what reports a typo, a deleted
90+
// branch, or a remote-only name; a guard that fires ahead of it would
91+
// assert a branch that doesn't exist. Fail closed on a lookup error.
92+
if !repo.branch(branch).exists_locally().unwrap_or(true) {
93+
return None;
94+
}
8995
let expected = compute_worktree_path(repo, branch, config).ok()?;
9096
worktrees
9197
.iter()
@@ -184,8 +190,14 @@ fn validate_remove_targets(
184190
// worktree it is about to remove as its own candidate is still
185191
// registered when the branch's plan is built. Refusing there
186192
// would leave prune unable to clean up either half.
187-
if let Some(detached) =
188-
worktrees.and_then(|wts| detached_worktree_for(repo, config, &branch, wts))
193+
//
194+
// Under `Keep` (`--no-delete-branch`, or
195+
// `[remove] delete-branch = false`) this arm deletes nothing,
196+
// so there is no ref to strand the worktree behind — the guard
197+
// would turn a no-op into a failure.
198+
if let Some(detached) = worktrees
199+
.filter(|_| !deletion_mode.should_keep())
200+
.and_then(|wts| detached_worktree_for(repo, config, &branch, wts))
189201
{
190202
plans.record_error(
191203
GitError::DetachedWorktreeForBranch {

‎tests/integration_tests/remove.rs‎

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -392,6 +392,40 @@ fn test_remove_default_branch_with_detached_main_worktree(repo: TestRepo) {
392392
assert_cmd_snapshot!(make_snapshot_cmd(&repo, "remove", &["main"], None));
393393
}
394394

395+
/// The #3769 guard runs ahead of the branch-existence check in
396+
/// `prepare_worktree_removal`, so it must not claim a branch that isn't there:
397+
/// a name whose templated path holds a detached worktree still reports the
398+
/// missing branch once the ref is gone.
399+
#[rstest]
400+
fn test_remove_missing_branch_with_detached_worktree_at_its_path(mut repo: TestRepo) {
401+
repo.add_worktree("feature-detached-orphan");
402+
repo.detach_head_in_worktree("feature-detached-orphan");
403+
repo.run_git(&["branch", "-D", "feature-detached-orphan"]);
404+
405+
assert_cmd_snapshot!(make_snapshot_cmd(
406+
&repo,
407+
"remove",
408+
&["feature-detached-orphan"],
409+
None
410+
));
411+
}
412+
413+
/// Under `--no-delete-branch` the branch-only removal deletes nothing, so there
414+
/// is no ref whose deletion could strand the detached worktree — the #3769
415+
/// guard must not turn that no-op into a failure.
416+
#[rstest]
417+
fn test_remove_branch_with_detached_worktree_keeping_branch(mut repo: TestRepo) {
418+
repo.add_worktree("feature-detached-keep");
419+
repo.detach_head_in_worktree("feature-detached-keep");
420+
421+
assert_cmd_snapshot!(make_snapshot_cmd(
422+
&repo,
423+
"remove",
424+
&["--no-delete-branch", "feature-detached-keep"],
425+
None
426+
));
427+
}
428+
395429
/// A detached worktree whose directory is already gone is stale metadata, not a
396430
/// worktree the #3769 guard protects — the branch-only removal proceeds rather
397431
/// than refusing and naming a path that isn't on disk.
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
---
2+
source: tests/integration_tests/remove.rs
3+
info:
4+
program: wt
5+
args:
6+
- remove
7+
- "--no-delete-branch"
8+
- feature-detached-keep
9+
env:
10+
APPDATA: "[TEST_CONFIG_HOME]"
11+
CLAUDE_CONFIG_DIR: "[TEST_CLAUDE_CONFIG]"
12+
CLICOLOR_FORCE: "1"
13+
COLUMNS: "500"
14+
GIT_ALLOW_PROTOCOL: file
15+
GIT_AUTHOR_DATE: "2025-01-01T00:00:00Z"
16+
GIT_AUTHOR_EMAIL: test@example.com
17+
GIT_AUTHOR_NAME: Test User
18+
GIT_COMMITTER_DATE: "2025-01-01T00:00:00Z"
19+
GIT_COMMITTER_EMAIL: test@example.com
20+
GIT_COMMITTER_NAME: Test User
21+
GIT_CONFIG_COUNT: "2"
22+
GIT_CONFIG_GLOBAL: /nonexistent/wt/gitconfig
23+
GIT_CONFIG_KEY_0: user.useConfigOnly
24+
GIT_CONFIG_KEY_1: rerere.enabled
25+
GIT_CONFIG_SYSTEM: /nonexistent/wt/gitconfig
26+
GIT_CONFIG_VALUE_0: "true"
27+
GIT_CONFIG_VALUE_1: "false"
28+
GIT_TERMINAL_PROMPT: "0"
29+
HOME: "[TEST_HOME]"
30+
LANG: C
31+
LC_ALL: C
32+
LLVM_PROFILE_FILE: "[LLVM_PROFILE_FILE]"
33+
OPENCODE_CONFIG_DIR: "[TEST_OPENCODE_CONFIG]"
34+
PATH: "[PATH]"
35+
TERM: alacritty
36+
USERPROFILE: "[TEST_HOME]"
37+
WORKTRUNK_APPROVALS_PATH: "[TEST_APPROVALS]"
38+
WORKTRUNK_CONFIG_PATH: "[TEST_CONFIG]"
39+
WORKTRUNK_SYSTEM_CONFIG_PATH: "[TEST_SYSTEM_CONFIG]"
40+
WORKTRUNK_TEST_BASH_INSTALLED: "0"
41+
WORKTRUNK_TEST_CLAUDE_INSTALLED: "0"
42+
WORKTRUNK_TEST_CODEX_INSTALLED: "0"
43+
WORKTRUNK_TEST_DELAYED_STREAM_MS: "-1"
44+
WORKTRUNK_TEST_EPOCH: "1735776000"
45+
WORKTRUNK_TEST_FISH_INSTALLED: "0"
46+
WORKTRUNK_TEST_GEMINI_INSTALLED: "0"
47+
WORKTRUNK_TEST_MOCK_CONFIG_DIR: "[TEST_MOCK_CONFIG]"
48+
WORKTRUNK_TEST_NUSHELL_ENV: "0"
49+
WORKTRUNK_TEST_OPENCODE_INSTALLED: "0"
50+
WORKTRUNK_TEST_PARENT_SHELL: ""
51+
WORKTRUNK_TEST_POWERSHELL_ENV: "0"
52+
WORKTRUNK_TEST_POWERSHELL_INSTALLED: "0"
53+
WORKTRUNK_TEST_PROBE_TIMEOUT_MS: "60000"
54+
WORKTRUNK_TEST_SKIP_URL_HEALTH_CHECK: "1"
55+
WORKTRUNK_TEST_ZSH_INSTALLED: "0"
56+
XDG_CONFIG_HOME: "[TEST_CONFIG_HOME]"
57+
---
58+
success: true
59+
exit_code: 0
60+
----- stdout -----
61+
62+
----- stderr -----
63+
○ No worktree found for branch feature-detached-keep
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
---
2+
source: tests/integration_tests/remove.rs
3+
info:
4+
program: wt
5+
args:
6+
- remove
7+
- feature-detached-orphan
8+
env:
9+
APPDATA: "[TEST_CONFIG_HOME]"
10+
CLAUDE_CONFIG_DIR: "[TEST_CLAUDE_CONFIG]"
11+
CLICOLOR_FORCE: "1"
12+
COLUMNS: "500"
13+
GIT_ALLOW_PROTOCOL: file
14+
GIT_AUTHOR_DATE: "2025-01-01T00:00:00Z"
15+
GIT_AUTHOR_EMAIL: test@example.com
16+
GIT_AUTHOR_NAME: Test User
17+
GIT_COMMITTER_DATE: "2025-01-01T00:00:00Z"
18+
GIT_COMMITTER_EMAIL: test@example.com
19+
GIT_COMMITTER_NAME: Test User
20+
GIT_CONFIG_COUNT: "2"
21+
GIT_CONFIG_GLOBAL: /nonexistent/wt/gitconfig
22+
GIT_CONFIG_KEY_0: user.useConfigOnly
23+
GIT_CONFIG_KEY_1: rerere.enabled
24+
GIT_CONFIG_SYSTEM: /nonexistent/wt/gitconfig
25+
GIT_CONFIG_VALUE_0: "true"
26+
GIT_CONFIG_VALUE_1: "false"
27+
GIT_TERMINAL_PROMPT: "0"
28+
HOME: "[TEST_HOME]"
29+
LANG: C
30+
LC_ALL: C
31+
LLVM_PROFILE_FILE: "[LLVM_PROFILE_FILE]"
32+
OPENCODE_CONFIG_DIR: "[TEST_OPENCODE_CONFIG]"
33+
PATH: "[PATH]"
34+
TERM: alacritty
35+
USERPROFILE: "[TEST_HOME]"
36+
WORKTRUNK_APPROVALS_PATH: "[TEST_APPROVALS]"
37+
WORKTRUNK_CONFIG_PATH: "[TEST_CONFIG]"
38+
WORKTRUNK_SYSTEM_CONFIG_PATH: "[TEST_SYSTEM_CONFIG]"
39+
WORKTRUNK_TEST_BASH_INSTALLED: "0"
40+
WORKTRUNK_TEST_CLAUDE_INSTALLED: "0"
41+
WORKTRUNK_TEST_CODEX_INSTALLED: "0"
42+
WORKTRUNK_TEST_DELAYED_STREAM_MS: "-1"
43+
WORKTRUNK_TEST_EPOCH: "1735776000"
44+
WORKTRUNK_TEST_FISH_INSTALLED: "0"
45+
WORKTRUNK_TEST_GEMINI_INSTALLED: "0"
46+
WORKTRUNK_TEST_MOCK_CONFIG_DIR: "[TEST_MOCK_CONFIG]"
47+
WORKTRUNK_TEST_NUSHELL_ENV: "0"
48+
WORKTRUNK_TEST_OPENCODE_INSTALLED: "0"
49+
WORKTRUNK_TEST_PARENT_SHELL: ""
50+
WORKTRUNK_TEST_POWERSHELL_ENV: "0"
51+
WORKTRUNK_TEST_POWERSHELL_INSTALLED: "0"
52+
WORKTRUNK_TEST_PROBE_TIMEOUT_MS: "60000"
53+
WORKTRUNK_TEST_SKIP_URL_HEALTH_CHECK: "1"
54+
WORKTRUNK_TEST_ZSH_INSTALLED: "0"
55+
XDG_CONFIG_HOME: "[TEST_CONFIG_HOME]"
56+
---
57+
success: false
58+
exit_code: 1
59+
----- stdout -----
60+
61+
----- stderr -----
62+
✗ No branch named feature-detached-orphan
63+
↳ To list branches, run wt list --branches --remotes

0 commit comments

Comments
 (0)