aboutsummaryrefslogtreecommitdiffstats
path: root/storage/transactional/object.go
blob: b43c96d3b4bd08a8f8a02fc2475862dcf743abd4 (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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
package transactional

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

// ObjectStorage implements the storer.EncodedObjectStorer for the transactional package.
type ObjectStorage struct {
	storer.EncodedObjectStorer
	temporal storer.EncodedObjectStorer
}

// NewObjectStorage returns a new EncodedObjectStorer based on a base storer and
// a temporal storer.
func NewObjectStorage(base, temporal storer.EncodedObjectStorer) *ObjectStorage {
	return &ObjectStorage{EncodedObjectStorer: base, temporal: temporal}
}

// SetEncodedObject honors the storer.EncodedObjectStorer interface.
func (o *ObjectStorage) SetEncodedObject(obj plumbing.EncodedObject) (plumbing.Hash, error) {
	return o.temporal.SetEncodedObject(obj)
}

// HasEncodedObject honors the storer.EncodedObjectStorer interface.
func (o *ObjectStorage) HasEncodedObject(h plumbing.Hash) error {
	err := o.EncodedObjectStorer.HasEncodedObject(h)
	if err == plumbing.ErrObjectNotFound {
		return o.temporal.HasEncodedObject(h)
	}

	return err
}

// EncodedObjectSize honors the storer.EncodedObjectStorer interface.
func (o *ObjectStorage) EncodedObjectSize(h plumbing.Hash) (int64, error) {
	sz, err := o.EncodedObjectStorer.EncodedObjectSize(h)
	if err == plumbing.ErrObjectNotFound {
		return o.temporal.EncodedObjectSize(h)
	}

	return sz, err
}

// EncodedObject honors the storer.EncodedObjectStorer interface.
func (o *ObjectStorage) EncodedObject(t plumbing.ObjectType, h plumbing.Hash) (plumbing.EncodedObject, error) {
	obj, err := o.EncodedObjectStorer.EncodedObject(t, h)
	if err == plumbing.ErrObjectNotFound {
		return o.temporal.EncodedObject(t, h)
	}

	return obj, err
}

// IterEncodedObjects honors the storer.EncodedObjectStorer interface.
func (o *ObjectStorage) IterEncodedObjects(t plumbing.ObjectType) (storer.EncodedObjectIter, error) {
	baseIter, err := o.EncodedObjectStorer.IterEncodedObjects(t)
	if err != nil {
		return nil, err
	}

	temporalIter, err := o.temporal.IterEncodedObjects(t)
	if err != nil {
		return nil, err
	}

	return storer.NewMultiEncodedObjectIter([]storer.EncodedObjectIter{
		baseIter,
		temporalIter,
	}), nil
}

// Commit it copies the objects of the temporal storage into the base storage.
func (o *ObjectStorage) Commit() error {
	iter, err := o.temporal.IterEncodedObjects(plumbing.AnyObject)
	if err != nil {
		return err
	}

	return iter.ForEach(func(obj plumbing.EncodedObject) error {
		_, err := o.EncodedObjectStorer.SetEncodedObject(obj)
		return err
	})
}

func (o *ObjectStorage) AddAlternate(remote string) error {
	return o.temporal.AddAlternate(remote)
}