Skip to content

fix: create worktrees from empty git repos#32367

Open
wgu9 wants to merge 1 commit into
anomalyco:devfrom
wgu9:wgu9/fix-empty-repo-worktree
Open

fix: create worktrees from empty git repos#32367
wgu9 wants to merge 1 commit into
anomalyco:devfrom
wgu9:wgu9/fix-empty-repo-worktree

Conversation

@wgu9

@wgu9 wgu9 commented Jun 15, 2026

Copy link
Copy Markdown

Issue for this PR

Fixes #20910

Related: #32341

Type of change

  • Bug fix
  • New feature
  • Refactor / code improvement
  • Documentation

What does this PR do?

Creating an opencode worktree from a git repo with no commits currently fails because git worktree add infers --orphan, while opencode also passes --no-checkout. Git rejects that combination.

This PR keeps the existing --no-checkout path for normal repositories with a valid HEAD, but omits it when the source repository has an unborn HEAD. That lets Git create the orphan worktree branch for empty repos. It also treats a missing branch during cleanup as already clean, which can happen after removing an unborn worktree branch.

The covered cases are:

  • empty git repo with no commits can create a named worktree
  • normal worktree create/list/remove lifecycle still passes
  • detached worktree creation from a normal repo still passes
  • remove still preserves existing non-empty worktree behavior
  • missing branch cleanup after an unborn worktree does not turn successful removal into an error

How did you verify your code works?

  • bun test test/project/worktree.test.ts
  • bun test test/project/worktree-remove.test.ts
  • bunx oxlint packages/opencode/src/worktree/index.ts packages/opencode/test/project/worktree.test.ts
  • git diff --check

Screenshots / recordings

Not applicable. This is git worktree lifecycle behavior covered by regression tests.

Checklist

  • I have tested my changes locally
  • I have not included unrelated changes in this PR

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.

[BUG]: application can't handle git repos with no head

1 participant