fish_indent: Prevent overwriting file with identical content

Fixes #10616
This commit is contained in:
Dezhi Wu 2024-07-21 18:57:48 +08:00
parent d9e5c6527f
commit 7308dbc7ad

View File

@ -7,6 +7,7 @@
#![allow(clippy::uninlined_format_args)]
use std::ffi::{CString, OsStr};
use std::fs::OpenOptions;
use std::io::{stdin, Read, Write};
use std::os::unix::ffi::OsStrExt;
use std::sync::atomic::Ordering;
@ -971,10 +972,16 @@ fn throwing_main() -> i32 {
colored_output = no_colorize(&output_wtext);
}
OutputType::File => {
match std::fs::File::create(OsStr::from_bytes(&wcs2string(output_location))) {
match OpenOptions::new()
.write(true)
.open(OsStr::from_bytes(&wcs2string(output_location)))
{
Ok(mut file) => {
// If the output is the same as the input, don't write it.
if output_wtext != src {
let _ = file.write_all(&wcs2string(&output_wtext));
}
}
Err(err) => {
eprintf!(
"%s",