feat: stream item contents directly into diff command without temp files

This commit is contained in:
Andrew Phillips (aider)
2025-05-06 11:02:33 -03:00
parent 61b41a1852
commit 3cd7f1c2a8

View File

@@ -516,30 +516,27 @@ fn mode_diff(cmd: &mut Command, args: Args, ids: &mut Vec<i64>, tags: &mut Vec<S
let compression_engine_b = compression::get_engine(compression_type_b).expect("Unable to get compression engine"); let compression_engine_b = compression::get_engine(compression_type_b).expect("Unable to get compression engine");
let (fd_a, path_a) = tempfile::NamedTempFile::new().unwrap(); let (fd_a_read, fd_a_write) = nix::unistd::pipe().unwrap();
let (fd_b, path_b) = tempfile::NamedTempFile::new().unwrap(); let (fd_b_read, fd_b_write) = nix::unistd::pipe().unwrap();
let mut stdout_a = io::stdout(); let mut child = std::process::Command::new("diff")
let mut stdout_b = io::stdout();
let mut stdout_a = stdout_a.duplicate().unwrap();
let mut stdout_b = stdout_b.duplicate().unwrap();
io::stdout().set_fd(fd_a.as_raw_fd()).unwrap();
compression_engine_a.cat(item_path_a.clone())?;
io::stdout().set_fd(stdout_a.as_raw_fd()).unwrap();
io::stdout().set_fd(fd_b.as_raw_fd()).unwrap();
compression_engine_b.cat(item_path_b.clone())?;
io::stdout().set_fd(stdout_b.as_raw_fd()).unwrap();
let output = std::process::Command::new("diff")
.arg("-u") .arg("-u")
.arg(path_a) .arg(format!("/dev/fd/{}", fd_a_read))
.arg(path_b) .arg(format!("/dev/fd/{}", fd_b_read))
.output() .stdin(std::process::Stdio::null())
.stdout(std::process::Stdio::piped())
.stderr(std::process::Stdio::piped())
.spawn()
.expect("Failed to execute diff command"); .expect("Failed to execute diff command");
let mut stdout_a = unsafe { std::fs::File::from_raw_fd(fd_a_write) };
let mut stdout_b = unsafe { std::fs::File::from_raw_fd(fd_b_write) };
compression_engine_a.cat(item_path_a.clone())?;
compression_engine_b.cat(item_path_b.clone())?;
let output = child.wait_with_output().expect("Failed to wait on diff command");
if output.status.success() { if output.status.success() {
println!("{}", String::from_utf8_lossy(&output.stdout)); println!("{}", String::from_utf8_lossy(&output.stdout));
} else { } else {