Skip to content
This repository was archived by the owner on Sep 11, 2020. It is now read-only.

Commit d3e7c90

Browse files
committed
Check reference also in norwfs SetRef
Signed-off-by: Javi Fontan <jfontan@gmail.com>
1 parent dae8c68 commit d3e7c90

File tree

1 file changed

+25
-2
lines changed

1 file changed

+25
-2
lines changed

storage/filesystem/internal/dotgit/dotgit_setref_norwfs.go

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,39 @@
22

33
package dotgit
44

5-
import "gopkg.in/src-d/go-git.v4/plumbing"
5+
import (
6+
"fmt"
67

7-
// There are some filesystems tha don't support opening files in RDWD mode.
8+
"gopkg.in/src-d/go-git.v4/plumbing"
9+
)
10+
11+
// There are some filesystems that don't support opening files in RDWD mode.
812
// In these filesystems the standard SetRef function can not be used as i
913
// reads the reference file to check that it's not modified before updating it.
1014
//
1115
// This version of the function writes the reference without extra checks
1216
// making it compatible with these simple filesystems. This is usually not
1317
// a problem as they should be accessed by only one process at a time.
1418
func (d *DotGit) setRef(fileName, content string, old *plumbing.Reference) error {
19+
_, err := d.fs.Stat(fileName)
20+
if err == nil && old != nil {
21+
fRead, err := d.fs.Open(fileName)
22+
if err != nil {
23+
return err
24+
}
25+
26+
ref, err := d.readReferenceFrom(fRead, old.Name().String())
27+
fRead.Close()
28+
29+
if err != nil {
30+
return err
31+
}
32+
33+
if ref.Hash() != old.Hash() {
34+
return fmt.Errorf("reference has changed concurrently")
35+
}
36+
}
37+
1538
f, err := d.fs.Create(fileName)
1639
if err != nil {
1740
return err

0 commit comments

Comments
 (0)