aboutsummaryrefslogtreecommitdiffstats
path: root/storage
diff options
context:
space:
mode:
Diffstat (limited to 'storage')
-rw-r--r--storage/filesystem/index.go44
-rw-r--r--storage/filesystem/internal/dotgit/dotgit.go11
-rw-r--r--storage/filesystem/internal/dotgit/dotgit_test.go32
-rw-r--r--storage/filesystem/object.go20
-rw-r--r--storage/filesystem/storage.go2
-rw-r--r--storage/memory/storage.go19
-rw-r--r--storage/test/storage_suite.go23
7 files changed, 141 insertions, 10 deletions
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/filesystem/internal/dotgit/dotgit.go b/storage/filesystem/internal/dotgit/dotgit.go
index f9763d1..1281b7e 100644
--- a/storage/filesystem/internal/dotgit/dotgit.go
+++ b/storage/filesystem/internal/dotgit/dotgit.go
@@ -18,6 +18,7 @@ const (
suffix = ".git"
packedRefsPath = "packed-refs"
configPath = "config"
+ indexPath = "index"
shallowPath = "shallow"
objectsPath = "objects"
@@ -72,6 +73,16 @@ func (d *DotGit) Config() (billy.File, error) {
return d.fs.Open(configPath)
}
+// IndexWriter returns a file pointer for write to the index file
+func (d *DotGit) IndexWriter() (billy.File, error) {
+ return d.fs.Create(indexPath)
+}
+
+// Index returns a file pointer for read to the index file
+func (d *DotGit) Index() (billy.File, error) {
+ return d.fs.Open(indexPath)
+}
+
// ShallowWriter returns a file pointer for write to the shallow file
func (d *DotGit) ShallowWriter() (billy.File, error) {
return d.fs.Create(shallowPath)
diff --git a/storage/filesystem/internal/dotgit/dotgit_test.go b/storage/filesystem/internal/dotgit/dotgit_test.go
index 48f02f0..bff90af 100644
--- a/storage/filesystem/internal/dotgit/dotgit_test.go
+++ b/storage/filesystem/internal/dotgit/dotgit_test.go
@@ -154,6 +154,38 @@ func (s *SuiteDotGit) TestConfigWriteAndConfig(c *C) {
c.Assert(string(cnt), Equals, "foo")
}
+func (s *SuiteDotGit) TestIndex(c *C) {
+ fs := fixtures.Basic().ByTag(".git").One().DotGit()
+ dir := New(fs)
+
+ idx, err := dir.Index()
+ c.Assert(err, IsNil)
+ c.Assert(idx, NotNil)
+}
+
+func (s *SuiteDotGit) TestIndexWriteAndIndex(c *C) {
+ tmp, err := ioutil.TempDir("", "dot-git")
+ c.Assert(err, IsNil)
+ defer os.RemoveAll(tmp)
+
+ fs := osfs.New(tmp)
+ dir := New(fs)
+
+ f, err := dir.IndexWriter()
+ c.Assert(err, IsNil)
+
+ _, err = f.Write([]byte("foo"))
+ c.Assert(err, IsNil)
+
+ f, err = dir.Index()
+ c.Assert(err, IsNil)
+
+ cnt, err := ioutil.ReadAll(f)
+ c.Assert(err, IsNil)
+
+ c.Assert(string(cnt), Equals, "foo")
+}
+
func (s *SuiteDotGit) TestShallow(c *C) {
fs := fixtures.Basic().ByTag(".git").One().DotGit()
dir := New(fs)
diff --git a/storage/filesystem/object.go b/storage/filesystem/object.go
index 1001032..b82ca30 100644
--- a/storage/filesystem/object.go
+++ b/storage/filesystem/object.go
@@ -17,13 +17,13 @@ import (
type ObjectStorage struct {
dir *dotgit.DotGit
- index map[plumbing.Hash]index
+ index map[plumbing.Hash]idx
}
func newObjectStorage(dir *dotgit.DotGit) (ObjectStorage, error) {
s := ObjectStorage{
dir: dir,
- index: make(map[plumbing.Hash]index, 0),
+ index: make(map[plumbing.Hash]idx, 0),
}
return s, s.loadIdxFiles()
@@ -45,13 +45,13 @@ func (s *ObjectStorage) loadIdxFiles() error {
}
func (s *ObjectStorage) loadIdxFile(h plumbing.Hash) error {
- idx, err := s.dir.ObjectPackIdx(h)
+ idxfile, err := s.dir.ObjectPackIdx(h)
if err != nil {
return err
}
- s.index[h] = make(index)
- return s.index[h].Decode(idx)
+ s.index[h] = make(idx)
+ return s.index[h].Decode(idxfile)
}
func (s *ObjectStorage) NewEncodedObject() plumbing.EncodedObject {
@@ -64,9 +64,9 @@ func (s *ObjectStorage) PackfileWriter() (io.WriteCloser, error) {
return nil, err
}
- w.Notify = func(h plumbing.Hash, idx idxfile.Idxfile) {
- s.index[h] = make(index)
- for _, e := range idx.Entries {
+ w.Notify = func(h plumbing.Hash, idxfile idxfile.Idxfile) {
+ s.index[h] = make(idx)
+ for _, e := range idxfile.Entries {
s.index[h][e.Hash] = int64(e.Offset)
}
}
@@ -244,9 +244,9 @@ func (s *ObjectStorage) buildPackfileIters(
return iters, nil
}
-type index map[plumbing.Hash]int64
+type idx map[plumbing.Hash]int64
-func (i index) Decode(r io.Reader) error {
+func (i idx) Decode(r io.Reader) error {
idx := &idxfile.Idxfile{}
d := idxfile.NewDecoder(r)
diff --git a/storage/filesystem/storage.go b/storage/filesystem/storage.go
index a60d0f4..56f84b8 100644
--- a/storage/filesystem/storage.go
+++ b/storage/filesystem/storage.go
@@ -13,6 +13,7 @@ import (
type Storage struct {
ObjectStorage
ReferenceStorage
+ IndexStorage
ShallowStorage
ConfigStorage
}
@@ -28,6 +29,7 @@ func NewStorage(fs billy.Filesystem) (*Storage, error) {
return &Storage{
ObjectStorage: o,
ReferenceStorage: ReferenceStorage{dir: dir},
+ IndexStorage: IndexStorage{dir: dir},
ShallowStorage: ShallowStorage{dir: dir},
ConfigStorage: ConfigStorage{dir: dir},
}, nil
diff --git a/storage/memory/storage.go b/storage/memory/storage.go
index ecf2efa..5d0bff1 100644
--- a/storage/memory/storage.go
+++ b/storage/memory/storage.go
@@ -6,6 +6,7 @@ import (
"gopkg.in/src-d/go-git.v4/config"
"gopkg.in/src-d/go-git.v4/plumbing"
+ "gopkg.in/src-d/go-git.v4/plumbing/format/index"
"gopkg.in/src-d/go-git.v4/plumbing/storer"
)
@@ -19,6 +20,7 @@ type Storage struct {
ConfigStorage
ObjectStorage
ShallowStorage
+ IndexStorage
ReferenceStorage
}
@@ -59,6 +61,23 @@ func (c *ConfigStorage) Config() (*config.Config, error) {
return c.config, nil
}
+type IndexStorage struct {
+ index *index.Index
+}
+
+func (c *IndexStorage) SetIndex(idx *index.Index) error {
+ c.index = idx
+ return nil
+}
+
+func (c *IndexStorage) Index() (*index.Index, error) {
+ if c.index == nil {
+ c.index = &index.Index{Version: 2}
+ }
+
+ return c.index, nil
+}
+
type ObjectStorage struct {
Objects map[plumbing.Hash]plumbing.EncodedObject
Commits map[plumbing.Hash]plumbing.EncodedObject
diff --git a/storage/test/storage_suite.go b/storage/test/storage_suite.go
index bf0d10d..ca2f783 100644
--- a/storage/test/storage_suite.go
+++ b/storage/test/storage_suite.go
@@ -9,6 +9,7 @@ import (
"gopkg.in/src-d/go-git.v4/config"
"gopkg.in/src-d/go-git.v4/plumbing"
+ "gopkg.in/src-d/go-git.v4/plumbing/format/index"
"gopkg.in/src-d/go-git.v4/plumbing/storer"
. "gopkg.in/check.v1"
@@ -18,6 +19,7 @@ type Storer interface {
storer.EncodedObjectStorer
storer.ReferenceStorer
storer.ShallowStorer
+ storer.IndexStorer
config.ConfigStorer
}
@@ -295,6 +297,27 @@ 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
+
+ err := s.Storer.SetIndex(expected)
+ c.Assert(err, IsNil)
+
+ idx, err := s.Storer.Index()
+ c.Assert(err, IsNil)
+ c.Assert(idx, DeepEquals, expected)
+}
+
func (s *BaseStorageSuite) TestSetConfigInvalid(c *C) {
cfg := config.NewConfig()
cfg.Remotes["foo"] = &config.RemoteConfig{}