Skip to content

Commit

Permalink
hclfmt: avoid rewrites when there are no changes in a file
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Mic92 committed Dec 20, 2022
1 parent 27df1ec commit 9e37c66
Showing 1 changed file with 7 additions and 1 deletion.
8 changes: 7 additions & 1 deletion cmd/hclfmt/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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)
Expand Down

0 comments on commit 9e37c66

Please sign in to comment.