Skip to content

Commit

Permalink
fix: Update WorkingHash() to also use tree.version + 1
Browse files Browse the repository at this point in the history
  • Loading branch information
drklee3 committed Aug 12, 2024
1 parent 5cf60dd commit 33f1680
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 2 deletions.
4 changes: 3 additions & 1 deletion mutable_tree.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,9 @@ func (tree *MutableTree) Hash() []byte {

// WorkingHash returns the hash of the current working tree.
func (tree *MutableTree) WorkingHash() []byte {
return tree.root.hashWithCount(tree.WorkingVersion())
// Use tree.version instead of tree.WorkingVersion() because tree.version
// is always the latest version
return tree.root.hashWithCount(tree.version + 1)
}

func (tree *MutableTree) WorkingVersion() int64 {
Expand Down
5 changes: 4 additions & 1 deletion tree_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1936,6 +1936,9 @@ func TestWorkingHashWithInitialVersion(t *testing.T) {
_, err = tree.Set([]byte("key1"), []byte("value1"))
require.NoError(t, err)

// WorkingHash sets the hashes in the nodes, SaveVersion doesn't recompute it.
// This test ensures WorkingHash and SaveVersion are consistent, using the
// same version number.
workingHash := tree.WorkingHash()
commitHash, _, err := tree.SaveVersion()
require.NoError(t, err)
Expand All @@ -1952,5 +1955,5 @@ func TestWorkingHashWithInitialVersion(t *testing.T) {

commitHash1, _, err := tree.SaveVersion()
require.NoError(t, err)
require.Equal(t, commitHash1, commitHash)
assert.Equal(t, hex.EncodeToString(commitHash1), hex.EncodeToString(commitHash))
}

0 comments on commit 33f1680

Please sign in to comment.