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

Commit 8d28840

Browse files
committed
Add a setRef version that supports non rw fs
There are some filesystems that do not support opening the files in read and write modes at the same time. The method SetRef is split in files with an extra version that only writes the reference. It can be activated with -tags norwfs on building. Signed-off-by: Javi Fontan <jfontan@gmail.com>
1 parent 55b5d73 commit 8d28840

File tree

3 files changed

+62
-29
lines changed

3 files changed

+62
-29
lines changed

storage/filesystem/internal/dotgit/dotgit.go

Lines changed: 2 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -323,36 +323,9 @@ func (d *DotGit) SetRef(r, old *plumbing.Reference) error {
323323
content = fmt.Sprintln(r.Hash().String())
324324
}
325325

326-
// If we are not checking an old ref, just truncate the file.
327-
mode := os.O_RDWR | os.O_CREATE
328-
if old == nil {
329-
mode |= os.O_TRUNC
330-
}
331-
332-
f, err := d.fs.OpenFile(r.Name().String(), mode, 0666)
333-
if err != nil {
334-
return err
335-
}
336-
337-
defer ioutil.CheckClose(f, &err)
338-
339-
// Lock is unlocked by the deferred Close above. This is because Unlock
340-
// does not imply a fsync and thus there would be a race between
341-
// Unlock+Close and other concurrent writers. Adding Sync to go-billy
342-
// could work, but this is better (and avoids superfluous syncs).
343-
err = f.Lock()
344-
if err != nil {
345-
return err
346-
}
347-
348-
// this is a no-op to call even when old is nil.
349-
err = d.checkReferenceAndTruncate(f, old)
350-
if err != nil {
351-
return err
352-
}
326+
fileName := r.Name().String()
353327

354-
_, err = f.Write([]byte(content))
355-
return err
328+
return d.setRef(fileName, content, old)
356329
}
357330

358331
// Refs scans the git directory collecting references, which it returns.
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// +build !norwfs
2+
3+
package dotgit
4+
5+
import (
6+
"os"
7+
8+
"gopkg.in/src-d/go-git.v4/plumbing"
9+
"gopkg.in/src-d/go-git.v4/utils/ioutil"
10+
)
11+
12+
func (d *DotGit) setRef(fileName, content string, old *plumbing.Reference) error {
13+
// If we are not checking an old ref, just truncate the file.
14+
mode := os.O_RDWR | os.O_CREATE
15+
if old == nil {
16+
mode |= os.O_TRUNC
17+
}
18+
19+
f, err := d.fs.OpenFile(fileName, mode, 0666)
20+
if err != nil {
21+
return err
22+
}
23+
24+
defer ioutil.CheckClose(f, &err)
25+
26+
// Lock is unlocked by the deferred Close above. This is because Unlock
27+
// does not imply a fsync and thus there would be a race between
28+
// Unlock+Close and other concurrent writers. Adding Sync to go-billy
29+
// could work, but this is better (and avoids superfluous syncs).
30+
err = f.Lock()
31+
if err != nil {
32+
return err
33+
}
34+
35+
// this is a no-op to call even when old is nil.
36+
err = d.checkReferenceAndTruncate(f, old)
37+
if err != nil {
38+
return err
39+
}
40+
41+
_, err = f.Write([]byte(content))
42+
return err
43+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// +build norwfs
2+
3+
package dotgit
4+
5+
import "gopkg.in/src-d/go-git.v4/plumbing"
6+
7+
func (d *DotGit) setRef(fileName, content string, old *plumbing.Reference) error {
8+
f, err := d.fs.Create(fileName)
9+
if err != nil {
10+
return err
11+
}
12+
13+
defer f.Close()
14+
15+
_, err = f.Write([]byte(content))
16+
return err
17+
}

0 commit comments

Comments
 (0)