Skip to content

Fix libgit2 conflict when a branch is already checked out elsewhere - #155

Merged
soorya-u merged 2 commits into
mainfrom
fix/144-worktree-branch-checked-out-elsewhere
Aug 6, 2026
Merged

Fix libgit2 conflict when a branch is already checked out elsewhere#155
soorya-u merged 2 commits into
mainfrom
fix/144-worktree-branch-checked-out-elsewhere

Conversation

@soorya-u

@soorya-u soorya-u commented Aug 5, 2026

Copy link
Copy Markdown
Owner

Summary

  • createGitWorktree now forks a fresh branch (named via generateName()) off the requested ref's commit and checks that new branch into the worktree, instead of attaching the literally requested branch — so it never collides with a branch already checked out in the main repo or another linked worktree.
  • checkoutGitRef still switches to the literal requested branch in place, but now pre-checks via a new findBranchCheckedOutElsewhere helper and returns a domain GitBranchCheckedOutError/branchAlreadyCheckedOutError instead of a raw libgit2 error, while treating "already on that branch here" as a no-op success.
  • If a later step in createGitWorktree fails (path validation, mkdir, or the worktree() call), the freshly created branch is cleaned up rather than left dangling.

Fixes #144

Test plan

  • bun test:unit (Vitest workspace + CLI Bun suite) — all pass
  • bun run check:types — all packages clean
  • New tests in apps/cli/src/git/git.test.ts covering: worktree creation forks a new branch, worktree creation succeeds even when the source branch is checked out in the main repo, checkout is a no-op when already on the requested branch, checkout returns GitBranchCheckedOutError when the branch is checked out in another worktree

🤖 Generated with Claude Code

…144)

createGitWorktree now forks a fresh branch (named via generateName())
off the requested ref instead of checking out that ref directly, so a
new worktree never collides with a branch already checked out in the
main repo or another linked worktree. checkoutGitRef keeps the literal
in-place checkout but now pre-checks via findBranchCheckedOutElsewhere
and returns a branchAlreadyCheckedOutError instead of a raw libgit2
error, while treating "already on that branch here" as a no-op.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
@vercel

vercel Bot commented Aug 5, 2026

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
cyrus Ready Ready Preview Aug 6, 2026 6:36am

@coderabbitai

coderabbitai Bot commented Aug 5, 2026

Copy link
Copy Markdown

Warning

Review limit reached

@soorya-u, you've reached your PR review limit, so we couldn't start this review.

Next review available in: 19 minutes

You've used all free OSS reviews for now. Wait for the free limit to reset to keep reviewing this public repository.

How can I continue?

After more reviews become available, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

To avoid repeated limits, reduce automatic review volume by pausing incremental auto-reviews earlier, using label-based review opt-in, excluding WIP or generated PR titles, or requesting reviews manually when the PR is ready. If your team needs uninterrupted high-volume reviews, an organization admin can enable usage-based reviews.

How do review limits work?

CodeRabbit enforces per-developer PR review limits for each organization. Most developers receive the normal plan review availability.

For paid Pro and Pro+ PR reviews, CodeRabbit uses adaptive limits for sustained high-volume activity. When a developer's recent PR review activity reaches the 95th percentile or higher among CodeRabbit users, additional reviews become available more gradually as earlier reviews age out of the rolling window.

Please refer docs for additional details.

Review details
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro Plus

Run ID: 7f4110fd-9533-48d3-87cd-62ada5142b41

📥 Commits

Reviewing files that changed from the base of the PR and between bcad6d1 and ae7e070.

📒 Files selected for processing (4)
  • apps/cli/src/git/checkout.ts
  • apps/cli/src/git/git.test.ts
  • apps/cli/src/git/worktree.ts
  • shared/errors/src/git.ts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

findBranchCheckedOutElsewhere only had one caller (checkoutGitRef),
so fold it and its helper back into checkout.ts instead of keeping a
separate single-use module.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
@soorya-u
soorya-u merged commit 1a3ca5c into main Aug 6, 2026
9 checks passed
@soorya-u
soorya-u deleted the fix/144-worktree-branch-checked-out-elsewhere branch August 6, 2026 06:44
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.

libgit2 error creating worktree/checkout for a branch already checked out elsewhere

1 participant