aboutsummaryrefslogtreecommitdiffstats
path: root/storage/filesystem/object.go
diff options
context:
space:
mode:
authorMáximo Cuadros <mcuadros@gmail.com>2016-09-05 19:55:01 +0200
committerMáximo Cuadros <mcuadros@gmail.com>2016-09-05 19:55:01 +0200
commit19da30a8ff233285a79df9f1da8b0d88d99eb626 (patch)
treebdb2313cc29381afbad9af75bd273c5ec6d9f24b /storage/filesystem/object.go
parent65427f64599e9383ab926aed75fc02f2db32022d (diff)
downloadgo-git-19da30a8ff233285a79df9f1da8b0d88d99eb626.tar.gz
core: ObjectStorage.Writer
Diffstat (limited to 'storage/filesystem/object.go')
-rw-r--r--storage/filesystem/object.go54
1 files changed, 54 insertions, 0 deletions
diff --git a/storage/filesystem/object.go b/storage/filesystem/object.go
index 0696c70..fbcc9ae 100644
--- a/storage/filesystem/object.go
+++ b/storage/filesystem/object.go
@@ -1,7 +1,9 @@
package filesystem
import (
+ "bytes"
"fmt"
+ "io"
"os"
"gopkg.in/src-d/go-git.v4/core"
@@ -30,6 +32,58 @@ func (s *ObjectStorage) NewObject() core.Object {
return &core.MemoryObject{}
}
+// Writer method not supported on Memory storage
+func (o *ObjectStorage) Writer() (io.WriteCloser, error) {
+ file := bytes.NewBuffer(nil)
+ return newPackWrite(o, file), nil
+}
+
+type packWriter struct {
+ writer io.Writer
+ pipeReader io.ReadCloser
+ pipeWriter io.WriteCloser
+ file io.Writer
+ result chan error
+}
+
+func newPackWrite(o *ObjectStorage, file io.Writer) io.WriteCloser {
+ r, w := io.Pipe()
+
+ ch := make(chan error)
+ go func(r io.ReadCloser) {
+ defer r.Close()
+ index, err := index.NewFromPackfileInMemory(r)
+ o.index = index
+
+ ch <- err
+ }(r)
+
+ return &packWriter{
+ writer: io.MultiWriter(w, file),
+ pipeReader: r,
+ pipeWriter: w,
+ file: file,
+ result: ch,
+ }
+
+}
+
+func (w *packWriter) Write(p []byte) (int, error) {
+ return w.writer.Write(p)
+}
+
+func (w *packWriter) Close() error {
+ defer func() {
+ close(w.result)
+ }()
+
+ if err := w.pipeWriter.Close(); err != nil {
+ return err
+ }
+
+ return <-w.result
+}
+
// 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.