worktrunk icon indicating copy to clipboard operation
worktrunk copied to clipboard

tab-complete silently fails in repos with no commits

Open max-sixty opened this issue 5 months ago • 5 comments

wt switch <tab> is silent if the default branch has no commits

Could we do a survey throughout the repo to ensure things behave well on repos with no commits?

max-sixty avatar Jun 16 '26 16:06 max-sixty

Root cause of the wt switch <tab> silence: branches_for_completion() sources candidates from local_branches(), which runs for-each-ref refs/heads/. In a freshly git init'd repo nothing exists under refs/heads/ yet (the unborn default branch is only a symbolic-ref target), so the completer returns an empty Vec and the BranchCompleter shortcuts back with no candidates. default_branch() resolves correctly in that state — branches_for_completion() just doesn't ask it.

A fix would be to fall through to the unborn default branch when local_branches() is empty: cheap, localized, and default_branch() already handles unborn HEAD via symbolic-ref HEAD. Whether to surface it as Worktree-categorized (since the primary worktree is checked out on it) or as its own "unborn" category is the only judgement call.

Survey: other commands on a fresh `git init` repo

Built wt at 4ab1b9a3 and ran each against /tmp/empty (git init -b main, no commits). The completion gap is the only durable miss I found — most operations either succeed or print a useful error.

Command Behavior
wt list, wt list --branches, wt list --remotes ✓ shows main with empty commit columns (the test_list_empty_repo regression covers this)
wt switch main / ^ ✓ "Already on worktree for main"
wt switch - ✓ "No previous branch found in history"
wt switch foo (nonexistent) ✓ "No branch named foo" + hint to use --create
wt switch --create feature ✓ creates a linked worktree; new worktree's HEAD is also unborn
wt switch <TAB>, wt remove <TAB> ✗ silent — no candidates (the reported bug; same root cause for both, since remove uses local_branches_completer which also reads local_branches())
wt remove main ✓ "The main worktree cannot be removed"
wt remove nonexistent ✓ "No branch named nonexistent"
wt step prune ✓ "No merged worktrees to remove"
wt step for-each -- <cmd> ✓ runs in main
wt step commit ✓ "Nothing to commit"
wt config show ✓

I didn't drive the interactive picker (wt switch with no arg, wt list --picker) — those are hard to test from the sandbox; flagging in case they want a closer look.

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

Yes, interesting; git branch indeed returns nothing

But git worktree does return things; should we also be getting completion candidates from there?

Running wt switch foo does work, and so it ideally would be reflected in our completion candidates

max-sixty avatar Jun 16 '26 16:06 max-sixty

Agreed — that's the cleaner approach. branches_for_completion() already pulls list_worktrees() but only uses it to re-categorize local-branch entries as Worktree; worktrees whose branch isn't in local_branches() (the unborn case, including a linked one made with wt switch --create on a fresh init) are silently dropped. Emitting a Worktree-category candidate for each such WorktreeInfo with a non-empty branch covers both the primary and any linked unborn worktree, falls out of data that's already in the function, and avoids the special-case "unborn default branch" path I was originally going to thread through.

Notes from re-checking
  • Verified: git worktree add -b feature ../path on a fresh git init -b main produces a 0000000 [feature] row in git worktree list with nothing under refs/heads/. Same for the primary main row. So list_worktrees() already has what completion needs.
  • wt switch <name> resolves these correctly today (tested both wt switch main and wt switch feature after wt switch --create feature on an empty repo). Completion is the only gap.
  • Open detail for the fix: WorktreeInfo doesn't carry a committer timestamp (the branch is unborn). For sort order within the Worktree category, simplest is to give unborn-derived entries i64::MAX (sort to the top — the user just created them) or 0 (sort to the bottom). I'd lean toward the top, but either works since in the empty-repo case there are no timestamped peers to interleave with.

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

ok, please implement

max-sixty avatar Jun 16 '26 17:06 max-sixty

Opened #3097.

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