aboutsummaryrefslogtreecommitdiffstats
path: root/storage/transactional/index.go
blob: 26042d682654c54ec2b08f2b8cc56029fc27f172 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package transactional

import (
	"gopkg.in/src-d/go-git.v4/plumbing/format/index"
	"gopkg.in/src-d/go-git.v4/plumbing/storer"
)

type IndexStorage struct {
	storer.IndexStorer
	temporal storer.IndexStorer

	set bool
}

func NewIndexStorage(s, temporal storer.IndexStorer) *IndexStorage {
	return &IndexStorage{
		IndexStorer: s,
		temporal:    temporal,
	}
}

func (s *IndexStorage) SetIndex(idx *index.Index) (err error) {
	if err := s.temporal.SetIndex(idx); err != nil {
		return err
	}

	s.set = true
	return nil
}

func (s *IndexStorage) Index() (*index.Index, error) {
	if !s.set {
		return s.IndexStorer.Index()
	}

	return s.temporal.Index()
}

func (c *IndexStorage) Commit() error {
	if !c.set {
		return nil
	}

	idx, err := c.temporal.Index()
	if err != nil {
		return err
	}

	return c.IndexStorer.SetIndex(idx)
}