Skip to content

Commit adb1156

Browse files
committed
*: don't check in repository pool methods if is a siva/git valid repositoriy when is added
Signed-off-by: Manuel Carmona <[email protected]>
1 parent 154c3dc commit adb1156

File tree

8 files changed

+90
-60
lines changed

8 files changed

+90
-60
lines changed

cmd/gitbase/command/server.go

Lines changed: 29 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ import (
1515
gopilosa "github.com/pilosa/go-pilosa"
1616
"github.com/sirupsen/logrus"
1717
"github.com/uber/jaeger-client-go/config"
18-
git "gopkg.in/src-d/go-git.v4"
1918
sqle "gopkg.in/src-d/go-mysql-server.v0"
2019
"gopkg.in/src-d/go-mysql-server.v0/server"
2120
"gopkg.in/src-d/go-mysql-server.v0/sql"
@@ -258,30 +257,8 @@ func (c *Server) addMatch(match string) error {
258257
}
259258

260259
if info.IsDir() {
261-
ok, err := isGitRepo(path)
262-
if err != nil {
263-
logrus.WithFields(logrus.Fields{
264-
"path": path,
265-
"error": err,
266-
}).Error("path couldn't be inspected")
267-
268-
return filepath.SkipDir
269-
}
270-
271-
if ok {
272-
if !c.DisableGit {
273-
if err := c.pool.AddGitWithID(info.Name(), path); err != nil {
274-
logrus.WithFields(logrus.Fields{
275-
"id": info.Name(),
276-
"path": path,
277-
"error": err,
278-
}).Error("repository could not be added")
279-
}
280-
281-
logrus.WithField("path", path).Debug("repository added")
282-
}
283-
284-
return filepath.SkipDir
260+
if err := c.addIfGitRepo(path); err != nil {
261+
return err
285262
}
286263

287264
depth := strings.Count(path, string(os.PathSeparator)) - initDepth
@@ -293,7 +270,7 @@ func (c *Server) addMatch(match string) error {
293270
}
294271

295272
if !c.DisableSiva &&
296-
info.Mode().IsRegular() && strings.HasSuffix(info.Name(), ".siva") {
273+
info.Mode().IsRegular() && gitbase.IsSivaFile(info.Name()) {
297274
if err := c.pool.AddSivaFile(path); err != nil {
298275
logrus.WithFields(logrus.Fields{
299276
"path": path,
@@ -310,14 +287,34 @@ func (c *Server) addMatch(match string) error {
310287
})
311288
}
312289

313-
func isGitRepo(path string) (bool, error) {
314-
if _, err := git.PlainOpen(path); err != nil {
315-
if git.ErrRepositoryNotExists == err {
316-
return false, nil
290+
func (c *Server) addIfGitRepo(path string) error {
291+
ok, err := gitbase.IsGitRepo(path)
292+
if err != nil {
293+
logrus.WithFields(logrus.Fields{
294+
"path": path,
295+
"error": err,
296+
}).Error("path couldn't be inspected")
297+
298+
return filepath.SkipDir
299+
}
300+
301+
if ok {
302+
if !c.DisableGit {
303+
base := filepath.Base(path)
304+
if err := c.pool.AddGitWithID(base, path); err != nil {
305+
logrus.WithFields(logrus.Fields{
306+
"id": base,
307+
"path": path,
308+
"error": err,
309+
}).Error("repository could not be added")
310+
}
311+
312+
logrus.WithField("path", path).Debug("repository added")
317313
}
318314

319-
return false, err
315+
// either the repository is added or not, the path must be skipped
316+
return filepath.SkipDir
320317
}
321318

322-
return true, nil
319+
return nil
323320
}

common_test.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,14 @@ func buildSession(t *testing.T, repos fixtures.Fixtures,
3838
pool := NewRepositoryPool()
3939
for _, fixture := range repos {
4040
path := fixture.Worktree().Root()
41-
if err := pool.AddGit(path); err == nil {
42-
_, err := pool.GetRepo(path)
43-
require.NoError(err)
44-
paths = append(paths, path)
41+
ok, err := IsGitRepo(path)
42+
require.NoError(err)
43+
if ok {
44+
if err := pool.AddGit(path); err == nil {
45+
_, err := pool.GetRepo(path)
46+
require.NoError(err)
47+
paths = append(paths, path)
48+
}
4549
}
4650
}
4751

integration_test.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -419,9 +419,8 @@ func TestMissingHeadRefs(t *testing.T) {
419419
return err
420420
}
421421

422-
err = pool.AddSivaFile(path)
423-
if err != nil {
424-
require.EqualError(err, "the repository is not: siva")
422+
if gitbase.IsSivaFile(path) {
423+
require.NoError(pool.AddSivaFile(path))
425424
}
426425

427426
return nil

path_utils.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@ package gitbase
33
import (
44
"path/filepath"
55
"regexp"
6+
"strings"
7+
8+
git "gopkg.in/src-d/go-git.v4"
69
)
710

811
// RegMatchChars matches a string with a glob expression inside.
@@ -32,3 +35,21 @@ func removeDsStore(matches []string) []string {
3235
}
3336
return result
3437
}
38+
39+
// IsGitRepo checks that the given path is a git repository.
40+
func IsGitRepo(path string) (bool, error) {
41+
if _, err := git.PlainOpen(path); err != nil {
42+
if git.ErrRepositoryNotExists == err {
43+
return false, nil
44+
}
45+
46+
return false, err
47+
}
48+
49+
return true, nil
50+
}
51+
52+
//IsSivaFile checks that the given file is a siva file.
53+
func IsSivaFile(file string) bool {
54+
return strings.HasSuffix(file, ".siva")
55+
}

path_utils_test.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"testing"
77

88
"github.com/stretchr/testify/require"
9+
fixtures "gopkg.in/src-d/go-git-fixtures.v3"
910
)
1011

1112
func TestPatternMatches(t *testing.T) {
@@ -40,3 +41,23 @@ func TestPatternMatches(t *testing.T) {
4041
})
4142
}
4243
}
44+
45+
func TestIsGitRepo(t *testing.T) {
46+
var require = require.New(t)
47+
48+
ok, err := IsGitRepo("/do/not/exist")
49+
require.NoError(err)
50+
require.False(ok)
51+
52+
path := fixtures.Basic().ByTag("worktree").One().Worktree().Root()
53+
ok, err = IsGitRepo(path)
54+
require.NoError(err)
55+
require.True(ok)
56+
}
57+
58+
func TestIsSivaFile(t *testing.T) {
59+
var require = require.New(t)
60+
61+
require.True(IsSivaFile("is.siva"))
62+
require.False(IsSivaFile("not-siva"))
63+
}

repository_pool.go

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
55
"io/ioutil"
66
"os"
77
"path/filepath"
8-
"strings"
98
"sync"
109
"sync/atomic"
1110

@@ -181,24 +180,13 @@ func (p *RepositoryPool) AddGit(path string) error {
181180
// AddGitWithID checks if a git repository can be opened and adds it to the
182181
// pool. ID should be specified.
183182
func (p *RepositoryPool) AddGitWithID(id, path string) error {
184-
_, err := git.PlainOpen(path)
185-
if err != nil {
186-
return errRepoCannotOpen.Wrap(err, path)
187-
}
188-
189183
return p.Add(gitRepo(id, path))
190184
}
191185

192186
// AddSivaFile adds to the pool the given file if it's a siva repository,
193187
// that is, has the .siva extension
194188
func (p *RepositoryPool) AddSivaFile(path string) error {
195-
file := filepath.Base(path)
196-
if !strings.HasSuffix(file, ".siva") {
197-
return errInvalidRepoKind.New("siva")
198-
}
199-
200-
p.Add(sivaRepo(path, path))
201-
return nil
189+
return p.Add(sivaRepo(path, path))
202190
}
203191

204192
// GetPos retrieves a repository at a given position. If the position is

repository_pool_test.go

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -87,10 +87,6 @@ func TestRepositoryPoolGit(t *testing.T) {
8787

8888
pool := NewRepositoryPool()
8989

90-
err := pool.AddGit("/do/not/exist")
91-
require.Error(err)
92-
require.True(errRepoCannotOpen.Is(err))
93-
9490
require.NoError(pool.AddGit(path))
9591

9692
repo, err := pool.GetPos(0)
@@ -252,9 +248,8 @@ func TestRepositoryPoolSiva(t *testing.T) {
252248
return err
253249
}
254250

255-
err = pool.AddSivaFile(path)
256-
if err != nil {
257-
require.True(errInvalidRepoKind.Is(err))
251+
if IsSivaFile(path) {
252+
require.NoError(pool.AddSivaFile(path))
258253
}
259254

260255
return nil

squash_iterator_test.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -726,7 +726,12 @@ func setupIterWithErrors(t *testing.T, badRepo bool, skipErrors bool) (*sql.Cont
726726
}
727727

728728
for _, f := range fixtures.ByTag("worktree") {
729-
pool.AddGit(f.Worktree().Root())
729+
path := f.Worktree().Root()
730+
ok, err := IsGitRepo(path)
731+
require.NoError(t, err)
732+
if ok {
733+
pool.AddGit(f.Worktree().Root())
734+
}
730735
}
731736

732737
session := NewSession(pool, WithSkipGitErrors(skipErrors))

0 commit comments

Comments
 (0)