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
43 changes: 41 additions & 2 deletions packages/opencode/src/tool/apply_patch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { Instance } from "../project/instance"
import { Patch } from "../patch"
import { createTwoFilesPatch, diffLines } from "diff"
import { assertExternalDirectory } from "./external-directory"
import { trimDiff } from "./edit"
import { trimDiff, normalizeLineEndings } from "./edit"
import { LSP } from "../lsp"
import { Filesystem } from "../util/filesystem"
import DESCRIPTION from "./apply_patch.txt"
Expand Down Expand Up @@ -159,7 +159,7 @@ export const ApplyPatchTool = Tool.define("apply_patch", {
}

// Build per-file metadata for UI rendering (used for both permission and result)
const files = fileChanges.map((change) => ({
let files = fileChanges.map((change) => ({
filePath: change.filePath,
relativePath: path.relative(Instance.worktree, change.movePath ?? change.filePath).replaceAll("\\", "/"),
type: change.type,
Expand Down Expand Up @@ -231,6 +231,45 @@ export const ApplyPatchTool = Tool.define("apply_patch", {
await Bus.publish(FileWatcher.Event.Updated, update)
}

// Re-read files after formatting and recompute diffs.
totalDiff = ""
for (const change of fileChanges) {
if (change.type === "delete") {
totalDiff += change.diff + "\n"
continue
}
const target = change.movePath ?? change.filePath
change.newContent = await fs.readFile(target, "utf-8")
change.diff = trimDiff(
createTwoFilesPatch(
change.filePath,
target,
normalizeLineEndings(change.oldContent),
normalizeLineEndings(change.newContent),
),
)
change.additions = 0
change.deletions = 0
for (const d of diffLines(change.oldContent, change.newContent)) {
if (d.added) change.additions += d.count || 0
if (d.removed) change.deletions += d.count || 0
}
totalDiff += change.diff + "\n"
}

// Rebuild per-file metadata with post-format content.
files = fileChanges.map((change) => ({
filePath: change.filePath,
relativePath: path.relative(Instance.worktree, change.movePath ?? change.filePath).replaceAll("\\", "/"),
type: change.type,
diff: change.diff,
before: change.oldContent,
after: change.newContent,
additions: change.additions,
deletions: change.deletions,
movePath: change.movePath,
}))

// Notify LSP of file changes and collect diagnostics
for (const change of fileChanges) {
if (change.type === "delete") continue
Expand Down
2 changes: 1 addition & 1 deletion packages/opencode/src/tool/edit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import { assertExternalDirectory } from "./external-directory"

const MAX_DIAGNOSTICS_PER_FILE = 20

function normalizeLineEndings(text: string): string {
export function normalizeLineEndings(text: string): string {
return text.replaceAll("\r\n", "\n")
}

Expand Down
Loading