mirror of
https://github.com/openai/codex.git
synced 2026-11-11 21:10:39 +00:00
## Why Updating a CRLF file with `apply_patch` previously normalized its line endings to LF unless preservation was explicitly enabled. Preserve the file's existing line endings without requiring an opt-in. ## What changed - Use line-ending-preserving updates for both tool calls and standalone `apply_patch` invocations, removing the legacy normalization mode. - Mark `apply_patch_preserve_line_endings` as removed. - Always set `CODEX_APPLY_PATCH_PRESERVE_LINE_ENDINGS=1` in child environments for compatibility with older patch executables, overriding stale values. ## Testing Update CLI, tool-call, shell-heredoc, and app-server tests to check CRLF preservation by default. Keep coverage for overriding stale compatibility environment values. GitOrigin-RevId: 96d0b0e5350faa3fc7a45d63627a132657b44523
86 lines
2.6 KiB
Rust
86 lines
2.6 KiB
Rust
use std::io::Read;
|
|
use std::io::Write;
|
|
|
|
pub fn main() -> ! {
|
|
let exit_code = run_main();
|
|
std::process::exit(exit_code);
|
|
}
|
|
|
|
/// We would prefer to return `std::process::ExitCode`, but its `exit_process()`
|
|
/// method is still a nightly API and we want main() to return !.
|
|
pub fn run_main() -> i32 {
|
|
// Expect either one argument (the full apply_patch payload) or read it from stdin.
|
|
let mut args = std::env::args_os();
|
|
let _argv0 = args.next();
|
|
|
|
let patch_arg = match args.next() {
|
|
Some(arg) => match arg.into_string() {
|
|
Ok(s) => s,
|
|
Err(_) => {
|
|
eprintln!("Error: apply_patch requires a UTF-8 PATCH argument.");
|
|
return 1;
|
|
}
|
|
},
|
|
None => {
|
|
// No argument provided; attempt to read the patch from stdin.
|
|
let mut buf = String::new();
|
|
match std::io::stdin().read_to_string(&mut buf) {
|
|
Ok(_) => {
|
|
if buf.is_empty() {
|
|
eprintln!("Usage: apply_patch 'PATCH'\n echo 'PATCH' | apply_patch");
|
|
return 2;
|
|
}
|
|
buf
|
|
}
|
|
Err(err) => {
|
|
eprintln!("Error: Failed to read PATCH from stdin.\n{err}");
|
|
return 1;
|
|
}
|
|
}
|
|
}
|
|
};
|
|
|
|
// Refuse extra args to avoid ambiguity.
|
|
if args.next().is_some() {
|
|
eprintln!("Error: apply_patch accepts exactly one argument.");
|
|
return 2;
|
|
}
|
|
|
|
let mut stdout = std::io::stdout();
|
|
let mut stderr = std::io::stderr();
|
|
let cwd = match codex_utils_absolute_path::AbsolutePathBuf::current_dir() {
|
|
Ok(cwd) => cwd,
|
|
Err(err) => {
|
|
eprintln!("Error: Failed to determine current directory.\n{err}");
|
|
return 1;
|
|
}
|
|
};
|
|
let runtime = match tokio::runtime::Builder::new_current_thread()
|
|
.enable_all()
|
|
.build()
|
|
{
|
|
Ok(runtime) => runtime,
|
|
Err(err) => {
|
|
eprintln!("Error: Failed to initialize runtime.\n{err}");
|
|
return 1;
|
|
}
|
|
};
|
|
// TODO(anp): Discover the standalone executable cwd as PathUri directly.
|
|
let cwd = codex_utils_path_uri::PathUri::from_abs_path(&cwd);
|
|
match runtime.block_on(crate::apply_patch(
|
|
&patch_arg,
|
|
&cwd,
|
|
&mut stdout,
|
|
&mut stderr,
|
|
codex_exec_server::LOCAL_FS.as_ref(),
|
|
/*sandbox*/ None,
|
|
)) {
|
|
Ok(_) => {
|
|
// Flush to ensure output ordering when used in pipelines.
|
|
let _ = stdout.flush();
|
|
0
|
|
}
|
|
Err(_) => 1,
|
|
}
|
|
}
|