Skip to content

Commit e282ce0

Browse files
committed
fix checksum on siva files and make consistent checksums
Signed-off-by: Miguel Molina <[email protected]>
1 parent ff072ec commit e282ce0

File tree

2 files changed

+84
-12
lines changed

2 files changed

+84
-12
lines changed

checksum.go

Lines changed: 32 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import (
55
"crypto/sha1"
66
"encoding/base64"
77
"io"
8+
"sort"
9+
"strings"
810

911
git "gopkg.in/src-d/go-git.v4"
1012
"gopkg.in/src-d/go-git.v4/plumbing"
@@ -79,6 +81,24 @@ func readChecksum(r repository) ([]byte, error) {
7981
return result, nil
8082
}
8183

84+
type reference struct {
85+
name string
86+
hash string
87+
}
88+
89+
type references []reference
90+
91+
type byHashAndName []reference
92+
93+
func (b byHashAndName) Len() int { return len(b) }
94+
func (b byHashAndName) Swap(i, j int) { b[i], b[j] = b[j], b[i] }
95+
func (b byHashAndName) Less(i, j int) bool {
96+
if cmp := strings.Compare(b[i].hash, b[j].hash); cmp != 0 {
97+
return cmp < 0
98+
}
99+
return strings.Compare(b[i].name, b[j].name) < 0
100+
}
101+
82102
func readRefs(r repository) ([]byte, error) {
83103
repo, err := r.Repo()
84104
if err != nil {
@@ -90,27 +110,29 @@ func readRefs(r repository) ([]byte, error) {
90110

91111
buf := bytes.NewBuffer(nil)
92112

93-
head, err := repo.Head()
94-
if err != nil && err != plumbing.ErrReferenceNotFound {
95-
return nil, err
96-
} else {
97-
buf.WriteString("HEAD")
98-
buf.WriteString(head.Hash().String())
99-
}
100-
101113
refs, err := repo.References()
102114
if err != nil {
103115
return nil, err
104116
}
105117

118+
var references []reference
106119
err = refs.ForEach(func(r *plumbing.Reference) error {
107-
buf.WriteString(string(r.Name()))
108-
buf.WriteString(r.Hash().String())
120+
references = append(references, reference{
121+
name: string(r.Name()),
122+
hash: r.Hash().String(),
123+
})
109124
return nil
110125
})
111126
if err != nil {
112127
return nil, err
113128
}
114129

130+
sort.Stable(byHashAndName(references))
131+
132+
for _, r := range references {
133+
buf.WriteString(r.name)
134+
buf.WriteString(r.hash)
135+
}
136+
115137
return buf.Bytes(), nil
116138
}

checksum_test.go

Lines changed: 52 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ package gitbase
22

33
import (
44
"fmt"
5+
"os"
6+
"path/filepath"
57
"testing"
68

79
"github.com/stretchr/testify/require"
@@ -27,7 +29,7 @@ func TestChecksum(t *testing.T) {
2729
c := &checksumable{pool}
2830
checksum, err := c.Checksum()
2931
require.NoError(err)
30-
require.Equal("ogfv7HAwFigDgtuW4tbnEP+Zl40=", checksum)
32+
require.Equal("mGPoKCyOIkXX4reGe1vTBPIOg2E=", checksum)
3133

3234
pool = NewRepositoryPool(cache.DefaultMaxSize)
3335
path := fixtures.ByTag("worktree").One().Worktree().Root()
@@ -36,5 +38,53 @@ func TestChecksum(t *testing.T) {
3638
c = &checksumable{pool}
3739
checksum, err = c.Checksum()
3840
require.NoError(err)
39-
require.Equal("5kfLCygyBSZFMh+nFzFNk3zAUTQ=", checksum)
41+
require.Equal("rwQnBj7HRazv9wuU//nQ+nuf0WY=", checksum)
42+
}
43+
44+
func TestChecksumSiva(t *testing.T) {
45+
require := require.New(t)
46+
47+
pool := NewRepositoryPool(cache.DefaultMaxSize)
48+
require.NoError(
49+
filepath.Walk("_testdata", func(path string, info os.FileInfo, err error) error {
50+
if err != nil {
51+
return err
52+
}
53+
54+
if IsSivaFile(path) {
55+
require.NoError(pool.AddSivaFile(path))
56+
}
57+
58+
return nil
59+
}),
60+
)
61+
62+
c := &checksumable{pool}
63+
checksum, err := c.Checksum()
64+
require.NoError(err)
65+
require.Equal("wJEvZNAc7QRszsf9KhGu+UeKto0=", checksum)
66+
}
67+
68+
func TestChecksumStable(t *testing.T) {
69+
require := require.New(t)
70+
71+
require.NoError(fixtures.Init())
72+
defer func() {
73+
require.NoError(fixtures.Clean())
74+
}()
75+
76+
pool := NewRepositoryPool(cache.DefaultMaxSize)
77+
78+
for i, f := range fixtures.ByTag("worktree") {
79+
path := f.Worktree().Root()
80+
require.NoError(pool.AddGitWithID(fmt.Sprintf("repo_%d", i), path))
81+
}
82+
83+
c := &checksumable{pool}
84+
85+
for i := 0; i < 100; i++ {
86+
checksum, err := c.Checksum()
87+
require.NoError(err)
88+
require.Equal("mGPoKCyOIkXX4reGe1vTBPIOg2E=", checksum)
89+
}
4090
}

0 commit comments

Comments
 (0)