diff options
author | Máximo Cuadros <mcuadros@gmail.com> | 2016-09-07 02:04:43 +0200 |
---|---|---|
committer | Máximo Cuadros <mcuadros@gmail.com> | 2016-09-07 02:04:43 +0200 |
commit | 56adb5be3ad26a0045ea6c6a6d24dafdff15ba1c (patch) | |
tree | bd8ecbb6674f01f03f97fb15083bed0a3e8e021d /storage | |
parent | 98a22e72a808aa0d5dd62339817404fd9e1c4db6 (diff) | |
download | go-git-56adb5be3ad26a0045ea6c6a6d24dafdff15ba1c.tar.gz |
format: packfile new interface
Diffstat (limited to 'storage')
-rw-r--r-- | storage/filesystem/internal/dotgit/dotgit_test.go | 4 | ||||
-rw-r--r-- | storage/filesystem/internal/index/index.go | 26 | ||||
-rw-r--r-- | storage/filesystem/internal/index/index_test.go | 29 | ||||
-rw-r--r-- | storage/filesystem/object.go | 24 | ||||
-rw-r--r-- | storage/filesystem/object_test.go | 14 | ||||
-rw-r--r-- | storage/memory/storage.go | 1 |
6 files changed, 57 insertions, 41 deletions
diff --git a/storage/filesystem/internal/dotgit/dotgit_test.go b/storage/filesystem/internal/dotgit/dotgit_test.go index 3f0a0eb..0097821 100644 --- a/storage/filesystem/internal/dotgit/dotgit_test.go +++ b/storage/filesystem/internal/dotgit/dotgit_test.go @@ -54,7 +54,7 @@ func (s *SuiteDotGit) SetUpSuite(c *C) { path, err := tgz.Extract(init.tgz) c.Assert(err, IsNil, com) - s.fixtures[init.name] = fs.NewOSClient(filepath.Join(path, ".git")) + s.fixtures[init.name] = fs.NewOS(filepath.Join(path, ".git")) } } @@ -233,7 +233,7 @@ func (s *SuiteDotGit) TestNewObjectPack(c *C) { log.Fatal(err) } - dot := New(fs.NewOSClient(dir)) + dot := New(fs.NewOS(dir)) r, err := os.Open("../../../../formats/packfile/fixtures/git-fixture.ofs-delta") c.Assert(err, IsNil) diff --git a/storage/filesystem/internal/index/index.go b/storage/filesystem/internal/index/index.go index 70b77c5..2fd2a55 100644 --- a/storage/filesystem/internal/index/index.go +++ b/storage/filesystem/internal/index/index.go @@ -1,7 +1,6 @@ package index import ( - "fmt" "io" "gopkg.in/src-d/go-git.v4/core" @@ -13,6 +12,10 @@ import ( // Objects are identified by their hash. type Index map[core.Hash]int64 +func New() Index { + return make(Index) +} + // Decode decodes a idxfile into the Index func (i *Index) Decode(r io.Reader) error { d := idxfile.NewDecoder(r) @@ -30,23 +33,16 @@ func (i *Index) Decode(r io.Reader) error { // NewFrompackfile returns a new index from a packfile reader. func NewFromPackfile(r io.Reader) (Index, core.Hash, error) { - index := make(Index) + p := packfile.NewScannerFromReader(r) + d := packfile.NewDecoder(p, nil) - p := packfile.NewParser(r) - _, count, err := p.Header() + checksum, err := d.Decode() if err != nil { return nil, core.ZeroHash, err } - for i := 0; i < int(count); i++ { - h, err := p.NextObjectHeader() - if err = index.Set(core.ZeroHash, h.Offset); err != nil { - return nil, core.ZeroHash, err - } - } - - hash, err := p.Checksum() - return index, hash, err + index := Index(d.Index()) + return index, checksum, p.Close() } // Get returns the offset that an object has the packfile. @@ -61,9 +57,9 @@ func (i Index) Get(h core.Hash) (int64, error) { // Set adds a new hash-offset pair to the index, or substitutes an existing one. func (i Index) Set(h core.Hash, o int64) error { - if _, ok := i[h]; ok { + /*if _, ok := i[h]; ok { return fmt.Errorf("index.Set failed: duplicated key: %s", h) - } + }*/ i[h] = o diff --git a/storage/filesystem/internal/index/index_test.go b/storage/filesystem/internal/index/index_test.go index 4ddfc25..5261012 100644 --- a/storage/filesystem/internal/index/index_test.go +++ b/storage/filesystem/internal/index/index_test.go @@ -1,6 +1,7 @@ package index import ( + "io/ioutil" "os" "testing" @@ -16,6 +17,22 @@ type SuiteIndex struct{} var _ = Suite(&SuiteIndex{}) +func (s *SuiteIndex) TestNewFromPackfile(c *C) { + path := "../../../../formats/packfile/fixtures/spinnaker-spinnaker.pack" + + pack, err := os.Open(path) + c.Assert(err, IsNil) + + _, checksum, err := NewFromPackfile(pack) + c.Assert(err, IsNil) + + leftover, err := ioutil.ReadAll(pack) + c.Assert(err, IsNil) + c.Assert(leftover, HasLen, 0) + + c.Assert(checksum.String(), Equals, "da4c488bbbdc4e599c7c97d01753bb3144fccd9c") +} + func (s *SuiteIndex) TestNewFromIdx(c *C) { for i, test := range [...]struct { idxPath string @@ -33,7 +50,9 @@ func (s *SuiteIndex) TestNewFromIdx(c *C) { idx, err := os.Open(test.idxPath) c.Assert(err, IsNil, com) - index, err := NewFromIdx(idx) + index := New() + err = index.Decode(idx) + if test.errRegexp != "" { c.Assert(err, ErrorMatches, test.errRegexp, com) } else { @@ -78,7 +97,9 @@ func (s *SuiteIndex) TestGet(c *C) { idx, err := os.Open(test.idx) c.Assert(err, IsNil, com) - index, err := NewFromIdx(idx) + index := New() + err = index.Decode(idx) + c.Assert(err, IsNil, com) obt, err := index.Get(test.hash) @@ -106,7 +127,9 @@ func (s *SuiteIndex) BenchmarkFromIdx(c *C) { for i := 0; i < c.N; i++ { c.StartTimer() - index, _ := NewFromIdx(idx) + index := New() + index.Decode(idx) + c.StopTimer() indexes = append(indexes, index) } diff --git a/storage/filesystem/object.go b/storage/filesystem/object.go index aef30ec..c6d5359 100644 --- a/storage/filesystem/object.go +++ b/storage/filesystem/object.go @@ -3,7 +3,6 @@ package filesystem import ( "fmt" "io" - "os" "gopkg.in/src-d/go-git.v4/core" "gopkg.in/src-d/go-git.v4/formats/objfile" @@ -94,7 +93,7 @@ func (s *ObjectStorage) getFromUnpacked(t core.ObjectType, h core.Hash) (obj cor // Get returns the object with the given hash, by searching for it in // the packfile. -func (s *ObjectStorage) getFromPackfile(t core.ObjectType, h core.Hash) (obj core.Object, err error) { +func (s *ObjectStorage) getFromPackfile(t core.ObjectType, h core.Hash) (core.Object, error) { offset, err := s.index.Get(h) if err != nil { return nil, err @@ -117,23 +116,18 @@ func (s *ObjectStorage) getFromPackfile(t core.ObjectType, h core.Hash) (obj cor } }() - _, err = f.Seek(offset, os.SEEK_SET) - if err != nil { - return nil, err - } - - r := packfile.NewSeekable(f) - r.HashToOffset = map[core.Hash]int64(s.index) - p := packfile.NewParser(r) + p := packfile.NewScanner(f) + d := packfile.NewDecoder(p, nil) - obj = s.NewObject() - err = p.FillObject(obj) + obj, err := d.ReadObjectAt(offset) if err != nil { return nil, err } + if core.AnyObject != t && obj.Type() != t { return nil, core.ErrObjectNotFound } + return obj, nil } @@ -213,7 +207,8 @@ func buildIndexFromPackfile(dir *dotgit.DotGit) (index.Index, error) { } }() - return index.NewFromPackfile(f) + index, _, err := index.NewFromPackfile(f) + return index, err } func buildIndexFromIdxfile(fs fs.Filesystem, path string) (index.Index, error) { @@ -229,7 +224,8 @@ func buildIndexFromIdxfile(fs fs.Filesystem, path string) (index.Index, error) { } }() - return index.NewFromIdx(f) + i := index.New() + return i, i.Decode(f) } func (o *ObjectStorage) Begin() core.TxObjectStorage { diff --git a/storage/filesystem/object_test.go b/storage/filesystem/object_test.go index e9cfa4c..142bad9 100644 --- a/storage/filesystem/object_test.go +++ b/storage/filesystem/object_test.go @@ -91,7 +91,7 @@ func (s *FsSuite) TestHashNotFound(c *C) { func (s *FsSuite) newObjectStorage(c *C, fixtureName string) core.ObjectStorage { path := fixture(fixtureName, c) - fs := fs.NewOSClient(filepath.Join(path, ".git/")) + fs := fs.NewOS(filepath.Join(path, ".git/")) store, err := NewStorage(fs) c.Assert(err, IsNil) @@ -110,7 +110,7 @@ func (s *FsSuite) TestGetCompareWithMemoryStorage(c *C) { i, fixId, path) gitPath := filepath.Join(path, ".git/") - fs := fs.NewOSClient(gitPath) + fs := fs.NewOS(gitPath) memSto, err := memStorageFromGitDir(fs, gitPath) c.Assert(err, IsNil, com) @@ -142,10 +142,10 @@ func memStorageFromGitDir(fs fs.Filesystem, path string) (core.ObjectStorage, er } sto := memory.NewStorage() - r := packfile.NewStream(f) + r := packfile.NewScanner(f) d := packfile.NewDecoder(r, sto.ObjectStorage()) - err = d.Decode() + _, err = d.Decode() if err != nil { return nil, err } @@ -241,7 +241,7 @@ func (s *FsSuite) TestIterCompareWithMemoryStorage(c *C) { i, fixId, path) gitPath := filepath.Join(path, ".git/") - fs := fs.NewOSClient(gitPath) + fs := fs.NewOS(gitPath) memSto, err := memStorageFromDirPath(fs, gitPath) c.Assert(err, IsNil, com) @@ -287,9 +287,9 @@ func memStorageFromDirPath(fs fs.Filesystem, path string) (core.ObjectStorage, e sto := memory.NewStorage() - r := packfile.NewStream(f) + r := packfile.NewScanner(f) d := packfile.NewDecoder(r, sto.ObjectStorage()) - err = d.Decode() + _, err = d.Decode() if err != nil { return nil, err } diff --git a/storage/memory/storage.go b/storage/memory/storage.go index 216336c..1bf99fc 100644 --- a/storage/memory/storage.go +++ b/storage/memory/storage.go @@ -130,6 +130,7 @@ func (o *ObjectStorage) Set(obj core.Object) (core.Hash, error) { case core.TagObject: o.Tags[h] = o.Objects[h] default: + fmt.Println(obj.Type()) return h, ErrUnsupportedObjectType } |