summaryrefslogtreecommitdiffhomepage
path: root/packages
diff options
context:
space:
mode:
authorSteven T. Cramer <[email protected]>2026-04-22 23:02:10 +0700
committerAiden Cline <[email protected]>2026-04-23 00:25:39 -0400
commit3a082a0efd5eaa5296c929241bfe565d460c277b (patch)
treee6520f0a7456a132ecdfaf37eba154ddddbe6a1f /packages
parent504fd1b373d0b35f39ee40b07fdb6957454de6b1 (diff)
downloadopencode-3a082a0efd5eaa5296c929241bfe565d460c277b.tar.gz
opencode-3a082a0efd5eaa5296c929241bfe565d460c277b.zip
fix(project): use git common dir for bare repo project cache (#19054)
Diffstat (limited to 'packages')
-rw-r--r--packages/opencode/src/project/project.ts12
-rw-r--r--packages/opencode/test/project/project.test.ts84
2 files changed, 90 insertions, 6 deletions
diff --git a/packages/opencode/src/project/project.ts b/packages/opencode/src/project/project.ts
index 6a2132274..d628f87f9 100644
--- a/packages/opencode/src/project/project.ts
+++ b/packages/opencode/src/project/project.ts
@@ -207,13 +207,13 @@ 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 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(pathSvc.join(worktree, ".git"))
+ id = yield* readCachedProjectId(common)
}
if (!id) {
@@ -226,7 +226,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)
}
}
diff --git a/packages/opencode/test/project/project.test.ts b/packages/opencode/test/project/project.test.ts
index 4dc9ee5ef..4664b6c25 100644
--- a/packages/opencode/test/project/project.test.ts
+++ b/packages/opencode/test/project/project.test.ts
@@ -472,3 +472,87 @@ 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)
+ expect(project.worktree).toBe(barePath)
+
+ 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()
+ }
+ })
+
+ 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()
+ }
+ })
+})