fix: clarify apply-patch delete-file misuse

This commit is contained in:
Codex
2026-06-03 09:49:56 +00:00
parent 1de0e08ae7
commit b2a1289295
2 changed files with 45 additions and 1 deletions
+26 -1
View File
@@ -188,8 +188,13 @@ export function parseApplyPatchV2(patchText: string): PatchParseResult {
continue;
}
if (line.startsWith(deleteFileMarker)) {
hunks.push({ kind: "delete", path: validatePatchPath(line.slice(deleteFileMarker.length), index + 1) });
const filePath = validatePatchPath(line.slice(deleteFileMarker.length), index + 1);
index += 1;
const nextContentIndex = nextNonBlankLineIndex(lines, index, lines.length - 1);
if (nextContentIndex < lines.length - 1 && !isFileHeader(lines[nextContentIndex] ?? "")) {
throw invalidDeleteFileLineError(lines[nextContentIndex] ?? "", nextContentIndex + 1, filePath);
}
hunks.push({ kind: "delete", path: filePath });
continue;
}
if (line.startsWith(updateFileMarker)) {
@@ -240,6 +245,26 @@ function invalidAddFileLineError(lineText: string, line: number, filePath: strin
);
}
function invalidDeleteFileLineError(lineText: string, line: number, filePath: string): ApplyPatchV2Error {
const details = { line, path: filePath, text: lineText };
if (lineText.trimStart().startsWith("@@")) {
return new ApplyPatchV2Error(
"invalid delete file line: remove @@ for Delete File. Delete File sections contain only the `*** Delete File: <path>` header; do not include hunk markers or file content.",
details,
);
}
return new ApplyPatchV2Error(
"invalid delete file line: Delete File sections contain only the `*** Delete File: <path>` header. Remove old file content lines such as -old line.",
details,
);
}
function nextNonBlankLineIndex(lines: string[], start: number, endExclusive: number): number {
let index = start;
while (index < endExclusive && (lines[index] ?? "").trim().length === 0) index += 1;
return index;
}
export function deriveUpdatedContent(filePath: string, originalContent: string, chunks: UpdateChunk[]): PatchUpdateResult {
const originalLines = splitContentLines(originalContent);
const replacements = computeReplacements(filePath, originalLines, chunks);