Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Fix reference updated concurrently error for the filesystem storer #1116

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

Merged
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
18 changes: 18 additions & 0 deletions 18 storage/filesystem/dotgit/dotgit.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,9 @@ var (
// ErrIsDir is returned when a reference file is attempting to be read,
// but the path specified is a directory.
ErrIsDir = errors.New("reference path is a directory")
// ErrEmptyRefFile is returned when a reference file is attempted to be read,
// but the file is empty
ErrEmptyRefFile = errors.New("ref file is empty")
)

// Options holds configuration for the storage.
Expand Down Expand Up @@ -661,18 +664,33 @@ func (d *DotGit) readReferenceFrom(rd io.Reader, name string) (ref *plumbing.Ref
return nil, err
}

if len(b) == 0 {
return nil, ErrEmptyRefFile
}

line := strings.TrimSpace(string(b))
return plumbing.NewReferenceFromStrings(name, line), nil
}

// checkReferenceAndTruncate reads the reference from the given file, or the `pack-refs` file if
// the file was empty. Then it checks that the old reference matches the stored reference and
// truncates the file.
func (d *DotGit) checkReferenceAndTruncate(f billy.File, old *plumbing.Reference) error {
if old == nil {
return nil
}

ref, err := d.readReferenceFrom(f, old.Name().String())
if errors.Is(err, ErrEmptyRefFile) {
// This may happen if the reference is being read from a newly created file.
// In that case, try getting the reference from the packed refs file.
ref, err = d.packedRef(old.Name())
}

if err != nil {
return err
}

if ref.Hash() != old.Hash() {
return storage.ErrReferenceHasChanged
}
Expand Down
61 changes: 61 additions & 0 deletions 61 storage/filesystem/dotgit/dotgit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"github.com/go-git/go-billy/v5/util"
fixtures "github.com/go-git/go-git-fixtures/v4"
"github.com/go-git/go-git/v5/plumbing"
"github.com/go-git/go-git/v5/storage"
"github.com/stretchr/testify/assert"
. "gopkg.in/check.v1"
)
Expand Down Expand Up @@ -1046,3 +1047,63 @@ func (s *SuiteDotGit) TestDeletedRefs(c *C) {
c.Assert(refs, HasLen, 1)
c.Assert(refs[0].Name(), Equals, plumbing.ReferenceName("refs/heads/foo"))
}

// Checks that seting a reference that has been packed and checking its old value is successful
func (s *SuiteDotGit) TestSetPackedRef(c *C) {
fs, clean := s.TemporalFilesystem()
defer clean()

dir := New(fs)

err := dir.SetRef(plumbing.NewReferenceFromStrings(
"refs/heads/foo",
"e8d3ffab552895c19b9fcf7aa264d277cde33881",
), nil)
c.Assert(err, IsNil)

refs, err := dir.Refs()
c.Assert(err, IsNil)
c.Assert(refs, HasLen, 1)
looseCount, err := dir.CountLooseRefs()
c.Assert(err, IsNil)
c.Assert(looseCount, Equals, 1)

err = dir.PackRefs()
c.Assert(err, IsNil)

// Make sure the refs are still there, but no longer loose.
refs, err = dir.Refs()
c.Assert(err, IsNil)
c.Assert(refs, HasLen, 1)
looseCount, err = dir.CountLooseRefs()
c.Assert(err, IsNil)
c.Assert(looseCount, Equals, 0)

ref, err := dir.Ref("refs/heads/foo")
c.Assert(err, IsNil)
c.Assert(ref, NotNil)
c.Assert(ref.Hash().String(), Equals, "e8d3ffab552895c19b9fcf7aa264d277cde33881")

// Attempt to update the reference using an invalid old reference value
err = dir.SetRef(plumbing.NewReferenceFromStrings(
"refs/heads/foo",
"b8d3ffab552895c19b9fcf7aa264d277cde33881",
), plumbing.NewReferenceFromStrings(
"refs/heads/foo",
"e8d3ffab552895c19b9fcf7aa264d277cde33882",
))
c.Assert(err, Equals, storage.ErrReferenceHasChanged)

// Now update the reference and it should pass
err = dir.SetRef(plumbing.NewReferenceFromStrings(
"refs/heads/foo",
"b8d3ffab552895c19b9fcf7aa264d277cde33881",
), plumbing.NewReferenceFromStrings(
"refs/heads/foo",
"e8d3ffab552895c19b9fcf7aa264d277cde33881",
))
c.Assert(err, IsNil)
looseCount, err = dir.CountLooseRefs()
c.Assert(err, IsNil)
c.Assert(looseCount, Equals, 1)
}
Loading
Morty Proxy This is a proxified and sanitized view of the page, visit original site.