| 1 |
package transactional |
| 2 |
|
| 3 |
import ( |
| 4 |
"github.com/go-git/go-git/v5/plumbing/format/index" |
| 5 |
"github.com/go-git/go-git/v5/plumbing/storer" |
| 6 |
) |
| 7 |
|
| 8 |
// IndexStorage implements the storer.IndexStorage for the transactional package. |
| 9 |
type IndexStorage struct { |
| 10 |
storer.IndexStorer |
| 11 |
temporal storer.IndexStorer |
| 12 |
|
| 13 |
set bool |
| 14 |
} |
| 15 |
|
| 16 |
// NewIndexStorage returns a new IndexStorer based on a base storer and a |
| 17 |
// temporal storer. |
| 18 |
func NewIndexStorage(s, temporal storer.IndexStorer) *IndexStorage { |
| 19 |
return &IndexStorage{ |
| 20 |
IndexStorer: s, |
| 21 |
temporal: temporal, |
| 22 |
} |
| 23 |
} |
| 24 |
|
| 25 |
// SetIndex honors the storer.IndexStorer interface. |
| 26 |
func (s *IndexStorage) SetIndex(idx *index.Index) (err error) { |
| 27 |
if err := s.temporal.SetIndex(idx); err != nil { |
| 28 |
return err |
| 29 |
} |
| 30 |
|
| 31 |
s.set = true |
| 32 |
return nil |
| 33 |
} |
| 34 |
|
| 35 |
// Index honors the storer.IndexStorer interface. |
| 36 |
func (s *IndexStorage) Index() (*index.Index, error) { |
| 37 |
if !s.set { |
| 38 |
return s.IndexStorer.Index() |
| 39 |
} |
| 40 |
|
| 41 |
return s.temporal.Index() |
| 42 |
} |
| 43 |
|
| 44 |
// Commit it copies the index from the temporal storage into the base storage. |
| 45 |
func (s *IndexStorage) Commit() error { |
| 46 |
if !s.set { |
| 47 |
return nil |
| 48 |
} |
| 49 |
|
| 50 |
idx, err := s.temporal.Index() |
| 51 |
if err != nil { |
| 52 |
return err |
| 53 |
} |
| 54 |
|
| 55 |
return s.IndexStorer.SetIndex(idx) |
| 56 |
} |