File: index.go

package info (click to toggle)
golang-github-go-git-go-git 5.14.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 3,648 kB
  • sloc: makefile: 81; sh: 76
file content (56 lines) | stat: -rw-r--r-- 1,191 bytes parent folder | download | duplicates (2)
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
51
52
53
54
55
56
package transactional

import (
	"github.com/go-git/go-git/v5/plumbing/format/index"
	"github.com/go-git/go-git/v5/plumbing/storer"
)

// IndexStorage implements the storer.IndexStorage for the transactional package.
type IndexStorage struct {
	storer.IndexStorer
	temporal storer.IndexStorer

	set bool
}

// NewIndexStorage returns a new IndexStorer based on a base storer and a
// temporal storer.
func NewIndexStorage(s, temporal storer.IndexStorer) *IndexStorage {
	return &IndexStorage{
		IndexStorer: s,
		temporal:    temporal,
	}
}

// SetIndex honors the storer.IndexStorer interface.
func (s *IndexStorage) SetIndex(idx *index.Index) (err error) {
	if err := s.temporal.SetIndex(idx); err != nil {
		return err
	}

	s.set = true
	return nil
}

// Index honors the storer.IndexStorer interface.
func (s *IndexStorage) Index() (*index.Index, error) {
	if !s.set {
		return s.IndexStorer.Index()
	}

	return s.temporal.Index()
}

// Commit it copies the index from the temporal storage into the base storage.
func (s *IndexStorage) Commit() error {
	if !s.set {
		return nil
	}

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

	return s.IndexStorer.SetIndex(idx)
}