diff options
-rw-r--r-- | plumbing/format/packfile/common.go | 22 | ||||
-rw-r--r-- | plumbing/format/packfile/scanner.go | 21 | ||||
-rw-r--r-- | storage/filesystem/config.go | 11 | ||||
-rw-r--r-- | storage/filesystem/index.go | 11 | ||||
-rw-r--r-- | storage/filesystem/internal/dotgit/dotgit.go | 8 | ||||
-rw-r--r-- | storage/filesystem/internal/dotgit/writers_test.go | 28 | ||||
-rw-r--r-- | storage/filesystem/object.go | 21 | ||||
-rw-r--r-- | storage/filesystem/shallow.go | 5 | ||||
-rw-r--r-- | storage/filesystem/storage_test.go | 1 | ||||
-rw-r--r-- | storage/memory/storage_test.go | 1 | ||||
-rw-r--r-- | storage/test/storage_suite.go | 36 |
11 files changed, 118 insertions, 47 deletions
diff --git a/plumbing/format/packfile/common.go b/plumbing/format/packfile/common.go index 6402cd0..728cb16 100644 --- a/plumbing/format/packfile/common.go +++ b/plumbing/format/packfile/common.go @@ -4,6 +4,7 @@ import ( "io" "gopkg.in/src-d/go-git.v4/plumbing/storer" + "gopkg.in/src-d/go-git.v4/utils/ioutil" ) var signature = []byte{'P', 'A', 'C', 'K'} @@ -24,14 +25,7 @@ const ( // packfile. func UpdateObjectStorage(s storer.EncodedObjectStorer, packfile io.Reader) error { if sw, ok := s.(storer.PackfileWriter); ok { - w, err := sw.PackfileWriter() - if err != nil { - return err - } - - defer w.Close() - _, err = io.Copy(w, packfile) - return err + return writePackfileToObjectStorage(sw, packfile) } stream := NewScanner(packfile) @@ -43,3 +37,15 @@ func UpdateObjectStorage(s storer.EncodedObjectStorer, packfile io.Reader) error _, err = d.Decode() return err } + +func writePackfileToObjectStorage(sw storer.PackfileWriter, packfile io.Reader) error { + var err error + w, err := sw.PackfileWriter() + if err != nil { + return err + } + + defer ioutil.CheckClose(w, &err) + _, err = io.Copy(w, packfile) + return err +} diff --git a/plumbing/format/packfile/scanner.go b/plumbing/format/packfile/scanner.go index 97512d1..1dab2f2 100644 --- a/plumbing/format/packfile/scanner.go +++ b/plumbing/format/packfile/scanner.go @@ -8,10 +8,11 @@ import ( "hash" "hash/crc32" "io" - "io/ioutil" + stdioutil "io/ioutil" "gopkg.in/src-d/go-git.v4/plumbing" "gopkg.in/src-d/go-git.v4/utils/binary" + "gopkg.in/src-d/go-git.v4/utils/ioutil" ) var ( @@ -198,7 +199,7 @@ func (s *Scanner) discardObjectIfNeeded() error { } h := s.pendingObject - n, _, err := s.NextObject(ioutil.Discard) + n, _, err := s.NextObject(stdioutil.Discard) if err != nil { return err } @@ -275,8 +276,7 @@ func (s *Scanner) NextObject(w io.Writer) (written int64, crc32 uint32, err erro // ReadRegularObject reads and write a non-deltified object // from it zlib stream in an object entry in the packfile. -func (s *Scanner) copyObject(w io.Writer) (int64, error) { - var err error +func (s *Scanner) copyObject(w io.Writer) (n int64, err error) { if s.zr == nil { zr, err := zlib.NewReader(s.r) if err != nil { @@ -290,14 +290,9 @@ func (s *Scanner) copyObject(w io.Writer) (int64, error) { } } - defer func() { - closeErr := s.zr.Close() - if err == nil { - err = closeErr - } - }() - - return io.Copy(w, s.zr) + defer ioutil.CheckClose(s.zr, &err) + n, err = io.Copy(w, s.zr) + return } // SeekFromStart sets a new offset from start, returns the old position before @@ -329,7 +324,7 @@ func (s *Scanner) Checksum() (plumbing.Hash, error) { // Close reads the reader until io.EOF func (s *Scanner) Close() error { - _, err := io.Copy(ioutil.Discard, s.r) + _, err := io.Copy(stdioutil.Discard, s.r) return err } diff --git a/storage/filesystem/config.go b/storage/filesystem/config.go index cad698a..a2cc173 100644 --- a/storage/filesystem/config.go +++ b/storage/filesystem/config.go @@ -1,11 +1,12 @@ package filesystem import ( - "io/ioutil" + stdioutil "io/ioutil" "os" "gopkg.in/src-d/go-git.v4/config" "gopkg.in/src-d/go-git.v4/storage/filesystem/internal/dotgit" + "gopkg.in/src-d/go-git.v4/utils/ioutil" ) type ConfigStorage struct { @@ -24,9 +25,9 @@ func (c *ConfigStorage) Config() (*config.Config, error) { return nil, err } - defer f.Close() + defer ioutil.CheckClose(f, &err) - b, err := ioutil.ReadAll(f) + b, err := stdioutil.ReadAll(f) if err != nil { return nil, err } @@ -35,7 +36,7 @@ func (c *ConfigStorage) Config() (*config.Config, error) { return nil, err } - return cfg, nil + return cfg, err } func (c *ConfigStorage) SetConfig(cfg *config.Config) error { @@ -48,7 +49,7 @@ func (c *ConfigStorage) SetConfig(cfg *config.Config) error { return err } - defer f.Close() + defer ioutil.CheckClose(f, &err) b, err := cfg.Marshal() if err != nil { diff --git a/storage/filesystem/index.go b/storage/filesystem/index.go index 456ef0b..14ab09a 100644 --- a/storage/filesystem/index.go +++ b/storage/filesystem/index.go @@ -5,6 +5,7 @@ import ( "gopkg.in/src-d/go-git.v4/plumbing/format/index" "gopkg.in/src-d/go-git.v4/storage/filesystem/internal/dotgit" + "gopkg.in/src-d/go-git.v4/utils/ioutil" ) type IndexStorage struct { @@ -17,10 +18,11 @@ func (s *IndexStorage) SetIndex(idx *index.Index) error { return err } - defer f.Close() + defer ioutil.CheckClose(f, &err) e := index.NewEncoder(f) - return e.Encode(idx) + err = e.Encode(idx) + return err } func (s *IndexStorage) Index() (*index.Index, error) { @@ -37,8 +39,9 @@ func (s *IndexStorage) Index() (*index.Index, error) { return nil, err } - defer f.Close() + defer ioutil.CheckClose(f, &err) d := index.NewDecoder(f) - return idx, d.Decode(idx) + err = d.Decode(idx) + return idx, err } diff --git a/storage/filesystem/internal/dotgit/dotgit.go b/storage/filesystem/internal/dotgit/dotgit.go index b672d4b..e2ff51b 100644 --- a/storage/filesystem/internal/dotgit/dotgit.go +++ b/storage/filesystem/internal/dotgit/dotgit.go @@ -253,10 +253,10 @@ func (d *DotGit) SetRef(r *plumbing.Reference) error { return err } - if _, err := f.Write([]byte(content)); err != nil { - return err - } - return f.Close() + defer ioutil.CheckClose(f, &err) + + _, err = f.Write([]byte(content)) + return err } // Refs scans the git directory collecting references, which it returns. diff --git a/storage/filesystem/internal/dotgit/writers_test.go b/storage/filesystem/internal/dotgit/writers_test.go index d2c7b6f..1342396 100644 --- a/storage/filesystem/internal/dotgit/writers_test.go +++ b/storage/filesystem/internal/dotgit/writers_test.go @@ -12,6 +12,7 @@ import ( . "gopkg.in/check.v1" "gopkg.in/src-d/go-billy.v3/osfs" + "gopkg.in/src-d/go-git.v4/plumbing/format/packfile" ) func (s *SuiteDotGit) TestNewObjectPack(c *C) { @@ -35,13 +36,30 @@ func (s *SuiteDotGit) TestNewObjectPack(c *C) { c.Assert(w.Close(), IsNil) - stat, err := fs.Stat(fmt.Sprintf("objects/pack/pack-%s.pack", f.PackfileHash)) + pfPath := fmt.Sprintf("objects/pack/pack-%s.pack", f.PackfileHash) + idxPath := fmt.Sprintf("objects/pack/pack-%s.idx", f.PackfileHash) + + stat, err := fs.Stat(pfPath) c.Assert(err, IsNil) c.Assert(stat.Size(), Equals, int64(84794)) - stat, err = fs.Stat(fmt.Sprintf("objects/pack/pack-%s.idx", f.PackfileHash)) + stat, err = fs.Stat(idxPath) c.Assert(err, IsNil) c.Assert(stat.Size(), Equals, int64(1940)) + + pf, err := fs.Open(pfPath) + c.Assert(err, IsNil) + pfs := packfile.NewScanner(pf) + _, objects, err := pfs.Header() + c.Assert(err, IsNil) + for i := uint32(0); i < objects; i++ { + _, err := pfs.NextObjectHeader() + if err != nil { + c.Assert(err, IsNil) + break + } + } + c.Assert(pfs.Close(), IsNil) } func (s *SuiteDotGit) TestNewObjectPackUnused(c *C) { @@ -63,6 +81,12 @@ func (s *SuiteDotGit) TestNewObjectPackUnused(c *C) { info, err := fs.ReadDir("objects/pack") c.Assert(err, IsNil) c.Assert(info, HasLen, 0) + + // check clean up of temporary files + info, err = fs.ReadDir("") + for _, fi := range info { + c.Assert(fi.IsDir(), Equals, true) + } } func (s *SuiteDotGit) TestSyncedReader(c *C) { diff --git a/storage/filesystem/object.go b/storage/filesystem/object.go index dc88108..8bf84f2 100644 --- a/storage/filesystem/object.go +++ b/storage/filesystem/object.go @@ -11,6 +11,7 @@ import ( "gopkg.in/src-d/go-git.v4/plumbing/storer" "gopkg.in/src-d/go-git.v4/storage/filesystem/internal/dotgit" "gopkg.in/src-d/go-git.v4/storage/memory" + "gopkg.in/src-d/go-git.v4/utils/ioutil" "gopkg.in/src-d/go-billy.v3" ) @@ -53,10 +54,11 @@ func (s *ObjectStorage) loadIdxFile(h plumbing.Hash) error { if err != nil { return err } - defer idxfile.Close() + defer ioutil.CheckClose(idxfile, &err) s.index[h] = make(idx) - return s.index[h].Decode(idxfile) + err = s.index[h].Decode(idxfile) + return err } func (s *ObjectStorage) NewEncodedObject() plumbing.EncodedObject { @@ -94,14 +96,14 @@ func (s *ObjectStorage) SetEncodedObject(o plumbing.EncodedObject) (plumbing.Has return plumbing.ZeroHash, err } - defer ow.Close() + defer ioutil.CheckClose(ow, &err) or, err := o.Reader() if err != nil { return plumbing.ZeroHash, err } - defer or.Close() + defer ioutil.CheckClose(or, &err) if err := ow.WriteHeader(o.Type(), o.Size()); err != nil { return plumbing.ZeroHash, err @@ -111,7 +113,7 @@ func (s *ObjectStorage) SetEncodedObject(o plumbing.EncodedObject) (plumbing.Has return plumbing.ZeroHash, err } - return o.Hash(), nil + return o.Hash(), err } // EncodedObject returns the object with the given hash, by searching for it in @@ -143,7 +145,7 @@ func (s *ObjectStorage) getFromUnpacked(h plumbing.Hash) (obj plumbing.EncodedOb return nil, err } - defer f.Close() + defer ioutil.CheckClose(f, &err) obj = s.NewEncodedObject() r, err := objfile.NewReader(f) @@ -151,7 +153,7 @@ func (s *ObjectStorage) getFromUnpacked(h plumbing.Hash) (obj plumbing.EncodedOb return nil, err } - defer r.Close() + defer ioutil.CheckClose(r, &err) t, size, err := r.Header() if err != nil { @@ -186,7 +188,7 @@ func (s *ObjectStorage) getFromPackfile(h plumbing.Hash) (plumbing.EncodedObject return nil, err } - defer f.Close() + defer ioutil.CheckClose(f, &err) p := packfile.NewScanner(f) d, err := packfile.NewDecoder(p, memory.NewStorage()) @@ -195,7 +197,8 @@ func (s *ObjectStorage) getFromPackfile(h plumbing.Hash) (plumbing.EncodedObject } d.SetOffsets(s.index[pack]) - return d.DecodeObjectAt(offset) + obj, err := d.DecodeObjectAt(offset) + return obj, err } func (s *ObjectStorage) findObjectInPackfile(h plumbing.Hash) (plumbing.Hash, int64) { diff --git a/storage/filesystem/shallow.go b/storage/filesystem/shallow.go index 107818c..394e6ed 100644 --- a/storage/filesystem/shallow.go +++ b/storage/filesystem/shallow.go @@ -6,6 +6,7 @@ import ( "gopkg.in/src-d/go-git.v4/plumbing" "gopkg.in/src-d/go-git.v4/storage/filesystem/internal/dotgit" + "gopkg.in/src-d/go-git.v4/utils/ioutil" ) // ShallowStorage where the shallow commits are stored, an internal to @@ -23,14 +24,14 @@ func (s *ShallowStorage) SetShallow(commits []plumbing.Hash) error { return err } - defer f.Close() + defer ioutil.CheckClose(f, &err) for _, h := range commits { if _, err := fmt.Fprintf(f, "%s\n", h); err != err { return err } } - return nil + return err } // Shallow return the shallow commits reading from shallo file from .git diff --git a/storage/filesystem/storage_test.go b/storage/filesystem/storage_test.go index 2ced9dd..22709f5 100644 --- a/storage/filesystem/storage_test.go +++ b/storage/filesystem/storage_test.go @@ -26,6 +26,7 @@ func (s *StorageSuite) SetUpTest(c *C) { c.Assert(err, IsNil) s.BaseStorageSuite = test.NewBaseStorageSuite(storage) + s.BaseStorageSuite.SetUpTest(c) } func (s *StorageSuite) TestFilesystem(c *C) { diff --git a/storage/memory/storage_test.go b/storage/memory/storage_test.go index ee8fa93..31a27fa 100644 --- a/storage/memory/storage_test.go +++ b/storage/memory/storage_test.go @@ -17,4 +17,5 @@ var _ = Suite(&StorageSuite{}) func (s *StorageSuite) SetUpTest(c *C) { s.BaseStorageSuite = test.NewBaseStorageSuite(NewStorage()) + s.BaseStorageSuite.SetUpTest(c) } diff --git a/storage/test/storage_suite.go b/storage/test/storage_suite.go index 5a7cf9d..7cb0fe3 100644 --- a/storage/test/storage_suite.go +++ b/storage/test/storage_suite.go @@ -13,6 +13,7 @@ import ( "gopkg.in/src-d/go-git.v4/plumbing/storer" "gopkg.in/src-d/go-git.v4/storage" + "github.com/src-d/go-git-fixtures" . "gopkg.in/check.v1" ) @@ -64,6 +65,14 @@ func NewBaseStorageSuite(s Storer) BaseStorageSuite { }} } +func (s *BaseStorageSuite) SetUpTest(c *C) { + c.Assert(fixtures.Init(), IsNil) +} + +func (s *BaseStorageSuite) TearDownTest(c *C) { + c.Assert(fixtures.Clean(), IsNil) +} + func (s *BaseStorageSuite) TestSetEncodedObjectAndEncodedObject(c *C) { for _, to := range s.testObjects { comment := Commentf("failed for type %s", to.Type.String()) @@ -143,6 +152,33 @@ func (s *BaseStorageSuite) TestIterEncodedObjects(c *C) { } } +func (s *BaseStorageSuite) TestPackfileWriter(c *C) { + pwr, ok := s.Storer.(storer.PackfileWriter) + if !ok { + c.Skip("not a storer.PackWriter") + } + + pw, err := pwr.PackfileWriter() + c.Assert(err, IsNil) + + f := fixtures.Basic().One() + _, err = io.Copy(pw, f.Packfile()) + c.Assert(err, IsNil) + + err = pw.Close() + c.Assert(err, IsNil) + + iter, err := s.Storer.IterEncodedObjects(plumbing.AnyObject) + c.Assert(err, IsNil) + objects := 0 + err = iter.ForEach(func(plumbing.EncodedObject) error { + objects++ + return nil + }) + c.Assert(err, IsNil) + c.Assert(objects, Equals, 31) +} + func (s *BaseStorageSuite) TestObjectStorerTxSetEncodedObjectAndCommit(c *C) { storer, ok := s.Storer.(storer.Transactioner) if !ok { |