Skip to content

armgit / codex

Public Imported from GitHub
0

Lightweight coding agent that runs in your terminal

Rust

apply-patch: sort replacements and add regression tests (#3425)

Jeremy Rose377af757308d
diff --git a/codex-rs/apply-patch/src/lib.rs b/codex-rs/apply-patch/src/lib.rs
index 1fdd6928e9..de22173c26 100644
--- a/codex-rs/apply-patch/src/lib.rs
+++ b/codex-rs/apply-patch/src/lib.rs
@@ -733,6 +733,8 @@ fn compute_replacements(
}
}
+ replacements.sort_by(|(lhs_idx, _, _), (rhs_idx, _, _)| lhs_idx.cmp(rhs_idx));
+
Ok(replacements)
}
@@ -1216,6 +1218,33 @@ PATCH"#,
assert_eq!(contents, "a\nB\nc\nd\nE\nf\ng\n");
}
+ #[test]
+ fn test_pure_addition_chunk_followed_by_removal() {
+ let dir = tempdir().unwrap();
+ let path = dir.path().join("panic.txt");
+ fs::write(&path, "line1\nline2\nline3\n").unwrap();
+ let patch = wrap_patch(&format!(
+ r#"*** Update File: {}
+@@
++after-context
++second-line
+@@
+ line1
+-line2
+-line3
++line2-replacement"#,
+ path.display()
+ ));
+ let mut stdout = Vec::new();
+ let mut stderr = Vec::new();
+ apply_patch(&patch, &mut stdout, &mut stderr).unwrap();
+ let contents = fs::read_to_string(path).unwrap();
+ assert_eq!(
+ contents,
+ "line1\nline2-replacement\nafter-context\nsecond-line\n"
+ );
+ }
+
/// Ensure that patches authored with ASCII characters can update lines that
/// contain typographic Unicode punctuation (e.g. EN DASH, NON-BREAKING
/// HYPHEN). Historically `git apply` succeeds in such scenarios but our