diff options
Diffstat (limited to 'storage/filesystem')
-rw-r--r-- | storage/filesystem/internal/dotgit/dotgit_setref_norwfs.go | 27 |
1 files changed, 25 insertions, 2 deletions
diff --git a/storage/filesystem/internal/dotgit/dotgit_setref_norwfs.go b/storage/filesystem/internal/dotgit/dotgit_setref_norwfs.go index 8b1e662..5695bd3 100644 --- a/storage/filesystem/internal/dotgit/dotgit_setref_norwfs.go +++ b/storage/filesystem/internal/dotgit/dotgit_setref_norwfs.go @@ -2,9 +2,13 @@ package dotgit -import "gopkg.in/src-d/go-git.v4/plumbing" +import ( + "fmt" -// There are some filesystems tha don't support opening files in RDWD mode. + "gopkg.in/src-d/go-git.v4/plumbing" +) + +// There are some filesystems that don't support opening files in RDWD mode. // In these filesystems the standard SetRef function can not be used as i // reads the reference file to check that it's not modified before updating it. // @@ -12,6 +16,25 @@ import "gopkg.in/src-d/go-git.v4/plumbing" // making it compatible with these simple filesystems. This is usually not // a problem as they should be accessed by only one process at a time. func (d *DotGit) setRef(fileName, content string, old *plumbing.Reference) error { + _, err := d.fs.Stat(fileName) + if err == nil && old != nil { + fRead, err := d.fs.Open(fileName) + if err != nil { + return err + } + + ref, err := d.readReferenceFrom(fRead, old.Name().String()) + fRead.Close() + + if err != nil { + return err + } + + if ref.Hash() != old.Hash() { + return fmt.Errorf("reference has changed concurrently") + } + } + f, err := d.fs.Create(fileName) if err != nil { return err |