From de1c0bfec0269ff2a31b62d13612d833da178ec4 Mon Sep 17 00:00:00 2001 From: Máximo Cuadros Date: Sun, 29 Jan 2017 02:16:26 +0100 Subject: storage: IndexStorer implementation --- storage/filesystem/index.go | 44 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 storage/filesystem/index.go (limited to 'storage/filesystem') 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) +} -- cgit