From d9911a9fc6ba7bf7705d4f37b3906244c00b79c7 Mon Sep 17 00:00:00 2001 From: "Steven T. Cramer" Date: Fri, 27 Mar 2026 07:34:31 +0700 Subject: [PATCH] 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. --- packages/opencode/src/project/project.ts | 4 +++- .../opencode/test/project/project.test.ts | 24 +++++++++++++++++++ 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/packages/opencode/src/project/project.ts b/packages/opencode/src/project/project.ts index dbdb74016d..d628f87f97 100644 --- a/packages/opencode/src/project/project.ts +++ b/packages/opencode/src/project/project.ts @@ -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) diff --git a/packages/opencode/test/project/project.test.ts b/packages/opencode/test/project/project.test.ts index 72de6e6118..4664b6c258 100644 --- a/packages/opencode/test/project/project.test.ts +++ b/packages/opencode/test/project/project.test.ts @@ -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() + } + }) })