From ae999ede139f5fa5601ffb7c55979608b112d274 Mon Sep 17 00:00:00 2001 From: Máximo Cuadros Date: Fri, 12 Aug 2016 19:11:27 +0200 Subject: storage: Storage entity support, and DotGit support for References --- storage/filesystem/reference.go | 61 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 storage/filesystem/reference.go (limited to 'storage/filesystem/reference.go') diff --git a/storage/filesystem/reference.go b/storage/filesystem/reference.go new file mode 100644 index 0000000..c8e5434 --- /dev/null +++ b/storage/filesystem/reference.go @@ -0,0 +1,61 @@ +package filesystem + +import ( + "fmt" + + "gopkg.in/src-d/go-git.v4/core" + "gopkg.in/src-d/go-git.v4/storage/filesystem/internal/dotgit" +) + +type ReferenceStorage struct { + dir *dotgit.DotGit + refs map[core.ReferenceName]*core.Reference +} + +func (r *ReferenceStorage) Set(ref *core.Reference) error { + return fmt.Errorf("not implemented yet") +} + +func (r *ReferenceStorage) Get(n core.ReferenceName) (*core.Reference, error) { + if err := r.load(); err != nil { + return nil, err + } + + ref, ok := r.refs[n] + if !ok { + return nil, core.ErrReferenceNotFound + } + + return ref, nil +} + +func (r *ReferenceStorage) Iter() (core.ReferenceIter, error) { + if err := r.load(); err != nil { + return nil, err + } + + var refs []*core.Reference + for _, ref := range r.refs { + refs = append(refs, ref) + } + + return core.NewReferenceSliceIter(refs), nil +} + +func (r *ReferenceStorage) load() error { + if len(r.refs) != 0 { + return nil + } + + refs, err := r.dir.Refs() + if err != nil { + return err + } + + r.refs = make(map[core.ReferenceName]*core.Reference, 0) + for _, ref := range refs { + r.refs[ref.Name()] = ref + } + + return nil +} -- cgit