diff --git a/packages/opencode/src/snapshot/index.ts b/packages/opencode/src/snapshot/index.ts index 4da9bc3ca864..6a2dd3fa0381 100644 --- a/packages/opencode/src/snapshot/index.ts +++ b/packages/opencode/src/snapshot/index.ts @@ -164,6 +164,23 @@ const layer: Layer.Layer fs.remove(file).pipe(Effect.catch(() => Effect.void)) const locked = (fx: Effect.Effect) => lock(state.gitdir).withPermits(1)(fx) + // A nested repository is staged as a gitlink to its HEAD. git add cannot record one whose branch has no + // commit yet and fails the whole batch with "does not have a commit checked out", so such a repository is + // left out until its first commit; a gitlink would hold none of its files anyway. + const unborn = Effect.fnUntraced(function* (dirs: string[]) { + const found = yield* Effect.forEach( + dirs, + Effect.fnUntraced(function* (dir) { + const nested = path.join(state.worktree, dir, ".git") + if (!(yield* exists(nested))) return + const head = yield* git(["--git-dir", nested, "rev-parse", "--verify", "--quiet", "HEAD"]) + if (head.code !== 0) return dir + }), + { concurrency: 8 }, + ) + return new Set(found.filter((dir): dir is string => dir !== undefined)) + }) + const enabled = Effect.fnUntraced(function* () { if (state.vcs !== "git") return false return (yield* config.get()).snapshot !== false @@ -274,27 +291,29 @@ const layer: Layer.Layer !ignored.has(item)) if (!allow.length) return - const large = new Set( - (yield* Effect.all( - allow.map((item) => - fs - .stat(path.join(state.worktree, item)) - .pipe(Effect.catch(() => Effect.void)) - .pipe( - Effect.map((stat) => { - if (!stat || stat.type !== "File") return - const size = typeof stat.size === "bigint" ? Number(stat.size) : stat.size - return size > limit ? item : undefined - }), - ), - ), - { concurrency: 8 }, - )).filter((item): item is string => Boolean(item)), + const stats = yield* Effect.all( + allow.map((item) => + fs + .stat(path.join(state.worktree, item)) + .pipe(Effect.catch(() => Effect.void)) + .pipe( + Effect.map((stat) => { + if (!stat || stat.type !== "File") return [item, 0, stat?.type === "Directory"] as const + return [item, typeof stat.size === "bigint" ? Number(stat.size) : stat.size, false] as const + }), + ), + ), + { concurrency: 8 }, ) + const large = new Set(stats.filter((item) => item[1] > limit).map((item) => item[0])) const block = new Set(untracked.filter((item) => large.has(item))) yield* sync(Array.from(block)) + const skip = yield* unborn(stats.filter((item) => item[2]).map((item) => item[0])) + if (skip.size) { + yield* Effect.logInfo("skipping nested repositories without a commit", { repos: Array.from(skip) }) + } // Stage only the allowed candidate paths so snapshot updates stay scoped. - yield* stage(allow.filter((item) => !block.has(item))) + yield* stage(allow.filter((item) => !block.has(item) && !skip.has(item))) }) const cleanup = Effect.fnUntraced(function* () { diff --git a/packages/opencode/test/snapshot/snapshot.test.ts b/packages/opencode/test/snapshot/snapshot.test.ts index a39624087c63..3d1b57bb37ba 100644 --- a/packages/opencode/test/snapshot/snapshot.test.ts +++ b/packages/opencode/test/snapshot/snapshot.test.ts @@ -237,6 +237,41 @@ it.instance( { git: true }, ) +it.instance( + "nested repository without a commit does not block the snapshot", + Effect.gen(function* () { + const tmp = yield* bootstrap() + yield* exec(tmp.path, ["git", "init", "-q", "unborn"]) + yield* write(`${tmp.path}/unborn/pending.txt`, "PENDING") + const snapshot = yield* Snapshot.Service + const before = yield* snapshot.track() + expect(before).toBeTruthy() + + yield* exec(tmp.path, ["git", "init", "-q", "born"]) + yield* write(`${tmp.path}/born/file.txt`, "BORN") + yield* exec(`${tmp.path}/born`, ["git", "add", "file.txt"]) + yield* exec(`${tmp.path}/born`, [ + "git", + "-c", + "user.name=test", + "-c", + "user.email=test@example.com", + "-c", + "commit.gpgsign=false", + "commit", + "-q", + "-m", + "born", + ]) + yield* write(`${tmp.path}/b.txt`, "MODIFIED") + const files = (yield* snapshot.patch(before!)).files + expect(files).toContain(fwd(tmp.path, "b.txt")) + expect(files).toContain(fwd(tmp.path, "born")) + expect(files.filter((file) => file.includes("unborn"))).toEqual([]) + }), + { git: true }, +) + it.instance( "special characters in filenames", withTrackedSnapshot(({ tmp, snapshot, before }) =>