From 8e55e206a177fc046b1d2bac42acd86a40b00658 Mon Sep 17 00:00:00 2001 From: Alexander Wang Date: Mon, 19 Dec 2022 13:20:41 -0800 Subject: [PATCH] write fmt only on diff --- ci/release/changelogs/next.md | 2 ++ fmt.go | 7 ++++++- 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/ci/release/changelogs/next.md b/ci/release/changelogs/next.md index f3c0d2a77..55bafe4af 100644 --- a/ci/release/changelogs/next.md +++ b/ci/release/changelogs/next.md @@ -3,3 +3,5 @@ #### Improvements 🧹 #### Bugfixes ⛑️ + +- `d2 fmt` only rewrites if it has changes, instead of always rewriting. [#470](https://github.com/terrastruct/d2/pull/470) diff --git a/fmt.go b/fmt.go index e3a755a79..58a4f671b 100644 --- a/fmt.go +++ b/fmt.go @@ -33,5 +33,10 @@ func fmtCmd(ctx context.Context, ms *xmain.State) (err error) { return err } - return ms.WritePath(inputPath, []byte(d2format.Format(m))) + output := []byte(d2format.Format(m)) + if !bytes.Equal(output, input) { + return ms.WritePath(inputPath, output) + } + + return nil }