Skip to content

[Feat] Client initialization cache and batch remove#63

Merged
samzong merged 2 commits intomainfrom
feat/client-cache
Apr 3, 2026
Merged

[Feat] Client initialization cache and batch remove#63
samzong merged 2 commits intomainfrom
feat/client-cache

Conversation

@samzong
Copy link
Copy Markdown
Owner

@samzong samzong commented Apr 3, 2026

What's changed?

  • Add sync.Once-based lazy init to Client struct caching bareRoot, worktreeRoot, searchRoot, repoDir — resolved once per CLI invocation
  • Add ListCached() / InvalidateList() for worktree list caching with explicit invalidation after mutations (Add, Remove, Dup, AddPR, Prune)
  • Add RemoveBatch() with fail-fast validation, sequential removal, and batched branch -D
  • Wire runWorktreeRemove in cmd layer to use RemoveBatch
  • Refactor all public methods (Sync, Prune, Promote, AddPR, etc.) to use cached fields instead of per-call resolution
  • Return defensive copy from ListCached() to prevent cache corruption
  • Extract resolveRemoveTarget helper shared by prepareRemove and RemoveBatch

Why

  • Client methods repeatedly compute values (FindBareRoot, GetWorktreeRoot, List()) that are constant within a single command lifecycle
  • Measured ~45% reduction in redundant git subprocesses for batch operations (wt rm --all, wt add a b c, wt dup)
  • RemoveBatch validates all names upfront (fail-fast for typos) and batches branch deletion at the end

Copy link
Copy Markdown

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces a caching mechanism for the git worktree list and centralizes repository initialization logic within the Client struct to improve performance and consistency. It also adds a RemoveBatch method for efficient multi-worktree removal and updates various operations to utilize the new cached state. Review feedback suggests further performance optimizations, specifically batching the git branch -D command and using internal synchronization methods to avoid redundant cache invalidations and git subprocess calls during worktree creation.

Comment thread internal/worktree/worktree.go Outdated
Comment thread internal/worktree/worktree.go Outdated
Comment thread internal/worktree/worktree.go Outdated
Comment thread internal/worktree/pr.go Outdated
@samzong samzong merged commit 2221112 into main Apr 3, 2026
1 check passed
samzong added a commit that referenced this pull request Apr 17, 2026
Fix 7 new lint violations from #63 and #64:
- lll: break long lines in worktree.go, worktree_test.go, prune.go, resource.go
- perfsprint: use errors.New instead of fmt.Errorf for static strings in prune.go
- gocritic/unlambda: simplify exec.Command wrapper in worktree_test.go

Signed-off-by: samzong <[email protected]>
samzong added a commit that referenced this pull request Apr 17, 2026
* fix: resolve lint issues introduced by protection and cache PRs

Fix 7 new lint violations from #63 and #64:
- lll: break long lines in worktree.go, worktree_test.go, prune.go, resource.go
- perfsprint: use errors.New instead of fmt.Errorf for static strings in prune.go
- gocritic/unlambda: simplify exec.Command wrapper in worktree_test.go

Signed-off-by: samzong <[email protected]>

* fix(wt): derive worktree name from -b when no name given

- Allow `gmc wt add -b <branch>` without positional args; worktree name defaults to the base branch value.

- Split the `-b/--base` flag variable between `wt add` and `wt dup` so dup's "main" default no longer pollutes add at init time.

## Considered and deferred

- cmd/worktree.go [BOT-NIT]: wtBaseBranch could be renamed to wtAddBase for symmetry with wtDupBase / wtPruneBase. Deferred — not a behavior concern.
- cmd/worktree.go [BOT-SCOPE]: No cmd-level test for the new zero-arg + -b path. Deferred for a follow-up test-only PR.

Signed-off-by: samzong <[email protected]>

---------

Signed-off-by: samzong <[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.

1 participant