Debian Codegraph @

storage/transactional/shallow.go

raw
1 package transactional
2
3 import (
4 "github.com/go-git/go-git/v5/plumbing"
5 "github.com/go-git/go-git/v5/plumbing/storer"
6 )
7
8 // ShallowStorage implements the storer.ShallowStorer for the transactional package.
9 type ShallowStorage struct {
10 storer.ShallowStorer
11 temporal storer.ShallowStorer
12 }
13
14 // NewShallowStorage returns a new ShallowStorage based on a base storer and
15 // a temporal storer.
16 func NewShallowStorage(base, temporal storer.ShallowStorer) *ShallowStorage {
17 return &ShallowStorage{
18 ShallowStorer: base,
19 temporal: temporal,
20 }
21 }
22
23 // SetShallow honors the storer.ShallowStorer interface.
24 func (s *ShallowStorage) SetShallow(commits []plumbing.Hash) error {
25 return s.temporal.SetShallow(commits)
26 }
27
28 // Shallow honors the storer.ShallowStorer interface.
29 func (s *ShallowStorage) Shallow() ([]plumbing.Hash, error) {
30 shallow, err := s.temporal.Shallow()
31 if err != nil {
32 return nil, err
33 }
34
35 if len(shallow) != 0 {
36 return shallow, nil
37 }
38
39 return s.ShallowStorer.Shallow()
40 }
41
42 // Commit it copies the shallow information of the temporal storage into the
43 // base storage.
44 func (s *ShallowStorage) Commit() error {
45 commits, err := s.temporal.Shallow()
46 if err != nil || len(commits) == 0 {
47 return err
48 }
49
50 return s.ShallowStorer.SetShallow(commits)
51 }