From 1fe3502e9590477be84ed3b739df956bc666d11b Mon Sep 17 00:00:00 2001 From: Daniel Baker Date: Sat, 14 Dec 2024 21:53:42 -0800 Subject: [PATCH] genemichaels: apply patch to only write files if changed When formatting rust files with `genemichaels`, the timestamp of the file is always modified whether or not the file was changed. This is problematic for detecting whether a file has been reformatted or not, such as when using tools such as `pre-commit`, `treefmt`, and `treefmt-nix`. See the following: https://github.com/andrewbaxter/genemichaels/issues/95 https://github.com/andrewbaxter/genemichaels/pull/96 https://github.com/numtide/treefmt-nix/pull/277 --- .../genemichaels-only-write-if-changed.patch | 20 +++++++++++++++++++ pkgs/by-name/ge/genemichaels/package.nix | 8 ++++++++ 2 files changed, 28 insertions(+) create mode 100644 pkgs/by-name/ge/genemichaels/genemichaels-only-write-if-changed.patch diff --git a/pkgs/by-name/ge/genemichaels/genemichaels-only-write-if-changed.patch b/pkgs/by-name/ge/genemichaels/genemichaels-only-write-if-changed.patch new file mode 100644 index 0000000000000..65841f8bbe4ca --- /dev/null +++ b/pkgs/by-name/ge/genemichaels/genemichaels-only-write-if-changed.patch @@ -0,0 +1,20 @@ +diff --git a/crates/genemichaels/src/main.rs b/crates/genemichaels/src/main.rs +index 724b38a..2fb23d0 100644 +--- a/crates/genemichaels/src/main.rs ++++ b/crates/genemichaels/src/main.rs +@@ -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 file", ea!()); ++ fs::write(&file, processed.as_bytes()).context("Error writing formatted code back")?; ++ } + return Ok(()); + }).stack_context(log, "Error formatting file"); + match res { diff --git a/pkgs/by-name/ge/genemichaels/package.nix b/pkgs/by-name/ge/genemichaels/package.nix index 557e17934a283..2daa4dbbc74f2 100644 --- a/pkgs/by-name/ge/genemichaels/package.nix +++ b/pkgs/by-name/ge/genemichaels/package.nix @@ -15,6 +15,14 @@ rustPlatform.buildRustPackage rec { hash = "sha256-qPyIC9AbRQI+inBft7Dd5R5v+tXtJcZdiV4lBIBwpyM="; }; + patches = [ + # TODO(@djacu): remove this if this patch is merged and the version is + # bumped to a release that has this change + # See: https://github.com/andrewbaxter/genemichaels/issues/95 + # See: https://github.com/andrewbaxter/genemichaels/pull/96 + ./genemichaels-only-write-if-changed.patch + ]; + cargoHash = "sha256-yPxgYf8N3Dqns/uKhdM++jpzG7zTPhCVVq+7WA9G/SM="; cargoBuildFlags = [ "--package ${pname}" ];