fix(project): store bare repo cache in git common dir

Use git-common-dir as the project ID cache location so bare-repo-backed worktrees do not write to a shared parent .git path. Add regression tests that reproduce parent-path cache collisions across bare repos.
This commit is contained in:
Steven T. Cramer 2026-03-25 11:17:21 +07:00 committed by James Long
parent 3205f122eb
commit d983613a62
2 changed files with 64 additions and 6 deletions

View file

@ -207,13 +207,11 @@ export const layer: Layer.Layer<
vcs: fakeVcs,
}
}
const worktree = (() => {
const common = resolveGitPath(sandbox, commonDir.text.trim())
return common === sandbox ? sandbox : pathSvc.dirname(common)
})()
const common = resolveGitPath(sandbox, commonDir.text.trim())
const worktree = common === sandbox ? sandbox : pathSvc.dirname(common)
if (id == null) {
id = yield* readCachedProjectId(pathSvc.join(worktree, ".git"))
id = yield* readCachedProjectId(common)
}
if (!id) {
@ -226,7 +224,7 @@ export const layer: Layer.Layer<
id = roots[0] ? ProjectID.make(roots[0]) : undefined
if (id) {
yield* fs.writeFileString(pathSvc.join(worktree, ".git", "opencode"), id).pipe(Effect.ignore)
yield* fs.writeFileString(pathSvc.join(common, "opencode"), id).pipe(Effect.ignore)
}
}

View file

@ -472,3 +472,63 @@ describe("Project.addSandbox and Project.removeSandbox", () => {
expect(events.some((e) => e.payload.type === Project.Event.Updated.type)).toBe(true)
})
})
describe("Project.fromDirectory with bare repos", () => {
test("worktree from bare repo should cache in bare repo, not parent", async () => {
await using tmp = await tmpdir({ git: true })
const parentDir = path.dirname(tmp.path)
const barePath = path.join(parentDir, `bare-${Date.now()}.git`)
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)
const correctCache = path.join(barePath, "opencode")
const wrongCache = path.join(parentDir, ".git", "opencode")
expect(await Bun.file(correctCache).exists()).toBe(true)
expect(await Bun.file(wrongCache).exists()).toBe(false)
} finally {
await $`rm -rf ${barePath} ${worktreePath}`.quiet().nothrow()
}
})
test("different bare repos under same parent should not share project ID", async () => {
await using tmp1 = await tmpdir({ git: true })
await using tmp2 = await tmpdir({ git: true })
const parentDir = path.dirname(tmp1.path)
const bareA = path.join(parentDir, `bare-a-${Date.now()}.git`)
const bareB = path.join(parentDir, `bare-b-${Date.now()}.git`)
const worktreeA = path.join(parentDir, `wt-a-${Date.now()}`)
const worktreeB = path.join(parentDir, `wt-b-${Date.now()}`)
try {
await $`git clone --bare ${tmp1.path} ${bareA}`.quiet()
await $`git clone --bare ${tmp2.path} ${bareB}`.quiet()
await $`git worktree add ${worktreeA} HEAD`.cwd(bareA).quiet()
await $`git worktree add ${worktreeB} HEAD`.cwd(bareB).quiet()
const { project: projA } = await run((svc) => svc.fromDirectory(worktreeA))
const { project: projB } = await run((svc) => svc.fromDirectory(worktreeB))
expect(projA.id).not.toBe(projB.id)
const cacheA = path.join(bareA, "opencode")
const cacheB = path.join(bareB, "opencode")
const wrongCache = path.join(parentDir, ".git", "opencode")
expect(await Bun.file(cacheA).exists()).toBe(true)
expect(await Bun.file(cacheB).exists()).toBe(true)
expect(await Bun.file(wrongCache).exists()).toBe(false)
} finally {
await $`rm -rf ${bareA} ${bareB} ${worktreeA} ${worktreeB}`.quiet().nothrow()
}
})
})