Skip to content

Commit

Permalink
fix checksum on siva files and make consistent checksums
Browse files Browse the repository at this point in the history
Signed-off-by: Miguel Molina <[email protected]>
  • Loading branch information
erizocosmico committed Jan 15, 2019
1 parent ff072ec commit e282ce0
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 12 deletions.
42 changes: 32 additions & 10 deletions checksum.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"crypto/sha1"
"encoding/base64"
"io"
"sort"
"strings"

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

type reference struct {
name string
hash string
}

type references []reference

type byHashAndName []reference

func (b byHashAndName) Len() int { return len(b) }
func (b byHashAndName) Swap(i, j int) { b[i], b[j] = b[j], b[i] }
func (b byHashAndName) Less(i, j int) bool {
if cmp := strings.Compare(b[i].hash, b[j].hash); cmp != 0 {
return cmp < 0
}
return strings.Compare(b[i].name, b[j].name) < 0
}

func readRefs(r repository) ([]byte, error) {
repo, err := r.Repo()
if err != nil {
Expand All @@ -90,27 +110,29 @@ func readRefs(r repository) ([]byte, error) {

buf := bytes.NewBuffer(nil)

head, err := repo.Head()
if err != nil && err != plumbing.ErrReferenceNotFound {
return nil, err
} else {
buf.WriteString("HEAD")
buf.WriteString(head.Hash().String())
}

refs, err := repo.References()
if err != nil {
return nil, err
}

var references []reference
err = refs.ForEach(func(r *plumbing.Reference) error {
buf.WriteString(string(r.Name()))
buf.WriteString(r.Hash().String())
references = append(references, reference{
name: string(r.Name()),
hash: r.Hash().String(),
})
return nil
})
if err != nil {
return nil, err
}

sort.Stable(byHashAndName(references))

for _, r := range references {
buf.WriteString(r.name)
buf.WriteString(r.hash)
}

return buf.Bytes(), nil
}
54 changes: 52 additions & 2 deletions checksum_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package gitbase

import (
"fmt"
"os"
"path/filepath"
"testing"

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

pool = NewRepositoryPool(cache.DefaultMaxSize)
path := fixtures.ByTag("worktree").One().Worktree().Root()
Expand All @@ -36,5 +38,53 @@ func TestChecksum(t *testing.T) {
c = &checksumable{pool}
checksum, err = c.Checksum()
require.NoError(err)
require.Equal("5kfLCygyBSZFMh+nFzFNk3zAUTQ=", checksum)
require.Equal("rwQnBj7HRazv9wuU//nQ+nuf0WY=", checksum)
}

func TestChecksumSiva(t *testing.T) {
require := require.New(t)

pool := NewRepositoryPool(cache.DefaultMaxSize)
require.NoError(
filepath.Walk("_testdata", func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}

if IsSivaFile(path) {
require.NoError(pool.AddSivaFile(path))
}

return nil
}),
)

c := &checksumable{pool}
checksum, err := c.Checksum()
require.NoError(err)
require.Equal("wJEvZNAc7QRszsf9KhGu+UeKto0=", checksum)
}

func TestChecksumStable(t *testing.T) {
require := require.New(t)

require.NoError(fixtures.Init())
defer func() {
require.NoError(fixtures.Clean())
}()

pool := NewRepositoryPool(cache.DefaultMaxSize)

for i, f := range fixtures.ByTag("worktree") {
path := f.Worktree().Root()
require.NoError(pool.AddGitWithID(fmt.Sprintf("repo_%d", i), path))
}

c := &checksumable{pool}

for i := 0; i < 100; i++ {
checksum, err := c.Checksum()
require.NoError(err)
require.Equal("mGPoKCyOIkXX4reGe1vTBPIOg2E=", checksum)
}
}

0 comments on commit e282ce0

Please sign in to comment.