Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
98 changes: 51 additions & 47 deletions packages/opencode/src/snapshot/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,12 +90,19 @@ export namespace Snapshot {

const args = (cmd: string[]) => ["--git-dir", state.gitdir, "--work-tree", state.worktree, ...cmd]

const enc = new TextEncoder()
const feed = (list: string[]) => Stream.make(enc.encode(list.join("\0") + "\0"))

const git = Effect.fnUntraced(
function* (cmd: string[], opts?: { cwd?: string; env?: Record<string, string> }) {
function* (
cmd: string[],
opts?: { cwd?: string; env?: Record<string, string>; stdin?: ChildProcess.CommandInput },
) {
const proc = ChildProcess.make("git", cmd, {
cwd: opts?.cwd,
env: opts?.env,
extendEnv: true,
stdin: opts?.stdin,
})
const handle = yield* spawner.spawn(proc)
const [text, stderr] = yield* Effect.all(
Expand All @@ -115,6 +122,43 @@ export namespace Snapshot {
),
)

const ignore = Effect.fnUntraced(function* (files: string[]) {
if (!files.length) return new Set<string>()
const check = yield* git(
[
...quote,
"--git-dir",
path.join(state.worktree, ".git"),
"--work-tree",
state.worktree,
"check-ignore",
"--no-index",
"--stdin",
"-z",
],
{
cwd: state.directory,
stdin: feed(files),
},
)
if (check.code !== 0 && check.code !== 1) return new Set<string>()
return new Set(check.text.split("\0").filter(Boolean))
})

const drop = Effect.fnUntraced(function* (files: string[]) {
if (!files.length) return
yield* git(
[
...cfg,
...args(["rm", "--cached", "-f", "--ignore-unmatch", "--pathspec-from-file=-", "--pathspec-file-nul"]),
],
{
cwd: state.directory,
stdin: feed(files),
},
)
})

const exists = (file: string) => fs.exists(file).pipe(Effect.orDie)
const read = (file: string) => fs.readFileString(file).pipe(Effect.catch(() => Effect.succeed("")))
const remove = (file: string) => fs.remove(file).pipe(Effect.catch(() => Effect.void))
Expand Down Expand Up @@ -180,29 +224,14 @@ export namespace Snapshot {
// Files may have been tracked before being gitignored, so we need to check
// against the source project's current gitignore rules
// Use --no-index to check purely against patterns (ignoring whether file is tracked)
const checkArgs = [
...quote,
"--git-dir",
path.join(state.worktree, ".git"),
"--work-tree",
state.worktree,
"check-ignore",
"--no-index",
"--",
...all,
]
const check = yield* git(checkArgs, { cwd: state.directory })
const ignored =
check.code === 0 ? new Set(check.text.trim().split("\n").filter(Boolean)) : new Set<string>()
const ignored = yield* ignore(all)
const filtered = all.filter((item) => !ignored.has(item))

// Remove newly-ignored files from snapshot index to prevent re-adding
if (ignored.size > 0) {
const ignoredFiles = Array.from(ignored)
log.info("removing gitignored files from snapshot", { count: ignoredFiles.length })
yield* git([...cfg, ...args(["rm", "--cached", "-f", "--", ...ignoredFiles])], {
cwd: state.directory,
})
yield* drop(ignoredFiles)
}

if (!filtered.length) return
Expand Down Expand Up @@ -297,20 +326,8 @@ export namespace Snapshot {

// Filter out files that are now gitignored
if (files.length > 0) {
const checkArgs = [
...quote,
"--git-dir",
path.join(state.worktree, ".git"),
"--work-tree",
state.worktree,
"check-ignore",
"--no-index",
"--",
...files,
]
const check = yield* git(checkArgs, { cwd: state.directory })
if (check.code === 0) {
const ignored = new Set(check.text.trim().split("\n").filter(Boolean))
const ignored = yield* ignore(files)
if (ignored.size > 0) {
const filtered = files.filter((item) => !ignored.has(item))
return {
hash,
Expand Down Expand Up @@ -674,21 +691,8 @@ export namespace Snapshot {

// Filter out files that are now gitignored
if (rows.length > 0) {
const files = rows.map((r) => r.file)
const checkArgs = [
...quote,
"--git-dir",
path.join(state.worktree, ".git"),
"--work-tree",
state.worktree,
"check-ignore",
"--no-index",
"--",
...files,
]
const check = yield* git(checkArgs, { cwd: state.directory })
if (check.code === 0) {
const ignored = new Set(check.text.trim().split("\n").filter(Boolean))
const ignored = yield* ignore(rows.map((r) => r.file))
if (ignored.size > 0) {
const filtered = rows.filter((r) => !ignored.has(r.file))
rows.length = 0
rows.push(...filtered)
Expand Down
31 changes: 31 additions & 0 deletions packages/opencode/test/snapshot/snapshot.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -632,6 +632,37 @@ test("files tracked in snapshot but now gitignored are filtered out", async () =
})
})

test("gitignore filtering handles large tracked sets", async () => {
await using tmp = await tmpdir<string[]>({
git: true,
init: async (dir) => {
const ids = Array.from({ length: 2000 }, (_, i) => `${"f".repeat(40)}-${i.toString().padStart(4, "0")}.txt`)
await Promise.all(ids.map((id) => Filesystem.write(`${dir}/bulk/${id}`, `before-${id}`)))
await $`git add .`.cwd(dir).quiet()
Comment thread
Hona marked this conversation as resolved.
Outdated
await $`git commit -m init`.cwd(dir).quiet()
return ids
},
})

await Instance.provide({
directory: tmp.path,
fn: async () => {
const before = await run(tmp.path, (snapshot) => snapshot.track())
expect(before).toBeTruthy()

await Promise.all(tmp.extra.map((id) => Filesystem.write(`${tmp.path}/bulk/${id}`, `after-${id}`)))
await Filesystem.write(`${tmp.path}/.gitignore`, "bulk/\n")
Comment thread
Hona marked this conversation as resolved.
Outdated
await Filesystem.write(`${tmp.path}/keep.txt`, "keep")

const patch = await run(tmp.path, (snapshot) => snapshot.patch(before!))

expect(patch.files).toContain(fwd(tmp.path, ".gitignore"))
expect(patch.files).toContain(fwd(tmp.path, "keep.txt"))
expect(patch.files.some((file) => file.includes("/bulk/"))).toBe(false)
},
})
})

test("gitignore updated between track calls filters from diff", async () => {
await using tmp = await bootstrap()
await Instance.provide({
Expand Down
Loading