2018-11-27 16:52:20 -05:00
|
|
|
package filesystem
|
|
|
|
|
|
|
|
import (
|
2019-06-18 22:14:15 -04:00
|
|
|
"bufio"
|
2018-11-27 16:52:20 -05:00
|
|
|
"os"
|
|
|
|
|
|
|
|
"gopkg.in/src-d/go-git.v4/plumbing/format/index"
|
|
|
|
"gopkg.in/src-d/go-git.v4/storage/filesystem/dotgit"
|
|
|
|
"gopkg.in/src-d/go-git.v4/utils/ioutil"
|
|
|
|
)
|
|
|
|
|
|
|
|
type IndexStorage struct {
|
|
|
|
dir *dotgit.DotGit
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *IndexStorage) SetIndex(idx *index.Index) (err error) {
|
|
|
|
f, err := s.dir.IndexWriter()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
defer ioutil.CheckClose(f, &err)
|
2019-07-31 12:45:42 -04:00
|
|
|
bw := bufio.NewWriter(f)
|
|
|
|
defer func() {
|
|
|
|
if e := bw.Flush(); err == nil && e != nil {
|
|
|
|
err = e
|
|
|
|
}
|
|
|
|
}()
|
2018-11-27 16:52:20 -05:00
|
|
|
|
2019-07-31 12:45:42 -04:00
|
|
|
e := index.NewEncoder(bw)
|
2018-11-27 16:52:20 -05:00
|
|
|
err = e.Encode(idx)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *IndexStorage) Index() (i *index.Index, err 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 ioutil.CheckClose(f, &err)
|
|
|
|
|
2019-06-18 22:14:15 -04:00
|
|
|
d := index.NewDecoder(bufio.NewReader(f))
|
2018-11-27 16:52:20 -05:00
|
|
|
err = d.Decode(idx)
|
|
|
|
return idx, err
|
|
|
|
}
|