opencode icon indicating copy to clipboard operation
opencode copied to clipboard

fix: create worktrees from empty git repos

Open wgu9 opened this issue 5 months ago • 0 comments

Issue for this PR

Fixes #20910

Related: #32341

Type of change

  • [x] 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

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

wgu9 avatar Jun 15 '26 02:06 wgu9