Skip to content

refactor(repo): return &[WorktreeInfo] from list_worktrees - #2383

Merged
max-sixty merged 1 commit into
mainfrom
list-worktrees-slice
Apr 22, 2026
Merged

max-sixty merged 1 commit into
mainfrom
list-worktrees-slice

Conversation

@max-sixty

Copy link
Copy Markdown
Owner

Follows #2375. That PR cached list_worktrees on RepoCache but kept the Vec<WorktreeInfo> return type (cloning from the cache on each call) to minimize churn — explicitly deferring this signature change. The sibling inventories local_branches and remote_branches already return &[T]; this brings list_worktrees in line and eliminates the per-call clones.

The implementation mirrors remote_branches in src/git/repository/branches.rs: get_or_try_init(...).map(Vec::as_slice).

Call-site migration is mostly mechanical — slice ops (.iter(), [0], .is_empty(), .len()) work unchanged. Callers that need ownership switch from .into_iter() to .iter().cloned(); one call to sort_worktrees_with_cache uses .to_vec(). In list/collect, the old OnceCell<Result<Vec<_>>> carrier inside the rayon scope is gone — the scope just warms repo.list_worktrees(), which already caches on RepoCache, and the value is re-read after the scope. In repository_ext::prepare_worktree_removal, the owned_worktrees scoped-local dance collapses to a single match binding. Also deletes unused Repository::current_worktree_info() (zero callers, noticed while reviewing the diff).

Subprocess count for wt switch is unchanged — still 1 git worktree list call per command, confirmed via WORKTRUNK_PICKER_DRY_RUN=1 RUST_LOG=worktrunk=debug wt switch.

This was written by Claude Code on behalf of @max-sixty

Mirrors the canonical shape of `local_branches`/`remote_branches`
(`get_or_try_init(...).map(Vec::as_slice)`) and drops the per-call clone
the cache introduced in #2375 deferred. Callers that need ownership use
`.iter().cloned()` / `.to_vec()`; callers that only iterate or index
work against the slice unchanged.

Side cleanups falling out of the migration:

- `prepare_worktree_removal` collapses its `owned_worktrees` local into
  a single match binding.
- `collect::collect` drops the `OnceCell<Result<Vec<_>>>` carrier in
  the rayon scope — the scope just warms `repo.list_worktrees()` now
  that the result is cached on `RepoCache` itself.
- Deletes unused `Repository::current_worktree_info()` (zero callers).

Co-Authored-By: Claude <[email protected]>
@max-sixty
max-sixty merged commit b9a5db7 into main Apr 22, 2026
25 checks passed
@max-sixty
max-sixty deleted the list-worktrees-slice branch April 22, 2026 03:22
max-sixty added a commit that referenced this pull request Apr 22, 2026
…-each (#2389)

Two small follow-ups to #2383.

`sort_worktrees_with_cache` takes `&[WorktreeInfo]` instead of an owned
`Vec<WorktreeInfo>`, cloning once when materializing the sorted output.
The call site in `collect::collect` drops its `.to_vec()` — one fewer
`Vec<WorktreeInfo>` allocation on the `wt list` hot path.

`step_for_each` holds `Vec<&WorktreeInfo>` rather than cloning each
entry into a `Vec<WorktreeInfo>` — the loop only borrows
(`worktree_display_name(wt, ...)`, `execute_shell_command(&wt.path,
...)`), so the clone was unnecessary.

No behavior change.

> _This was written by Claude Code on behalf of @max-sixty_

Co-authored-by: Claude <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants