Skip to content
This repository was archived by the owner on Sep 11, 2020. It is now read-only.

storage/dotgit: add KeepDescriptors option #942

Merged
merged 4 commits into from
Sep 6, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions plumbing/storer/object.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ type EncodedObjectStorer interface {
// HasEncodedObject returns ErrObjNotFound if the object doesn't
// exist. If the object does exist, it returns nil.
HasEncodedObject(plumbing.Hash) error
// Close any opened files.
Close() error
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about having Close() only on the filesystem implementation, not on the general interface?

Copy link
Contributor Author

@jfontan jfontan Sep 4, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Somehow I understood that you didn't want to export Close in git.Repository. It's now deleted from EncodedObjectStorer.

}

// DeltaObjectStorer is an EncodedObjectStorer that can return delta
Expand Down
4 changes: 4 additions & 0 deletions plumbing/storer/object_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,3 +157,7 @@ func (o *MockObjectStorage) IterEncodedObjects(t plumbing.ObjectType) (EncodedOb
func (o *MockObjectStorage) Begin() Transaction {
return nil
}

func (o *MockObjectStorage) Close() error {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is not needed.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right. Deleted that and Close in memory storer. Also added a new test for filesystem storer and KeepDescriptors option.

return nil
}
43 changes: 42 additions & 1 deletion storage/filesystem/dotgit/dotgit.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,9 @@ type Options struct {
// ExclusiveAccess means that the filesystem is not modified externally
// while the repo is open.
ExclusiveAccess bool
// KeepDescriptors makes the file descriptors to be reused but they will
// need to be manually closed calling Close().
KeepDescriptors bool
}

// The DotGit type represents a local git repository on disk. This
Expand All @@ -78,6 +81,8 @@ type DotGit struct {
objectMap map[plumbing.Hash]struct{}
packList []plumbing.Hash
packMap map[plumbing.Hash]struct{}

files map[string]billy.File
}

// New returns a DotGit value ready to be used. The path argument must
Expand Down Expand Up @@ -123,6 +128,28 @@ func (d *DotGit) Initialize() error {
return nil
}

// Close closes all opened files.
func (d *DotGit) Close() error {
var firstError error
if d.files != nil {
for _, f := range d.files {
err := f.Close()
if err != nil && firstError == nil {
firstError = err
continue
}
}

d.files = nil
}

if firstError != nil {
return firstError
}

return nil
}

// ConfigWriter returns a file pointer for write to the config file
func (d *DotGit) ConfigWriter() (billy.File, error) {
return d.fs.Create(configPath)
Expand Down Expand Up @@ -217,12 +244,22 @@ func (d *DotGit) objectPackPath(hash plumbing.Hash, extension string) string {
}

func (d *DotGit) objectPackOpen(hash plumbing.Hash, extension string) (billy.File, error) {
if d.files == nil {
d.files = make(map[string]billy.File)
}

err := d.hasPack(hash)
if err != nil {
return nil, err
}

pack, err := d.fs.Open(d.objectPackPath(hash, extension))
path := d.objectPackPath(hash, extension)
f, ok := d.files[path]
if ok {
return f, nil
}

pack, err := d.fs.Open(path)
if err != nil {
if os.IsNotExist(err) {
return nil, ErrPackfileNotFound
Expand All @@ -231,6 +268,10 @@ func (d *DotGit) objectPackOpen(hash plumbing.Hash, extension string) (billy.Fil
return nil, err
}

if d.options.KeepDescriptors && extension == "pack" {
d.files[path] = pack
}

return pack, nil
}

Expand Down
28 changes: 28 additions & 0 deletions storage/filesystem/dotgit/dotgit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -465,6 +465,34 @@ func (s *SuiteDotGit) TestObjectPack(c *C) {
c.Assert(filepath.Ext(pack.Name()), Equals, ".pack")
}

func (s *SuiteDotGit) TestObjectPackWithKeepDescriptors(c *C) {
f := fixtures.Basic().ByTag(".git").One()
fs := f.DotGit()
dir := NewWithOptions(fs, Options{KeepDescriptors: true})

pack, err := dir.ObjectPack(f.PackfileHash)
c.Assert(err, IsNil)
c.Assert(filepath.Ext(pack.Name()), Equals, ".pack")

pack2, err := dir.ObjectPack(f.PackfileHash)
c.Assert(err, IsNil)
c.Assert(pack, Equals, pack2)

err = dir.Close()
c.Assert(err, IsNil)

pack2, err = dir.ObjectPack(f.PackfileHash)
c.Assert(err, IsNil)
c.Assert(pack, Not(Equals), pack2)

err = pack2.Close()
c.Assert(err, IsNil)

err = dir.Close()
c.Assert(err, NotNil)

}

func (s *SuiteDotGit) TestObjectPackIdx(c *C) {
f := fixtures.Basic().ByTag(".git").One()
fs := f.DotGit()
Expand Down
10 changes: 9 additions & 1 deletion storage/filesystem/object.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ func (s *ObjectStorage) loadIdxFile(h plumbing.Hash) (err error) {
}

defer ioutil.CheckClose(f, &err)

idxf := idxfile.NewMemoryIndex()
d := idxfile.NewDecoder(f)
if err = d.Decode(idxf); err != nil {
Expand Down Expand Up @@ -280,7 +281,9 @@ func (s *ObjectStorage) getFromPackfile(h plumbing.Hash, canBeDelta bool) (
return nil, err
}

defer ioutil.CheckClose(f, &err)
if !s.options.KeepDescriptors {
defer ioutil.CheckClose(f, &err)
}

idx := s.index[pack]
if canBeDelta {
Expand Down Expand Up @@ -423,6 +426,11 @@ func (s *ObjectStorage) buildPackfileIters(t plumbing.ObjectType, seen map[plumb
}, nil
}

// Close closes all opened files.
func (s *ObjectStorage) Close() error {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not covered by a test

return s.dir.Close()
}

type lazyPackfilesIter struct {
hashes []plumbing.Hash
open func(h plumbing.Hash) (storer.EncodedObjectIter, error)
Expand Down
4 changes: 4 additions & 0 deletions storage/filesystem/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ type Options struct {
// ExclusiveAccess means that the filesystem is not modified externally
// while the repo is open.
ExclusiveAccess bool
// KeepDescriptors makes the file descriptors to be reused but they will
// need to be manually closed calling Close().
KeepDescriptors bool
}

// NewStorage returns a new Storage backed by a given `fs.Filesystem`
Expand All @@ -41,6 +44,7 @@ func NewStorageWithOptions(
) (*Storage, error) {
dirOps := dotgit.Options{
ExclusiveAccess: ops.ExclusiveAccess,
KeepDescriptors: ops.KeepDescriptors,
}

dir := dotgit.NewWithOptions(fs, dirOps)
Expand Down
3 changes: 3 additions & 0 deletions storage/memory/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,9 @@ func (o *ObjectStorage) ObjectPacks() ([]plumbing.Hash, error) {
func (o *ObjectStorage) DeleteOldObjectPackAndIndex(plumbing.Hash, time.Time) error {
return nil
}
func (s *ObjectStorage) Close() error {
return nil
}

var errNotSupported = fmt.Errorf("Not supported")

Expand Down