mirror of
https://github.com/openai/codex.git
synced 2026-11-23 23:10:47 +00:00
Gate EOF matching on update mode
This commit is contained in:
@@ -838,6 +838,7 @@ fn compute_replacements(
|
||||
std::slice::from_ref(ctx_line),
|
||||
line_index,
|
||||
/*eof*/ false,
|
||||
update_file_mode,
|
||||
) {
|
||||
line_index = idx + 1;
|
||||
} else {
|
||||
@@ -871,8 +872,13 @@ fn compute_replacements(
|
||||
// touching the end‑of‑file can be located reliably.
|
||||
|
||||
let mut pattern: &[String] = &chunk.old_lines;
|
||||
let mut found =
|
||||
seek_sequence::seek_sequence(original_lines, pattern, line_index, chunk.is_end_of_file);
|
||||
let mut found = seek_sequence::seek_sequence(
|
||||
original_lines,
|
||||
pattern,
|
||||
line_index,
|
||||
chunk.is_end_of_file,
|
||||
update_file_mode,
|
||||
);
|
||||
|
||||
let mut new_slice: &[String] = &chunk.new_lines;
|
||||
|
||||
@@ -889,6 +895,7 @@ fn compute_replacements(
|
||||
pattern,
|
||||
line_index,
|
||||
chunk.is_end_of_file,
|
||||
update_file_mode,
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
@@ -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()).max(start)
|
||||
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
|
||||
);
|
||||
}
|
||||
|
||||
@@ -107,6 +107,26 @@ fn test_apply_patch_cli_rejects_overlapping_end_of_file_chunks() -> anyhow::Resu
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_apply_patch_cli_allows_overlapping_eof_chunks_in_legacy_mode() -> anyhow::Result<()> {
|
||||
let tmp = tempdir()?;
|
||||
let target_path = tmp.path().join("overlapping.txt");
|
||||
fs::write(&target_path, "one\n")?;
|
||||
|
||||
let patch = "*** Begin Patch\n*** Update File: overlapping.txt\n@@\n-one\n+first\n@@\n-one\n+second\n*** End of File\n*** End Patch";
|
||||
|
||||
Command::new(codex_utils_cargo_bin::cargo_bin("apply_patch")?)
|
||||
.env_remove(CODEX_APPLY_PATCH_PRESERVE_LINE_ENDINGS_ENV_VAR)
|
||||
.arg(patch)
|
||||
.current_dir(tmp.path())
|
||||
.assert()
|
||||
.success()
|
||||
.stdout("Success. Updated the following files:\nM overlapping.txt\n");
|
||||
|
||||
assert_eq!(fs::read_to_string(target_path)?, "first\n");
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_apply_patch_cli_preserves_crlf_from_target_file() -> anyhow::Result<()> {
|
||||
let patch = "*** Begin Patch\n*** Update File: crlf.txt\n@@\n-one\n+uno\n@@\n two\n+\n+between\n three\n*** End Patch";
|
||||
|
||||
Reference in New Issue
Block a user