Repository navigation
fix(tools): apply_patch writes all files or none, reads blank context - #489
Merged
Merged
Conversation
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
enabled auto-merge (squash)
November 25, 2026 14:56
# Conflicts: # crates/tools/src/file/apply_patch_tests.rs
Merged
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
apply_patchdamaged 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+morebecamekeepmore. An existing test asserted that output; it now expectskeep\nmore\n. Every line except the last now gets a line end.\ No newline at end of fileafter 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.rshas 100% line coverage.🤖 Generated with Claude Code