From 9e37c66148b4dd12f873376500821ed5daeb3137 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rg=20Thalheim?= Date: Tue, 20 Dec 2022 12:22:01 +0100 Subject: [PATCH] hclfmt: avoid rewrites when there are no changes in a file this is faster for large trees and also helps to make tools like [treefmt](https://github.com/numtide/treefmt) to work, since it does not update the mtime of the file. --- cmd/hclfmt/main.go | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/cmd/hclfmt/main.go b/cmd/hclfmt/main.go index 01a8d413..41901cf6 100644 --- a/cmd/hclfmt/main.go +++ b/cmd/hclfmt/main.go @@ -106,6 +106,7 @@ func processFiles() error { func processFile(fn string, in *os.File) error { var err error + var hasLocalChanges bool = false if in == nil { in, err = os.Open(fn) if err != nil { @@ -131,10 +132,15 @@ func processFile(fn string, in *os.File) error { if !bytes.Equal(inSrc, outSrc) { changed = append(changed, fn) + hasLocalChanges = true } if *overwrite { - return ioutil.WriteFile(fn, outSrc, 0644) + if hasLocalChanges { + return ioutil.WriteFile(fn, outSrc, 0644) + } else { + return nil + } } _, err = os.Stdout.Write(outSrc)