diff options
author | Taru Karttunen <taruti@taruti.net> | 2017-09-12 19:06:47 +0300 |
---|---|---|
committer | Jeremy Stribling <strib@alum.mit.edu> | 2017-11-27 11:43:01 -0800 |
commit | 3834e571da171844efecc4e26fd89082419079a1 (patch) | |
tree | 32756a7a66fcf746574393fbba478bc8aac7b368 /storage/memory/storage.go | |
parent | 702718fd59be0aa4b8bf8492403c465107ca17af (diff) | |
download | go-git-3834e571da171844efecc4e26fd89082419079a1.tar.gz |
Use optionally locking when updating refs
Diffstat (limited to 'storage/memory/storage.go')
-rw-r--r-- | storage/memory/storage.go | 15 |
1 files changed, 15 insertions, 0 deletions
diff --git a/storage/memory/storage.go b/storage/memory/storage.go index 2380fed..69394af 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, @@ -202,6 +203,20 @@ func (r ReferenceStorage) SetReference(ref *plumbing.Reference) error { return nil } +func (r ReferenceStorage) CheckAndSetReference(ref, old *plumbing.Reference) error { + if ref != 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 { |