Skip to content

fix(worktree): seed empty initial commit on unborn HEAD before worktree setup - #675

Open
tarikcosovic97 wants to merge 1 commit into
andresharpe:releases/4.1.0from
tarikcosovic97:fix/659-unborn-repo-autocommit
Open

fix(worktree): seed empty initial commit on unborn HEAD before worktree setup#675
tarikcosovic97 wants to merge 1 commit into
andresharpe:releases/4.1.0from
tarikcosovic97:fix/659-unborn-repo-autocommit

Conversation

@tarikcosovic97

Copy link
Copy Markdown
Contributor

Linked issue

Closes #659

Summary of changes

Starting any workflow in a brand-new project (git init + dotbot init + workflow run) used to surface a scary ⚠ Branch guard warning: Cannot find base branch line on every Git version — and on Git <2.42 also crashed the first worktree add with a cryptic fatal: invalid reference: task/… (because git worktree add --orphan didn't exist yet and the fallback tried to attach to a branch that wasn't there).

The earlier attempt at #664 raised the operating floor to Git 2.42. That's incompatible with Ubuntu 22.04 LTS (Git 2.34.1, supported through May 2027), which is why the reviewer closed it. This PR takes the ticket's originally-preferred path instead: seed the repo with an empty initial commit at worktree-setup time so downstream code has a real branch to attach to, on any Git version.

  • New helper Initialize-UnbornRepositoryForWorktree in Dotbot.Worktree. Runs git commit --allow-empty -m "chore: initial commit" with dotbot@localhost identity overrides so it works on machines where user.name / user.email have never been configured. Reuses whatever branch git already selected via init.defaultBranch — no naming policy imposed. Idempotent: returns created=false when the repo already has commits.
  • Wired into Initialize-DotbotTaskWorktreeForProcess (Invoke-WorkflowProcess.ps1) directly before Assert-OnBaseBranch, so the base-branch resolution, integration-branch creation, and git worktree add -b <task> <path> <base> calls that follow have a real HEAD to reference. The old --orphan code paths become cold code — kept as-is for defense-in-depth.
  • Assert-OnBaseBranch returns $null early on unborn HEAD as defense-in-depth for any caller that bypasses the seed. In the normal flow the seed above means it never sees an unborn repo, but this removes the misleading Cannot find base branch warning from every direct-call site too.

Together these turn the "no commits yet" first-run flow from noisy-and-broken (old Git) or noisy-but-works (new Git) into quiet and correct on any Git ≥2.20 or so — comfortably including the Ubuntu 22.04 default.

Screenshots / recordings

N/A — CLI-only behaviour change.

Testing notes

New assertions land in tests/Test-Worktree.ps1 under a new "Unborn HEAD — auto initial commit + silent Assert" section:

  • Initialize-UnbornRepositoryForWorktreecreated=true on unborn HEAD; reports branch name; HEAD is now valid; commit message is chore: initial commit; one commit exists; second call is idempotent (created=false, no extra commit).
  • Assert-OnBaseBranch — does not throw on unborn HEAD; returns $null; does not create a commit.

Results:

  • tests/Test-Worktree.ps187/87 pass (10 new assertions)
  • tests/Test-WorkflowManifest.ps1538/538 pass (no regression)
  • tests/Test-Runtime.ps1124/124 pass (1 skip, unrelated)

Manual verification on Git 2.53: Initialize-UnbornRepositoryForWorktree seeds a single commit on a fresh git init repo with no configured git identity; subsequent worktree operations proceed without the branch-guard warning.

No prerequisite bump this time — README is unchanged; the fix works on Ubuntu 22.04's default Git 2.34.1.

Checklist

  • Tests added or updated
  • Docs updated (if behaviour changed) — no doc changes needed
  • Linked issue exists
  • Follows the contribution guide

…ee setup

Fresh `git init` + `dotbot init` + workflow run used to produce a scary
`Cannot find base branch` warning on every Git version, and on Git <2.42
also crashed the first `git worktree add` with `fatal: invalid reference`
(because `worktree add --orphan` didn't exist yet, and the fallback tried
to attach to a branch that wasn't there).

Solve both symptoms without a Git version floor — Ubuntu 22.04 LTS ships
Git 2.34.1 through May 2027, so raising the floor as tried in the earlier
attempt (andresharpe#664) locks that platform out.

- Add `Initialize-UnbornRepositoryForWorktree` in Dotbot.Worktree: creates
  an empty commit with `dotbot@localhost` identity overrides on unborn
  HEAD, idempotent, uses the branch git already selected via
  `init.defaultBranch`. Called from `Initialize-DotbotTaskWorktreeForProcess`
  right before `Assert-OnBaseBranch`, so downstream base-branch resolution
  and `git worktree add -b <task> <path> <base>` just work on any Git.
- `Assert-OnBaseBranch` now returns `$null` early on unborn HEAD as
  defense-in-depth for callers that reach it directly. In the normal
  flow the seed above means it never sees an unborn repo.

Closes andresharpe#659

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Inbox

Development

Successfully merging this pull request may close these issues.

2 participants