fix(server): actionable error when creating a worktree in a commit-less repo#4183
fix(server): actionable error when creating a worktree in a commit-less repo#4183sideeffffect wants to merge 1 commit into
Conversation
|
Important Review skippedAuto reviews are disabled on this repository. Please check the settings in the CodeRabbit UI or the ⚙️ Run configurationConfiguration used: Repository UI Review profile: CHILL Plan: Pro Run ID: You can disable this status message by setting the Use the checkbox below for a quick retry:
✨ Finishing Touches🧪 Generate unit tests (beta)
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. Comment |
ApprovabilityVerdict: Approved Straightforward UX improvement that adds early validation when creating a worktree in a commit-less repository, returning an actionable error message instead of a cryptic git failure. Normal-path behavior is unchanged, scope is limited, and test coverage is included. You can customize Macroscope's approvability policy. Learn more. |
…ss repo git worktree add cannot branch from an unborn HEAD, so starting a thread in New worktree mode inside a freshly-initialized repository (no commits yet) failed with a cryptic invalid-reference error. Detect the missing base commit up front and fail with actionable guidance to make an initial commit or use the current checkout instead. Co-authored-by: codex <codex@users.noreply.github.com>
97c388f to
9c53228
Compare
What Changed
GitVcsDriver.createWorktreenow verifies that the base ref actually points at a commit before runninggit worktree add. When it doesn't (an unbornHEAD), it fails with a clear, actionableGitCommandError:Added an integration test covering a freshly-
git init'd repo with no commits.Why
Starting a thread in New worktree mode inside a repository that has no commits yet (e.g. a freshly
git init'd directory, or a project nested under a barely-initialized parent repo) failed to start the agent.git worktree add -b <tmp> <path> <base>cannot branch from an unbornHEAD, so git aborts with a crypticfatal: invalid reference: <ref>that surfaced to the user with no guidance on how to proceed.A worktree fundamentally cannot be created from a repo with no commits, so this detects the condition up front and explains what to do instead (make an initial commit, or use the current checkout). Scope is limited to the pre-flight check plus its error message; healthy repos are unaffected — the extra
git rev-parse --verify --quiet <ref>^{commit}resolves and creation proceeds exactly as before.Note: an alternative would be to silently fall back to the current checkout in this case. I went with an explicit, actionable error to avoid changing the meaning of an explicit "New worktree" selection — happy to switch to a fallback if you'd prefer.
UI Changes
None.
Checklist
Note
Low Risk
Small, localized guard on worktree creation with clearer errors; normal repos only pay one extra git call on the existing path.
Overview
New worktree creation in repos with no commits (unborn
HEAD) used to fail with git’s opaquefatal: invalid referencewhen starting a thread.GitVcsDriverCore.createWorktreenow runsrev-parse --verify --quiet <ref>^{commit}beforegit worktree add; if the ref resolves to nothing, it returns aGitCommandErrortelling the user to make an initial commit or use the current checkout instead of running worktree add.Repos that already have commits behave the same after the extra rev-parse. An integration test covers
initwith no commits: expects the new error text, and confirms no worktree directory is left behind.Reviewed by Cursor Bugbot for commit 9c53228. Bugbot is set up for automated code reviews on this repo. Configure here.
Note
Return actionable error from
createWorktreewhen base ref has no commitWhen creating a worktree from a branch with an unborn HEAD (e.g. a repo with no commits),
GitVcsDriverCore.createWorktreenow runsrev-parse --verify --quiet <ref>^{commit}before invokinggit worktree add. If the ref resolves to nothing, it returns aGitCommandErrorwith a message explaining the ref has no commit, rather than letting the git command fail with an opaque error.Macroscope summarized 9c53228.