diff options
author | Máximo Cuadros <mcuadros@gmail.com> | 2018-12-10 14:46:45 +0100 |
---|---|---|
committer | Máximo Cuadros <mcuadros@gmail.com> | 2018-12-10 14:46:45 +0100 |
commit | a2b39f540e5b07cf361b530e19de30707b8ca0d7 (patch) | |
tree | 6cb4cefebd27d4bc3684771370ff00d7e940c0d7 | |
parent | 3052df3d6cc39257d1c9d938ddfb5b862b913999 (diff) | |
download | go-git-a2b39f540e5b07cf361b530e19de30707b8ca0d7.tar.gz |
storage: new storage.ErrReferenceHasChanged error and test for CheckAndSetReference
Signed-off-by: Máximo Cuadros <mcuadros@gmail.com>
-rw-r--r-- | storage/filesystem/dotgit/dotgit.go | 3 | ||||
-rw-r--r-- | storage/memory/storage.go | 3 | ||||
-rw-r--r-- | storage/storer.go | 4 | ||||
-rw-r--r-- | storage/test/storage_suite.go | 51 |
4 files changed, 58 insertions, 3 deletions
diff --git a/storage/filesystem/dotgit/dotgit.go b/storage/filesystem/dotgit/dotgit.go index a58c248..ba9667e 100644 --- a/storage/filesystem/dotgit/dotgit.go +++ b/storage/filesystem/dotgit/dotgit.go @@ -14,6 +14,7 @@ import ( "gopkg.in/src-d/go-billy.v4/osfs" "gopkg.in/src-d/go-git.v4/plumbing" + "gopkg.in/src-d/go-git.v4/storage" "gopkg.in/src-d/go-git.v4/utils/ioutil" "gopkg.in/src-d/go-billy.v4" @@ -596,7 +597,7 @@ func (d *DotGit) checkReferenceAndTruncate(f billy.File, old *plumbing.Reference return err } if ref.Hash() != old.Hash() { - return fmt.Errorf("reference has changed concurrently") + return storage.ErrReferenceHasChanged } _, err = f.Seek(0, io.SeekStart) if err != nil { diff --git a/storage/memory/storage.go b/storage/memory/storage.go index 6e11742..f240f2a 100644 --- a/storage/memory/storage.go +++ b/storage/memory/storage.go @@ -13,7 +13,6 @@ 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, @@ -258,7 +257,7 @@ func (r ReferenceStorage) CheckAndSetReference(ref, old *plumbing.Reference) err if old != nil { tmp := r[ref.Name()] if tmp != nil && tmp.Hash() != old.Hash() { - return ErrRefHasChanged + return storage.ErrReferenceHasChanged } } r[ref.Name()] = ref diff --git a/storage/storer.go b/storage/storer.go index d1a94f2..5de0cfb 100644 --- a/storage/storer.go +++ b/storage/storer.go @@ -1,10 +1,14 @@ package storage import ( + "errors" + "gopkg.in/src-d/go-git.v4/config" "gopkg.in/src-d/go-git.v4/plumbing/storer" ) +var ErrReferenceHasChanged = errors.New("reference has changed concurrently") + // Storer is a generic storage of objects, references and any information // related to a particular repository. The package gopkg.in/src-d/go-git.v4/storage // contains two implementation a filesystem base implementation (such as `.git`) diff --git a/storage/test/storage_suite.go b/storage/test/storage_suite.go index 79a12c5..e050b73 100644 --- a/storage/test/storage_suite.go +++ b/storage/test/storage_suite.go @@ -280,6 +280,57 @@ func (s *BaseStorageSuite) TestSetReferenceAndGetReference(c *C) { c.Assert(e.Hash().String(), Equals, "bc9968d75e48de59f0870ffb71f5e160bbbdcf52") } +func (s *BaseStorageSuite) TestCheckAndSetReference(c *C) { + err := s.Storer.SetReference( + plumbing.NewReferenceFromStrings("foo", "482e0eada5de4039e6f216b45b3c9b683b83bfa"), + ) + c.Assert(err, IsNil) + + err = s.Storer.CheckAndSetReference( + plumbing.NewReferenceFromStrings("foo", "bc9968d75e48de59f0870ffb71f5e160bbbdcf52"), + plumbing.NewReferenceFromStrings("foo", "482e0eada5de4039e6f216b45b3c9b683b83bfa"), + ) + c.Assert(err, IsNil) + + e, err := s.Storer.Reference(plumbing.ReferenceName("foo")) + c.Assert(err, IsNil) + c.Assert(e.Hash().String(), Equals, "bc9968d75e48de59f0870ffb71f5e160bbbdcf52") +} + +func (s *BaseStorageSuite) TestCheckAndSetReferenceNil(c *C) { + err := s.Storer.SetReference( + plumbing.NewReferenceFromStrings("foo", "482e0eada5de4039e6f216b45b3c9b683b83bfa"), + ) + c.Assert(err, IsNil) + + err = s.Storer.CheckAndSetReference( + plumbing.NewReferenceFromStrings("foo", "bc9968d75e48de59f0870ffb71f5e160bbbdcf52"), + nil, + ) + c.Assert(err, IsNil) + + e, err := s.Storer.Reference(plumbing.ReferenceName("foo")) + c.Assert(err, IsNil) + c.Assert(e.Hash().String(), Equals, "bc9968d75e48de59f0870ffb71f5e160bbbdcf52") +} + +func (s *BaseStorageSuite) TestCheckAndSetReferenceError(c *C) { + err := s.Storer.SetReference( + plumbing.NewReferenceFromStrings("foo", "c3f4688a08fd86f1bf8e055724c84b7a40a09733"), + ) + c.Assert(err, IsNil) + + err = s.Storer.CheckAndSetReference( + plumbing.NewReferenceFromStrings("foo", "bc9968d75e48de59f0870ffb71f5e160bbbdcf52"), + plumbing.NewReferenceFromStrings("foo", "482e0eada5de4039e6f216b45b3c9b683b83bfa"), + ) + c.Assert(err, Equals, storage.ErrReferenceHasChanged) + + e, err := s.Storer.Reference(plumbing.ReferenceName("foo")) + c.Assert(err, IsNil) + c.Assert(e.Hash().String(), Equals, "c3f4688a08fd86f1bf8e055724c84b7a40a09733") +} + func (s *BaseStorageSuite) TestRemoveReference(c *C) { err := s.Storer.SetReference( plumbing.NewReferenceFromStrings("foo", "bc9968d75e48de59f0870ffb71f5e160bbbdcf52"), |