diff options
author | Santiago M. Mola <santi@mola.io> | 2016-11-03 17:10:43 +0100 |
---|---|---|
committer | Máximo Cuadros <mcuadros@gmail.com> | 2016-11-03 17:10:43 +0100 |
commit | 3f7fbc6c49e50eb22e3de8a5143817fa7c0c54dd (patch) | |
tree | 0bf81a6dd858278000d1d7f7afc578d993fba791 /storage/filesystem/object.go | |
parent | 94f5e9c949963893d1c3d3e987a591ee15265327 (diff) | |
download | go-git-3f7fbc6c49e50eb22e3de8a5143817fa7c0c54dd.tar.gz |
storage/filesystem: implement missing functionality. (#110)
* storage/filesystem: added ObjectStorage Set.
* storage/filesystem: now passes all tests, except those specific to transactions.
* formats/config: Encoder now encodes subsections with no options.
* formats/config: add HasSubsection on Section.
* dotgit: add Ref method to get specific reference.
Diffstat (limited to 'storage/filesystem/object.go')
-rw-r--r-- | storage/filesystem/object.go | 57 |
1 files changed, 36 insertions, 21 deletions
diff --git a/storage/filesystem/object.go b/storage/filesystem/object.go index f2f5351..6dbeae9 100644 --- a/storage/filesystem/object.go +++ b/storage/filesystem/object.go @@ -1,7 +1,6 @@ package filesystem import ( - "fmt" "io" "os" @@ -82,11 +81,35 @@ func (s *ObjectStorage) Writer() (io.WriteCloser, error) { return w, nil } -// Set adds a new object to the storage. As this functionality is not -// yet supported, this method always returns a "not implemented yet" -// error an zero hash. -func (s *ObjectStorage) Set(core.Object) (core.Hash, error) { - return core.ZeroHash, fmt.Errorf("set - not implemented yet") +// Set adds a new object to the storage. +func (s *ObjectStorage) Set(o core.Object) (core.Hash, error) { + if o.Type() == core.OFSDeltaObject || o.Type() == core.REFDeltaObject { + return core.ZeroHash, core.ErrInvalidType + } + + ow, err := s.dir.NewObject() + if err != nil { + return core.ZeroHash, err + } + + defer ow.Close() + + or, err := o.Reader() + if err != nil { + return core.ZeroHash, err + } + + defer or.Close() + + if err := ow.WriteHeader(o.Type(), o.Size()); err != nil { + return core.ZeroHash, err + } + + if _, err := io.Copy(ow, or); err != nil { + return core.ZeroHash, err + } + + return o.Hash(), nil } // Get returns the object with the given hash, by searching for it in @@ -228,26 +251,18 @@ func (s *ObjectStorage) buildPackfileIters( return iters, nil } +// Begin opens a new transaction. However, this implementation is not +// transactional, so Commit and Rollback have no effect. func (o *ObjectStorage) Begin() core.TxObjectStorage { - return &TxObjectStorage{} -} - -type TxObjectStorage struct{} - -func (tx *TxObjectStorage) Set(obj core.Object) (core.Hash, error) { - return core.ZeroHash, fmt.Errorf("tx.Set - not implemented yet") -} - -func (tx *TxObjectStorage) Get(core.ObjectType, core.Hash) (core.Object, error) { - return nil, fmt.Errorf("tx.Get - not implemented yet") + return o } -func (tx *TxObjectStorage) Commit() error { - return fmt.Errorf("tx.Commit - not implemented yet") +func (tx *ObjectStorage) Commit() error { + return nil } -func (tx *TxObjectStorage) Rollback() error { - return fmt.Errorf("tx.Rollback - not implemented yet") +func (tx *ObjectStorage) Rollback() error { + return nil } type index map[core.Hash]int64 |