Skip to content

Commit

Permalink
Merge pull request #576 from Mic92/improve-hclfmt
Browse files Browse the repository at this point in the history
hclfmt: avoid rewrites when there are no changes in a file
  • Loading branch information
alisdair authored Jan 30, 2023
2 parents 4557953 + 9e37c66 commit 938be77
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 938be77

Please sign in to comment.