Skip to content

fix: clear error when adding worktree on unborn repo - #45

Merged
sebasv merged 3 commits into
mainfrom
fix/unborn-head-worktree
May 4, 2026
Merged

fix: clear error when adding worktree on unborn repo#45
sebasv merged 3 commits into
mainfrom
fix/unborn-head-worktree

Conversation

@sebasv

@sebasv sebasv commented May 4, 2026

Copy link
Copy Markdown
Owner

Summary

  • Detects unborn HEAD (a freshly git init'd repo with no commits) before invoking git worktree add, and bails with "repo has no commits yet — make an initial commit before adding worktrees" instead of letting git fail with fatal: invalid reference: main.
  • Adds git::is_unborn_head helper plus three unit tests covering: unborn repo, repo after first commit, non-repo path.

Why

Reported in #44: on a brand-new project (mkdir x && cd x && git init . && grove .), pressing w to create a worktree fails with a cryptic ! git worktree add -b ... error and the only option is Cancel.

Root cause: detect_default_branch returns None when there's no origin/HEAD, so base_branch falls back to the configured default ("main"). On an unborn repo, main has no commit, so git worktree add -b <branch> <path> main aborts with fatal: invalid reference: main. The fix surfaces this as an actionable in-modal error so the user knows what to do (make the first commit) rather than staring at raw git output.

Closes #44

Test plan

  • cargo test --bin grove — 144 tests pass
  • cargo fmt --check, cargo clippy --all-targets -- -D warnings clean
  • Manual: mkdir /tmp/grove-test && cd /tmp/grove-test && git init . && grove ., press w, type a branch name, hit Enter — confirm the new error message appears in the modal
  • Manual: existing repos with commits still create worktrees normally

🤖 Generated with Claude Code

sebasv and others added 3 commits May 4, 2026 11:37
`git worktree add -b <branch> <path> main` aborts with `fatal: invalid
reference: main` when the repo has no commits yet, leaving the user with
the cryptic raw-git error and no path forward beyond Cancel. Detect the
unborn HEAD up front and bail with an actionable message instead.

Closes #44

Co-Authored-By: Claude <noreply@anthropic.com>
The error row was a single, unwrapped `Paragraph`, so the unborn-HEAD
message added in the previous commit (and any future error longer than
the modal's inner width) spilled past the right border. Allocate two
rows for the error, render with `Wrap { trim: false }`, and shorten the
unborn-HEAD message so it fits on one line in the common case.

Adds a snapshot test that renders an intentionally over-wide error to
lock the wrapped layout in. Documents the verification step in agents.md
so future TUI changes don't reintroduce the overflow.

Co-Authored-By: Claude <noreply@anthropic.com>
libgit2's `is_empty` / `head` returned `false` on Linux CI for repos
that had just been `git init`'d with no commits, so the unit test
asserting `is_unborn_head` is `true` for a freshly-initialised repo
failed there. macOS libgit2 reported them correctly. Shelling out to
`git rev-parse --verify HEAD` sidesteps the platform difference and
matches how the rest of git.rs talks to git.

Co-Authored-By: Claude <noreply@anthropic.com>
@sebasv
sebasv merged commit d0f5e23 into main May 4, 2026
1 check passed
@sebasv
sebasv deleted the fix/unborn-head-worktree branch May 4, 2026 10:42
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.

Cannot create worktree on new project

1 participant