Skip to content

Commit 1ee2440

Browse files
committed
cli: added support to specify format per directory
Global format can be specified with --format, --bare, --bucket and --rooted. To mix directories the format can be specified as a file URL: -d file:///path/to/library?format=siva&bucket=4&rooted=false Also: * Updated builder container to go 1.12 * Added SIVA env to docker init.sh to configure gitbase to use siva library Signed-off-by: Javi Fontan <[email protected]>
1 parent 3bec256 commit 1ee2440

File tree

5 files changed

+285
-74
lines changed

5 files changed

+285
-74
lines changed

Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#================================
22
# Stage 1: Build Gitbase
33
#================================
4-
FROM golang:1.11-alpine as builder
4+
FROM golang:1.12-alpine as builder
55

66
ENV GITBASE_REPO=github.com/src-d/gitbase
77
ENV GITBASE_PATH=$GOPATH/src/$GITBASE_REPO

cmd/gitbase/command/server.go

Lines changed: 140 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,24 @@ package command
33
import (
44
"fmt"
55
"net"
6+
"net/url"
67
"os"
78
"path/filepath"
9+
"regexp"
810
"runtime"
911
"strconv"
1012
"time"
1113

1214
"github.com/src-d/gitbase"
1315
"github.com/src-d/gitbase/internal/function"
1416
"github.com/src-d/gitbase/internal/rule"
15-
"github.com/src-d/go-borges/libraries"
16-
"github.com/src-d/go-borges/plain"
17-
"github.com/src-d/go-borges/siva"
1817

1918
"github.com/opentracing/opentracing-go"
2019
"github.com/sirupsen/logrus"
2120
"github.com/src-d/go-borges"
21+
"github.com/src-d/go-borges/libraries"
22+
"github.com/src-d/go-borges/plain"
23+
"github.com/src-d/go-borges/siva"
2224
sqle "github.com/src-d/go-mysql-server"
2325
"github.com/src-d/go-mysql-server/auth"
2426
"github.com/src-d/go-mysql-server/server"
@@ -53,8 +55,10 @@ type Server struct {
5355
Name string `long:"db" default:"gitbase" description:"Database name"`
5456
Version string // Version of the application.
5557
Directories []string `short:"d" long:"directories" description:"Path where standard git repositories are located, multiple directories can be defined."`
56-
Siva []string `short:"s" long:"siva" description:"Path where siva git repositories are located, multiple directories can be defined."`
58+
Format string `long:"format" default:"git" choice:"git" choice:"siva" description:"Library format"`
5759
Bucket int `long:"bucket" default:"2" description:"Bucketing level to use with siva libraries"`
60+
Bare bool `long:"bare" description:"Sets the library to use bare git repositories, used only with git format libraries"`
61+
NonRooted bool `long:"non-rooted" description:"Disables treating siva files as rooted repositories"`
5862
Host string `long:"host" default:"localhost" description:"Host where the server is going to listen"`
5963
Port int `short:"p" long:"port" default:"3306" description:"Port where the server is going to listen"`
6064
User string `short:"u" long:"user" default:"root" description:"User name used for connection"`
@@ -261,21 +265,45 @@ func (c *Server) registerDrivers() error {
261265
}
262266

263267
func (c *Server) addDirectories() error {
264-
if len(c.Directories) == 0 && len(c.Siva) == 0 {
265-
logrus.Error("At least one folder should be provided.")
268+
if len(c.Directories) == 0 {
269+
logrus.Error("at least one folder should be provided.")
266270
}
267271

268-
sivaOpts := siva.LibraryOptions{
269-
Transactional: true,
270-
RootedRepo: true,
271-
Cache: c.sharedCache,
272-
Bucket: c.Bucket,
273-
Performance: true,
274-
RegistryCache: 100000,
272+
for _, d := range c.Directories {
273+
dir := directory{
274+
Path: d,
275+
Format: c.Format,
276+
Bare: c.Bare,
277+
Bucket: c.Bucket,
278+
Rooted: !c.NonRooted,
279+
}
280+
281+
dir, err := parseDirectory(dir)
282+
if err != nil {
283+
return err
284+
}
285+
286+
err = c.addDirectory(dir)
287+
if err != nil {
288+
return err
289+
}
275290
}
276291

277-
for _, d := range c.Siva {
278-
lib, err := siva.NewLibrary(d, osfs.New(d), sivaOpts)
292+
return nil
293+
}
294+
295+
func (c *Server) addDirectory(d directory) error {
296+
if d.Format == "siva" {
297+
sivaOpts := siva.LibraryOptions{
298+
Transactional: true,
299+
RootedRepo: d.Rooted,
300+
Cache: c.sharedCache,
301+
Bucket: d.Bucket,
302+
Performance: true,
303+
RegistryCache: 100000,
304+
}
305+
306+
lib, err := siva.NewLibrary(d.Path, osfs.New(d.Path), sivaOpts)
279307
if err != nil {
280308
return err
281309
}
@@ -284,26 +312,116 @@ func (c *Server) addDirectories() error {
284312
if err != nil {
285313
return err
286314
}
287-
}
288315

289-
if len(c.Directories) == 0 {
290316
return nil
291317
}
292318

293319
plainOpts := &plain.LocationOptions{
294320
Cache: c.sharedCache,
295321
Performance: true,
322+
Bare: d.Bare,
296323
}
297324

298-
p := plain.NewLibrary(borges.LibraryID("plain"))
299-
for _, d := range c.Directories {
300-
loc, err := plain.NewLocation(borges.LocationID(d), osfs.New(d), plainOpts)
325+
if c.plainLibrary == nil {
326+
c.plainLibrary = plain.NewLibrary(borges.LibraryID("plain"))
327+
err := c.rootLibrary.Add(c.plainLibrary)
301328
if err != nil {
302329
return err
303330
}
331+
}
332+
333+
loc, err := plain.NewLocation(
334+
borges.LocationID(d.Path),
335+
osfs.New(d.Path),
336+
plainOpts)
337+
if err != nil {
338+
return err
339+
}
340+
341+
c.plainLibrary.AddLocation(loc)
342+
343+
return nil
344+
}
345+
346+
type directory struct {
347+
Path string
348+
Format string
349+
Bucket int
350+
Rooted bool
351+
Bare bool
352+
}
353+
354+
var (
355+
uriReg = regexp.MustCompile(`^\w+:.*`)
356+
ErrInvalid = fmt.Errorf("invalid option")
357+
)
304358

305-
p.AddLocation(loc)
359+
func parseDirectory(dir directory) (directory, error) {
360+
d := dir.Path
361+
362+
if !uriReg.Match([]byte(d)) {
363+
return dir, nil
364+
}
365+
366+
u, err := url.ParseRequestURI(d)
367+
if err != nil {
368+
logrus.Errorf("invalid directory format %v", d)
369+
return dir, err
370+
}
371+
372+
if u.Scheme != "file" {
373+
logrus.Errorf("only file scheme is supported: %v", d)
374+
return dir, fmt.Errorf("scheme not suported in directory %v", d)
375+
}
376+
377+
dir.Path = filepath.Join(u.Hostname(), u.Path)
378+
query := u.Query()
379+
380+
for k, v := range query {
381+
if len(v) != 1 {
382+
logrus.Errorf("invalid number of options for %v", v)
383+
return dir, ErrInvalid
384+
}
385+
386+
val := v[0]
387+
switch k {
388+
case "format":
389+
if val != "siva" && val != "git" {
390+
logrus.Errorf("invalid format in directory, it can only "+
391+
"be siva or git %v", val)
392+
return dir, ErrInvalid
393+
}
394+
dir.Format = val
395+
396+
case "bare":
397+
if val != "true" && val != "false" {
398+
logrus.Errorf("invalid format in bare, it can only "+
399+
"be true or false %v", val)
400+
return dir, ErrInvalid
401+
}
402+
dir.Bare = (val == "true")
403+
404+
case "rooted":
405+
if val != "true" && val != "false" {
406+
logrus.Errorf("invalid format in rooted, it can only "+
407+
"be true or false %v", val)
408+
return dir, ErrInvalid
409+
}
410+
dir.Rooted = (val == "true")
411+
412+
case "bucket":
413+
num, err := strconv.Atoi(val)
414+
if err != nil {
415+
logrus.Errorf("invalid number in bucket: %v", val)
416+
return dir, ErrInvalid
417+
}
418+
dir.Bucket = num
419+
420+
default:
421+
logrus.Errorf("invalid option: %v", k)
422+
return dir, ErrInvalid
423+
}
306424
}
307425

308-
return c.rootLibrary.Add(p)
426+
return dir, nil
309427
}

0 commit comments

Comments
 (0)