diff options
author | Javi Fontan <jfontan@gmail.com> | 2018-07-27 12:49:28 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-07-27 12:49:28 +0200 |
commit | b7131019e6b2639f69623029a5a1f178bc920a4e (patch) | |
tree | e26b73542e52cc5619d69aaf64ee027a15b679ad /storage/filesystem/index.go | |
parent | a08061aa298ec4b92bbe470a1834465f25a0ad0d (diff) | |
parent | 3657a32e0ead55601a2af578abecd65dd2d8b64b (diff) | |
download | go-git-b7131019e6b2639f69623029a5a1f178bc920a4e.tar.gz |
Merge pull request #902 from jfontan/feature/new-packfile-parser
Bugfixes and IndexStorage
Diffstat (limited to 'storage/filesystem/index.go')
-rw-r--r-- | storage/filesystem/index.go | 47 |
1 files changed, 47 insertions, 0 deletions
diff --git a/storage/filesystem/index.go b/storage/filesystem/index.go new file mode 100644 index 0000000..2ebf57e --- /dev/null +++ b/storage/filesystem/index.go @@ -0,0 +1,47 @@ +package filesystem + +import ( + "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) + + e := index.NewEncoder(f) + 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) + + d := index.NewDecoder(f) + err = d.Decode(idx) + return idx, err +} |