feat: stream item contents directly into diff command without temp files
This commit is contained in:
37
src/main.rs
37
src/main.rs
@@ -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 (fd_a, path_a) = tempfile::NamedTempFile::new().unwrap();
|
||||
let (fd_b, path_b) = tempfile::NamedTempFile::new().unwrap();
|
||||
let (fd_a_read, fd_a_write) = nix::unistd::pipe().unwrap();
|
||||
let (fd_b_read, fd_b_write) = nix::unistd::pipe().unwrap();
|
||||
|
||||
let mut stdout_a = io::stdout();
|
||||
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")
|
||||
let mut child = std::process::Command::new("diff")
|
||||
.arg("-u")
|
||||
.arg(path_a)
|
||||
.arg(path_b)
|
||||
.output()
|
||||
.arg(format!("/dev/fd/{}", fd_a_read))
|
||||
.arg(format!("/dev/fd/{}", fd_b_read))
|
||||
.stdin(std::process::Stdio::null())
|
||||
.stdout(std::process::Stdio::piped())
|
||||
.stderr(std::process::Stdio::piped())
|
||||
.spawn()
|
||||
.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() {
|
||||
println!("{}", String::from_utf8_lossy(&output.stdout));
|
||||
} else {
|
||||
|
||||
Reference in New Issue
Block a user