diff --git a/packages/core/src/git.ts b/packages/core/src/git.ts index 704045e11eb..5144d6931b6 100644 --- a/packages/core/src/git.ts +++ b/packages/core/src/git.ts @@ -44,8 +44,9 @@ export type TreeID = typeof TreeID.Type const temporaryIndexPrefix = "index.opencode-" // Like `git gc --auto`, pack once loose objects accumulate, and combine packs before lookups slow down. -// Room below Windows' 32,767-character command line for the Git path, repository flags, and quoting. -const argumentBudget = 24_000 +// Room for paths on one Git command line after the Git path, repository flags, and quoting. Windows caps a command +// line at 32,767 characters. macOS caps arguments plus environment at 1 MiB, and Linux caps them at 2 MiB by default. +const argumentBudget = process.platform === "win32" ? 24_000 : 128 * 1024 const looseObjectLimit = 2048 const packCountLimit = 16 @@ -799,14 +800,6 @@ const layer = Layer.effect( ).map((file) => RelativePath.make(file)) }) - /** - * Three batched invocations over the tree pair instead of three per file. An - * explicit empty selection diffs nothing; an absent one diffs every changed path. - * Patch output is capped like VCS diffs: files past the cap get an empty patch. - * A selection too long for one Windows command line is diffed in groups that - * share the patch cap; Git orders output by path, so the groups concatenate in - * the order a single call would produce. - */ const treeDiff = Effect.fn("Git.tree.diff")(function* (input: { repository: Repository from: TreeID @@ -933,10 +926,6 @@ const layer = Layer.effect( ), ) - /** - * One ls-tree per tree. Windows caps a command line at 32,767 characters, so a - * selection too long to pass as arguments lists the whole tree instead. - */ const pathsInTree = Effect.fnUntraced(function* ( repository: Repository, tree: TreeID, @@ -1380,12 +1369,11 @@ function packIndexObjectIDs(bytes: Uint8Array) { } /** - * Windows caps a command line at 32,767 characters; elsewhere one call takes any - * selection. Groups are sorted by bytes, the order Git prints paths in, and each - * path is counted with room for the quotes Windows adds around spaces. + * Groups are sorted like Git's output, so groups of file paths concatenate in the + * order one call prints them. Each path is counted with room for the quotes Windows + * adds around spaces. */ function pathGroups(paths: readonly RelativePath[]) { - if (process.platform !== "win32") return [paths] return paths .toSorted((left, right) => Buffer.compare(Buffer.from(left), Buffer.from(right))) .reduce<{ groups: RelativePath[][]; length: number }>( diff --git a/packages/core/test/git.test.ts b/packages/core/test/git.test.ts index 2e1554569f5..be7576907fe 100644 --- a/packages/core/test/git.test.ts +++ b/packages/core/test/git.test.ts @@ -235,6 +235,58 @@ describe("Git trees", () => { }), ) + it.live( + "diffs a path selection longer than any platform command line", + () => + Effect.gen(function* () { + const root = yield* Effect.acquireRelease( + Effect.promise(() => tmpdir()), + (dir) => Effect.promise(() => dir[Symbol.asyncDispose]()), + ) + yield* Effect.promise(async () => { + await initRepo(root.path) + await fs.mkdir(path.join(root.path, "dir", "nested"), { recursive: true }) + await Bun.write(path.join(root.path, "changed.txt"), "before\n") + await Bun.write(path.join(root.path, "removed.txt"), "removed\n") + await Bun.write(path.join(root.path, "dir", "nested", "file.txt"), "before\n") + }) + const git = yield* Git.Service + const repository = yield* git.repo.discover(AbsolutePath.make(root.path)) + if (!repository) throw new Error("Repository not found") + const before = yield* git.tree.capture({ repository, scopes: [RelativePath.make(".")] }) + yield* Effect.promise(async () => { + await Bun.write(path.join(root.path, "changed.txt"), "after\n") + await fs.rm(path.join(root.path, "removed.txt")) + await Bun.write(path.join(root.path, "dir", "nested", "file.txt"), "after\n") + await Bun.write(path.join(root.path, "first.txt"), "first\n") + await Bun.write(path.join(root.path, "last.txt"), "last\n") + await Bun.write(path.join(root.path, "unselected.txt"), "unselected\n") + }) + const after = yield* git.tree.capture({ repository, scopes: [RelativePath.make(".")] }) + const missing = Array.from({ length: 30_000 }, (_, index) => + RelativePath.make(`missing/${String(index).padStart(6, "0")}/${"x".repeat(80)}.txt`), + ) + const paths = ["first.txt", "changed.txt", "removed.txt", "dir", ...missing, "last.txt"].map((file) => + RelativePath.make(file), + ) + + const diffs = yield* git.tree.diff({ repository, from: before, to: after, paths }) + expect(diffs.map((item) => [item.file, item.status, item.additions, item.deletions])).toEqual([ + ["changed.txt", "modified", 1, 1], + ["dir/nested/file.txt", "modified", 1, 1], + ["first.txt", "added", 1, 0], + ["last.txt", "added", 1, 0], + ["removed.txt", "deleted", 0, 1], + ]) + expect(diffs[0]?.patch).toContain("-before\n+after\n") + expect(diffs[2]?.patch).toContain("+first\n") + expect(diffs[4]?.patch).toContain("-removed\n") + expect(yield* git.tree.diff({ repository, from: before, to: after, paths: missing })).toEqual([]) + expect(yield* git.tree.write(repository)).toBe(after) + }), + { timeout: 60_000 }, + ) + it.live("captures, compares, previews, and restores scoped trees", () => Effect.gen(function* () { const root = yield* Effect.acquireRelease(