Skip to content

Detect bare repositories as worktree sources - #2158

Merged
SawyerHood merged 1 commit into
mainfrom
bb/fix-1595-bare-repo-worktree-detection
Aug 21, 2026
Merged

SawyerHood merged 1 commit into
mainfrom
bb/fix-1595-bare-repo-worktree-detection

Conversation

@SawyerHood

@SawyerHood SawyerHood commented Aug 21, 2026 •

Copy link
Copy Markdown
Collaborator

What was wrong

detectGitRepo() in packages/host-workspace/src/git.ts asked git rev-parse --is-inside-work-tree and treated false as "not a git repository". A bare repository answers false with exit code 0. The "bare clone + sibling worktrees" layout from #1595 (<root>/.bare bare clone, <root>/.git file containing gitdir: ./.bare, one sibling directory per worktree) therefore read as a non-git path everywhere that helper was used as a gate: host.list_branches returned checkout.kind: "unknown" with no branches, the app disabled "New worktree" in the environment picker, and bb thread spawn --new-environment worktree failed in createWorktree with not_git_repo ("Cannot create a worktree because the source is not a Git repository").

Two notions were conflated in one boolean: "this path is a git repository and can seed worktrees / list refs" (a bare repo qualifies) and "this path is a checkout that can be diffed, committed, and switched" (it cannot).

Issue: #1595. Report: https://get-bb.github.io/reports/issues/1595.html

What changed

packages/host-workspace/src/git.ts

  • New detectGitRepoKind(cwd) returns "work-tree" | "bare" | "none" from a single git rev-parse --is-inside-work-tree --is-bare-repository.
  • detectGitRepo() keeps work-tree semantics (now implemented via the kind helper). provision.ts and Workspace still use it, so a "Work locally" thread at a bare root keeps isGitRepo: false and gets no status/diff/commit UI.
  • New detectGitSource() is true for a checkout or a bare repository. It now gates ensureGitRepo, readGitRepositoryState (feeds createWorktree), and getCheckoutRef.

apps/host-daemon/src/command-handlers/host-branches.ts

  • listHostBranches classifies the path once, returns the empty payload only for "none", and skips the two work-tree-only probes (hasUncommittedChanges, getWorkspaceGitOperation) for a bare source, reporting false / { kind: "none" } instead of running git status where there is no work tree.
  • host.list_branch_options is unchanged: it is called with environment (checkout) paths.

No wire shape changes, so HOST_DAEMON_PROTOCOL_VERSION is unchanged. No CLI or docs surfaces change. The report's prototype matched this shape; I kept ensureGitRepo widened (its message already says "not a git repository") and moved the bare special-casing into the one handler that needs it instead of into the two primitives.

Not in scope: the issue's second half ("Existing worktree" lists only bb environments, not git worktree list) is a feature request tracked in #1624.

How you verified

New tests (5 in @bb/host-workspace, 1 in @bb/host-daemon) build the layout from the issue in a temp dir. With the source files checked out from origin/main they fail:

FAIL test/git.test.ts > readGitRepositoryState > treats a bare repository root as a repository with commits
AssertionError: expected 'not_git' to be 'has_commits'
FAIL test/git.test.ts > getCheckoutRef > reports the HEAD branch of a bare repository root
AssertionError: expected { kind: 'unknown', …(1) } to deeply equal { kind: 'branch', …(2) }
FAIL test/provisioning.test.ts > creates worktrees from a bare repository root that holds sibling worktrees
Caused by: WorkspaceError: Cannot create a worktree because the source is not a Git repository: … { code: 'not_git_repo' }
FAIL test/command/host-branches-dispatch.test.ts > lists branches for a bare repository root that holds sibling worktrees
AssertionError: expected { kind: 'unknown', …(1) } to match object { kind: 'branch', …(1) }

(detectGitRepoKind test fails with TypeError: detectGitRepoKind is not a function there.) All pass on this branch.

Run from the committed tree (git status --porcelain empty):

  • pnpm exec turbo run typecheck --filter=@bb/host-workspace --filter=@bb/host-daemon → Tasks: 5 successful, 5 total
  • pnpm exec turbo run test --filter=@bb/host-workspace --filter=@bb/host-daemon --force → Tasks: 6 successful, 6 total (host-workspace 214 passed, host-daemon 552 passed)

