Skip to content

Commit e3ac221

Browse files
authored
Merge branch 'master' into integration_test/883
2 parents 2bff150 + 5aba59c commit e3ac221

16 files changed

+270
-44
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
66

77
## [Unreleased]
88

9+
- git libraries bare or non bare format is automatically detected ([#897](https://github.com/src-d/gitbase/pull/897))
10+
- Fix bug that created multiple object cache with incorrect size ([#898](https://github.com/src-d/gitbase/pull/898))
11+
912
## [0.22.0-beta1] - 2019-06-20
1013

1114
### Added

cmd/gitbase/command/server.go

Lines changed: 62 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ type Server struct {
5959
Format string `long:"format" default:"git" choice:"git" choice:"siva" description:"Library format"`
6060
Bucket int `long:"bucket" default:"2" description:"Bucketing level to use with siva libraries"`
6161
Bare bool `long:"bare" description:"Sets the library to use bare git repositories, used only with git format libraries"`
62+
NonBare bool `long:"non-bare" description:"Sets the library to use non bare git repositories, used only with git format libraries"`
6263
NonRooted bool `long:"non-rooted" description:"Disables treating siva files as rooted repositories"`
6364
Host string `long:"host" default:"localhost" description:"Host where the server is going to listen"`
6465
Port int `short:"p" long:"port" default:"3306" description:"Port where the server is going to listen"`
@@ -122,6 +123,10 @@ func (c *Server) Execute(args []string) error {
122123
logrus.SetLevel(logrus.DebugLevel)
123124
}
124125

126+
if c.Bare && c.NonBare {
127+
return fmt.Errorf("cannot use both --bare and --non-bare")
128+
}
129+
125130
// info is the default log level
126131
if c.LogLevel != "info" {
127132
level, err := logrus.ParseLevel(c.LogLevel)
@@ -220,10 +225,10 @@ func (c *Server) buildDatabase() error {
220225
)
221226
}
222227

223-
c.rootLibrary = libraries.New(libraries.Options{})
224-
c.pool = gitbase.NewRepositoryPool(c.CacheSize*cache.MiByte, c.rootLibrary)
228+
c.sharedCache = cache.NewObjectLRU(c.CacheSize * cache.MiByte)
225229

226-
c.sharedCache = cache.NewObjectLRU(512 * cache.MiByte)
230+
c.rootLibrary = libraries.New(libraries.Options{})
231+
c.pool = gitbase.NewRepositoryPool(c.sharedCache, c.rootLibrary)
227232

228233
if err := c.addDirectories(); err != nil {
229234
return err
@@ -270,11 +275,19 @@ func (c *Server) addDirectories() error {
270275
logrus.Error("at least one folder should be provided.")
271276
}
272277

278+
defaultBare := bareAuto
279+
switch {
280+
case c.Bare:
281+
defaultBare = bareOn
282+
case c.NonBare:
283+
defaultBare = bareOff
284+
}
285+
273286
for _, d := range c.Directories {
274287
dir := directory{
275288
Path: d,
276289
Format: c.Format,
277-
Bare: c.Bare,
290+
Bare: defaultBare,
278291
Bucket: c.Bucket,
279292
Rooted: !c.NonRooted,
280293
}
@@ -327,10 +340,15 @@ func (c *Server) addDirectory(d directory) error {
327340
return nil
328341
}
329342

343+
bare, err := discoverBare(d)
344+
if err != nil {
345+
return err
346+
}
347+
330348
plainOpts := &plain.LocationOptions{
331349
Cache: c.sharedCache,
332350
Performance: true,
333-
Bare: d.Bare,
351+
Bare: bare,
334352
}
335353

336354
if c.plainLibrary == nil {
@@ -354,12 +372,20 @@ func (c *Server) addDirectory(d directory) error {
354372
return nil
355373
}
356374

375+
type bareOpt int
376+
377+
const (
378+
bareAuto bareOpt = iota
379+
bareOn
380+
bareOff
381+
)
382+
357383
type directory struct {
358384
Path string
359385
Format string
360386
Bucket int
361387
Rooted bool
362-
Bare bool
388+
Bare bareOpt
363389
}
364390

365391
var (
@@ -405,12 +431,18 @@ func parseDirectory(dir directory) (directory, error) {
405431
dir.Format = val
406432

407433
case "bare":
408-
if val != "true" && val != "false" {
434+
switch val {
435+
case "true":
436+
dir.Bare = bareOn
437+
case "false":
438+
dir.Bare = bareOff
439+
case "auto":
440+
dir.Bare = bareAuto
441+
default:
409442
logrus.Errorf("invalid value in bare, it can only "+
410-
"be true or false %v", val)
443+
"be true, false, or auto %v", val)
411444
return dir, ErrInvalid
412445
}
413-
dir.Bare = (val == "true")
414446

415447
case "rooted":
416448
if val != "true" && val != "false" {
@@ -436,3 +468,24 @@ func parseDirectory(dir directory) (directory, error) {
436468

437469
return dir, nil
438470
}
471+
472+
func discoverBare(d directory) (bool, error) {
473+
fs := osfs.New(d.Path)
474+
475+
var bare bool
476+
if d.Bare == bareAuto {
477+
b, err := plain.IsFirstRepositoryBare(fs, "/")
478+
if plain.ErrRepositoriesNotFound.Is(err) {
479+
logrus.WithField("path", d.Path).
480+
Errorf("could not find repositories, assuming non bare format")
481+
} else if err != nil {
482+
return false, err
483+
}
484+
485+
bare = b
486+
} else {
487+
bare = d.Bare == bareOn
488+
}
489+
490+
return bare, nil
491+
}

cmd/gitbase/command/server_test.go

Lines changed: 175 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
11
package command
22

33
import (
4+
"fmt"
5+
"io/ioutil"
6+
"os"
7+
"path/filepath"
48
"testing"
59

10+
fixtures "github.com/src-d/go-git-fixtures"
611
"github.com/stretchr/testify/require"
12+
"gopkg.in/src-d/go-git.v4/plumbing"
713
)
814

915
func TestDirectories(t *testing.T) {
@@ -70,14 +76,14 @@ func TestDirectories(t *testing.T) {
7076
path: "file:///siva/path?bare=true",
7177
expected: directory{
7278
Path: "/siva/path",
73-
Bare: true,
79+
Bare: bareOn,
7480
},
7581
},
7682
{
7783
path: "file:///siva/path?bare=false",
7884
expected: directory{
7985
Path: "/siva/path",
80-
Bare: false,
86+
Bare: bareOff,
8187
},
8288
},
8389
{
@@ -118,7 +124,7 @@ func TestDirectories(t *testing.T) {
118124
expected: directory{
119125
Path: "/siva/path",
120126
Format: "git",
121-
Bare: false,
127+
Bare: bareOff,
122128
},
123129
},
124130
{
@@ -147,3 +153,169 @@ func TestDirectories(t *testing.T) {
147153
})
148154
}
149155
}
156+
157+
func TestDiscoverBare(t *testing.T) {
158+
defer func() {
159+
require.NoError(t, fixtures.Clean())
160+
}()
161+
162+
tmpDir, err := ioutil.TempDir("", "gitbase")
163+
require.NoError(t, err)
164+
defer os.RemoveAll(tmpDir)
165+
166+
emptyDir := filepath.Join(tmpDir, "empty")
167+
err = os.Mkdir(emptyDir, 0777)
168+
require.NoError(t, err)
169+
170+
bareDir := filepath.Join(tmpDir, "bare")
171+
err = os.Mkdir(bareDir, 0777)
172+
require.NoError(t, err)
173+
dir := fixtures.ByTag("worktree").One().DotGit().Root()
174+
err = os.Rename(dir, filepath.Join(bareDir, "repo"))
175+
require.NoError(t, err)
176+
177+
nonBareDir := filepath.Join(tmpDir, "non_bare")
178+
err = os.Mkdir(nonBareDir, 0777)
179+
require.NoError(t, err)
180+
dir = fixtures.ByTag("worktree").One().Worktree().Root()
181+
err = os.Rename(dir, filepath.Join(nonBareDir, "repo"))
182+
require.NoError(t, err)
183+
184+
tests := []struct {
185+
path string
186+
bare bareOpt
187+
expected bool
188+
err bool
189+
}{
190+
{
191+
path: "/does/not/exist",
192+
err: true,
193+
},
194+
{
195+
path: emptyDir,
196+
bare: bareAuto,
197+
expected: false,
198+
},
199+
{
200+
path: emptyDir,
201+
bare: bareOn,
202+
expected: true,
203+
},
204+
{
205+
path: emptyDir,
206+
bare: bareOff,
207+
expected: false,
208+
},
209+
{
210+
path: bareDir,
211+
bare: bareAuto,
212+
expected: true,
213+
},
214+
{
215+
path: bareDir,
216+
bare: bareOn,
217+
expected: true,
218+
},
219+
{
220+
path: bareDir,
221+
bare: bareOff,
222+
expected: false,
223+
},
224+
{
225+
path: nonBareDir,
226+
bare: bareAuto,
227+
expected: false,
228+
},
229+
{
230+
path: nonBareDir,
231+
bare: bareOn,
232+
expected: true,
233+
},
234+
{
235+
path: nonBareDir,
236+
bare: bareOff,
237+
expected: false,
238+
},
239+
}
240+
241+
for _, test := range tests {
242+
dir := directory{
243+
Path: test.path,
244+
Bare: test.bare,
245+
}
246+
247+
t.Run(bareTestName(dir, test.err), func(t *testing.T) {
248+
bare, err := discoverBare(dir)
249+
if test.err {
250+
require.Error(t, err)
251+
return
252+
}
253+
254+
require.NoError(t, err)
255+
require.Equal(t, test.expected, bare)
256+
})
257+
}
258+
}
259+
260+
func bareTestName(d directory, err bool) string {
261+
bare := ""
262+
switch d.Bare {
263+
case bareOn:
264+
bare = "bare"
265+
case bareOff:
266+
bare = "non-bare"
267+
case bareAuto:
268+
bare = "auto"
269+
}
270+
271+
if err {
272+
bare = "error"
273+
}
274+
275+
return fmt.Sprintf("%s_%s", d.Path, bare)
276+
}
277+
278+
func TestCache(t *testing.T) {
279+
require := require.New(t)
280+
281+
tmpDir, err := ioutil.TempDir("", "gitbase")
282+
require.NoError(err)
283+
func() {
284+
require.NoError(os.RemoveAll(tmpDir))
285+
}()
286+
287+
server := &Server{
288+
CacheSize: 512,
289+
Format: "siva",
290+
Bucket: 0,
291+
LogLevel: "debug",
292+
Directories: []string{"../../../_testdata"},
293+
IndexDir: tmpDir,
294+
}
295+
296+
err = server.buildDatabase()
297+
require.NoError(err)
298+
299+
cache := server.sharedCache
300+
pool := server.pool
301+
hash := plumbing.NewHash("dbfab055c70379219cbcf422f05316fdf4e1aed3")
302+
303+
_, ok := cache.Get(hash)
304+
require.False(ok)
305+
306+
repo, err := pool.GetRepo("015da2f4-6d89-7ec8-5ac9-a38329ea875b")
307+
require.NoError(err)
308+
309+
_, ok = repo.Cache().Get(hash)
310+
require.False(ok)
311+
require.Equal(cache, repo.Cache())
312+
313+
_, err = repo.CommitObject(hash)
314+
require.NoError(err)
315+
316+
_, ok = cache.Get(hash)
317+
require.True(ok)
318+
319+
_, ok = repo.Cache().Get(hash)
320+
require.True(ok)
321+
}

common_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ func buildSession(
4949
lib, err := newMultiLibrary()
5050
require.NoError(err)
5151

52-
pool := NewRepositoryPool(cache.DefaultMaxSize, lib)
52+
pool := NewRepositoryPool(cache.NewObjectLRUDefault(), lib)
5353
for _, fixture := range repos {
5454
path := fixture.Worktree().Root()
5555
ok, err := IsGitRepo(path)
@@ -173,7 +173,7 @@ func setupSivaCloseRepos(t *testing.T, dir string) (*sql.Context, *closedLibrary
173173
require.NoError(err)
174174

175175
closedLib := &closedLibrary{multiLibrary: lib}
176-
pool := NewRepositoryPool(cache.DefaultMaxSize, closedLib)
176+
pool := NewRepositoryPool(cache.NewObjectLRUDefault(), closedLib)
177177

178178
cwd, err := os.Getwd()
179179
require.NoError(err)
@@ -298,7 +298,7 @@ func newMultiPool() (*multiLibrary, *RepositoryPool, error) {
298298
return nil, nil, err
299299
}
300300

301-
pool := NewRepositoryPool(cache.DefaultMaxSize, lib)
301+
pool := NewRepositoryPool(cache.NewObjectLRUDefault(), lib)
302302

303303
return lib, pool, err
304304
}

0 commit comments

Comments
 (0)