Skip to content
Closed
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
26 changes: 24 additions & 2 deletions packages/opencode/src/git/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ const cfg = [
"-c",
"core.quotepath=false",
] as const
const patchPrefixArgs = ["--src-prefix=a/", "--dst-prefix=b/"] as const

const out = (result: { text(): string }) => result.text().trim()
const nuls = (text: string) => text.split("\0").filter(Boolean)
Expand Down Expand Up @@ -261,15 +262,35 @@ export const layer = Layer.effect(

const patch = Effect.fn("Git.patch")(function* (cwd: string, ref: string, file: string, options?: PatchOptions) {
const result = yield* run(
["diff", "--patch", "--no-ext-diff", "--no-renames", `--unified=${options?.context ?? 3}`, ref, "--", file],
[
"diff",
"--patch",
"--no-ext-diff",
"--no-renames",
...patchPrefixArgs,
`--unified=${options?.context ?? 3}`,
ref,
"--",
file,
],
{ cwd, maxOutputBytes: options?.maxOutputBytes },
)
return { text: result.truncated ? "" : result.text(), truncated: result.truncated } satisfies Patch
})

const patchAll = Effect.fn("Git.patchAll")(function* (cwd: string, ref: string, options?: PatchOptions) {
const result = yield* run(
["diff", "--patch", "--no-ext-diff", "--no-renames", `--unified=${options?.context ?? 3}`, ref, "--", "."],
[
"diff",
"--patch",
"--no-ext-diff",
"--no-renames",
...patchPrefixArgs,
`--unified=${options?.context ?? 3}`,
ref,
"--",
".",
],
{ cwd, maxOutputBytes: options?.maxOutputBytes },
)
return { text: result.text(), truncated: result.truncated } satisfies Patch
Expand All @@ -287,6 +308,7 @@ export const layer = Layer.effect(
"--patch",
"--no-ext-diff",
"--no-renames",
...patchPrefixArgs,
`--unified=${options?.context ?? 3}`,
"--",
"/dev/null",
Expand Down
25 changes: 25 additions & 0 deletions packages/opencode/test/project/vcs.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,31 @@ describe("Vcs diff", () => {
20_000,
)

it.instance(
"diff('git') ignores mnemonic prefix config when rendering patches",
() =>
Effect.gen(function* () {
const test = yield* TestInstance
yield* write(path.join(test.directory, "file.txt"), "original\n")
yield* git(test.directory, ["add", "."])
yield* git(test.directory, ["commit", "--no-gpg-sign", "-m", "add file"])
yield* git(test.directory, ["config", "diff.mnemonicprefix", "true"])
yield* write(path.join(test.directory, "file.txt"), "changed\n")

const vcs = yield* init()
const diff = yield* vcs.diff("git")
const file = diff.find((item) => item.file === "file.txt")

expect(file?.patch).toContain("diff --git a/file.txt b/file.txt")
expect(file?.patch).toContain("--- a/file.txt")
expect(file?.patch).toContain("+++ b/file.txt")
expect(file?.patch).not.toContain("i/file.txt")
expect(file?.patch).not.toContain("w/file.txt")
expect(() => parsePatch(file?.patch ?? "")).not.toThrow()
}),
{ git: true },
)

it.instance(
"diff('branch') returns changes against default branch",
() =>
Expand Down
Loading