diff options
author | Máximo Cuadros <mcuadros@gmail.com> | 2017-01-29 02:16:26 +0100 |
---|---|---|
committer | Máximo Cuadros <mcuadros@gmail.com> | 2017-01-29 02:16:26 +0100 |
commit | de1c0bfec0269ff2a31b62d13612d833da178ec4 (patch) | |
tree | 9a47045401c1991747dd1f2e5497efff1e3dc1e5 | |
parent | cf9efc629eae67dc5c05c06e3cb0470bb1696f73 (diff) | |
download | go-git-de1c0bfec0269ff2a31b62d13612d833da178ec4.tar.gz |
storage: IndexStorer implementation
-rw-r--r-- | common.go | 1 | ||||
-rw-r--r-- | storage/filesystem/index.go | 44 | ||||
-rw-r--r-- | storage/memory/storage.go | 2 | ||||
-rw-r--r-- | storage/test/storage_suite.go | 9 |
4 files changed, 55 insertions, 1 deletions
@@ -15,6 +15,7 @@ type Storer interface { storer.EncodedObjectStorer storer.ReferenceStorer storer.ShallowStorer + storer.IndexStorer config.ConfigStorer } diff --git a/storage/filesystem/index.go b/storage/filesystem/index.go new file mode 100644 index 0000000..456ef0b --- /dev/null +++ b/storage/filesystem/index.go @@ -0,0 +1,44 @@ +package filesystem + +import ( + "os" + + "gopkg.in/src-d/go-git.v4/plumbing/format/index" + "gopkg.in/src-d/go-git.v4/storage/filesystem/internal/dotgit" +) + +type IndexStorage struct { + dir *dotgit.DotGit +} + +func (s *IndexStorage) SetIndex(idx *index.Index) error { + f, err := s.dir.IndexWriter() + if err != nil { + return err + } + + defer f.Close() + + e := index.NewEncoder(f) + return e.Encode(idx) +} + +func (s *IndexStorage) Index() (*index.Index, error) { + idx := &index.Index{ + Version: 2, + } + + f, err := s.dir.Index() + if err != nil { + if os.IsNotExist(err) { + return idx, nil + } + + return nil, err + } + + defer f.Close() + + d := index.NewDecoder(f) + return idx, d.Decode(idx) +} diff --git a/storage/memory/storage.go b/storage/memory/storage.go index 444f289..5d0bff1 100644 --- a/storage/memory/storage.go +++ b/storage/memory/storage.go @@ -72,7 +72,7 @@ func (c *IndexStorage) SetIndex(idx *index.Index) error { func (c *IndexStorage) Index() (*index.Index, error) { if c.index == nil { - c.index = &index.Index{} + c.index = &index.Index{Version: 2} } return c.index, nil diff --git a/storage/test/storage_suite.go b/storage/test/storage_suite.go index 088edce..ca2f783 100644 --- a/storage/test/storage_suite.go +++ b/storage/test/storage_suite.go @@ -297,6 +297,15 @@ func (s *BaseStorageSuite) TestSetConfigAndConfig(c *C) { c.Assert(cfg, DeepEquals, expected) } +func (s *BaseStorageSuite) TestIndex(c *C) { + expected := &index.Index{} + expected.Version = 2 + + idx, err := s.Storer.Index() + c.Assert(err, IsNil) + c.Assert(idx, DeepEquals, expected) +} + func (s *BaseStorageSuite) TestSetIndexAndIndex(c *C) { expected := &index.Index{} expected.Version = 2 |