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

Commit fa19052

Browse files
author
Dj Gilcrease
committed
Add global and system support
Closes #760
1 parent 1a7db85 commit fa19052

File tree

7 files changed

+140
-42
lines changed

7 files changed

+140
-42
lines changed

config/config.go

+25
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ type Config struct {
6262
// Branches list of branches, the key is the branch name and should
6363
// equal Branch.Name
6464
Branches map[string]*Branch
65+
User *User
6566
// Raw contains the raw information of a config file. The main goal is
6667
// preserve the parsed information from the original format, to avoid
6768
// dropping unsupported fields.
@@ -74,6 +75,7 @@ func NewConfig() *Config {
7475
Remotes: make(map[string]*RemoteConfig),
7576
Submodules: make(map[string]*Submodule),
7677
Branches: make(map[string]*Branch),
78+
User: new(User),
7779
Raw: format.New(),
7880
}
7981

@@ -121,6 +123,9 @@ const (
121123
windowKey = "window"
122124
mergeKey = "merge"
123125
rebaseKey = "rebase"
126+
userSection = "user"
127+
nameKey = "name"
128+
emailKey = "email"
124129

125130
// DefaultPackWindow holds the number of previous objects used to
126131
// generate deltas. The value 10 is the same used by git command.
@@ -138,6 +143,7 @@ func (c *Config) Unmarshal(b []byte) error {
138143
}
139144

140145
c.unmarshalCore()
146+
c.unmarshalUser()
141147
if err := c.unmarshalPack(); err != nil {
142148
return err
143149
}
@@ -160,6 +166,16 @@ func (c *Config) unmarshalCore() {
160166
c.Core.CommentChar = s.Options.Get(commentCharKey)
161167
}
162168

169+
func (c *Config) unmarshalUser() {
170+
s := c.Raw.Section(userSection)
171+
if name := s.Options.Get(nameKey); name != "" {
172+
c.User.Name = name
173+
}
174+
if email := s.Options.Get(emailKey); email != "" {
175+
c.User.Email = email
176+
}
177+
}
178+
163179
func (c *Config) unmarshalPack() error {
164180
s := c.Raw.Section(packSection)
165181
window := s.Options.Get(windowKey)
@@ -224,6 +240,7 @@ func (c *Config) Marshal() ([]byte, error) {
224240
c.marshalRemotes()
225241
c.marshalSubmodules()
226242
c.marshalBranches()
243+
c.marshalUser()
227244

228245
buf := bytes.NewBuffer(nil)
229246
if err := format.NewEncoder(buf).Encode(c.Raw); err != nil {
@@ -242,6 +259,14 @@ func (c *Config) marshalCore() {
242259
}
243260
}
244261

262+
func (c *Config) marshalUser() {
263+
if c.User.Name != "" || c.User.Email != "" {
264+
s := c.Raw.Section(userSection)
265+
s.SetOption(nameKey, c.User.Name)
266+
s.SetOption(emailKey, c.User.Email)
267+
}
268+
}
269+
245270
func (c *Config) marshalPack() {
246271
s := c.Raw.Section(packSection)
247272
if c.Pack.Window != DefaultPackWindow {

config/config_test.go

+13-1
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@ func (s *ConfigSuite) TestUnmarshal(c *C) {
3333
[branch "master"]
3434
remote = origin
3535
merge = refs/heads/master
36+
[user]
37+
name = Soandso
38+
3639
`)
3740

3841
cfg := NewConfig()
@@ -58,6 +61,8 @@ func (s *ConfigSuite) TestUnmarshal(c *C) {
5861
c.Assert(cfg.Submodules["qux"].Branch, Equals, "bar")
5962
c.Assert(cfg.Branches["master"].Remote, Equals, "origin")
6063
c.Assert(cfg.Branches["master"].Merge, Equals, plumbing.ReferenceName("refs/heads/master"))
64+
c.Assert(cfg.User.Name, Equals, "Soandso")
65+
c.Assert(cfg.User.Email, Equals, "[email protected]")
6166
}
6267

6368
func (s *ConfigSuite) TestMarshal(c *C) {
@@ -80,6 +85,9 @@ func (s *ConfigSuite) TestMarshal(c *C) {
8085
[branch "master"]
8186
remote = origin
8287
merge = refs/heads/master
88+
[user]
89+
name = Soandso
90+
8391
`)
8492

8593
cfg := NewConfig()
@@ -112,10 +120,11 @@ func (s *ConfigSuite) TestMarshal(c *C) {
112120
Remote: "origin",
113121
Merge: "refs/heads/master",
114122
}
123+
cfg.User.Name = "Soandso"
124+
cfg.User.Email = "[email protected]"
115125

116126
b, err := cfg.Marshal()
117127
c.Assert(err, IsNil)
118-
119128
c.Assert(string(b), Equals, string(output))
120129
}
121130

@@ -135,6 +144,9 @@ func (s *ConfigSuite) TestUnmarshalMarshal(c *C) {
135144
[branch "master"]
136145
remote = origin
137146
merge = refs/heads/master
147+
[user]
148+
name = Soandso
149+
138150
`)
139151

140152
cfg := NewConfig()

config/user.go

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package config
2+
3+
type User struct {
4+
Name string
5+
Email string
6+
}

go.mod

+8-6
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ require (
44
github.com/alcortesm/tgz v0.0.0-20161220082320-9c5fe88206d7 // indirect
55
github.com/anmitsu/go-shlex v0.0.0-20161002113705-648efa622239 // indirect
66
github.com/armon/go-socks5 v0.0.0-20160902184237-e75332964ef5
7+
github.com/davecgh/go-spew v1.1.1 // indirect
78
github.com/emirpasic/gods v1.12.0
89
github.com/flynn/go-shlex v0.0.0-20150515145356-3f9db97f8568 // indirect
910
github.com/gliderlabs/ssh v0.2.2
@@ -12,18 +13,19 @@ require (
1213
github.com/jessevdk/go-flags v1.4.0
1314
github.com/kevinburke/ssh_config v0.0.0-20190725054713-01f96b0aa0cd
1415
github.com/mitchellh/go-homedir v1.1.0
15-
github.com/pelletier/go-buffruneio v0.2.0 // indirect
1616
github.com/pkg/errors v0.8.1 // indirect
1717
github.com/sergi/go-diff v1.0.0
1818
github.com/src-d/gcfg v1.4.0
19-
github.com/stretchr/objx v0.2.0 // indirect
19+
github.com/stretchr/testify v1.3.0 // indirect
2020
github.com/xanzy/ssh-agent v0.2.1
21-
golang.org/x/crypto v0.0.0-20190701094942-4def268fd1a4
22-
golang.org/x/net v0.0.0-20190724013045-ca1201d0de80
21+
golang.org/x/crypto v0.0.0-20191112222119-e1110fd1c708
22+
golang.org/x/net v0.0.0-20191112182307-2180aed22343
23+
golang.org/x/sys v0.0.0-20191113165036-4c7a9d0fe056 // indirect
2324
golang.org/x/text v0.3.2
24-
golang.org/x/tools v0.0.0-20190729092621-ff9f1409240a // indirect
25-
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127
25+
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15
2626
gopkg.in/src-d/go-billy.v4 v4.3.2
2727
gopkg.in/src-d/go-git-fixtures.v3 v3.5.0
2828
gopkg.in/warnings.v0 v0.1.2 // indirect
2929
)
30+
31+
go 1.13

go.sum

+10-28
Original file line numberDiff line numberDiff line change
@@ -12,19 +12,14 @@ github.com/emirpasic/gods v1.12.0 h1:QAUIPSaCu4G+POclxeqb3F+WPpdKqFGlw36+yOzGlrg
1212
github.com/emirpasic/gods v1.12.0/go.mod h1:YfzfFFoVP/catgzJb4IKIqXjX78Ha8FMSDh3ymbK86o=
1313
github.com/flynn/go-shlex v0.0.0-20150515145356-3f9db97f8568 h1:BHsljHzVlRcyQhjrss6TZTdY2VfCqZPbv5k3iBFa2ZQ=
1414
github.com/flynn/go-shlex v0.0.0-20150515145356-3f9db97f8568/go.mod h1:xEzjJPgXI435gkrCt3MPfRiAkVrwSbHsst4LCFVfpJc=
15-
github.com/gliderlabs/ssh v0.1.3 h1:cBU46h1lYQk5f2Z+jZbewFKy+1zzE2aUX/ilcPDAm9M=
16-
github.com/gliderlabs/ssh v0.1.3/go.mod h1:U7qILu1NlMHj9FlMhZLlkCdDnU1DBEAqr0aevW3Awn0=
1715
github.com/gliderlabs/ssh v0.2.2 h1:6zsha5zo/TWhRhwqCD3+EarCAgZ2yN28ipRnGPnwkI0=
1816
github.com/gliderlabs/ssh v0.2.2/go.mod h1:U7qILu1NlMHj9FlMhZLlkCdDnU1DBEAqr0aevW3Awn0=
19-
github.com/google/go-cmp v0.2.0 h1:+dTQ8DZQJz0Mb/HjFlkptS1FeQ4cWSnN941F8aEG4SQ=
20-
github.com/google/go-cmp v0.2.0/go.mod h1:oXzfMopK8JAjlY9xF4vHSVASa0yLyX7SntLO5aqRK0M=
17+
github.com/google/go-cmp v0.3.0 h1:crn/baboCvb5fXaQ0IJ1SGTsTVrWpDsCWC8EGETZijY=
2118
github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU=
2219
github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99 h1:BQSFePA1RWJOlocH6Fxy8MmwDt+yVQYULKfN0RoTN8A=
2320
github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99/go.mod h1:1lJo3i6rXxKeerYnT8Nvf0QmHCRC1n8sfWVwXF2Frvo=
2421
github.com/jessevdk/go-flags v1.4.0 h1:4IU2WS7AumrZ/40jfhf4QVDMsQwqA7VEHozFRrGARJA=
2522
github.com/jessevdk/go-flags v1.4.0/go.mod h1:4FA24M0QyGHXBuZZK/XkWh8h0e1EYbRYJSGM75WSRxI=
26-
github.com/kevinburke/ssh_config v0.0.0-20180830205328-81db2a75821e h1:RgQk53JHp/Cjunrr1WlsXSZpqXn+uREuHvUVcK82CV8=
27-
github.com/kevinburke/ssh_config v0.0.0-20180830205328-81db2a75821e/go.mod h1:CT57kijsi8u/K/BOFA39wgDQJ9CxiF4nAY/ojJ6r6mM=
2823
github.com/kevinburke/ssh_config v0.0.0-20190725054713-01f96b0aa0cd h1:Coekwdh0v2wtGp9Gmz1Ze3eVRAWJMLokvN3QjdzCHLY=
2924
github.com/kevinburke/ssh_config v0.0.0-20190725054713-01f96b0aa0cd/go.mod h1:CT57kijsi8u/K/BOFA39wgDQJ9CxiF4nAY/ojJ6r6mM=
3025
github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI=
@@ -35,8 +30,6 @@ github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
3530
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
3631
github.com/mitchellh/go-homedir v1.1.0 h1:lukF9ziXFxDFPkA1vsr5zpc1XuPDn/wFntq5mG+4E0Y=
3732
github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0=
38-
github.com/pelletier/go-buffruneio v0.2.0 h1:U4t4R6YkofJ5xHm3dJzuRpPZ0mr5MMCoAWooScCR7aA=
39-
github.com/pelletier/go-buffruneio v0.2.0/go.mod h1:JkE26KsDizTr40EUHkXVtNPvgGtbSNq5BcowyYOWdKo=
4033
github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I=
4134
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
4235
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
@@ -46,44 +39,33 @@ github.com/sergi/go-diff v1.0.0/go.mod h1:0CfEIISq7TuYL3j771MWULgwwjU+GofnZX9QAm
4639
github.com/src-d/gcfg v1.4.0 h1:xXbNR5AlLSA315x2UO+fTSSAXCDf+Ar38/6oyGbDKQ4=
4740
github.com/src-d/gcfg v1.4.0/go.mod h1:p/UMsR43ujA89BJY9duynAwIpvqEujIH/jFlfL7jWoI=
4841
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
49-
github.com/stretchr/objx v0.2.0/go.mod h1:qt09Ya8vawLte6SNmTgCsAVtYtaKzEcn8ATUoHMkEqE=
5042
github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q=
5143
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
5244
github.com/xanzy/ssh-agent v0.2.1 h1:TCbipTQL2JiiCprBWx9frJ2eJlCYT00NmctrHxVAr70=
5345
github.com/xanzy/ssh-agent v0.2.1/go.mod h1:mLlQY/MoOhWBj+gOGMQkOeiEvkx+8pJSI+0Bx9h2kr4=
5446
golang.org/x/crypto v0.0.0-20190219172222-a4c6cb3142f2/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
5547
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
56-
golang.org/x/crypto v0.0.0-20190422183909-d864b10871cd h1:sMHc2rZHuzQmrbVoSpt9HgerkXPyIeCSO6k0zUMGfFk=
57-
golang.org/x/crypto v0.0.0-20190422183909-d864b10871cd/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
58-
golang.org/x/crypto v0.0.0-20190701094942-4def268fd1a4 h1:HuIa8hRrWRSrqYzx1qI49NNxhdi2PrY7gxVSq1JjLDc=
59-
golang.org/x/crypto v0.0.0-20190701094942-4def268fd1a4/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
48+
golang.org/x/crypto v0.0.0-20191112222119-e1110fd1c708 h1:pXVtWnwHkrWD9ru3sDxY/qFK/bfc0egRovX91EjWjf4=
49+
golang.org/x/crypto v0.0.0-20191112222119-e1110fd1c708/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
6050
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
61-
golang.org/x/net v0.0.0-20190420063019-afa5a82059c6 h1:HdqqaWmYAUI7/dmByKKEw+yxDksGSo+9GjkUc9Zp34E=
62-
golang.org/x/net v0.0.0-20190420063019-afa5a82059c6/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
63-
golang.org/x/net v0.0.0-20190502183928-7f726cade0ab h1:9RfW3ktsOZxgo9YNbBAjq1FWzc/igwEcUzZz8IXgSbk=
64-
golang.org/x/net v0.0.0-20190502183928-7f726cade0ab/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
65-
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
66-
golang.org/x/net v0.0.0-20190724013045-ca1201d0de80 h1:Ao/3l156eZf2AW5wK8a7/smtodRU+gha3+BeqJ69lRk=
67-
golang.org/x/net v0.0.0-20190724013045-ca1201d0de80/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
68-
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
69-
golang.org/x/sys v0.0.0-20180903190138-2b024373dcd9 h1:lkiLiLBHGoH3XnqSLUIaBsilGMUjI+Uy2Xu2JLUtTas=
70-
golang.org/x/sys v0.0.0-20180903190138-2b024373dcd9/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
51+
golang.org/x/net v0.0.0-20191112182307-2180aed22343 h1:00ohfJ4K98s3m6BGUoBd8nyfp4Yl0GoIKvw5abItTjI=
52+
golang.org/x/net v0.0.0-20191112182307-2180aed22343/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
7153
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
7254
golang.org/x/sys v0.0.0-20190221075227-b4e8571b14e0/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
7355
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
74-
golang.org/x/sys v0.0.0-20190422165155-953cdadca894 h1:Cz4ceDQGXuKRnVBDTS23GTn/pU5OE2C0WrNTOYK1Uuc=
75-
golang.org/x/sys v0.0.0-20190422165155-953cdadca894/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
7656
golang.org/x/sys v0.0.0-20190726091711-fc99dfbffb4e h1:D5TXcfTk7xF7hvieo4QErS3qqCB4teTffacDWr7CI+0=
7757
golang.org/x/sys v0.0.0-20190726091711-fc99dfbffb4e/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
58+
golang.org/x/sys v0.0.0-20191113165036-4c7a9d0fe056 h1:dHtDnRWQtSx0Hjq9kvKFpBh9uPPKfQN70NZZmvssGwk=
59+
golang.org/x/sys v0.0.0-20191113165036-4c7a9d0fe056/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
7860
golang.org/x/text v0.3.0 h1:g61tztE5qeGQ89tm6NTjjM9VPIm088od1l6aSorWRWg=
7961
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
62+
golang.org/x/text v0.3.2 h1:tW2bmiBqwgJj/UpqtC8EpXEZVYOwU0yG4iWbprSVAcs=
8063
golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=
8164
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
82-
golang.org/x/tools v0.0.0-20190729092621-ff9f1409240a/go.mod h1:jcCCGcm9btYwXyDqrUWc6MKQKKGJCWEQ3AfLSRIbEuI=
8365
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 h1:qIbj1fsPNlZgppZ+VLlY7N33q108Sa+fhmuc+sWQYwY=
8466
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
85-
gopkg.in/src-d/go-billy.v4 v4.3.0 h1:KtlZ4c1OWbIs4jCv5ZXrTqG8EQocr0g/d4DjNg70aek=
86-
gopkg.in/src-d/go-billy.v4 v4.3.0/go.mod h1:tm33zBoOwxjYHZIE+OV8bxTWFMJLrconzFMd38aARFk=
67+
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 h1:YR8cESwS4TdDjEe65xsg0ogRM/Nc3DYOhEAlW+xobZo=
68+
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
8769
gopkg.in/src-d/go-billy.v4 v4.3.2 h1:0SQA1pRztfTFx2miS8sA97XvooFeNOmvUenF4o0EcVg=
8870
gopkg.in/src-d/go-billy.v4 v4.3.2/go.mod h1:nDjArDMp+XMs1aFAESLRjfGSgfvoYN0hDfzEk0GjC98=
8971
gopkg.in/src-d/go-git-fixtures.v3 v3.5.0 h1:ivZFOIltbce2Mo8IjzUHAFoq/IylO9WHhNOAJK+LsJg=

storage/filesystem/config.go

+57-7
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package filesystem
22

33
import (
4+
"fmt"
45
stdioutil "io/ioutil"
56
"os"
67

@@ -15,28 +16,77 @@ type ConfigStorage struct {
1516

1617
func (c *ConfigStorage) Config() (conf *config.Config, err error) {
1718
cfg := config.NewConfig()
19+
var b []byte
20+
data := []byte("\n")
1821

19-
f, err := c.dir.Config()
20-
if err != nil {
22+
if b, err = c.systemConfig(); err != nil {
23+
return nil, err
24+
}
25+
data = append(data, b...)
26+
data = append(data, []byte("\n")...)
27+
28+
if b, err = c.userConfig(); err != nil {
29+
return nil, err
30+
}
31+
data = append(data, b...)
32+
data = append(data, []byte("\n")...)
33+
34+
if b, err = c.localConfig(); err != nil {
35+
return nil, err
36+
}
37+
data = append(data, b...)
38+
data = append(data, []byte("\n")...)
39+
40+
if err = cfg.Unmarshal(data); err != nil {
41+
return nil, err
42+
}
43+
44+
return cfg, err
45+
}
46+
47+
func (c *ConfigStorage) systemConfig() (b []byte, err error) {
48+
if b, err = c.dir.SystemConfig(); err != nil {
2149
if os.IsNotExist(err) {
22-
return cfg, nil
50+
return []byte{}, nil
2351
}
2452

2553
return nil, err
2654
}
2755

28-
defer ioutil.CheckClose(f, &err)
56+
return b, nil
57+
}
58+
59+
func (c *ConfigStorage) userConfig() (b []byte, err error) {
60+
if b, err = c.dir.UserConfig(); err != nil {
61+
fmt.Println("USER ERROR: ", err)
62+
if os.IsNotExist(err) {
63+
return []byte{}, nil
64+
}
65+
66+
return nil, err
67+
}
68+
69+
return b, nil
70+
}
2971

30-
b, err := stdioutil.ReadAll(f)
72+
func (c *ConfigStorage) localConfig() (b []byte, err error) {
73+
f, err := c.dir.Config()
3174
if err != nil {
75+
if os.IsNotExist(err) {
76+
return []byte{}, nil
77+
}
78+
3279
return nil, err
3380
}
3481

35-
if err = cfg.Unmarshal(b); err != nil {
82+
defer ioutil.CheckClose(f, &err)
83+
84+
b, err = stdioutil.ReadAll(f)
85+
if err != nil {
3686
return nil, err
3787
}
3888

39-
return cfg, err
89+
return b, nil
4090
}
4191

4292
func (c *ConfigStorage) SetConfig(cfg *config.Config) (err error) {

storage/filesystem/dotgit/dotgit.go

+21
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import (
88
"io"
99
stdioutil "io/ioutil"
1010
"os"
11+
"os/user"
12+
"path"
1113
"path/filepath"
1214
"strings"
1315
"time"
@@ -23,6 +25,8 @@ import (
2325
const (
2426
suffix = ".git"
2527
packedRefsPath = "packed-refs"
28+
systemConfigPath = "/etc/gitconfig"
29+
userConfigFile = ".gitconfig"
2630
configPath = "config"
2731
indexPath = "index"
2832
shallowPath = "shallow"
@@ -156,6 +160,23 @@ func (d *DotGit) ConfigWriter() (billy.File, error) {
156160
return d.fs.Create(configPath)
157161
}
158162

163+
// Config returns a file pointer for read to the config file
164+
func (d *DotGit) SystemConfig() ([]byte, error) {
165+
return stdioutil.ReadFile(systemConfigPath)
166+
}
167+
168+
// UserConfig returns a file pointer for read to the users config file
169+
func (d *DotGit) UserConfig() ([]byte, error) {
170+
usr, err := user.Current()
171+
if err != nil {
172+
usr = &user.User{
173+
HomeDir: os.Getenv("HOME"),
174+
}
175+
}
176+
177+
return stdioutil.ReadFile(path.Join(usr.HomeDir, userConfigFile))
178+
}
179+
159180
// Config returns a file pointer for read to the config file
160181
func (d *DotGit) Config() (billy.File, error) {
161182
return d.fs.Open(configPath)

0 commit comments

Comments
 (0)