From 65d9d4cfa307f5b1d868f83d752387339a77189f Mon Sep 17 00:00:00 2001 From: Manuel Carmona Date: Mon, 25 Feb 2019 10:07:36 +0000 Subject: [PATCH] siva: return error in repositoy commit if not transactional Signed-off-by: Manuel Carmona --- siva/location.go | 2 +- siva/location_test.go | 49 +++++++++++++++++++++++++++++++++---------- siva/repository.go | 25 ++++++++++++++-------- 3 files changed, 55 insertions(+), 21 deletions(-) diff --git a/siva/location.go b/siva/location.go index 12c4244..99f9700 100644 --- a/siva/location.go +++ b/siva/location.go @@ -265,7 +265,7 @@ func (l *Location) repository( return nil, err } - return newRepository(id, fs, mode, l) + return newRepository(id, fs, mode, l.lib.transactional, l) } func (l *Location) getRepoFS(mode borges.Mode) (sivafs.SivaFS, error) { diff --git a/siva/location_test.go b/siva/location_test.go index 350ef9a..187638d 100644 --- a/siva/location_test.go +++ b/siva/location_test.go @@ -42,7 +42,12 @@ func (s *locationSuite) TestCreate() { require.NoError(err) require.NotNil(r) err = r.Commit() - require.NoError(err) + if s.transactional { + require.NoError(err) + } else { + require.EqualError(err, + borges.ErrNonTransactional.New().Error()) + } iter, err := location.Repositories(borges.RWMode) require.NoError(err) @@ -91,7 +96,12 @@ func (s *locationSuite) TestInitExists() { r, err := location.Init("http://github.com/foo/no") require.NoError(err) err = r.Commit() - require.NoError(err) + if s.transactional { + require.NoError(err) + } else { + require.EqualError(err, + borges.ErrNonTransactional.New().Error()) + } has, err = location.Has("http://github.com/foo/bar") require.NoError(err) @@ -123,8 +133,11 @@ func (s *locationSuite) TestAddLocation() { require.Equal(l.ID(), r.LocationID()) _, err = r.R().CreateTag("test", plumbing.ZeroHash, nil) require.NoError(err) - err = r.Commit() - require.NoError(err) + if s.transactional { + require.NoError(r.Commit()) + } else { + require.NoError(r.Close()) + } locs, err := s.lib.Locations() require.NoError(err) @@ -140,13 +153,19 @@ func (s *locationSuite) TestAddLocation() { r, err = l.Get(repoID, borges.RWMode) require.NoError(err) - err = r.Commit() - require.NoError(err) + if s.transactional { + require.NoError(r.Commit()) + } else { + require.NoError(r.Close()) + } r, err = s.lib.Get(repoID, borges.RWMode) require.NoError(err) - err = r.Commit() - require.NoError(err) + if s.transactional { + require.NoError(r.Commit()) + } else { + require.NoError(r.Close()) + } } func (s *locationSuite) TestHasURL() { @@ -185,8 +204,11 @@ func (s *locationSuite) TestHasURL() { err = r.Storer.SetConfig(config) require.NoError(err) - err = repo.Commit() - require.NoError(err) + if s.transactional { + require.NoError(repo.Commit()) + } else { + require.NoError(repo.Close()) + } found, _, _, err := s.lib.Has("github.com/src-d/invalid") require.NoError(err) @@ -255,7 +277,12 @@ func (s *locationSuite) TestRepositories() { e, err := loc.Init(borges.RepositoryID(id)) require.NoError(err) err = e.Commit() - require.NoError(err) + if s.transactional { + require.NoError(err) + } else { + require.EqualError(err, + borges.ErrNonTransactional.New().Error()) + } } it, err := loc.Repositories(borges.ReadOnlyMode) diff --git a/siva/repository.go b/siva/repository.go index 436f268..1ceea77 100644 --- a/siva/repository.go +++ b/siva/repository.go @@ -20,10 +20,11 @@ var ErrRepoAlreadyClosed = errors.NewKind("repository % already closed") // Repository is an implementation for siva files of borges.Repository // interface. type Repository struct { - id borges.RepositoryID - repo *git.Repository - fs sivafs.SivaFS - mode borges.Mode + id borges.RepositoryID + repo *git.Repository + fs sivafs.SivaFS + mode borges.Mode + transactional bool mu sync.Mutex closed bool @@ -38,6 +39,7 @@ func newRepository( id borges.RepositoryID, fs sivafs.SivaFS, m borges.Mode, + transactional bool, l *Location, ) (*Repository, error) { var sto storage.Storer @@ -57,11 +59,12 @@ func newRepository( } return &Repository{ - id: id, - repo: repo, - fs: fs, - mode: m, - location: l, + id: id, + repo: repo, + fs: fs, + mode: m, + transactional: transactional, + location: l, }, nil } @@ -82,6 +85,10 @@ func (r *Repository) Mode() borges.Mode { // Commit implements borges.Repository interface. func (r *Repository) Commit() error { + if !r.transactional { + return borges.ErrNonTransactional.New() + } + if r.mode != borges.RWMode { return nil }