aboutsummaryrefslogtreecommitdiffstats
path: root/storage/memory/storage.go
diff options
context:
space:
mode:
Diffstat (limited to 'storage/memory/storage.go')
-rw-r--r--storage/memory/storage.go34
1 files changed, 25 insertions, 9 deletions
diff --git a/storage/memory/storage.go b/storage/memory/storage.go
index 2380fed..927ec41 100644
--- a/storage/memory/storage.go
+++ b/storage/memory/storage.go
@@ -12,6 +12,7 @@ import (
)
var ErrUnsupportedObjectType = fmt.Errorf("unsupported object type")
+var ErrRefHasChanged = fmt.Errorf("reference has changed concurrently")
// Storage is an implementation of git.Storer that stores data on memory, being
// ephemeral. The use of this storage should be done in controlled envoriments,
@@ -29,17 +30,17 @@ type Storage struct {
// NewStorage returns a new Storage base on memory
func NewStorage() *Storage {
return &Storage{
- ReferenceStorage: make(ReferenceStorage, 0),
+ ReferenceStorage: make(ReferenceStorage),
ConfigStorage: ConfigStorage{},
ShallowStorage: ShallowStorage{},
ObjectStorage: ObjectStorage{
- Objects: make(map[plumbing.Hash]plumbing.EncodedObject, 0),
- Commits: make(map[plumbing.Hash]plumbing.EncodedObject, 0),
- Trees: make(map[plumbing.Hash]plumbing.EncodedObject, 0),
- Blobs: make(map[plumbing.Hash]plumbing.EncodedObject, 0),
- Tags: make(map[plumbing.Hash]plumbing.EncodedObject, 0),
+ Objects: make(map[plumbing.Hash]plumbing.EncodedObject),
+ Commits: make(map[plumbing.Hash]plumbing.EncodedObject),
+ Trees: make(map[plumbing.Hash]plumbing.EncodedObject),
+ Blobs: make(map[plumbing.Hash]plumbing.EncodedObject),
+ Tags: make(map[plumbing.Hash]plumbing.EncodedObject),
},
- ModuleStorage: make(ModuleStorage, 0),
+ ModuleStorage: make(ModuleStorage),
}
}
@@ -151,7 +152,7 @@ func flattenObjectMap(m map[plumbing.Hash]plumbing.EncodedObject) []plumbing.Enc
func (o *ObjectStorage) Begin() storer.Transaction {
return &TxObjectStorage{
Storage: o,
- Objects: make(map[plumbing.Hash]plumbing.EncodedObject, 0),
+ Objects: make(map[plumbing.Hash]plumbing.EncodedObject),
}
}
@@ -188,7 +189,7 @@ func (tx *TxObjectStorage) Commit() error {
}
func (tx *TxObjectStorage) Rollback() error {
- tx.Objects = make(map[plumbing.Hash]plumbing.EncodedObject, 0)
+ tx.Objects = make(map[plumbing.Hash]plumbing.EncodedObject)
return nil
}
@@ -202,6 +203,21 @@ func (r ReferenceStorage) SetReference(ref *plumbing.Reference) error {
return nil
}
+func (r ReferenceStorage) CheckAndSetReference(ref, old *plumbing.Reference) error {
+ if ref == nil {
+ return nil
+ }
+
+ if old != nil {
+ tmp := r[ref.Name()]
+ if tmp != nil && tmp.Hash() != old.Hash() {
+ return ErrRefHasChanged
+ }
+ }
+ r[ref.Name()] = ref
+ return nil
+}
+
func (r ReferenceStorage) Reference(n plumbing.ReferenceName) (*plumbing.Reference, error) {
ref, ok := r[n]
if !ok {