Skip to content

Commit 13ed1b4

Browse files
authored
[go-borges] Use go-borges to access repositories (#888)
[go-borges] Use go-borges to access repositories
2 parents 46fa72a + 044094c commit 13ed1b4

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

51 files changed

+1321
-850
lines changed

CHANGELOG.md

+5
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
66

77
## [Unreleased]
88

9+
- Now gitbase uses [go-borges](https://github.com/src-d/go-borges) to access repositories
10+
- The type of files in each directory has to be specified ([#867](https://github.com/src-d/gitbase/pull/867))
11+
- Supports new rooted repository format and separates references and objects from each repo (https://github.com/src-d/borges/issues/389)
12+
- Changed cli to be able to specify different formats ([#866](https://github.com/src-d/gitbase/issues/866))
13+
914
## [0.21.0-beta3] - 2019-06-19
1015

1116
### Fixed
Binary file not shown.
Binary file not shown.

blobs.go

+5-6
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ func (i *blobRowIter) nextByHash() (sql.Row, error) {
209209
return nil, err
210210
}
211211

212-
return blobToRow(i.repo.ID, blob, i.readContent)
212+
return blobToRow(i.repo.ID(), blob, i.readContent)
213213
}
214214
}
215215

@@ -236,7 +236,7 @@ func (i *blobRowIter) next() (sql.Row, error) {
236236
return nil, err
237237
}
238238

239-
return blobToRow(i.repo.ID, o, i.readContent)
239+
return blobToRow(i.repo.ID(), o, i.readContent)
240240
}
241241
}
242242

@@ -347,8 +347,7 @@ func newBlobsKeyValueIter(
347347
return nil, err
348348
}
349349

350-
r := pool.repositories[repo.ID]
351-
idx, err := newRepositoryIndex(r)
350+
idx, err := newRepositoryIndex(repo)
352351
if err != nil {
353352
return nil, err
354353
}
@@ -379,7 +378,7 @@ func (i *blobsKeyValueIter) Next() ([]interface{}, []byte, error) {
379378
}
380379

381380
key, err := encodeIndexKey(&packOffsetIndexKey{
382-
Repository: i.repo.ID,
381+
Repository: i.repo.ID(),
383382
Packfile: packfile.String(),
384383
Offset: offset,
385384
Hash: hash,
@@ -388,7 +387,7 @@ func (i *blobsKeyValueIter) Next() ([]interface{}, []byte, error) {
388387
return nil, nil, err
389388
}
390389

391-
row, err := blobToRow(i.repo.ID, blob, stringContains(i.columns, "blob_content"))
390+
row, err := blobToRow(i.repo.ID(), blob, stringContains(i.columns, "blob_content"))
392391
if err != nil {
393392
return nil, nil, err
394393
}

blobs_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ package gitbase
33
import (
44
"testing"
55

6-
"github.com/stretchr/testify/require"
76
"github.com/src-d/go-mysql-server/sql"
87
"github.com/src-d/go-mysql-server/sql/expression"
8+
"github.com/stretchr/testify/require"
99
)
1010

1111
func TestBlobsTable(t *testing.T) {

checksum.go

+49-12
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88
"sort"
99
"strings"
1010

11-
git "gopkg.in/src-d/go-git.v4"
1211
"gopkg.in/src-d/go-git.v4/plumbing"
1312
)
1413

@@ -18,9 +17,23 @@ type checksumable struct {
1817

1918
func (c *checksumable) Checksum() (string, error) {
2019
hash := sha1.New()
21-
for _, id := range c.pool.idOrder {
22-
repo := c.pool.repositories[id]
23-
hash.Write([]byte(id))
20+
iter, err := c.pool.RepoIter()
21+
if err != nil {
22+
return "", err
23+
}
24+
defer iter.Close()
25+
26+
var checksums checksums
27+
for {
28+
hash.Reset()
29+
30+
repo, err := iter.Next()
31+
if err == io.EOF {
32+
break
33+
}
34+
if err != nil {
35+
return "", err
36+
}
2437

2538
bytes, err := readChecksum(repo)
2639
if err != nil {
@@ -39,12 +52,28 @@ func (c *checksumable) Checksum() (string, error) {
3952
if _, err = hash.Write(bytes); err != nil {
4053
return "", err
4154
}
55+
56+
c := checksum{
57+
name: repo.ID(),
58+
hash: hash.Sum(nil),
59+
}
60+
61+
checksums = append(checksums, c)
62+
}
63+
64+
sort.Stable(checksums)
65+
hash.Reset()
66+
67+
for _, c := range checksums {
68+
if _, err = hash.Write(c.hash); err != nil {
69+
return "", err
70+
}
4271
}
4372

4473
return base64.StdEncoding.EncodeToString(hash.Sum(nil)), nil
4574
}
4675

47-
func readChecksum(r repository) ([]byte, error) {
76+
func readChecksum(r *Repository) ([]byte, error) {
4877
fs, err := r.FS()
4978
if err != nil {
5079
return nil, err
@@ -99,15 +128,23 @@ func (b byHashAndName) Less(i, j int) bool {
99128
return strings.Compare(b[i].name, b[j].name) < 0
100129
}
101130

102-
func readRefs(r repository) ([]byte, error) {
103-
repo, err := r.Repo()
104-
if err != nil {
105-
if err == git.ErrRepositoryNotExists {
106-
return nil, nil
107-
}
108-
return nil, err
131+
type checksum struct {
132+
name string
133+
hash []byte
134+
}
135+
136+
type checksums []checksum
137+
138+
func (b checksums) Len() int { return len(b) }
139+
func (b checksums) Swap(i, j int) { b[i], b[j] = b[j], b[i] }
140+
func (b checksums) Less(i, j int) bool {
141+
if cmp := bytes.Compare(b[i].hash, b[j].hash); cmp != 0 {
142+
return cmp < 0
109143
}
144+
return strings.Compare(b[i].name, b[j].name) < 0
145+
}
110146

147+
func readRefs(repo *Repository) ([]byte, error) {
111148
buf := bytes.NewBuffer(nil)
112149

113150
refs, err := repo.References()

checksum_test.go

+28-13
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,13 @@ import (
88

99
fixtures "github.com/src-d/go-git-fixtures"
1010
"github.com/stretchr/testify/require"
11-
"gopkg.in/src-d/go-git.v4/plumbing/cache"
11+
"gopkg.in/src-d/go-billy.v4/osfs"
12+
)
13+
14+
const (
15+
checksumMulti = "W/lxpR0jZ6O6BqVANTYTDMlAS/4="
16+
checksumSingle = "zqLF31JlrtJ57XNC+cQ+2hSkBkw="
17+
checksumSiva = "X27U+Lww5UOk1+/21bVFgI4uJyM="
1218
)
1319

1420
func TestChecksum(t *testing.T) {
@@ -18,40 +24,48 @@ func TestChecksum(t *testing.T) {
1824
require.NoError(fixtures.Clean())
1925
}()
2026

21-
pool := NewRepositoryPool(cache.DefaultMaxSize)
27+
lib, pool, err := newMultiPool()
28+
require.NoError(err)
2229

2330
for i, f := range fixtures.ByTag("worktree") {
2431
path := f.Worktree().Root()
25-
require.NoError(pool.AddGitWithID(fmt.Sprintf("repo_%d", i), path))
32+
require.NoError(lib.AddPlain(fmt.Sprintf("repo_%d", i), path, nil))
2633
}
2734

2835
c := &checksumable{pool}
2936
checksum, err := c.Checksum()
3037
require.NoError(err)
31-
require.Equal("mGPoKCyOIkXX4reGe1vTBPIOg2E=", checksum)
38+
require.Equal(checksumMulti, checksum)
3239

33-
pool = NewRepositoryPool(cache.DefaultMaxSize)
40+
lib, pool, err = newMultiPool()
41+
require.NoError(err)
3442
path := fixtures.ByTag("worktree").One().Worktree().Root()
35-
require.NoError(pool.AddGitWithID("worktree", path))
43+
require.NoError(lib.AddPlain("worktree", path, nil))
3644

3745
c = &checksumable{pool}
3846
checksum, err = c.Checksum()
3947
require.NoError(err)
40-
require.Equal("rwQnBj7HRazv9wuU//nQ+nuf0WY=", checksum)
48+
require.Equal(checksumSingle, checksum)
4149
}
4250

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

46-
pool := NewRepositoryPool(cache.DefaultMaxSize)
54+
lib, pool, err := newMultiPool()
55+
require.NoError(err)
56+
57+
cwd, err := os.Getwd()
58+
require.NoError(err)
59+
cwdFS := osfs.New(cwd)
60+
4761
require.NoError(
4862
filepath.Walk("_testdata", func(path string, info os.FileInfo, err error) error {
4963
if err != nil {
5064
return err
5165
}
5266

5367
if IsSivaFile(path) {
54-
require.NoError(pool.AddSivaFile(path))
68+
require.NoError(lib.AddSiva(path, cwdFS))
5569
}
5670

5771
return nil
@@ -61,7 +75,7 @@ func TestChecksumSiva(t *testing.T) {
6175
c := &checksumable{pool}
6276
checksum, err := c.Checksum()
6377
require.NoError(err)
64-
require.Equal("wJEvZNAc7QRszsf9KhGu+UeKto0=", checksum)
78+
require.Equal(checksumSiva, checksum)
6579
}
6680

6781
func TestChecksumStable(t *testing.T) {
@@ -71,18 +85,19 @@ func TestChecksumStable(t *testing.T) {
7185
require.NoError(fixtures.Clean())
7286
}()
7387

74-
pool := NewRepositoryPool(cache.DefaultMaxSize)
88+
lib, pool, err := newMultiPool()
89+
require.NoError(err)
7590

7691
for i, f := range fixtures.ByTag("worktree") {
7792
path := f.Worktree().Root()
78-
require.NoError(pool.AddGitWithID(fmt.Sprintf("repo_%d", i), path))
93+
require.NoError(lib.AddPlain(fmt.Sprintf("repo_%d", i), path, nil))
7994
}
8095

8196
c := &checksumable{pool}
8297

8398
for i := 0; i < 100; i++ {
8499
checksum, err := c.Checksum()
85100
require.NoError(err)
86-
require.Equal("mGPoKCyOIkXX4reGe1vTBPIOg2E=", checksum)
101+
require.Equal(checksumMulti, checksum)
87102
}
88103
}

0 commit comments

Comments
 (0)