Skip to content

Commit a6399e4

Browse files
authored
Merge pull request #182 from jfontan/fix/do-not-share-git-repositories
Store a list of paths instead of instantiated reposititories
2 parents 2985f22 + 05406e9 commit a6399e4

10 files changed

+124
-113
lines changed

blobs_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import (
1414
func TestBlobsTable_Name(t *testing.T) {
1515
require := require.New(t)
1616

17-
f := fixtures.Basic().One()
17+
f := fixtures.ByTag("worktree").One()
1818
table := getTable(require, f, blobsTableName)
1919
require.Equal(blobsTableName, table.Name())
2020

@@ -27,25 +27,25 @@ func TestBlobsTable_Name(t *testing.T) {
2727
func TestBlobsTable_Children(t *testing.T) {
2828
require := require.New(t)
2929

30-
f := fixtures.Basic().One()
30+
f := fixtures.ByTag("worktree").One()
3131
table := getTable(require, f, blobsTableName)
3232
require.Equal(0, len(table.Children()))
3333
}
3434

3535
func TestBlobsTable_RowIter(t *testing.T) {
3636
require := require.New(t)
3737

38-
f := fixtures.Basic().One()
38+
f := fixtures.ByTag("worktree").One()
3939
table := getTable(require, f, blobsTableName)
4040

4141
rows, err := sql.NodeToRows(sql.NewBaseSession(context.TODO()), table)
42-
require.Nil(err)
42+
require.NoError(err)
4343
require.Len(rows, 10)
4444

4545
schema := table.Schema()
4646
for idx, row := range rows {
4747
err := schema.CheckRow(row)
48-
require.Nil(err, "row %d doesn't conform to schema", idx)
48+
require.NoError(err, "row %d doesn't conform to schema", idx)
4949
}
5050
}
5151

commits_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import (
1414
func TestCommitsTable_Name(t *testing.T) {
1515
require := require.New(t)
1616

17-
f := fixtures.Basic().One()
17+
f := fixtures.ByTag("worktree").One()
1818
table := getTable(require, f, commitsTableName)
1919
require.Equal(commitsTableName, table.Name())
2020

@@ -27,15 +27,15 @@ func TestCommitsTable_Name(t *testing.T) {
2727
func TestCommitsTable_Children(t *testing.T) {
2828
require := require.New(t)
2929

30-
f := fixtures.Basic().One()
30+
f := fixtures.ByTag("worktree").One()
3131
table := getTable(require, f, commitsTableName)
3232
require.Equal(0, len(table.Children()))
3333
}
3434

3535
func TestCommitsTable_RowIter(t *testing.T) {
3636
require := require.New(t)
3737

38-
f := fixtures.Basic().One()
38+
f := fixtures.ByTag("worktree").One()
3939
table := getTable(require, f, commitsTableName)
4040

4141
rows, err := sql.NodeToRows(sql.NewBaseSession(context.TODO()), table)

database_test.go

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,7 @@ import (
77
"github.com/stretchr/testify/require"
88
"gopkg.in/src-d/go-mysql-server.v0/sql"
99

10-
"gopkg.in/src-d/go-billy.v4/memfs"
1110
"gopkg.in/src-d/go-git-fixtures.v3"
12-
"gopkg.in/src-d/go-git.v4"
13-
"gopkg.in/src-d/go-git.v4/storage/filesystem"
1411
)
1512

1613
func init() {
@@ -24,7 +21,7 @@ const (
2421
func TestDatabase_Tables(t *testing.T) {
2522
require := require.New(t)
2623

27-
f := fixtures.Basic().One()
24+
f := fixtures.ByTag("worktree").One()
2825
db := getDB(require, f, testDBName)
2926

3027
tables := db.Tables()
@@ -50,22 +47,21 @@ func TestDatabase_Tables(t *testing.T) {
5047
func TestDatabase_Name(t *testing.T) {
5148
require := require.New(t)
5249

53-
f := fixtures.Basic().One()
50+
f := fixtures.ByTag("worktree").One()
5451
db := getDB(require, f, testDBName)
5552
require.Equal(testDBName, db.Name())
5653
}
5754

58-
func getDB(require *require.Assertions, fixture *fixtures.Fixture,
59-
name string) sql.Database {
55+
func getDB(
56+
require *require.Assertions,
57+
fixture *fixtures.Fixture,
58+
name string,
59+
) sql.Database {
6060

61-
s, err := filesystem.NewStorage(fixture.DotGit())
62-
require.NoError(err)
63-
64-
r, err := git.Open(s, memfs.New())
65-
require.NoError(err)
61+
fixtures.Init()
6662

6763
pool := NewRepositoryPool()
68-
pool.Add("repo", r)
64+
pool.Add("repo", fixture.Worktree().Root())
6965

7066
db := NewDatabase(name, &pool)
7167
require.NotNil(db)

integration_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ func TestIntegration(t *testing.T) {
2020
}()
2121

2222
path := fixtures.ByTag("worktree").One().Worktree().Root()
23+
2324
pool := gitquery.NewRepositoryPool()
2425
_, err := pool.AddGit(path)
2526
require.NoError(t, err)

references_test.go

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import (
1515
func TestReferencesTable_Name(t *testing.T) {
1616
require := require.New(t)
1717

18-
f := fixtures.Basic().One()
18+
f := fixtures.ByTag("worktree").One()
1919
table := getTable(require, f, referencesTableName)
2020
require.Equal(referencesTableName, table.Name())
2121

@@ -28,36 +28,34 @@ func TestReferencesTable_Name(t *testing.T) {
2828
func TestReferencesTable_Children(t *testing.T) {
2929
require := require.New(t)
3030

31-
f := fixtures.Basic().One()
31+
f := fixtures.ByTag("worktree").One()
3232
table := getTable(require, f, referencesTableName)
3333
require.Equal(0, len(table.Children()))
3434
}
3535

3636
func TestReferencesTable_RowIter(t *testing.T) {
3737
require := require.New(t)
3838

39-
f := fixtures.Basic().One()
39+
f := fixtures.ByTag("worktree").One()
4040
table := getTable(require, f, referencesTableName)
4141

4242
rows, err := sql.NodeToRows(sql.NewBaseSession(context.TODO()), plan.NewSort(
4343
[]plan.SortField{{Column: expression.NewGetField(0, sql.Text, "name", false), Order: plan.Ascending}},
4444
table))
45-
require.Nil(err)
45+
require.NoError(err)
4646

4747
expected := []sql.Row{
4848
sql.NewRow("repo", "HEAD", "6ecf0ef2c2dffb796033e5a02219af86ec6584e5"),
49-
sql.NewRow("repo", "refs/heads/branch", "e8d3ffab552895c19b9fcf7aa264d277cde33881"),
5049
sql.NewRow("repo", "refs/heads/master", "6ecf0ef2c2dffb796033e5a02219af86ec6584e5"),
5150
sql.NewRow("repo", "refs/remotes/origin/branch", "e8d3ffab552895c19b9fcf7aa264d277cde33881"),
5251
sql.NewRow("repo", "refs/remotes/origin/master", "6ecf0ef2c2dffb796033e5a02219af86ec6584e5"),
53-
sql.NewRow("repo", "refs/tags/v1.0.0", "6ecf0ef2c2dffb796033e5a02219af86ec6584e5"),
5452
}
5553
require.ElementsMatch(expected, rows)
5654

5755
schema := table.Schema()
5856
for idx, row := range rows {
5957
err := schema.CheckRow(row)
60-
require.Nil(err, "row %d doesn't conform to schema", idx)
58+
require.NoError(err, "row %d doesn't conform to schema", idx)
6159
}
6260
}
6361

remotes_test.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import (
1616
func TestRemotesTable_Name(t *testing.T) {
1717
require := require.New(t)
1818

19-
f := fixtures.Basic().One()
19+
f := fixtures.ByTag("worktree").One()
2020
table := getTable(require, f, remotesTableName)
2121
require.Equal(remotesTableName, table.Name())
2222

@@ -29,23 +29,23 @@ func TestRemotesTable_Name(t *testing.T) {
2929
func TestRemotesTable_Children(t *testing.T) {
3030
require := require.New(t)
3131

32-
f := fixtures.Basic().One()
32+
f := fixtures.ByTag("worktree").One()
3333
table := getTable(require, f, remotesTableName)
3434
require.Equal(0, len(table.Children()))
3535
}
3636

3737
func TestRemotesTable_RowIter(t *testing.T) {
3838
require := require.New(t)
3939

40-
f := fixtures.Basic().One()
40+
f := fixtures.ByTag("worktree").One()
4141
table := getTable(require, f, remotesTableName)
4242

4343
remotes, ok := table.(*remotesTable)
4444
require.True(ok)
4545

4646
pool := remotes.pool
47-
repository, ok := pool.GetPos(0)
48-
require.True(ok)
47+
repository, err := pool.GetPos(0)
48+
require.NoError(err)
4949

5050
repo := repository.Repo
5151

@@ -58,17 +58,17 @@ func TestRemotesTable_RowIter(t *testing.T) {
5858
},
5959
}
6060

61-
_, err := repo.CreateRemote(&config)
62-
require.Nil(err)
61+
_, err = repo.CreateRemote(&config)
62+
require.NoError(err)
6363

6464
rows, err := sql.NodeToRows(sql.NewBaseSession(context.TODO()), table)
65-
require.Nil(err)
65+
require.NoError(err)
6666
require.Len(rows, 3)
6767

6868
schema := table.Schema()
6969
for idx, row := range rows {
7070
err := schema.CheckRow(row)
71-
require.Nil(err, "row %d doesn't conform to schema", idx)
71+
require.NoError(err, "row %d doesn't conform to schema", idx)
7272

7373
if row[1] == "my_remote" {
7474
urlstring, ok := row[2].(string)

repositories_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import (
1313
func TestRepositoriesTable_Name(t *testing.T) {
1414
require := require.New(t)
1515

16-
f := fixtures.Basic().One()
16+
f := fixtures.ByTag("worktree").One()
1717
table := getTable(require, f, repositoriesTableName)
1818
require.Equal(repositoriesTableName, table.Name())
1919

@@ -26,7 +26,7 @@ func TestRepositoriesTable_Name(t *testing.T) {
2626
func TestRepositoriesTable_Children(t *testing.T) {
2727
require := require.New(t)
2828

29-
f := fixtures.Basic().One()
29+
f := fixtures.ByTag("worktree").One()
3030
table := getTable(require, f, repositoriesTableName)
3131
require.Equal(0, len(table.Children()))
3232
}
@@ -42,7 +42,7 @@ func TestRepositoriesTable_RowIter(t *testing.T) {
4242
pool := NewRepositoryPool()
4343

4444
for _, id := range repoIDs {
45-
pool.Add(id, nil)
45+
pool.Add(id, "")
4646
}
4747

4848
db := NewDatabase(repositoriesTableName, &pool)
@@ -55,7 +55,7 @@ func TestRepositoriesTable_RowIter(t *testing.T) {
5555
require.NotNil(table)
5656

5757
rows, err := sql.NodeToRows(sql.NewBaseSession(context.TODO()), table)
58-
require.Nil(err)
58+
require.NoError(err)
5959
require.Len(rows, len(repoIDs))
6060

6161
idArray := make([]string, len(repoIDs))
@@ -67,7 +67,7 @@ func TestRepositoriesTable_RowIter(t *testing.T) {
6767
schema := table.Schema()
6868
for idx, row := range rows {
6969
err := schema.CheckRow(row)
70-
require.Nil(err, "row %d doesn't conform to schema", idx)
70+
require.NoError(err, "row %d doesn't conform to schema", idx)
7171
}
7272
}
7373

repository_pool.go

Lines changed: 48 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -25,32 +25,50 @@ func NewRepository(id string, repo *git.Repository) Repository {
2525
}
2626
}
2727

28-
// RepositoryPool holds a pool of initialized git repositories and
28+
// NewRepositoryFromPath creates and initializes a new Repository structure
29+
// and initializes a go-git repository
30+
func NewRepositoryFromPath(id, path string) (Repository, error) {
31+
repo, err := git.PlainOpen(path)
32+
if err != nil {
33+
return Repository{}, err
34+
}
35+
36+
return NewRepository(id, repo), nil
37+
}
38+
39+
// RepositoryPool holds a pool git repository paths and
2940
// functionality to open and iterate them.
3041
type RepositoryPool struct {
31-
repositories []Repository
42+
repositories map[string]string
43+
idOrder []string
3244
}
3345

3446
// NewRepositoryPool initializes a new RepositoryPool
3547
func NewRepositoryPool() RepositoryPool {
36-
return RepositoryPool{}
48+
return RepositoryPool{
49+
repositories: make(map[string]string),
50+
}
3751
}
3852

3953
// Add inserts a new repository in the pool
40-
func (p *RepositoryPool) Add(id string, repo *git.Repository) {
41-
repository := NewRepository(id, repo)
42-
p.repositories = append(p.repositories, repository)
54+
func (p *RepositoryPool) Add(id, path string) {
55+
_, ok := p.repositories[id]
56+
if !ok {
57+
p.idOrder = append(p.idOrder, id)
58+
}
59+
60+
p.repositories[id] = path
4361
}
4462

45-
// AddGit opens a new git repository and adds it to the pool. It
63+
// AddGit checks if a git repository can be opened and adds it to the pool. It
4664
// also sets its path as ID.
4765
func (p *RepositoryPool) AddGit(path string) (string, error) {
48-
repo, err := git.PlainOpen(path)
66+
_, err := git.PlainOpen(path)
4967
if err != nil {
5068
return "", err
5169
}
5270

53-
p.Add(path, repo)
71+
p.Add(path, path)
5472

5573
return path, nil
5674
}
@@ -65,26 +83,33 @@ func (p *RepositoryPool) AddDir(path string) error {
6583
for _, f := range dirs {
6684
if f.IsDir() {
6785
name := filepath.Join(path, f.Name())
68-
repo, err := git.PlainOpen(name)
69-
if err != nil {
70-
// TODO: log that the repo could not be opened
71-
} else {
72-
p.Add(f.Name(), repo)
73-
}
86+
// TODO: log that the repo could not be opened
87+
p.AddGit(name)
7488
}
7589
}
7690

7791
return nil
7892
}
7993

80-
// GetPos retrieves a repository at a given position. It returns false
81-
// as second return value if the position is out of bounds.
82-
func (p *RepositoryPool) GetPos(pos int) (*Repository, bool) {
94+
// GetPos retrieves a repository at a given position. If the position is
95+
// out of bounds it returns io.EOF
96+
func (p *RepositoryPool) GetPos(pos int) (*Repository, error) {
8397
if pos >= len(p.repositories) {
84-
return nil, false
98+
return nil, io.EOF
99+
}
100+
101+
id := p.idOrder[pos]
102+
if id == "" {
103+
return nil, io.EOF
104+
}
105+
106+
path := p.repositories[id]
107+
repo, err := NewRepositoryFromPath(id, path)
108+
if err != nil {
109+
return nil, err
85110
}
86111

87-
return &p.repositories[pos], true
112+
return &repo, nil
88113
}
89114

90115
// RepoIter creates a new Repository iterator
@@ -106,9 +131,9 @@ type RepositoryIter struct {
106131
// Next retrieves the next Repository. It returns io.EOF as error
107132
// when there are no more Repositories to retrieve.
108133
func (i *RepositoryIter) Next() (*Repository, error) {
109-
r, ok := i.pool.GetPos(i.pos)
110-
if !ok {
111-
return nil, io.EOF
134+
r, err := i.pool.GetPos(i.pos)
135+
if err != nil {
136+
return nil, err
112137
}
113138

114139
i.pos++

0 commit comments

Comments
 (0)