Skip to content

Commit 9dcd907

Browse files
committed
cmd/gitbase: load siva files and git indistinctly
Signed-off-by: Manuel Carmona <[email protected]>
1 parent 844d748 commit 9dcd907

File tree

2 files changed

+101
-22
lines changed

2 files changed

+101
-22
lines changed

cmd/gitbase/command/server.go

Lines changed: 89 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"os"
66
"path/filepath"
77
"strconv"
8+
"strings"
89

910
"github.com/src-d/gitbase"
1011
"github.com/src-d/gitbase/internal/function"
@@ -14,6 +15,7 @@ import (
1415
gopilosa "github.com/pilosa/go-pilosa"
1516
"github.com/sirupsen/logrus"
1617
"github.com/uber/jaeger-client-go/config"
18+
git "gopkg.in/src-d/go-git.v4"
1719
sqle "gopkg.in/src-d/go-mysql-server.v0"
1820
"gopkg.in/src-d/go-mysql-server.v0/server"
1921
"gopkg.in/src-d/go-mysql-server.v0/sql"
@@ -34,8 +36,10 @@ const (
3436
// Server represents the `server` command of gitbase cli tool.
3537
type Server struct {
3638
Verbose bool `short:"v" description:"Activates the verbose mode"`
37-
Git []string `short:"g" long:"git" description:"Path where the git repositories are located, multiple directories can be defined. Accepts globs."`
38-
Siva []string `long:"siva" description:"Path where the siva repositories are located, multiple directories can be defined. Accepts globs."`
39+
Directories []string `short:"d" long:"directories" description:"Path where the git repositories are located (standard and siva), multiple directories can be defined. Accepts globs."`
40+
Depth int `long:"depth" default:"1000" description:"load repositories looking at less than <depth> nested subdirectories."`
41+
DisableGit bool `long:"no-git" description:"disable the load of git standard repositories."`
42+
DisableSiva bool `long:"no-siva" description:"disable the load of siva files."`
3943
Host string `long:"host" default:"localhost" description:"Host where the server is going to listen"`
4044
Port int `short:"p" long:"port" default:"3306" description:"Port where the server is going to listen"`
4145
User string `short:"u" long:"user" default:"root" description:"User name used for connection"`
@@ -196,53 +200,116 @@ func (c *Server) registerDrivers() error {
196200
}
197201

198202
func (c *Server) addDirectories() error {
199-
if len(c.Git) == 0 && len(c.Siva) == 0 {
200-
logrus.Error("At least one git folder or siva folder should be provided.")
203+
if len(c.Directories) == 0 {
204+
logrus.Error("At least one folder should be provided.")
201205
}
202206

203-
for _, pattern := range c.Git {
204-
if err := c.addGitPattern(pattern); err != nil {
205-
return err
206-
}
207+
if c.DisableGit && c.DisableSiva {
208+
logrus.Warn("The load of git repositories and siva files are disabled," +
209+
" no repository will be added.")
210+
211+
return nil
207212
}
208213

209-
for _, pattern := range c.Siva {
210-
if err := c.addSivaPattern(pattern); err != nil {
214+
if c.Depth < 1 {
215+
logrus.Warn("--depth flag set to a number less than 1," +
216+
" no repository will be added.")
217+
218+
return nil
219+
}
220+
221+
for _, directory := range c.Directories {
222+
if err := c.addDirectory(directory); err != nil {
211223
return err
212224
}
213225
}
214226

215227
return nil
216228
}
217229

218-
func (c *Server) addGitPattern(pattern string) error {
219-
prefix, matches, err := gitbase.PatternMatches(pattern)
230+
func (c *Server) addDirectory(directory string) error {
231+
_, matches, err := gitbase.PatternMatches(directory)
220232
if err != nil {
221233
return err
222234
}
223235

224-
for _, m := range matches {
225-
logrus.WithField("dir", m).Debug("git repositories directory added")
226-
if err := c.pool.AddDir(prefix, m); err != nil {
227-
return err
236+
for _, match := range matches {
237+
if err := c.addMatch(match); err != nil {
238+
logrus.WithFields(logrus.Fields{
239+
"path": match,
240+
"error": err,
241+
}).Error("path couldn't be inspected")
228242
}
229243
}
230244

231245
return nil
232246
}
233247

234-
func (c *Server) addSivaPattern(pattern string) error {
235-
matches, err := filepath.Glob(pattern)
248+
func (c *Server) addMatch(match string) error {
249+
root, err := filepath.Abs(match)
236250
if err != nil {
237251
return err
238252
}
239253

240-
for _, m := range matches {
241-
logrus.WithField("dir", m).Debug("siva repositories directory added")
242-
if err := c.pool.AddSivaDir(m); err != nil {
254+
initDepth := strings.Count(root, string(os.PathSeparator))
255+
return filepath.Walk(root, func(path string, info os.FileInfo, err error) error {
256+
if err != nil {
243257
return err
244258
}
259+
260+
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
285+
}
286+
287+
depth := strings.Count(path, string(os.PathSeparator)) - initDepth
288+
if depth >= c.Depth {
289+
return filepath.SkipDir
290+
}
291+
292+
return nil
293+
}
294+
295+
if !c.DisableSiva &&
296+
info.Mode().IsRegular() && strings.HasSuffix(info.Name(), ".siva") {
297+
c.pool.AddSivaFile(path, path)
298+
logrus.WithField("path", path).Debug("repository added")
299+
}
300+
301+
return nil
302+
})
303+
}
304+
305+
func isGitRepo(path string) (bool, error) {
306+
if _, err := git.PlainOpen(path); err != nil {
307+
if git.ErrRepositoryNotExists == err {
308+
return false, nil
309+
}
310+
311+
return false, err
245312
}
246313

247-
return nil
314+
return true, nil
248315
}

repository_pool.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,18 @@ func (p *RepositoryPool) addSivaDir(root, path string, recursive bool) error {
249249
return nil
250250
}
251251

252+
// AddSivaFile adds to the pool the given file if it's a siva repository,
253+
// that is, has the .siva extension
254+
func (p *RepositoryPool) AddSivaFile(id, path string) {
255+
file := filepath.Base(path)
256+
if !strings.HasSuffix(file, ".siva") {
257+
logrus.WithField("file", file).Warn("found a non-siva file")
258+
}
259+
260+
p.Add(sivaRepo(id, path))
261+
logrus.WithField("file", file).Debug("repository added")
262+
}
263+
252264
// addSivaFile adds to the pool the given file if it's a siva repository,
253265
// that is, has the .siva extension.
254266
func (p *RepositoryPool) addSivaFile(root, path string, f os.FileInfo) {

0 commit comments

Comments
 (0)