From f479efa5f9484c61bcedb6375da0754a452fa280 Mon Sep 17 00:00:00 2001 From: Shoubhit Dash Date: Mon, 7 Sep 2026 21:40:33 +0530 Subject: [PATCH] fix(core): keep batched tree patches quotepath-safe Patch headers have no -z form, so core.quotepath=false keeps chunksByFile matching non-ASCII names, and --no-ext-diff keeps a user's external diff tool out of the batched spawn. Snapshot.diff now diffs first and filters ignored paths from the result instead of listing changed files twice, and returns early for an empty explicit selection. --- packages/core/src/git.ts | 22 +++++++-------- packages/core/src/snapshot.ts | 49 +++++++++++++++++++++++----------- packages/core/test/git.test.ts | 12 ++++++--- 3 files changed, 51 insertions(+), 32 deletions(-) diff --git a/packages/core/src/git.ts b/packages/core/src/git.ts index e53204dfa1d..59909940d58 100644 --- a/packages/core/src/git.ts +++ b/packages/core/src/git.ts @@ -387,9 +387,7 @@ const layer = Layer.effect( maximumUntrackedFileBytes?: number }) { const list = (args: string[]) => - repositoryOperation("refresh", input.repository, args).pipe( - Effect.map((result) => result.text.split("\0").filter(Boolean)), - ) + repositoryOperation("refresh", input.repository, args).pipe(Effect.map((result) => nuls(result.text))) const [tracked, untracked] = yield* Effect.all( [ list(["diff-files", "--name-only", "-z", "--", input.scope]), @@ -466,13 +464,7 @@ const layer = Layer.effect( directory: input.repository.worktree, message: result.stderr.toString("utf8").trim() || "Failed to check ignored paths", }) - return new Set( - result.stdout - .toString("utf8") - .split("\0") - .filter(Boolean) - .map((file) => RelativePath.make(file)), - ) + return new Set(nuls(result.stdout.toString("utf8")).map((file) => RelativePath.make(file))) }) const writeTree = Effect.fn("Git.tree.write")(function* (repository: Repository) { @@ -527,13 +519,17 @@ const layer = Layer.effect( }) { if (input.paths?.length === 0) return [] const args = ["--no-renames", input.from, input.to, "--", ...(input.paths ?? [])] + // Patch headers have no -z form: unquoted paths keep chunksByFile matching non-ASCII names. const [names, numbers, patch] = yield* Effect.all( [ repositoryOperation("diff", input.repository, ["diff", "--name-status", "-z", ...args]), repositoryOperation("diff", input.repository, ["diff", "--numstat", "-z", ...args]), - repositoryOperation("diff", input.repository, ["diff", `--unified=${input.context ?? 3}`, ...args], { - maxOutputBytes: VcsPatch.MAX_TOTAL_PATCH_BYTES, - }), + repositoryOperation( + "diff", + input.repository, + ["-c", "core.quotepath=false", "diff", "--no-ext-diff", `--unified=${input.context ?? 3}`, ...args], + { maxOutputBytes: VcsPatch.MAX_TOTAL_PATCH_BYTES }, + ), ], { concurrency: 3 }, ) diff --git a/packages/core/src/snapshot.ts b/packages/core/src/snapshot.ts index 93256fecc4e..887f920fdb6 100644 --- a/packages/core/src/snapshot.ts +++ b/packages/core/src/snapshot.ts @@ -131,36 +131,55 @@ const layer = Layer.effect( ) }) - const compare = Effect.fnUntraced(function* (operation: "files" | "diff", input: CompareInput) { + const comparison = Effect.fnUntraced(function* (operation: "files" | "diff", input: CompareInput) { const repo = yield* repository.pipe(Effect.mapError((cause) => failure(operation, cause))) - const comparison = { + return { + source: repo.source, repository: repo.snapshotRepository, from: Git.TreeID.make(input.from), to: Git.TreeID.make(input.to), } - const files = yield* git.tree.files(comparison).pipe(Effect.mapError((cause) => failure(operation, cause))) - const ignored = yield* git.index - .ignored({ repository: repo.source, paths: files }) + }) + + // Snapshots track every scoped file; the source repository's ignore rules decide what callers see. + const ignored = Effect.fnUntraced(function* ( + operation: "files" | "diff", + source: Git.Repository, + paths: readonly RelativePath[], + ) { + return yield* git.index + .ignored({ repository: source, paths }) .pipe(Effect.mapError((cause) => failure(operation, cause))) - return { - input: comparison, - files, - ignored, - } }) const files = Effect.fn("Snapshot.files")(function* (input: CompareInput) { - const comparison = yield* compare("files", input) - return comparison.files.filter((file) => !comparison.ignored.has(file)) + const compared = yield* comparison("files", input) + const changed = yield* git.tree + .files({ repository: compared.repository, from: compared.from, to: compared.to }) + .pipe(Effect.mapError((cause) => failure("files", cause))) + const skipped = yield* ignored("files", compared.source, changed) + return changed.filter((file) => !skipped.has(file)) }) const diff = Effect.fn("Snapshot.diff")(function* (input: DiffInput) { - const comparison = yield* compare("diff", input) + if (input.paths?.length === 0) return [] + const compared = yield* comparison("diff", input) // Only an explicit selection becomes a pathspec; ignored paths are dropped from the result instead. const diffs = yield* git.tree - .diff({ ...comparison.input, context: input.context, paths: input.paths }) + .diff({ + repository: compared.repository, + from: compared.from, + to: compared.to, + context: input.context, + paths: input.paths, + }) .pipe(Effect.mapError((cause) => failure("diff", cause))) - return diffs.filter((file) => !comparison.ignored.has(RelativePath.make(file.file))) + const skipped = yield* ignored( + "diff", + compared.source, + diffs.map((file) => RelativePath.make(file.file)), + ) + return diffs.filter((file) => !skipped.has(RelativePath.make(file.file))) }) const plan = Effect.fnUntraced(function* (worktree: AbsolutePath, input: RestoreInput) { diff --git a/packages/core/test/git.test.ts b/packages/core/test/git.test.ts index 4929171d78b..9482a030c20 100644 --- a/packages/core/test/git.test.ts +++ b/packages/core/test/git.test.ts @@ -197,7 +197,7 @@ describe("Git trees", () => { }), ) - it.live("caps batched tree patches and keeps per-file stats past the cap", () => + it.live("caps batched tree patches, keeps per-file stats past the cap, and matches non-ASCII names", () => Effect.gen(function* () { const root = yield* Effect.acquireRelease( Effect.promise(() => tmpdir()), @@ -213,18 +213,22 @@ describe("Git trees", () => { await Bun.write(path.join(root.path, "a-small.txt"), "small\n") await Bun.write(path.join(root.path, "b-large.txt"), `${"x".repeat(79)}\n`.repeat(lines)) await Bun.write(path.join(root.path, "c-binary.bin"), new Uint8Array([0, 1, 2, 3])) + await Bun.write(path.join(root.path, "a-caf\u00e9.txt"), "caf\u00e9\n") }) const after = yield* git.tree.capture({ repository, scopes: [RelativePath.make(".")] }) const diffs = yield* git.tree.diff({ repository, from: before, to: after, context: 0 }) expect(diffs.map((item) => [item.file, item.status, item.additions, item.deletions])).toEqual([ + ["a-caf\u00e9.txt", "added", 1, 0], ["a-small.txt", "added", 1, 0], ["b-large.txt", "added", lines, 0], ["c-binary.bin", "added", 0, 0], ]) - expect(diffs[0]?.patch).toContain("+small\n") - expect(diffs[1]?.patch).toBe(VcsPatch.emptyPatch("b-large.txt")) - expect(diffs[2]?.patch).toBe("") + // Patch headers are not NUL-delimited; a quoted (octal-escaped) header would orphan this chunk. + expect(diffs[0]?.patch).toContain("+caf\u00e9\n") + expect(diffs[1]?.patch).toContain("+small\n") + expect(diffs[2]?.patch).toBe(VcsPatch.emptyPatch("b-large.txt")) + expect(diffs[3]?.patch).toBe("") expect(yield* git.tree.diff({ repository, from: before, to: after, paths: [] })).toEqual([]) }), )