Manual, against my own dev instance with the rebuilt daemon, project source = a bare layout built exactly like the issue (cosmos/.bare, cosmos/.git = gitdir: ./.bare, worktrees feature-a, feature-b):

  • GET /api/v1/projects/:id/branches now returns {"branches":["main","feature-a","feature-b"],"checkout":{"kind":"branch","branchName":"main","headSha":"6d25f68…"},"defaultBranch":"main","hasUncommittedChanges":false,"operation":{"kind":"none"},…,"defaultWorktreeBaseBranch":"main"} (on main: checkout.kind: "unknown", branches: []).
  • bb thread spawn --new-environment worktree --provider codex --prompt "Reply only with ok." provisioned (Created worktree, Using branch: bb/bare-wt-thr_…) and the assistant answered ok; git worktree list in the bare root shows the new bb worktree next to feature-a and feature-b.

Fixes #1595

AGENT GENERATED: by Claude Opus 5

Independent verification

Verified by a separate agent in its own worktree (git fetch origin bb/fix-1595-bare-repo-worktree-detection && git checkout -b verify-1595-r1 FETCH_HEAD, head ec7fd0665, origin/main is an ancestor, mergeable: MERGEABLE).

Fail-before: with apps/host-daemon/src/command-handlers/host-branches.ts, packages/host-workspace/src/git.ts, and packages/host-workspace/src/index.ts checked out from origin/main:

  • pnpm exec vitest run test/git.test.ts test/provisioning.test.ts in packages/host-workspace: 4 failed / 60 passed. Assertions: TypeError: detectGitRepoKind is not a function; AssertionError: expected 'not_git' to be 'has_commits'; AssertionError: expected { kind: 'unknown', …(1) } to deeply equal { kind: 'branch', …(2) }; promise rejected "WorkspaceError: Cannot create a worktree …" { code: 'not_git_repo' }.
  • pnpm exec vitest run test/command/host-branches-dispatch.test.ts in apps/host-daemon: 1 failed / 13 passed. AssertionError: expected { kind: 'unknown', …(1) } to match object { kind: 'branch', …(1) }.

Pass-after (files restored, git status --porcelain empty): the same runs give 64/64 and 14/14 passed.

  • pnpm exec turbo run typecheck --filter=@bb/host-workspace --filter=@bb/host-daemon --filter=@bb/server: Tasks: 6 successful, 6 total.
  • pnpm exec turbo run test --filter=@bb/host-workspace --filter=@bb/host-daemon --force: Tasks: 6 successful, 6 total (host-workspace 214 passed, host-daemon 552 passed).

Repro on the fixed branch (own dev instance, ports 12936/20936/28936): built the issue's layout (<root>/.bare bare clone, <root>/.git = gitdir: ./.bare, sibling worktrees feature-a, feature-b; git rev-parse --is-inside-work-tree --is-bare-repository prints false / true), registered it as a local_path project. GET /api/v1/projects/:id/branches?hostId=... returned branches: ["main","feature-a","feature-b"], checkout: {kind: "branch", branchName: "main"}, hasUncommittedChanges: false, operation: {kind: "none"}, defaultWorktreeBaseBranch: "main". bb thread spawn --new-environment worktree --provider codex --prompt "Reply only with ok." provisioned (Created worktree, Using branch: bb/bare-wt-thr_…), git worktree list in the bare root shows the new bb worktree next to feature-a/feature-b, and the thread answered ok. No longer reproduces.

CI at verification time: all Linux checks, tests, and package smoke green; macOS package smoke pending.

Residual risks reviewed: ensureGitRepo now admits bare paths, but every daemon workspace command is gated by requireGit on workspace.isGitRepo (apps/host-daemon/src/workspace-resolution.ts), which still comes from the work-tree-only detectGitRepo, so Workspace status/diff/commit cannot reach a bare path. host.list_branch_options keeps the work-tree gate, which is correct because its callers are environment-scoped. No wire shape changed, so no HOST_DAEMON_PROTOCOL_VERSION bump is needed. The .worktreeinclude copy would fail git ls-files at a bare root, but copyIncludedFiles already reports that in the transcript without failing provisioning.

AGENT GENERATED: by Claude Opus 5

detectGitRepo() asked git --is-inside-work-tree, which answers false with
exit 0 for a bare repository. The bare-clone-plus-sibling-worktrees layout
(<root>/.bare, <root>/.git gitdir file) therefore read as 'not a git
repository': host.list_branches returned checkout.kind unknown, the app
disabled New worktree, and createWorktree failed with not_git_repo.

Split the two notions. detectGitRepoKind() classifies a path as work-tree,
bare, or none from a single rev-parse. detectGitRepo() keeps work-tree
semantics so a bare root never gets status/diff/commit UI as a workspace.
detectGitSource() (work tree or bare) now gates ensureGitRepo,
readGitRepositoryState, and getCheckoutRef, and listHostBranches skips
the work-tree-only dirty/operation probes for a bare source.

