worktrunk icon indicating copy to clipboard operation
worktrunk copied to clipboard

fix(completion): surface unborn worktree branches

Open worktrunk-bot opened this issue 5 months ago • 0 comments

Problem

wt switch <TAB> and wt remove <TAB> were silent in repos with no commits. branches_for_completion() sourced its candidates from local_branches(), which runs for-each-ref refs/heads/. On a fresh git init -b main repo, nothing exists under refs/heads/ yet — the unborn default branch is only a symbolic-ref target — so the scan returned an empty Vec and the completer shortcut back with no candidates.

Closes #3094.

Solution

After the existing local-branches pass, iterate list_worktrees() and emit a Worktree-category candidate for each worktree whose branch isn't already in local_names. That covers:

  • The primary unborn worktree (e.g. main on a fresh git init -b main).
  • Any linked worktree made by wt switch --create on the same empty repo (also unborn, also absent from refs/heads/).

Unborn entries are pinned to i64::MAX so they sort to the top of the Worktree category — there's no commit timestamp to interleave with, and the user typically just created them. On a born repo every worktree's branch is also in local_names, so the new loop is a no-op and existing completion order is preserved.

Testing

  • Two new unit tests in src/git/repository/branches.rs:
    • branches_for_completion_includes_unborn_default_branch — empty repo returns [main] as a Worktree candidate.
    • branches_for_completion_includes_unborn_linked_worktree — after git worktree add -b feature ../path on a still-unborn repo, both main and feature surface.
  • Updated test_complete_empty_repo (integration): the prior assertion encoded the bug ("only option suggestions"); it now asserts main is a candidate.
  • End-to-end sanity check: wt switch <TAB> against a fresh git init -b main returns main.
  • All 57 completion integration tests and 151 remove integration tests pass locally.

worktrunk-bot avatar Jun 16 '26 17:06 worktrunk-bot