Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 36 additions & 17 deletions packages/opencode/src/snapshot/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,23 @@ const layer: Layer.Layer<Service, never, FSUtil.Service | AppProcess.Service | C
const remove = (file: string) => fs.remove(file).pipe(Effect.catch(() => Effect.void))
const locked = <A, E, R>(fx: Effect.Effect<A, E, R>) => 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
Expand Down Expand Up @@ -274,27 +291,29 @@ const layer: Layer.Layer<Service, never, FSUtil.Service | AppProcess.Service | C
const allow = all.filter((item) => !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* () {
Expand Down
35 changes: 35 additions & 0 deletions packages/opencode/test/snapshot/snapshot.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 }) =>
Expand Down
Loading