| 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 |
// ObjectStorage implements the storer.EncodedObjectStorer for the transactional package. |
| 9 |
type ObjectStorage struct { |
| 10 |
storer.EncodedObjectStorer |
| 11 |
temporal storer.EncodedObjectStorer |
| 12 |
} |
| 13 |
|
| 14 |
// NewObjectStorage returns a new EncodedObjectStorer based on a base storer and |
| 15 |
// a temporal storer. |
| 16 |
func NewObjectStorage(base, temporal storer.EncodedObjectStorer) *ObjectStorage { |
| 17 |
return &ObjectStorage{EncodedObjectStorer: base, temporal: temporal} |
| 18 |
} |
| 19 |
|
| 20 |
// SetEncodedObject honors the storer.EncodedObjectStorer interface. |
| 21 |
func (o *ObjectStorage) SetEncodedObject(obj plumbing.EncodedObject) (plumbing.Hash, error) { |
| 22 |
return o.temporal.SetEncodedObject(obj) |
| 23 |
} |
| 24 |
|
| 25 |
// HasEncodedObject honors the storer.EncodedObjectStorer interface. |
| 26 |
func (o *ObjectStorage) HasEncodedObject(h plumbing.Hash) error { |
| 27 |
err := o.EncodedObjectStorer.HasEncodedObject(h) |
| 28 |
if err == plumbing.ErrObjectNotFound { |
| 29 |
return o.temporal.HasEncodedObject(h) |
| 30 |
} |
| 31 |
|
| 32 |
return err |
| 33 |
} |
| 34 |
|
| 35 |
// EncodedObjectSize honors the storer.EncodedObjectStorer interface. |
| 36 |
func (o *ObjectStorage) EncodedObjectSize(h plumbing.Hash) (int64, error) { |
| 37 |
sz, err := o.EncodedObjectStorer.EncodedObjectSize(h) |
| 38 |
if err == plumbing.ErrObjectNotFound { |
| 39 |
return o.temporal.EncodedObjectSize(h) |
| 40 |
} |
| 41 |
|
| 42 |
return sz, err |
| 43 |
} |
| 44 |
|
| 45 |
// EncodedObject honors the storer.EncodedObjectStorer interface. |
| 46 |
func (o *ObjectStorage) EncodedObject(t plumbing.ObjectType, h plumbing.Hash) (plumbing.EncodedObject, error) { |
| 47 |
obj, err := o.EncodedObjectStorer.EncodedObject(t, h) |
| 48 |
if err == plumbing.ErrObjectNotFound { |
| 49 |
return o.temporal.EncodedObject(t, h) |
| 50 |
} |
| 51 |
|
| 52 |
return obj, err |
| 53 |
} |
| 54 |
|
| 55 |
// IterEncodedObjects honors the storer.EncodedObjectStorer interface. |
| 56 |
func (o *ObjectStorage) IterEncodedObjects(t plumbing.ObjectType) (storer.EncodedObjectIter, error) { |
| 57 |
baseIter, err := o.EncodedObjectStorer.IterEncodedObjects(t) |
| 58 |
if err != nil { |
| 59 |
return nil, err |
| 60 |
} |
| 61 |
|
| 62 |
temporalIter, err := o.temporal.IterEncodedObjects(t) |
| 63 |
if err != nil { |
| 64 |
return nil, err |
| 65 |
} |
| 66 |
|
| 67 |
return storer.NewMultiEncodedObjectIter([]storer.EncodedObjectIter{ |
| 68 |
baseIter, |
| 69 |
temporalIter, |
| 70 |
}), nil |
| 71 |
} |
| 72 |
|
| 73 |
// Commit it copies the objects of the temporal storage into the base storage. |
| 74 |
func (o *ObjectStorage) Commit() error { |
| 75 |
iter, err := o.temporal.IterEncodedObjects(plumbing.AnyObject) |
| 76 |
if err != nil { |
| 77 |
return err |
| 78 |
} |
| 79 |
|
| 80 |
return iter.ForEach(func(obj plumbing.EncodedObject) error { |
| 81 |
_, err := o.EncodedObjectStorer.SetEncodedObject(obj) |
| 82 |
return err |
| 83 |
}) |
| 84 |
} |
| 85 |
|
| 86 |
func (o *ObjectStorage) AddAlternate(remote string) error { |
| 87 |
return o.temporal.AddAlternate(remote) |
| 88 |
} |