Skip to content

fix(server): actionable error when creating a worktree in a commit-less repo#4183

Open
sideeffffect wants to merge 1 commit into
pingdotgg:mainfrom
sideeffffect:fix/worktree-mode-no-base
Open

fix(server): actionable error when creating a worktree in a commit-less repo#4183
sideeffffect wants to merge 1 commit into
pingdotgg:mainfrom
sideeffffect:fix/worktree-mode-no-base

Conversation

@sideeffffect

@sideeffffect sideeffffect commented Jul 20, 2026

Copy link
Copy Markdown

What Changed

GitVcsDriver.createWorktree now verifies that the base ref actually points at a commit before running git worktree add. When it doesn't (an unborn HEAD), it fails with a clear, actionable GitCommandError:

Cannot create a worktree from '<ref>' because it does not point at any commit yet. Make an initial commit in this repository, or start the thread in the current checkout instead.

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 unborn HEAD, so git aborts with a cryptic fatal: 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

  • This PR is small and focused
  • I explained what changed and why
  • I included before/after screenshots for any UI changes (N/A)
  • I included a video for animation/interaction changes (N/A)

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 opaque fatal: invalid reference when starting a thread. GitVcsDriverCore.createWorktree now runs rev-parse --verify --quiet <ref>^{commit} before git worktree add; if the ref resolves to nothing, it returns a GitCommandError telling 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 init with 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 createWorktree when base ref has no commit

When creating a worktree from a branch with an unborn HEAD (e.g. a repo with no commits), GitVcsDriverCore.createWorktree now runs rev-parse --verify --quiet <ref>^{commit} before invoking git worktree add. If the ref resolves to nothing, it returns a GitCommandError with a message explaining the ref has no commit, rather than letting the git command fail with an opaque error.

Macroscope summarized 9c53228.

@coderabbitai

coderabbitai Bot commented Jul 20, 2026

Copy link
Copy Markdown

Important

Review skipped

Auto reviews are disabled on this repository. Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro

Run ID: b18d34b5-d95a-4bfb-925a-44c7df862065

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • 🔍 Trigger review
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

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.

@github-actions github-actions Bot added vouch:unvouched PR author is not yet trusted in the VOUCHED list. size:S 10-29 changed lines (additions + deletions). labels Jul 20, 2026
@macroscopeapp

macroscopeapp Bot commented Jul 20, 2026

Copy link
Copy Markdown
Contributor

Approvability

Verdict: 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>
@juliusmarminge
juliusmarminge force-pushed the fix/worktree-mode-no-base branch from 97c388f to 9c53228 Compare July 20, 2026 16:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size:S 10-29 changed lines (additions + deletions). vouch:unvouched PR author is not yet trusted in the VOUCHED list.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant