@@ -3,6 +3,7 @@ package config
3
3
import (
4
4
. "gopkg.in/check.v1"
5
5
"github.com/go-git/go-git/v5/plumbing"
6
+ format "github.com/go-git/go-git/v5/plumbing/format/config"
6
7
)
7
8
8
9
type ConfigSuite struct {}
@@ -60,6 +61,76 @@ func (s *ConfigSuite) TestUnmarshal(c *C) {
60
61
c .Assert (cfg .Branches ["master" ].Merge , Equals , plumbing .ReferenceName ("refs/heads/master" ))
61
62
}
62
63
64
+ func (s * ConfigSuite ) TestMergedUnmarshal (c * C ) {
65
+ localInput := []byte (`[core]
66
+ bare = true
67
+ worktree = foo
68
+ commentchar = bar
69
+ [pack]
70
+ window = 20
71
+ [remote "origin"]
72
+ url = [email protected] :mcuadros/go-git.git
73
+ fetch = +refs/heads/*:refs/remotes/origin/*
74
+ [remote "alt"]
75
+ url = [email protected] :mcuadros/go-git.git
76
+ url = [email protected] :src-d/go-git.git
77
+ fetch = +refs/heads/*:refs/remotes/origin/*
78
+ fetch = +refs/pull/*:refs/remotes/origin/pull/*
79
+ [remote "win-local"]
80
+ url = X:\\Git\\
81
+ [submodule "qux"]
82
+ path = qux
83
+ url = https://github.com/foo/qux.git
84
+ branch = bar
85
+ [branch "master"]
86
+ remote = origin
87
+ merge = refs/heads/master
88
+ [user]
89
+ name = Override
90
+ ` )
91
+
92
+ globalInput := []byte (`
93
+ [user]
94
+ name = Soandso
95
+
96
+ [core]
97
+ editor = nvim
98
+ [push]
99
+ default = simple
100
+ ` )
101
+
102
+ cfg := NewConfig ()
103
+
104
+ err := cfg .UnmarshalScoped (format .LocalScope , localInput )
105
+ c .Assert (err , IsNil )
106
+
107
+ err = cfg .UnmarshalScoped (format .GlobalScope , globalInput )
108
+ c .Assert (err , IsNil )
109
+
110
+ c .Assert (cfg .Core .IsBare , Equals , true )
111
+ c .Assert (cfg .Core .Worktree , Equals , "foo" )
112
+ c .Assert (cfg .Core .CommentChar , Equals , "bar" )
113
+ c .Assert (cfg .Pack .Window , Equals , uint (20 ))
114
+ c .Assert (cfg .Remotes , HasLen , 3 )
115
+ c .Assert (cfg .Remotes ["origin" ].Name , Equals , "origin" )
116
+ c .
Assert (
cfg .
Remotes [
"origin" ].
URLs ,
DeepEquals , []
string {
"[email protected] :mcuadros/go-git.git" })
117
+ c .Assert (cfg .Remotes ["origin" ].Fetch , DeepEquals , []RefSpec {"+refs/heads/*:refs/remotes/origin/*" })
118
+ c .Assert (cfg .Remotes ["alt" ].Name , Equals , "alt" )
119
+ c .
Assert (
cfg .
Remotes [
"alt" ].
URLs ,
DeepEquals , []
string {
"[email protected] :mcuadros/go-git.git" ,
"[email protected] :src-d/go-git.git" })
120
+ c .Assert (cfg .Remotes ["alt" ].Fetch , DeepEquals , []RefSpec {"+refs/heads/*:refs/remotes/origin/*" , "+refs/pull/*:refs/remotes/origin/pull/*" })
121
+ c .Assert (cfg .Remotes ["win-local" ].Name , Equals , "win-local" )
122
+ c .Assert (cfg .Remotes ["win-local" ].URLs , DeepEquals , []string {"X:\\ Git\\ " })
123
+ c .Assert (cfg .Submodules , HasLen , 1 )
124
+ c .Assert (cfg .Submodules ["qux" ].Name , Equals , "qux" )
125
+ c .Assert (cfg .Submodules ["qux" ].URL , Equals , "https://github.com/foo/qux.git" )
126
+ c .Assert (cfg .Submodules ["qux" ].Branch , Equals , "bar" )
127
+ c .Assert (cfg .Branches ["master" ].Remote , Equals , "origin" )
128
+ c .Assert (cfg .Branches ["master" ].Merge , Equals , plumbing .ReferenceName ("refs/heads/master" ))
129
+ c .Assert (cfg .Merged .Section ("user" ).Option ("name" ), Equals , "Override" )
130
+ c .
Assert (
cfg .
Merged .
Section (
"user" ).
Option (
"email" ),
Equals ,
"[email protected] " )
131
+ c .Assert (cfg .Merged .Section ("push" ).Option ("default" ), Equals , "simple" )
132
+ }
133
+
63
134
func (s * ConfigSuite ) TestMarshal (c * C ) {
64
135
output := []byte (`[core]
65
136
bare = true
@@ -119,6 +190,95 @@ func (s *ConfigSuite) TestMarshal(c *C) {
119
190
c .Assert (string (b ), Equals , string (output ))
120
191
}
121
192
193
+ func (s * ConfigSuite ) TestMergedMarshal (c * C ) {
194
+ localOutput := []byte (`[user]
195
+ name = Override
196
+ [custom]
197
+ key = value
198
+ [core]
199
+ bare = true
200
+ worktree = bar
201
+ [pack]
202
+ window = 20
203
+ [remote "alt"]
204
+ url = [email protected] :mcuadros/go-git.git
205
+ url = [email protected] :src-d/go-git.git
206
+ fetch = +refs/heads/*:refs/remotes/origin/*
207
+ fetch = +refs/pull/*:refs/remotes/origin/pull/*
208
+ [remote "origin"]
209
+ url = [email protected] :mcuadros/go-git.git
210
+ [remote "win-local"]
211
+ url = "X:\\Git\\"
212
+ [submodule "qux"]
213
+ url = https://github.com/foo/qux.git
214
+ [branch "master"]
215
+ remote = origin
216
+ merge = refs/heads/master
217
+ ` )
218
+
219
+ globalOutput := []byte (`[user]
220
+ name = Soandso
221
+
222
+ [core]
223
+ editor = nvim
224
+ [push]
225
+ default = simple
226
+ ` )
227
+
228
+ cfg := NewConfig ()
229
+
230
+ cfg .Core .IsBare = true
231
+ cfg .Core .Worktree = "bar"
232
+ cfg .Pack .Window = 20
233
+ cfg .Remotes ["origin" ] = & RemoteConfig {
234
+ Name : "origin" ,
235
+ URLs : []
string {
"[email protected] :mcuadros/go-git.git" },
236
+ }
237
+
238
+ cfg .Remotes ["alt" ] = & RemoteConfig {
239
+ Name : "alt" ,
240
+ URLs : []
string {
"[email protected] :mcuadros/go-git.git" ,
"[email protected] :src-d/go-git.git" },
241
+ Fetch : []RefSpec {"+refs/heads/*:refs/remotes/origin/*" , "+refs/pull/*:refs/remotes/origin/pull/*" },
242
+ }
243
+
244
+ cfg .Remotes ["win-local" ] = & RemoteConfig {
245
+ Name : "win-local" ,
246
+ URLs : []string {"X:\\ Git\\ " },
247
+ }
248
+
249
+ cfg .Submodules ["qux" ] = & Submodule {
250
+ Name : "qux" ,
251
+ URL : "https://github.com/foo/qux.git" ,
252
+ }
253
+
254
+ cfg .Branches ["master" ] = & Branch {
255
+ Name : "master" ,
256
+ Remote : "origin" ,
257
+ Merge : "refs/heads/master" ,
258
+ }
259
+
260
+ cfg .Merged .GlobalConfig ().Section ("user" ).SetOption ("name" , "Soandso" )
261
+ cfg .Merged .LocalConfig ().Section ("user" ).SetOption ("name" , "Override" )
262
+ cfg .
Merged .
GlobalConfig ().
Section (
"user" ).
SetOption (
"email" ,
"[email protected] " )
263
+ cfg .Merged .GlobalConfig ().Section ("core" ).AddOption ("editor" , "nvim" )
264
+ cfg .Merged .LocalConfig ().Section ("custom" ).SetOption ("key" , "value" )
265
+ cfg .Merged .GlobalConfig ().Section ("push" ).AddOption ("default" , "simple" )
266
+
267
+ c .Assert (cfg .Merged .Section ("user" ).Option ("name" ), Equals , "Override" )
268
+
269
+ localBytes , err := cfg .Marshal ()
270
+ c .Assert (err , IsNil )
271
+ c .Assert (string (localBytes ), Equals , string (localOutput ))
272
+
273
+ globalBytes , err := cfg .MarshalScope (format .GlobalScope )
274
+ c .Assert (err , IsNil )
275
+ c .Assert (string (globalBytes ), Equals , string (globalOutput ))
276
+
277
+ systemBytes , err := cfg .MarshalScope (format .SystemScope )
278
+ c .Assert (err , IsNil )
279
+ c .Assert (string (systemBytes ), Equals , "" )
280
+ }
281
+
122
282
func (s * ConfigSuite ) TestUnmarshalMarshal (c * C ) {
123
283
input := []byte (`[core]
124
284
bare = true
0 commit comments