mirror of
https://github.com/openai/codex.git
synced 2026-11-17 22:59:25 +00:00
Add a line-ending preservation mode to apply_patch (#37757)
## Why Updating a file with `apply_patch` historically normalized its contents to LF, which can rewrite line endings outside the requested change. ## What changed - Add an opt-in `PreserveLineEndings` update mode and thread it through patch verification, application, and the standalone executable. Existing APIs keep the LF-normalizing mode by default. - Retain the original endings of untouched and context lines, including in mixed-ending files, and use the file's first line ending for inserted or replaced lines. - Track patch context separately from identical changed lines so preservation does not alter replacement ordering or matching. ## Testing Add coverage for CRLF, CR, mixed endings, trailing blank lines, repeated lines, EOF overlap rejection, and legacy LF normalization. GitOrigin-RevId: 085f0cbc57e0965aa62c6a2d0e80870424e762f5
This commit is contained in:
1 parent
f344a80a3b
commit
21aa552e87
19 files changed
+1174
-520
No files matched your search
@@ -14,6 +14,7 @@ pub(crate) fn seek_sequence(
|
||||
pattern: &[String],
|
||||
start: usize,
|
||||
eof: bool,
|
||||
update_file_mode: crate::ApplyPatchFileUpdateMode,
|
||||
) -> Option<usize> {
|
||||
if pattern.is_empty() {
|
||||
return Some(start);
|
||||
@@ -27,7 +28,11 @@ pub(crate) fn seek_sequence(
|
||||
return None;
|
||||
}
|
||||
let search_start = if eof && lines.len() >= pattern.len() {
|
||||
lines.len() - pattern.len()
|
||||
let eof_start = lines.len() - pattern.len();
|
||||
match update_file_mode {
|
||||
crate::ApplyPatchFileUpdateMode::NormalizeToLf => eof_start,
|
||||
crate::ApplyPatchFileUpdateMode::PreserveLineEndings => eof_start.max(start),
|
||||
}
|
||||
} else {
|
||||
start
|
||||
};
|
||||
@@ -112,6 +117,7 @@ pub(crate) fn seek_sequence(
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::seek_sequence;
|
||||
use crate::ApplyPatchFileUpdateMode;
|
||||
use std::string::ToString;
|
||||
|
||||
fn to_vec(strings: &[&str]) -> Vec<String> {
|
||||
@@ -123,7 +129,13 @@ mod tests {
|
||||
let lines = to_vec(&["foo", "bar", "baz"]);
|
||||
let pattern = to_vec(&["bar", "baz"]);
|
||||
assert_eq!(
|
||||
seek_sequence(&lines, &pattern, /*start*/ 0, /*eof*/ false),
|
||||
seek_sequence(
|
||||
&lines,
|
||||
&pattern,
|
||||
/*start*/ 0,
|
||||
/*eof*/ false,
|
||||
ApplyPatchFileUpdateMode::NormalizeToLf,
|
||||
),
|
||||
Some(1)
|
||||
);
|
||||
}
|
||||
@@ -134,7 +146,13 @@ mod tests {
|
||||
// Pattern omits trailing whitespace.
|
||||
let pattern = to_vec(&["foo", "bar"]);
|
||||
assert_eq!(
|
||||
seek_sequence(&lines, &pattern, /*start*/ 0, /*eof*/ false),
|
||||
seek_sequence(
|
||||
&lines,
|
||||
&pattern,
|
||||
/*start*/ 0,
|
||||
/*eof*/ false,
|
||||
ApplyPatchFileUpdateMode::NormalizeToLf,
|
||||
),
|
||||
Some(0)
|
||||
);
|
||||
}
|
||||
@@ -145,7 +163,13 @@ mod tests {
|
||||
// Pattern omits any additional whitespace.
|
||||
let pattern = to_vec(&["foo", "bar"]);
|
||||
assert_eq!(
|
||||
seek_sequence(&lines, &pattern, /*start*/ 0, /*eof*/ false),
|
||||
seek_sequence(
|
||||
&lines,
|
||||
&pattern,
|
||||
/*start*/ 0,
|
||||
/*eof*/ false,
|
||||
ApplyPatchFileUpdateMode::NormalizeToLf,
|
||||
),
|
||||
Some(0)
|
||||
);
|
||||
}
|
||||
@@ -156,7 +180,13 @@ mod tests {
|
||||
let pattern = to_vec(&["too", "many", "lines"]);
|
||||
// Should not panic – must return None when pattern cannot possibly fit.
|
||||
assert_eq!(
|
||||
seek_sequence(&lines, &pattern, /*start*/ 0, /*eof*/ false),
|
||||
seek_sequence(
|
||||
&lines,
|
||||
&pattern,
|
||||
/*start*/ 0,
|
||||
/*eof*/ false,
|
||||
ApplyPatchFileUpdateMode::NormalizeToLf,
|
||||
),
|
||||
None
|
||||
);
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user