No wire shapes change, so HOST_DAEMON_PROTOCOL_VERSION stays.

Fixes #1595

Co-Authored-By: Claude <[email protected]>
@SawyerHood
SawyerHood marked this pull request as ready for review August 21, 2026 06:19
@SawyerHood
SawyerHood merged commit 0880d11 into main Aug 21, 2026
13 checks passed
@SawyerHood
SawyerHood deleted the bb/fix-1595-bare-repo-worktree-detection branch August 21, 2026 14:38
ymichael added a commit that referenced this pull request Aug 21, 2026
## What was wrong

PR #2158's bare-repository provisioning regression test compared `git
worktree list --porcelain` with the lexical temp path passed to
`createWorktree`. On macOS, `os.tmpdir()` yields `/tmp` while Git
canonicalizes that symlink to `/private/tmp`, so the feature assertions
pass but the package suite fails on path spelling.

## What changed

`packages/host-workspace/test/provisioning.test.ts` now canonicalizes
the created target with `fs.realpath()` before comparing it with Git
porcelain, and compares exact output lines.

This is test-only. No production behavior, CLI/guide/docs, SDK/plugin
API, or host-daemon wire shape changed; `HOST_DAEMON_PROTOCOL_VERSION`
remains unchanged.

## How you verified

- Before the change, the required macOS Turbo suite failed with 213
passed / 1 failed.
- The focused bare-worktree regression passes after the change.
- The full `@bb/host-workspace` Turbo suite passes with 214/214 tests.
- The package Turbo typecheck passes; the package has no Turbo build
task.
- `git diff --check` passes.

Follow-up to #2158.

> AGENT GENERATED: by GPT-5.6-Sol
SawyerHood added a commit that referenced this pull request Aug 21, 2026
## What was wrong

`bb thread spawn --new-environment worktree --base-branch main` sent the
plain name `main` straight through the server to the host daemon. The
server's local-vs-origin resolution
(`resolveManagedDefaultBaseBranchForCreate`) returned early for any `{
kind: "named" }` spec, and the daemon's `createWorktree` only fetches
remote-qualified bases (`origin/main`) before `git worktree add`. Git
resolved the bare `main` to the local `refs/heads/main`, so a project
checkout that had not been pulled seeded every spawned worktree from the
stale commit, with no fetch and no warning. Omitting the flag or passing
`origin/main` was already fresh; the plain name was the one input that
got neither treatment.

Issue: #1770. Report: https://get-bb.github.io/reports/issues/1770.html

## What changed

- `apps/server/src/services/projects/worktree-base-branch.ts`: new
`resolveManagedNamedBaseBranchSpec`. When the named branch is the
checkout's default branch, it applies the same policy the default spec
uses: `origin/<default>` when local is equal or behind, the local branch
when it is ahead, diverged, or unknown. Any other plain name is returned
unchanged.
- `apps/server/src/services/threads/thread-create.ts`:
`resolveManagedDefaultBaseBranchForCreate` is now
`resolveManagedBaseBranchForCreate`. It calls `host.list_branches` for
named specs too (that RPC already runs a throttled `git fetch --all
--prune` on the daemon) and routes the result through the named or
default resolver. A fork (`originKind !== null`) keeps its source
environment's branch verbatim and makes no RPC, since it continues the
conversation from the branch the source is on.
- The resolved spec is stored on the environment as before, so `bb
thread show` now reports `baseBranch: "origin/main"` for a spawn that
was redirected, and the provisioning transcript shows the `Fetching
origin/main` step.
- Docs: `docs/worktrees.md` and the `--base-branch` help text in
`apps/cli/src/commands/thread/spawn.ts` now say what a plain name
resolves to.
- Test helper: `registerTestHostRpcCapture` accepts an optional
`listBranchesResult` so a route test can set the default-branch relation
without swapping the capture socket.

No wire change: `host.list_branches` and `environment.provision` are
unchanged, so no `HOST_DAEMON_PROTOCOL_VERSION` bump. This is
server-only policy, per the server/daemon split.

Deviation from the report's proposals: I did not add upstream tracking
for arbitrary non-default names. The daemon only reports the relation
for the default branch; doing it for any branch needs a new result field
(protocol bump) or daemon-side policy. The reported case (`--base-branch
main`) and its `master`/`develop`-as-default equivalents are fixed;
other plain names keep today's local-only behaviour and the docs now say
so.

## How you verified

New tests:

- `apps/server/test/public/public-threads.named-base-branch.test.ts`
(route level, real in-memory DB, fake daemon): named `main` with
`local-behind` -> provision `baseBranch` is `origin/main`; named `main`
with `local-ahead` -> `main`; named `release/2026-05` -> unchanged; an
isolated fork from an environment on `main` with `local-behind` -> still
`main`.
- `apps/server/test/services/worktree-base-branch.test.ts`: unit cases
for `resolveManagedNamedBaseBranchSpec` (equal/behind -> origin;
ahead/diverged/unknown/no-origin -> local; non-default names untouched).

Fail before / pass after (checked out `origin/main` versions of the two
source files, ran the route test):

```
× bases on origin when the named default branch is behind origin
AssertionError: expected 'main' to be 'origin/main' // Object.is equality
Tests  1 failed | 3 passed (4)
```

With the fix: `Tests  11 passed (11)` across both files.

Turbo, from the committed tree (`git status --porcelain` empty, after
rebase on `origin/main`):

```
pnpm exec turbo run typecheck test --filter=@bb/server --filter=@bb/cli
@bb/cli:test:     Tests  453 passed (453)
@bb/server:test:  Tests  1830 passed (1830)
Tasks:    10 successful, 10 total
```

Manual repro on my own dev instance (this branch): bare `origin` at
commit B, project checkout at commit A (1 behind, `origin/main` ref
stale), `bb thread spawn --project ... --new-environment worktree
--base-branch main --provider codex --prompt "Reply only with ok."`.
Result: worktree HEAD = B (`f412fb6 B: pushed by someone else`),
`remote.txt` present, `bb thread show` reports `"baseBranch":
"origin/main"`, and the provisioning transcript contains `Fetching
origin/main` / `Fetched origin/main`. On `origin/main` code the report
shows the same setup landing on A with no fetch step.

Fixes #1770

> AGENT GENERATED: by Claude Opus 5


## Independent verification

Verified by a separate agent in its own worktree (`verify-1770-r1` at
11fc8fe, `origin/main` is an ancestor; PR is mergeable).

Commands run:

- `pnpm install --frozen-lockfile --prefer-offline && pnpm exec turbo
run build` (18/18 tasks).
- Fail-before: `git checkout origin/main --
apps/server/src/services/projects/worktree-base-branch.ts
apps/server/src/services/threads/thread-create.ts`, then `pnpm exec
vitest run test/public/public-threads.named-base-branch.test.ts` in
`apps/server`: `× bases on origin when the named default branch is
behind origin — AssertionError: expected 'main' to be 'origin/main'` (1
failed | 3 passed).
- Pass-after: restored the PR files and ran
`test/public/public-threads.named-base-branch.test.ts` +
`test/services/worktree-base-branch.test.ts`: 2 files, 11/11 passed.
- `umask 022 && pnpm exec turbo run typecheck test --filter=@bb/server
--filter=@bb/cli`: `Tasks: 10 successful, 10 total` (server 196 test
files passed, cli 48 test files passed).
- CI on 11fc8fe: 11 checks pass, 2 skipped, none failing.

Repro on the fixed branch (own dev instance, ports 16163/24163/32163,
bare remote + checkout one commit behind):

- `bb thread spawn --new-environment worktree --base-branch main
--provider codex --prompt "Reply only with ok."` -> `thread show`
reports `baseBranch: "origin/main"`, worktree HEAD is the remote tip
(`a549651 B: pushed by someone else`), `remote.txt` present, checkout's
`origin/main` refreshed, provisioning transcript shows `Fetching
origin/main` / `Fetched origin/main`. On `origin/main` (per the report)
the same spawn lands on the stale local commit with no fetch step. No
longer reproduces.
- Guard case: after making local `main` one commit ahead of origin, the
same spawn keeps `baseBranch: "main"` and the worktree contains the
local-only commit (`C: local only`).

Review notes: server-only policy change; no host-protocol shapes
changed, so no `HOST_DAEMON_PROTOCOL_VERSION` bump is needed. Forks
(`originKind !== null`) skip the resolution and keep the source branch
verbatim (covered by a test).

Residual risks (minor, non-blocking):

- A plain non-default name (`--base-branch develop`) still resolves to
the local branch without a fetch; documented in `docs/worktrees.md`.
- `resolveDefaultThreadEnvironment` (thread-default-policy) pins a named
base from its own `host.list_branches` call, then
`createThreadFromRequest` now calls `host.list_branches` a second time
for that named spec. The result is identical and the daemon throttles
the fetch, so this is one extra cheap RPC.
- `packages/templates/src/templates/bb-guide-threads.md` and the bb-cli
skill still describe `--base-branch` as "Base branch for a new managed
worktree"; still accurate, just terser than the new `--help` text.

> AGENT GENERATED: by Claude Opus 5


## Rebase

Rebased onto `origin/main` at `75d6fc4d4` (was 33 commits behind, based
on `f6fb434ab`) and squashed the two commits into one (`94d0324e8`); the
resulting tree is identical to the pre-squash rebased tree. No
conflicts: none of the 33 commits on main touched the seven files in
this PR. The only nearby change on main was #2158 (bare repositories as
worktree sources), which changes `detectGitRepo` in
`packages/host-workspace`; it does not alter `host.list_branches` result
shapes or the server-side base-branch resolution this PR adds, so the
fix applies unchanged.

Re-verified on the new base:

- Fail-before: with `worktree-base-branch.ts` and `thread-create.ts`
checked out from `origin/main`, the two new test files give `Tests 4
failed | 7 passed (11)` (`AssertionError: expected 'main' to be
'origin/main'`). Restored: `Tests 11 passed (11)`.
- From the committed tree (`git status --porcelain` empty), `umask 022`,
`--force`:
- `turbo run typecheck test --filter=@bb/server`: `Tests 1904 passed
(1904)`, `Tasks: 8 successful, 8 total`
- `turbo run typecheck test --filter=@bb/cli --filter=@bb/host-daemon`:
cli `453 passed (453)`, host-daemon `555 passed (555)`, `Tasks: 10
successful, 10 total`
- `turbo run typecheck test --filter=@bb/integration-tests`: `55 passed
(55)`, `Tasks: 8 successful, 8 total`

> AGENT GENERATED: by Claude Opus 5



## Independent verification (post-rebase)

Re-verified by a separate agent after the rebase, in its own worktree
(`verify-2152-rb` at `94d0324e8`, merge base `75d6fc4d4`; `git
merge-tree` against the current `origin/main` `27d1017fe` is
conflict-free, and none of the commits on main since the rebase touch
the PR's files or the `host.list_branches` / `createWorktree` paths).

Commands run:

- `pnpm install --frozen-lockfile --prefer-offline && pnpm exec turbo
run build` (18/18 tasks).
- Fail-before: `git checkout 75d6fc4 --
apps/server/src/services/projects/worktree-base-branch.ts
apps/server/src/services/threads/thread-create.ts`, then `pnpm exec
vitest run test/public/public-threads.named-base-branch.test.ts
test/services/worktree-base-branch.test.ts` in `apps/server`: `Tests 4
failed | 7 passed (11)`; the route test fails with `AssertionError:
expected 'main' to be 'origin/main'` (the other three fail because
`resolveManagedNamedBaseBranchSpec` does not exist on main).
- Pass-after: restored the PR files (`git status --porcelain` empty):
`Tests 11 passed (11)`.
- `umask 022 && pnpm exec turbo run typecheck test --filter=@bb/server
--filter=@bb/cli --force --concurrency=1`: cli `Tests 453 passed (453)`;
server `Tests 1904 passed (1904)`, `Tasks: 8 successful, 8 total` on the
clean re-run. (A first server run had one unrelated failure,
`test/services/plugins/plugin-update.test.ts`, from `disk quota
exceeded` on this machine's shared `/tmp` tmpfs at 98% inodes; it passes
with `TMPDIR` elsewhere and on the re-run.)
- CI on `94d0324e8`: all 10 required checks pass, 2 skipped, none
failing.

Repro on the fixed branch (own dev instance, ports 18795/26795/34795,
bare remote + checkout one commit behind):

- `bb thread spawn --project ... --new-environment worktree
--base-branch main --provider codex --prompt "Reply only with ok."` ->
`thread show` reports `baseBranch: "origin/main"`, worktree HEAD is the
remote tip (`f0ac52f B: pushed by someone else`), `remote.txt` present,
the checkout's `origin/main` was refreshed, and the thread events
contain `Fetching origin/main` / `Fetched origin/main`. No longer
reproduces.
- Guard case: after making local `main` one commit ahead (`afef38b C:
local only`), the same spawn keeps `baseBranch: "main"` and the worktree
contains `local.txt`.

Residual risks: unchanged from the earlier verification section
(non-default plain names still resolve locally, documented; one extra
cheap `host.list_branches` RPC when the default policy already pinned a
named base).

> AGENT GENERATED: by Claude Opus 5

Co-authored-by: Claude <[email protected]>
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.

Bare repo + worktrees layout is not detected as a git repository

1 participant