fix(project): set worktree to bare repo path, not parent directory

Detect bare repos by checking if git-common-dir basename ends with .git (excluding .git itself). This ensures both the worktree field in the database and the cache location are correct for bare-repo-backed worktrees.
This commit is contained in:
Steven T. Cramer 2026-03-27 07:34:31 +07:00 committed by James Long
parent d983613a62
commit d9911a9fc6
2 changed files with 27 additions and 1 deletions

View file

@ -208,7 +208,9 @@ export const layer: Layer.Layer<
}
}
const common = resolveGitPath(sandbox, commonDir.text.trim())
const worktree = common === sandbox ? sandbox : pathSvc.dirname(common)
const bareCheck = yield* git(["config", "--bool", "core.bare"], { cwd: sandbox })
const isBareRepo = bareCheck.code === 0 && bareCheck.text.trim() === "true"
const worktree = common === sandbox ? sandbox : isBareRepo ? common : pathSvc.dirname(common)
if (id == null) {
id = yield* readCachedProjectId(common)

View file

@ -488,6 +488,7 @@ describe("Project.fromDirectory with bare repos", () => {
const { project } = await run((svc) => svc.fromDirectory(worktreePath))
expect(project.id).not.toBe(ProjectID.global)
expect(project.worktree).toBe(barePath)
const correctCache = path.join(barePath, "opencode")
const wrongCache = path.join(parentDir, ".git", "opencode")
@ -531,4 +532,27 @@ describe("Project.fromDirectory with bare repos", () => {
await $`rm -rf ${bareA} ${bareB} ${worktreeA} ${worktreeB}`.quiet().nothrow()
}
})
test("bare repo without .git suffix is still detected via core.bare", async () => {
await using tmp = await tmpdir({ git: true })
const parentDir = path.dirname(tmp.path)
const barePath = path.join(parentDir, `bare-no-suffix-${Date.now()}`)
const worktreePath = path.join(parentDir, `worktree-${Date.now()}`)
try {
await $`git clone --bare ${tmp.path} ${barePath}`.quiet()
await $`git worktree add ${worktreePath} HEAD`.cwd(barePath).quiet()
const { project } = await run((svc) => svc.fromDirectory(worktreePath))
expect(project.id).not.toBe(ProjectID.global)
expect(project.worktree).toBe(barePath)
const correctCache = path.join(barePath, "opencode")
expect(await Bun.file(correctCache).exists()).toBe(true)
} finally {
await $`rm -rf ${barePath} ${worktreePath}`.quiet().nothrow()
}
})
})