summaryrefslogtreecommitdiff
path: root/config.go
diff options
context:
space:
mode:
authorCarlos Martín Nieto <[email protected]>2013-09-12 01:39:35 +0200
committerCarlos Martín Nieto <[email protected]>2014-02-26 10:37:01 +0100
commitaf2446b1da13a86df348e8f167a220a849619b54 (patch)
tree7958fd7224d1fe4bd6e6fdeb062fd44ae46c024e /config.go
parent129105d410a9fe188d64bab5833d9c13f981101f (diff)
Add iterators and ConfigEntry
Diffstat (limited to 'config.go')
-rw-r--r--config.go84
1 files changed, 84 insertions, 0 deletions
diff --git a/config.go b/config.go
index 76d25c7..f1fafde 100644
--- a/config.go
+++ b/config.go
@@ -35,6 +35,19 @@ const (
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 {
ptr *C.git_config
@@ -127,6 +140,55 @@ func (c *Config) LookupBool(name string) (bool, error) {
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 := new(ConfigIterator)
+ ret := C.git_config_multivar_iterator_new(&iter.ptr, c.ptr, cname, cregexp)
+ if ret < 0 {
+ return nil, LastError()
+ }
+
+ 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 := new(ConfigIterator)
+ ret := C.git_config_iterator_new(&iter.ptr, c.ptr)
+ if ret < 0 {
+ return nil, LastError()
+ }
+
+ 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 := new(ConfigIterator)
+ cregexp := C.CString(regexp)
+ defer C.free(unsafe.Pointer(cregexp))
+
+ ret := C.git_config_iterator_glob_new(&iter.ptr, c.ptr, cregexp)
+ if ret < 0 {
+ return nil, LastError()
+ }
+
+ return iter, nil
+}
+
func (c *Config) SetString(name, value string) (err error) {
cname := C.CString(name)
defer C.free(unsafe.Pointer(cname))
@@ -251,3 +313,25 @@ func (c *Config) Refresh() error {
return nil
}
+
+type ConfigIterator struct {
+ ptr *C.git_config_iterator
+}
+
+// Next returns the next entry for this iterator
+func (iter *ConfigIterator) Next() (*ConfigEntry, error) {
+ var centry *C.git_config_entry
+
+ ret := C.git_config_next(&centry, iter.ptr)
+ if ret < 0 {
+ return nil, LastError()
+ }
+
+ return newConfigEntryFromC(centry), nil
+}
+
+func (iter *ConfigIterator) Free() {
+ runtime.SetFinalizer(iter, nil)
+ C.free(unsafe.Pointer(iter.ptr))
+}
+