Preserve EOF insertion after blank lines

This commit is contained in:
Charlie Marsh
2026-07-07 10:36:22 -04:00
parent a95b425f46
commit 4883b1dd52
2 changed files with 24 additions and 6 deletions
+12 -6
View File
@@ -849,12 +849,18 @@ fn compute_replacements(
}
if chunk.old_lines.is_empty() {
// Pure addition (no old lines). We'll add them at the end or just
// before the final empty line if one exists.
let insertion_idx = if original_lines.last().is_some_and(String::is_empty) {
original_lines.len() - 1
} else {
original_lines.len()
// Preserve the legacy split representation's handling of a final
// empty line. `SourceFile` only exposes real source lines, so its
// insertion point is always after the final line.
let insertion_idx = match update_file_mode {
ApplyPatchFileUpdateMode::NormalizeToLf => {
if original_lines.last().is_some_and(String::is_empty) {
original_lines.len() - 1
} else {
original_lines.len()
}
}
ApplyPatchFileUpdateMode::PreserveLineEndings => original_lines.len(),
};
replacements.push((insertion_idx, 0, chunk.new_lines.clone()));
continue;
+12
View File
@@ -139,6 +139,18 @@ fn test_apply_patch_cli_preserves_crlf_from_target_file() -> anyhow::Result<()>
)
}
#[test]
fn test_apply_patch_cli_appends_after_trailing_blank_crlf_line() -> anyhow::Result<()> {
let patch = "*** Begin Patch\n*** Update File: trailing_blank.txt\n@@\n+new\n*** End Patch";
assert_apply_patch_updates_file(
"trailing_blank.txt",
b"a\r\n\r\n",
patch,
b"a\r\n\r\nnew\r\n",
)
}
#[test]
fn test_apply_patch_cli_uses_legacy_line_handling_without_rollout_env() -> anyhow::Result<()> {
let tmp = tempdir()?;