Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

siva: return error in repositoy commit if not transactional #17

Merged
merged 1 commit into from
Feb 25, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion siva/location.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
49 changes: 38 additions & 11 deletions siva/location_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand All @@ -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() {
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
25 changes: 16 additions & 9 deletions siva/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
}

Expand All @@ -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
}
Expand Down