Skip to content

fix(tools): apply_patch writes all files or none, reads blank context - #489

Merged
mrnasil merged 3 commits into
mainfrom
fix/apply-patch-hunks
Nov 25, 2026
Merged

mrnasil merged 3 commits into
mainfrom
fix/apply-patch-hunks

Conversation

@mrnasil

@mrnasil mrnasil commented Nov 25, 2026

Copy link
Copy Markdown
Member

apply_patch damaged or rejected good patches in three ways.

Partial multi-file writes. Each file was written as soon as its hunks applied. A mismatch in a later file left the earlier files patched and the rest unchanged. Now every file is patched in memory first, and files are written only when all of them applied. If the same file appears twice in one patch, the later hunks apply on top of the earlier ones.

Blank context lines. An empty line inside a hunk was skipped. Writers often strip the space off a blank context line, so the next context line then failed to match. Empty lines inside a hunk are now blank context lines; empty lines at the end of a hunk are still ignored.

Glued lines at EOF. Lines added after a last line that had no newline were joined onto it: keep + more became keepmore. An existing test asserted that output; it now expects keep\nmore\n. Every line except the last now gets a line end. \ No newline at end of file after an added line leaves the new last line without one.

Tests cover each case, including a failing second file that leaves the first untouched. On Linux (WSL), apply_patch.rs has 100% line coverage.

🤖 Generated with Claude Code

Three ways apply_patch damaged or rejected good patches:

- A multi-file patch wrote each file as soon as its hunks applied. A
  mismatch in a later file left the earlier ones patched and the rest
  not. Now every file is patched in memory first and written only when
  all of them applied; the same file twice in one patch stacks.
- An empty line inside a hunk was skipped. Writers often strip the space
  off a blank context line, so the next context line then failed to
  match. Empty lines are now blank context, except trailing ones.
- Lines added after a last line without a newline were glued onto it
  ("keep" + "more" became "keepmore"). Every line but the last now gets
  a line end, and "\ No newline at end of file" after an added line
  leaves the new last line bare.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@mrnasil
mrnasil enabled auto-merge (squash) November 25, 2026 14:56
@mrnasil
mrnasil merged commit c645eed into main Nov 25, 2026
7 checks passed
@mrnasil
mrnasil deleted the fix/apply-patch-hunks branch November 25, 2026 21:11
@mrnasil mrnasil mentioned this pull request Nov 26, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant