-
Notifications
You must be signed in to change notification settings - Fork 315
/
config.go
468 lines (353 loc) · 10 KB
/
config.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
package git
/*
#include <git2.h>
*/
import "C"
import (
"runtime"
"unsafe"
)
type ConfigLevel int
const (
// System-wide on Windows, for compatibility with portable git
ConfigLevelProgramdata ConfigLevel = C.GIT_CONFIG_LEVEL_PROGRAMDATA
// System-wide configuration file; /etc/gitconfig on Linux systems
ConfigLevelSystem ConfigLevel = C.GIT_CONFIG_LEVEL_SYSTEM
// XDG compatible configuration file; typically ~/.config/git/config
ConfigLevelXDG ConfigLevel = C.GIT_CONFIG_LEVEL_XDG
// User-specific configuration file (also called Global configuration
// file); typically ~/.gitconfig
ConfigLevelGlobal ConfigLevel = C.GIT_CONFIG_LEVEL_GLOBAL
// Repository specific configuration file; $WORK_DIR/.git/config on
// non-bare repos
ConfigLevelLocal ConfigLevel = C.GIT_CONFIG_LEVEL_LOCAL
// Application specific configuration file; freely defined by applications
ConfigLevelApp ConfigLevel = C.GIT_CONFIG_LEVEL_APP
// Represents the highest level available config file (i.e. the most
// specific config file available that actually is loaded)
ConfigLevelHighest ConfigLevel = C.GIT_CONFIG_HIGHEST_LEVEL
)
type ConfigEntry struct {
Name string
Value string
Level ConfigLevel
}
func newConfigEntryFromC(centry *C.git_config_entry) *ConfigEntry {
return &ConfigEntry{
Name: C.GoString(centry.name),
Value: C.GoString(centry.value),
Level: ConfigLevel(centry.level),
}
}
type Config struct {
doNotCompare
ptr *C.git_config
}
// NewConfig creates a new empty configuration object
func NewConfig() (*Config, error) {
config := new(Config)
runtime.LockOSThread()
defer runtime.UnlockOSThread()
if ret := C.git_config_new(&config.ptr); ret < 0 {
return nil, MakeGitError(ret)
}
return config, nil
}
// AddFile adds a file-backed backend to the config object at the specified level.
func (c *Config) AddFile(path string, level ConfigLevel, force bool) error {
cpath := C.CString(path)
defer C.free(unsafe.Pointer(cpath))
runtime.LockOSThread()
defer runtime.UnlockOSThread()
ret := C.git_config_add_file_ondisk(c.ptr, cpath, C.git_config_level_t(level), nil, cbool(force))
runtime.KeepAlive(c)
if ret < 0 {
return MakeGitError(ret)
}
return nil
}
func (c *Config) LookupInt32(name string) (int32, error) {
var out C.int32_t
cname := C.CString(name)
defer C.free(unsafe.Pointer(cname))
runtime.LockOSThread()
defer runtime.UnlockOSThread()
ret := C.git_config_get_int32(&out, c.ptr, cname)
runtime.KeepAlive(c)
if ret < 0 {
return 0, MakeGitError(ret)
}
return int32(out), nil
}
func (c *Config) LookupInt64(name string) (int64, error) {
var out C.int64_t
cname := C.CString(name)
defer C.free(unsafe.Pointer(cname))
runtime.LockOSThread()
defer runtime.UnlockOSThread()
ret := C.git_config_get_int64(&out, c.ptr, cname)
runtime.KeepAlive(c)
if ret < 0 {
return 0, MakeGitError(ret)
}
return int64(out), nil
}
func (c *Config) LookupString(name string) (string, error) {
cname := C.CString(name)
defer C.free(unsafe.Pointer(cname))
valBuf := C.git_buf{}
runtime.LockOSThread()
defer runtime.UnlockOSThread()
ret := C.git_config_get_string_buf(&valBuf, c.ptr, cname)
runtime.KeepAlive(c)
if ret < 0 {
return "", MakeGitError(ret)
}
defer C.git_buf_dispose(&valBuf)
return C.GoString(valBuf.ptr), nil
}
func (c *Config) LookupBool(name string) (bool, error) {
var out C.int
cname := C.CString(name)
defer C.free(unsafe.Pointer(cname))
runtime.LockOSThread()
defer runtime.UnlockOSThread()
ret := C.git_config_get_bool(&out, c.ptr, cname)
runtime.KeepAlive(c)
if ret < 0 {
return false, MakeGitError(ret)
}
return out != 0, nil
}
func (c *Config) NewMultivarIterator(name, regexp string) (*ConfigIterator, error) {
cname := C.CString(name)
defer C.free(unsafe.Pointer(cname))
var cregexp *C.char
if regexp == "" {
cregexp = nil
} else {
cregexp = C.CString(regexp)
defer C.free(unsafe.Pointer(cregexp))
}
iter := &ConfigIterator{cfg: c}
runtime.LockOSThread()
defer runtime.UnlockOSThread()
ret := C.git_config_multivar_iterator_new(&iter.ptr, c.ptr, cname, cregexp)
if ret < 0 {
return nil, MakeGitError(ret)
}
runtime.SetFinalizer(iter, (*ConfigIterator).Free)
return iter, nil
}
// NewIterator creates an iterator over each entry in the
// configuration
func (c *Config) NewIterator() (*ConfigIterator, error) {
iter := &ConfigIterator{cfg: c}
runtime.LockOSThread()
defer runtime.UnlockOSThread()
ret := C.git_config_iterator_new(&iter.ptr, c.ptr)
if ret < 0 {
return nil, MakeGitError(ret)
}
return iter, nil
}
// NewIteratorGlob creates an iterator over each entry in the
// configuration whose name matches the given regular expression
func (c *Config) NewIteratorGlob(regexp string) (*ConfigIterator, error) {
iter := &ConfigIterator{cfg: c}
cregexp := C.CString(regexp)
defer C.free(unsafe.Pointer(cregexp))
runtime.LockOSThread()
defer runtime.UnlockOSThread()
ret := C.git_config_iterator_glob_new(&iter.ptr, c.ptr, cregexp)
if ret < 0 {
return nil, MakeGitError(ret)
}
return iter, nil
}
func (c *Config) SetString(name, value string) (err error) {
cname := C.CString(name)
defer C.free(unsafe.Pointer(cname))
cvalue := C.CString(value)
defer C.free(unsafe.Pointer(cvalue))
runtime.LockOSThread()
defer runtime.UnlockOSThread()
ret := C.git_config_set_string(c.ptr, cname, cvalue)
runtime.KeepAlive(c)
if ret < 0 {
return MakeGitError(ret)
}
return nil
}
func (c *Config) Free() {
runtime.SetFinalizer(c, nil)
C.git_config_free(c.ptr)
}
func (c *Config) SetInt32(name string, value int32) (err error) {
cname := C.CString(name)
defer C.free(unsafe.Pointer(cname))
runtime.LockOSThread()
defer runtime.UnlockOSThread()
ret := C.git_config_set_int32(c.ptr, cname, C.int32_t(value))
runtime.KeepAlive(c)
if ret < 0 {
return MakeGitError(ret)
}
return nil
}
func (c *Config) SetInt64(name string, value int64) (err error) {
cname := C.CString(name)
defer C.free(unsafe.Pointer(cname))
runtime.LockOSThread()
defer runtime.UnlockOSThread()
ret := C.git_config_set_int64(c.ptr, cname, C.int64_t(value))
runtime.KeepAlive(c)
if ret < 0 {
return MakeGitError(ret)
}
return nil
}
func (c *Config) SetBool(name string, value bool) (err error) {
cname := C.CString(name)
defer C.free(unsafe.Pointer(cname))
runtime.LockOSThread()
defer runtime.UnlockOSThread()
ret := C.git_config_set_bool(c.ptr, cname, cbool(value))
runtime.KeepAlive(c)
if ret < 0 {
return MakeGitError(ret)
}
return nil
}
func (c *Config) SetMultivar(name, regexp, value string) (err error) {
cname := C.CString(name)
defer C.free(unsafe.Pointer(cname))
cregexp := C.CString(regexp)
defer C.free(unsafe.Pointer(cregexp))
cvalue := C.CString(value)
defer C.free(unsafe.Pointer(cvalue))
runtime.LockOSThread()
defer runtime.UnlockOSThread()
ret := C.git_config_set_multivar(c.ptr, cname, cregexp, cvalue)
runtime.KeepAlive(c)
if ret < 0 {
return MakeGitError(ret)
}
return nil
}
func (c *Config) Delete(name string) error {
cname := C.CString(name)
defer C.free(unsafe.Pointer(cname))
runtime.LockOSThread()
defer runtime.UnlockOSThread()
ret := C.git_config_delete_entry(c.ptr, cname)
runtime.KeepAlive(c)
if ret < 0 {
return MakeGitError(ret)
}
return nil
}
// OpenLevel creates a single-level focused config object from a multi-level one
func (c *Config) OpenLevel(parent *Config, level ConfigLevel) (*Config, error) {
config := new(Config)
runtime.LockOSThread()
defer runtime.UnlockOSThread()
ret := C.git_config_open_level(&config.ptr, parent.ptr, C.git_config_level_t(level))
runtime.KeepAlive(c)
runtime.KeepAlive(parent)
if ret < 0 {
return nil, MakeGitError(ret)
}
return config, nil
}
// OpenOndisk creates a new config instance containing a single on-disk file
func OpenOndisk(path string) (*Config, error) {
cpath := C.CString(path)
defer C.free(unsafe.Pointer(cpath))
config := new(Config)
runtime.LockOSThread()
defer runtime.UnlockOSThread()
if ret := C.git_config_open_ondisk(&config.ptr, cpath); ret < 0 {
return nil, MakeGitError(ret)
}
return config, nil
}
type ConfigIterator struct {
doNotCompare
ptr *C.git_config_iterator
cfg *Config
}
// Next returns the next entry for this iterator
func (iter *ConfigIterator) Next() (*ConfigEntry, error) {
var centry *C.git_config_entry
runtime.LockOSThread()
defer runtime.UnlockOSThread()
ret := C.git_config_next(¢ry, iter.ptr)
if ret < 0 {
return nil, MakeGitError(ret)
}
entry := newConfigEntryFromC(centry)
runtime.KeepAlive(iter)
return entry, nil
}
func (iter *ConfigIterator) Free() {
runtime.SetFinalizer(iter, nil)
C.free(unsafe.Pointer(iter.ptr))
}
func ConfigFindGlobal() (string, error) {
var buf C.git_buf
defer C.git_buf_dispose(&buf)
runtime.LockOSThread()
defer runtime.UnlockOSThread()
ret := C.git_config_find_global(&buf)
if ret < 0 {
return "", MakeGitError(ret)
}
return C.GoString(buf.ptr), nil
}
func ConfigFindSystem() (string, error) {
var buf C.git_buf
defer C.git_buf_dispose(&buf)
runtime.LockOSThread()
defer runtime.UnlockOSThread()
ret := C.git_config_find_system(&buf)
if ret < 0 {
return "", MakeGitError(ret)
}
return C.GoString(buf.ptr), nil
}
func ConfigFindXDG() (string, error) {
var buf C.git_buf
defer C.git_buf_dispose(&buf)
runtime.LockOSThread()
defer runtime.UnlockOSThread()
ret := C.git_config_find_xdg(&buf)
if ret < 0 {
return "", MakeGitError(ret)
}
return C.GoString(buf.ptr), nil
}
// ConfigFindProgramdata locate the path to the configuration file in ProgramData.
//
// Look for the file in %PROGRAMDATA%\Git\config used by portable git.
func ConfigFindProgramdata() (string, error) {
var buf C.git_buf
defer C.git_buf_dispose(&buf)
runtime.LockOSThread()
defer runtime.UnlockOSThread()
ret := C.git_config_find_programdata(&buf)
if ret < 0 {
return "", MakeGitError(ret)
}
return C.GoString(buf.ptr), nil
}
// OpenDefault opens the default config according to git rules
func OpenDefault() (*Config, error) {
runtime.LockOSThread()
defer runtime.UnlockOSThread()
config := new(Config)
if ret := C.git_config_open_default(&config.ptr); ret < 0 {
return nil, MakeGitError(ret)
}
return config, nil
}