From 6af765f5eb5f8d550e541e008b6485d408dd69fc Mon Sep 17 00:00:00 2001 From: Daniel Baker Date: Sat, 14 Dec 2024 21:19:04 -0800 Subject: [PATCH] Only write files on change. When formatting files, genemichaels always writes the formatted file to disk which modifies the file timestamp. This is problematic for tools that detect whether a file has been reformatted or not. This commit changes the behavior by comparing the source against the formatted output and only writing to disk if they are different. See: https://github.com/andrewbaxter/genemichaels/issues/95 --- crates/genemichaels/src/main.rs | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/crates/genemichaels/src/main.rs b/crates/genemichaels/src/main.rs index 724b38a..06089e8 100644 --- a/crates/genemichaels/src/main.rs +++ b/crates/genemichaels/src/main.rs @@ -337,7 +337,7 @@ impl FormatPool { fn process_file(&mut self, file: PathBuf) { let log = self.log.fork(ea!(file = file.to_string_lossy())); - log.log_with(loga::INFO, "Formatting file", ea!()); + log.log_with(loga::INFO, "Processing file", ea!()); let config = self.config.clone(); let errors = self.errors.clone(); self.pool.execute(move || { @@ -348,10 +348,11 @@ impl FormatPool { log.log_with(loga::INFO, "Skipping due to skip comment", ea!()); return Ok(()); } - fs::write( - &file, - process_file_contents(log, &config, &source).context("Error doing formatting")?.as_bytes(), - ).context("Error writing formatted code back")?; + let processed = process_file_contents(log, &config, &source).context("Error doing formatting")?; + if source != processed { + log.log_with(loga::INFO, "Writing newly formatted file", ea!()); + fs::write(&file, processed.as_bytes()).context("Error writing formatted code back")?; + } return Ok(()); }).stack_context(log, "Error formatting file"); match res {