Skip to content

Commit c69d533

Browse files
committed
plumbing: format, use os.UserHomeDir()
1 parent 67d3490 commit c69d533

File tree

9 files changed

+72
-155
lines changed

9 files changed

+72
-155
lines changed

plumbing/format/commitgraph/commitgraph_test.go

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
package commitgraph_test
22

33
import (
4-
"io/ioutil"
54
"os"
6-
"path"
75
"testing"
86

7+
"github.com/go-git/go-billy/v5"
8+
"github.com/go-git/go-billy/v5/util"
99
"github.com/go-git/go-git/v5/plumbing"
1010
"github.com/go-git/go-git/v5/plumbing/format/commitgraph"
1111

@@ -21,8 +21,8 @@ type CommitgraphSuite struct {
2121

2222
var _ = Suite(&CommitgraphSuite{})
2323

24-
func testDecodeHelper(c *C, path string) {
25-
reader, err := os.Open(path)
24+
func testDecodeHelper(c *C, fs billy.Filesystem, path string) {
25+
reader, err := fs.Open(path)
2626
c.Assert(err, IsNil)
2727
defer reader.Close()
2828
index, err := commitgraph.OpenFileIndex(reader)
@@ -76,38 +76,39 @@ func testDecodeHelper(c *C, path string) {
7676
func (s *CommitgraphSuite) TestDecode(c *C) {
7777
fixtures.ByTag("commit-graph").Test(c, func(f *fixtures.Fixture) {
7878
dotgit := f.DotGit()
79-
testDecodeHelper(c, path.Join(dotgit.Root(), "objects", "info", "commit-graph"))
79+
testDecodeHelper(c, dotgit, dotgit.Join("objects", "info", "commit-graph"))
8080
})
8181
}
8282

8383
func (s *CommitgraphSuite) TestReencode(c *C) {
8484
fixtures.ByTag("commit-graph").Test(c, func(f *fixtures.Fixture) {
8585
dotgit := f.DotGit()
8686

87-
reader, err := os.Open(path.Join(dotgit.Root(), "objects", "info", "commit-graph"))
87+
reader, err := dotgit.Open(dotgit.Join("objects", "info", "commit-graph"))
8888
c.Assert(err, IsNil)
8989
defer reader.Close()
9090
index, err := commitgraph.OpenFileIndex(reader)
9191
c.Assert(err, IsNil)
9292

93-
writer, err := ioutil.TempFile(dotgit.Root(), "commit-graph")
93+
writer, err := util.TempFile(dotgit, "", "commit-graph")
9494
c.Assert(err, IsNil)
9595
tmpName := writer.Name()
9696
defer os.Remove(tmpName)
97+
9798
encoder := commitgraph.NewEncoder(writer)
9899
err = encoder.Encode(index)
99100
c.Assert(err, IsNil)
100101
writer.Close()
101102

102-
testDecodeHelper(c, tmpName)
103+
testDecodeHelper(c, dotgit, tmpName)
103104
})
104105
}
105106

106107
func (s *CommitgraphSuite) TestReencodeInMemory(c *C) {
107108
fixtures.ByTag("commit-graph").Test(c, func(f *fixtures.Fixture) {
108109
dotgit := f.DotGit()
109110

110-
reader, err := os.Open(path.Join(dotgit.Root(), "objects", "info", "commit-graph"))
111+
reader, err := dotgit.Open(dotgit.Join("objects", "info", "commit-graph"))
111112
c.Assert(err, IsNil)
112113
index, err := commitgraph.OpenFileIndex(reader)
113114
c.Assert(err, IsNil)
@@ -119,15 +120,16 @@ func (s *CommitgraphSuite) TestReencodeInMemory(c *C) {
119120
}
120121
reader.Close()
121122

122-
writer, err := ioutil.TempFile(dotgit.Root(), "commit-graph")
123+
writer, err := util.TempFile(dotgit, "", "commit-graph")
123124
c.Assert(err, IsNil)
124125
tmpName := writer.Name()
125126
defer os.Remove(tmpName)
127+
126128
encoder := commitgraph.NewEncoder(writer)
127129
err = encoder.Encode(memoryIndex)
128130
c.Assert(err, IsNil)
129131
writer.Close()
130132

131-
testDecodeHelper(c, tmpName)
133+
testDecodeHelper(c, dotgit, tmpName)
132134
})
133135
}

plumbing/format/gitattributes/dir.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package gitattributes
22

33
import (
44
"os"
5-
"os/user"
65

76
"github.com/go-git/go-billy/v5"
87
"github.com/go-git/go-git/v5/plumbing/format/config"
@@ -106,12 +105,12 @@ func loadPatterns(fs billy.Filesystem, path string) ([]MatchAttribute, error) {
106105
// the function will return nil. The function assumes fs is rooted at the root
107106
// filesystem.
108107
func LoadGlobalPatterns(fs billy.Filesystem) (attributes []MatchAttribute, err error) {
109-
usr, err := user.Current()
108+
home, err := os.UserHomeDir()
110109
if err != nil {
111110
return
112111
}
113112

114-
return loadPatterns(fs, fs.Join(usr.HomeDir, gitconfigFile))
113+
return loadPatterns(fs, fs.Join(home, gitconfigFile))
115114
}
116115

117116
// LoadSystemPatterns loads gitattributes patterns and attributes from the

plumbing/format/gitattributes/dir_test.go

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package gitattributes
22

33
import (
44
"os"
5-
"os/user"
65
"strconv"
76

87
"github.com/go-git/go-billy/v5"
@@ -23,8 +22,7 @@ type MatcherSuite struct {
2322
var _ = Suite(&MatcherSuite{})
2423

2524
func (s *MatcherSuite) SetUpTest(c *C) {
26-
// setup root that contains user home
27-
usr, err := user.Current()
25+
home, err := os.UserHomeDir()
2826
c.Assert(err, IsNil)
2927

3028
gitAttributesGlobal := func(fs billy.Filesystem, filename string) {
@@ -62,59 +60,59 @@ func (s *MatcherSuite) SetUpTest(c *C) {
6260
fs.MkdirAll("vendor/github.com", os.ModePerm)
6361
fs.MkdirAll("vendor/gopkg.in", os.ModePerm)
6462

65-
gitAttributesGlobal(fs, fs.Join(usr.HomeDir, ".gitattributes_global"))
63+
gitAttributesGlobal(fs, fs.Join(home, ".gitattributes_global"))
6664

6765
s.GFS = fs
6866

6967
fs = memfs.New()
70-
err = fs.MkdirAll(usr.HomeDir, os.ModePerm)
68+
err = fs.MkdirAll(home, os.ModePerm)
7169
c.Assert(err, IsNil)
7270

73-
f, err = fs.Create(fs.Join(usr.HomeDir, gitconfigFile))
71+
f, err = fs.Create(fs.Join(home, gitconfigFile))
7472
c.Assert(err, IsNil)
7573
_, err = f.Write([]byte("[core]\n"))
7674
c.Assert(err, IsNil)
77-
_, err = f.Write([]byte(" attributesfile = " + strconv.Quote(fs.Join(usr.HomeDir, ".gitattributes_global")) + "\n"))
75+
_, err = f.Write([]byte(" attributesfile = " + strconv.Quote(fs.Join(home, ".gitattributes_global")) + "\n"))
7876
c.Assert(err, IsNil)
7977
err = f.Close()
8078
c.Assert(err, IsNil)
8179

82-
gitAttributesGlobal(fs, fs.Join(usr.HomeDir, ".gitattributes_global"))
80+
gitAttributesGlobal(fs, fs.Join(home, ".gitattributes_global"))
8381

8482
s.RFS = fs
8583

8684
// root that contains user home, but missing ~/.gitconfig
8785
fs = memfs.New()
88-
gitAttributesGlobal(fs, fs.Join(usr.HomeDir, ".gitattributes_global"))
86+
gitAttributesGlobal(fs, fs.Join(home, ".gitattributes_global"))
8987

9088
s.MCFS = fs
9189

9290
// setup root that contains user home, but missing attributesfile entry
9391
fs = memfs.New()
94-
err = fs.MkdirAll(usr.HomeDir, os.ModePerm)
92+
err = fs.MkdirAll(home, os.ModePerm)
9593
c.Assert(err, IsNil)
9694

97-
f, err = fs.Create(fs.Join(usr.HomeDir, gitconfigFile))
95+
f, err = fs.Create(fs.Join(home, gitconfigFile))
9896
c.Assert(err, IsNil)
9997
_, err = f.Write([]byte("[core]\n"))
10098
c.Assert(err, IsNil)
10199
err = f.Close()
102100
c.Assert(err, IsNil)
103101

104-
gitAttributesGlobal(fs, fs.Join(usr.HomeDir, ".gitattributes_global"))
102+
gitAttributesGlobal(fs, fs.Join(home, ".gitattributes_global"))
105103

106104
s.MEFS = fs
107105

108106
// setup root that contains user home, but missing .gitattributes
109107
fs = memfs.New()
110-
err = fs.MkdirAll(usr.HomeDir, os.ModePerm)
108+
err = fs.MkdirAll(home, os.ModePerm)
111109
c.Assert(err, IsNil)
112110

113-
f, err = fs.Create(fs.Join(usr.HomeDir, gitconfigFile))
111+
f, err = fs.Create(fs.Join(home, gitconfigFile))
114112
c.Assert(err, IsNil)
115113
_, err = f.Write([]byte("[core]\n"))
116114
c.Assert(err, IsNil)
117-
_, err = f.Write([]byte(" attributesfile = " + strconv.Quote(fs.Join(usr.HomeDir, ".gitattributes_global")) + "\n"))
115+
_, err = f.Write([]byte(" attributesfile = " + strconv.Quote(fs.Join(home, ".gitattributes_global")) + "\n"))
118116
c.Assert(err, IsNil)
119117
err = f.Close()
120118
c.Assert(err, IsNil)

plumbing/format/gitignore/dir.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
55
"bytes"
66
"io/ioutil"
77
"os"
8-
"os/user"
98
"strings"
109

1110
"github.com/go-git/go-billy/v5"
@@ -116,12 +115,12 @@ func loadPatterns(fs billy.Filesystem, path string) (ps []Pattern, err error) {
116115
//
117116
// The function assumes fs is rooted at the root filesystem.
118117
func LoadGlobalPatterns(fs billy.Filesystem) (ps []Pattern, err error) {
119-
usr, err := user.Current()
118+
home, err := os.UserHomeDir()
120119
if err != nil {
121120
return
122121
}
123122

124-
return loadPatterns(fs, fs.Join(usr.HomeDir, gitconfigFile))
123+
return loadPatterns(fs, fs.Join(home, gitconfigFile))
125124
}
126125

127126
// LoadSystemPatterns loads gitignore patterns from from the gitignore file

plumbing/format/gitignore/dir_test.go

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package gitignore
22

33
import (
44
"os"
5-
"os/user"
65
"strconv"
76

87
"github.com/go-git/go-billy/v5"
@@ -55,23 +54,23 @@ func (s *MatcherSuite) SetUpTest(c *C) {
5554
s.GFS = fs
5655

5756
// setup root that contains user home
58-
usr, err := user.Current()
57+
home, err := os.UserHomeDir()
5958
c.Assert(err, IsNil)
6059

6160
fs = memfs.New()
62-
err = fs.MkdirAll(usr.HomeDir, os.ModePerm)
61+
err = fs.MkdirAll(home, os.ModePerm)
6362
c.Assert(err, IsNil)
6463

65-
f, err = fs.Create(fs.Join(usr.HomeDir, gitconfigFile))
64+
f, err = fs.Create(fs.Join(home, gitconfigFile))
6665
c.Assert(err, IsNil)
6766
_, err = f.Write([]byte("[core]\n"))
6867
c.Assert(err, IsNil)
69-
_, err = f.Write([]byte(" excludesfile = " + strconv.Quote(fs.Join(usr.HomeDir, ".gitignore_global")) + "\n"))
68+
_, err = f.Write([]byte(" excludesfile = " + strconv.Quote(fs.Join(home, ".gitignore_global")) + "\n"))
7069
c.Assert(err, IsNil)
7170
err = f.Close()
7271
c.Assert(err, IsNil)
7372

74-
f, err = fs.Create(fs.Join(usr.HomeDir, ".gitignore_global"))
73+
f, err = fs.Create(fs.Join(home, ".gitignore_global"))
7574
c.Assert(err, IsNil)
7675
_, err = f.Write([]byte("# IntelliJ\n"))
7776
c.Assert(err, IsNil)
@@ -86,10 +85,10 @@ func (s *MatcherSuite) SetUpTest(c *C) {
8685

8786
// root that contains user home, but missing ~/.gitconfig
8887
fs = memfs.New()
89-
err = fs.MkdirAll(usr.HomeDir, os.ModePerm)
88+
err = fs.MkdirAll(home, os.ModePerm)
9089
c.Assert(err, IsNil)
9190

92-
f, err = fs.Create(fs.Join(usr.HomeDir, ".gitignore_global"))
91+
f, err = fs.Create(fs.Join(home, ".gitignore_global"))
9392
c.Assert(err, IsNil)
9493
_, err = f.Write([]byte("# IntelliJ\n"))
9594
c.Assert(err, IsNil)
@@ -104,17 +103,17 @@ func (s *MatcherSuite) SetUpTest(c *C) {
104103

105104
// setup root that contains user home, but missing excludesfile entry
106105
fs = memfs.New()
107-
err = fs.MkdirAll(usr.HomeDir, os.ModePerm)
106+
err = fs.MkdirAll(home, os.ModePerm)
108107
c.Assert(err, IsNil)
109108

110-
f, err = fs.Create(fs.Join(usr.HomeDir, gitconfigFile))
109+
f, err = fs.Create(fs.Join(home, gitconfigFile))
111110
c.Assert(err, IsNil)
112111
_, err = f.Write([]byte("[core]\n"))
113112
c.Assert(err, IsNil)
114113
err = f.Close()
115114
c.Assert(err, IsNil)
116115

117-
f, err = fs.Create(fs.Join(usr.HomeDir, ".gitignore_global"))
116+
f, err = fs.Create(fs.Join(home, ".gitignore_global"))
118117
c.Assert(err, IsNil)
119118
_, err = f.Write([]byte("# IntelliJ\n"))
120119
c.Assert(err, IsNil)
@@ -129,14 +128,14 @@ func (s *MatcherSuite) SetUpTest(c *C) {
129128

130129
// setup root that contains user home, but missing .gitnignore
131130
fs = memfs.New()
132-
err = fs.MkdirAll(usr.HomeDir, os.ModePerm)
131+
err = fs.MkdirAll(home, os.ModePerm)
133132
c.Assert(err, IsNil)
134133

135-
f, err = fs.Create(fs.Join(usr.HomeDir, gitconfigFile))
134+
f, err = fs.Create(fs.Join(home, gitconfigFile))
136135
c.Assert(err, IsNil)
137136
_, err = f.Write([]byte("[core]\n"))
138137
c.Assert(err, IsNil)
139-
_, err = f.Write([]byte(" excludesfile = " + strconv.Quote(fs.Join(usr.HomeDir, ".gitignore_global")) + "\n"))
138+
_, err = f.Write([]byte(" excludesfile = " + strconv.Quote(fs.Join(home, ".gitignore_global")) + "\n"))
140139
c.Assert(err, IsNil)
141140
err = f.Close()
142141
c.Assert(err, IsNil)

0 commit comments

Comments
 (0)