Skip to content

Commit f1f29b6

Browse files
committed
docs(remove): correct what the branch-existence guard actually guarantees
`exists_locally` reports a failed lookup as `Ok(false)`, so `unwrap_or(true)` never fires and the guard is skipped rather than held — what keeps that from deleting a ref is the identical call downstream in `prepare_worktree_removal`, which bails with `BranchNotFound` / `RemoteOnlyBranch`. The neighbouring `is_linked().unwrap_or(true)` *is* a real fail-closed, so say which is which. Also name the case the `Keep` gate's reasoning covers but doesn't gate: an unintegrated branch under `SafeDelete` retains its ref too, but only the downstream deletion attempt knows that, so a detached worktree refuses there rather than exiting 0. Pinned by `test_remove_unmerged_branch_with_detached_worktree_refuses`.
1 parent 94f7f8b commit f1f29b6

3 files changed

Lines changed: 102 additions & 2 deletions

File tree

‎src/commands/remove.rs‎

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,10 @@ fn detached_worktree_for<'a>(
8888
) -> Option<&'a Path> {
8989
// Downstream, `prepare_worktree_removal` is what reports a typo, a deleted
9090
// 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.
91+
// assert a branch that doesn't exist. `exists_locally` reports a failed
92+
// lookup as `false` rather than an error, so this can't fail closed — but
93+
// the same call downstream then refuses the removal, so a guard skipped
94+
// that way still never deletes a ref.
9295
if !repo.branch(branch).exists_locally().unwrap_or(true) {
9396
return None;
9497
}
@@ -194,7 +197,12 @@ fn validate_remove_targets(
194197
// Under `Keep` (`--no-delete-branch`, or
195198
// `[remove] delete-branch = false`) this arm deletes nothing,
196199
// so there is no ref to strand the worktree behind — the guard
197-
// would turn a no-op into a failure.
200+
// would turn a no-op into a failure. `SafeDelete` on an
201+
// unintegrated branch retains its ref too, but only the
202+
// deletion attempt downstream knows that
203+
// (`Repository::integration_reason`), so those refuse here
204+
// rather than exiting 0 — the conservative direction, since
205+
// what the refusal names is still on disk.
198206
if let Some(detached) = worktrees
199207
.filter(|_| !deletion_mode.should_keep())
200208
.and_then(|wts| detached_worktree_for(repo, config, &branch, wts))

‎tests/integration_tests/remove.rs‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -426,6 +426,35 @@ fn test_remove_branch_with_detached_worktree_keeping_branch(mut repo: TestRepo)
426426
));
427427
}
428428

429+
/// An unintegrated branch keeps its ref under `SafeDelete` too, but only the
430+
/// deletion attempt downstream knows that — so unlike the `--no-delete-branch`
431+
/// case above, a detached worktree refuses here rather than exiting 0 with the
432+
/// branch retained. The refusal names a worktree that is still on disk, so this
433+
/// pins the conservative direction rather than an accident.
434+
#[rstest]
435+
fn test_remove_unmerged_branch_with_detached_worktree_refuses(mut repo: TestRepo) {
436+
let worktree_path = repo.add_worktree("feature-detached-unmerged");
437+
std::fs::write(worktree_path.join("feature.txt"), "new feature").unwrap();
438+
repo.git_command()
439+
.args(["add", "feature.txt"])
440+
.current_dir(&worktree_path)
441+
.run()
442+
.unwrap();
443+
repo.git_command()
444+
.args(["commit", "-m", "Add feature"])
445+
.current_dir(&worktree_path)
446+
.run()
447+
.unwrap();
448+
repo.detach_head_in_worktree("feature-detached-unmerged");
449+
450+
assert_cmd_snapshot!(make_snapshot_cmd(
451+
&repo,
452+
"remove",
453+
&["feature-detached-unmerged"],
454+
None
455+
));
456+
}
457+
429458
/// A detached worktree whose directory is already gone is stale metadata, not a
430459
/// worktree the #3769 guard protects — the branch-only removal proceeds rather
431460
/// 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+
- feature-detached-unmerged
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+
✗ Branch feature-detached-unmerged has no worktree — the worktree @ _REPO_.feature-detached-unmerged is detached
63+
↳ To remove the detached worktree, run wt remove _REPO_.feature-detached-unmerged

0 commit comments

Comments
 (0)