Skip to content
This repository was archived by the owner on Sep 11, 2020. It is now read-only.

Commit de1c0bf

Browse files
committed
storage: IndexStorer implementation
1 parent cf9efc6 commit de1c0bf

File tree

4 files changed

+55
-1
lines changed

4 files changed

+55
-1
lines changed

common.go

+1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ type Storer interface {
1515
storer.EncodedObjectStorer
1616
storer.ReferenceStorer
1717
storer.ShallowStorer
18+
storer.IndexStorer
1819
config.ConfigStorer
1920
}
2021

storage/filesystem/index.go

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package filesystem
2+
3+
import (
4+
"os"
5+
6+
"gopkg.in/src-d/go-git.v4/plumbing/format/index"
7+
"gopkg.in/src-d/go-git.v4/storage/filesystem/internal/dotgit"
8+
)
9+
10+
type IndexStorage struct {
11+
dir *dotgit.DotGit
12+
}
13+
14+
func (s *IndexStorage) SetIndex(idx *index.Index) error {
15+
f, err := s.dir.IndexWriter()
16+
if err != nil {
17+
return err
18+
}
19+
20+
defer f.Close()
21+
22+
e := index.NewEncoder(f)
23+
return e.Encode(idx)
24+
}
25+
26+
func (s *IndexStorage) Index() (*index.Index, error) {
27+
idx := &index.Index{
28+
Version: 2,
29+
}
30+
31+
f, err := s.dir.Index()
32+
if err != nil {
33+
if os.IsNotExist(err) {
34+
return idx, nil
35+
}
36+
37+
return nil, err
38+
}
39+
40+
defer f.Close()
41+
42+
d := index.NewDecoder(f)
43+
return idx, d.Decode(idx)
44+
}

storage/memory/storage.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ func (c *IndexStorage) SetIndex(idx *index.Index) error {
7272

7373
func (c *IndexStorage) Index() (*index.Index, error) {
7474
if c.index == nil {
75-
c.index = &index.Index{}
75+
c.index = &index.Index{Version: 2}
7676
}
7777

7878
return c.index, nil

storage/test/storage_suite.go

+9
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,15 @@ func (s *BaseStorageSuite) TestSetConfigAndConfig(c *C) {
297297
c.Assert(cfg, DeepEquals, expected)
298298
}
299299

300+
func (s *BaseStorageSuite) TestIndex(c *C) {
301+
expected := &index.Index{}
302+
expected.Version = 2
303+
304+
idx, err := s.Storer.Index()
305+
c.Assert(err, IsNil)
306+
c.Assert(idx, DeepEquals, expected)
307+
}
308+
300309
func (s *BaseStorageSuite) TestSetIndexAndIndex(c *C) {
301310
expected := &index.Index{}
302311
expected.Version = 2

0 commit comments

Comments
 (0)