fix(core): enforce patch EOF anchors (#38039)

This commit is contained in:
Aiden Cline 2026-07-21 10:15:51 -05:00 committed by GitHub
commit 3ea8895299
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 58 additions and 7 deletions

View file

@ -157,6 +157,10 @@ function parseUpdate(lines: ReadonlyArray<string>, start: number, end: number) {
else if (line.startsWith("+")) newLines.push(line.slice(1))
index++
}
if (lines[index]?.trim() === "*** End of File") {
endOfFile = true
index++
}
chunks.push({ oldLines, newLines, changeContext, endOfFile: endOfFile || undefined })
}
return { chunks, next: index }
@ -192,11 +196,15 @@ function computeReplacements(lines: ReadonlyArray<string>, path: string, chunks:
function seek(lines: ReadonlyArray<string>, pattern: ReadonlyArray<string>, start: number, eof = false) {
if (pattern.length === 0) return -1
for (const compare of [exact, rstrip, trim, normalized]) {
if (eof) {
const offset = lines.length - pattern.length
if (offset >= start && matches(lines, pattern, offset, compare)) return offset
if (eof) {
const offset = lines.length - pattern.length
if (offset < start) return -1
for (const compare of [exact, rstrip, trim, normalized]) {
if (matches(lines, pattern, offset, compare)) return offset
}
return -1
}
for (const compare of [exact, rstrip, trim, normalized]) {
for (let offset = start; offset <= lines.length - pattern.length; offset++) {
if (matches(lines, pattern, offset, compare)) return offset
}