Skip to content

Commit 547cf22

Browse files
committed
Add all repositories in the given directory
Signed-off-by: Javi Fontan <[email protected]>
1 parent 66041e6 commit 547cf22

File tree

3 files changed

+85
-14
lines changed

3 files changed

+85
-14
lines changed

cmd/gitquery/query_base.go

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,10 @@ package main
33
import (
44
"io"
55
"os"
6-
"path/filepath"
76

87
"github.com/src-d/gitquery"
98
"github.com/src-d/gitquery/internal/format"
109

11-
gogit "gopkg.in/src-d/go-git.v4"
1210
"gopkg.in/src-d/go-git.v4/utils/ioutil"
1311
sqle "gopkg.in/src-d/go-mysql-server.v0"
1412
"gopkg.in/src-d/go-mysql-server.v0/sql"
@@ -17,7 +15,7 @@ import (
1715
type cmdQueryBase struct {
1816
cmd
1917

20-
Path []string `short:"p" long:"path" description:"Path where the git repository is located, can be used several times"`
18+
Path string `short:"p" long:"path" description:"Path where the git repositories are located, one per dir"`
2119

2220
engine *sqle.Engine
2321
name string
@@ -33,16 +31,10 @@ func (c *cmdQueryBase) buildDatabase() error {
3331
var err error
3432

3533
pool := gitquery.NewRepositoryPool()
36-
37-
for _, path := range c.Path {
38-
r, err := gogit.PlainOpen(path)
39-
if err != nil {
40-
return err
41-
}
42-
43-
pool.Add(path, r)
44-
45-
c.name = filepath.Base(filepath.Join(path, ".."))
34+
err = pool.AddDir(c.Path)
35+
if err != nil {
36+
println("ERR", err.Error())
37+
return err
4638
}
4739

4840
c.engine.AddDatabase(gitquery.NewDatabase(c.name, &pool))

repository_pool.go

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ package gitquery
22

33
import (
44
"io"
5+
"io/ioutil"
6+
"path/filepath"
57
"runtime"
68
"sync"
79

@@ -53,6 +55,28 @@ func (p *RepositoryPool) AddGit(path string) (string, error) {
5355
return path, nil
5456
}
5557

58+
// AddDir adds all direct subdirectories from path as repos
59+
func (p *RepositoryPool) AddDir(path string) error {
60+
dirs, err := ioutil.ReadDir(path)
61+
if err != nil {
62+
return err
63+
}
64+
65+
for _, f := range dirs {
66+
if f.IsDir() {
67+
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+
}
74+
}
75+
}
76+
77+
return nil
78+
}
79+
5680
// GetPos retrieves a repository at a given position. It returns false
5781
// as second return value if the position is out of bounds.
5882
func (p *RepositoryPool) GetPos(pos int) (*Repository, bool) {
@@ -130,7 +154,6 @@ func NewRowRepoIter(
130154
pool *RepositoryPool,
131155
iter RowRepoIter,
132156
) (*rowRepoIter, error) {
133-
134157
rIter, err := pool.RepoIter()
135158
if err != nil {
136159
return nil, err

repository_pool_test.go

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@ package gitquery
22

33
import (
44
"io"
5+
"io/ioutil"
6+
"os"
7+
"path/filepath"
8+
"strconv"
59
"sync"
610
"testing"
711

@@ -213,3 +217,55 @@ func TestRepositoryRowIterator(t *testing.T) {
213217

214218
wg.Wait()
215219
}
220+
221+
func TestRepositoryPoolAddDir(t *testing.T) {
222+
require := require.New(t)
223+
224+
tmpDir, err := ioutil.TempDir("", "gitquery-test")
225+
require.Nil(err)
226+
227+
max := 64
228+
229+
for i := 0; i < max; i++ {
230+
orig := fixtures.Basic().ByTag("worktree").One().Worktree().Root()
231+
p := filepath.Join(tmpDir, strconv.Itoa(i))
232+
233+
err := os.Rename(orig, p)
234+
require.Nil(err)
235+
}
236+
237+
pool := NewRepositoryPool()
238+
err = pool.AddDir(tmpDir)
239+
require.Nil(err)
240+
241+
require.Equal(max, len(pool.repositories))
242+
243+
arrayID := make([]string, max)
244+
arrayExpected := make([]string, max)
245+
246+
for i := 0; i < max; i++ {
247+
repo, ok := pool.GetPos(i)
248+
require.True(ok)
249+
arrayID[i] = repo.ID
250+
arrayExpected[i] = strconv.Itoa(i)
251+
252+
iter, err := repo.Repo.CommitObjects()
253+
require.Nil(err)
254+
255+
counter := 0
256+
for {
257+
commit, err := iter.Next()
258+
if err == io.EOF {
259+
break
260+
}
261+
262+
require.Nil(err)
263+
require.NotNil(commit)
264+
counter++
265+
}
266+
267+
require.Equal(9, counter)
268+
}
269+
270+
require.ElementsMatch(arrayExpected, arrayID)
271+
}

0 commit comments

Comments
 (0)