Skip to content

Commit

Permalink
Only write files on change.
Browse files Browse the repository at this point in the history
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: #95
  • Loading branch information
djacu committed Dec 15, 2024
1 parent 41db645 commit 6af765f
Showing 1 changed file with 6 additions and 5 deletions.
11 changes: 6 additions & 5 deletions crates/genemichaels/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 || {
Expand All @@ -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 {
Expand Down

0 comments on commit 6af765f

Please sign in to comment.