1package statefile
2
3import (
4	"bytes"
5
6	"github.com/hashicorp/terraform/states"
7)
8
9// StatesMarshalEqual returns true if and only if the two given states have
10// an identical (byte-for-byte) statefile representation.
11//
12// This function compares only the portions of the state that are persisted
13// in state files, so for example it will not return false if the only
14// differences between the two states are local values or descendent module
15// outputs.
16func StatesMarshalEqual(a, b *states.State) bool {
17	var aBuf bytes.Buffer
18	var bBuf bytes.Buffer
19
20	// nil states are not valid states, and so they can never martial equal.
21	if a == nil || b == nil {
22		return false
23	}
24
25	// We write here some temporary files that have no header information
26	// populated, thus ensuring that we're only comparing the state itself
27	// and not any metadata.
28	err := Write(&File{State: a}, &aBuf)
29	if err != nil {
30		// Should never happen, because we're writing to an in-memory buffer
31		panic(err)
32	}
33	err = Write(&File{State: b}, &bBuf)
34	if err != nil {
35		// Should never happen, because we're writing to an in-memory buffer
36		panic(err)
37	}
38
39	return bytes.Equal(aBuf.Bytes(), bBuf.Bytes())
40}
41