From fbd8698002c3eb398215f6dd6778a34edff5c2be Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Mart=C3=ADn=20Nieto?= Date: Wed, 11 Sep 2013 23:31:24 +0200 Subject: [PATCH 1/4] Add a few missing config setters and getters --- config.go | 89 ++++++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 85 insertions(+), 4 deletions(-) diff --git a/config.go b/config.go index 2aa073a..adaa069 100644 --- a/config.go +++ b/config.go @@ -14,7 +14,7 @@ type Config struct { ptr *C.git_config } -func (c *Config) LookupInt32(name string) (v int32, err error) { +func (c *Config) LookupInt32(name string) (int32, error) { var out C.int32_t cname := C.CString(name) defer C.free(unsafe.Pointer(cname)) @@ -30,7 +30,7 @@ func (c *Config) LookupInt32(name string) (v int32, err error) { return int32(out), nil } -func (c *Config) LookupInt64(name string) (v int64, err error) { +func (c *Config) LookupInt64(name string) (int64, error) { var out C.int64_t cname := C.CString(name) defer C.free(unsafe.Pointer(cname)) @@ -46,7 +46,7 @@ func (c *Config) LookupInt64(name string) (v int64, err error) { return int64(out), nil } -func (c *Config) LookupString(name string) (v string, err error) { +func (c *Config) LookupString(name string) (string, error) { var ptr *C.char cname := C.CString(name) defer C.free(unsafe.Pointer(cname)) @@ -62,7 +62,21 @@ func (c *Config) LookupString(name string) (v string, err error) { return C.GoString(ptr), nil } -func (c *Config) Set(name, value string) (err error) { + +func (c *Config) LookupBool(name string) (bool, error) { + var out C.int + cname := C.CString(name) + defer C.free(unsafe.Pointer(cname)) + + ret := C.git_config_get_bool(&out, c.ptr, cname) + if ret < 0 { + return false, LastError() + } + + return out != 0, nil +} + +func (c *Config) SetString(name, value string) (err error) { cname := C.CString(name) defer C.free(unsafe.Pointer(cname)) @@ -84,3 +98,70 @@ 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)) + + ret := C.git_config_set_int32(c.ptr, cname, C.int32_t(value)) + if ret < 0 { + return LastError() + } + + return nil +} + +func (c *Config) SetInt64(name string, value int64) (err error) { + cname := C.CString(name) + defer C.free(unsafe.Pointer(cname)) + + ret := C.git_config_set_int64(c.ptr, cname, C.int64_t(value)) + if ret < 0 { + return LastError() + } + + return nil +} + +func (c *Config) SetBool(name string, value bool) (err error) { + cname := C.CString(name) + defer C.free(unsafe.Pointer(cname)) + + ret := C.git_config_set_bool(c.ptr, cname, cbool(value)) + if ret < 0 { + return LastError() + } + + 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)) + + ret := C.git_config_set_multivar(c.ptr, cname, cregexp, cvalue) + if ret < 0 { + return LastError() + } + + return nil +} + +func (c *Config) Delete(name string) error { + cname := C.CString(name) + defer C.free(unsafe.Pointer(cname)) + + ret := C.git_config_delete_entry(c.ptr, cname) + + if ret < 0 { + return LastError() + } + + return nil +} -- 2.45.2 From 129105d410a9fe188d64bab5833d9c13f981101f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Mart=C3=ADn=20Nieto?= Date: Thu, 12 Sep 2013 01:01:52 +0200 Subject: [PATCH 2/4] Add a few more missing config functions --- config.go | 86 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 86 insertions(+) diff --git a/config.go b/config.go index adaa069..76d25c7 100644 --- a/config.go +++ b/config.go @@ -10,10 +10,61 @@ import ( "unsafe" ) +type ConfigLevel int + +const ( + // 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 Config struct { ptr *C.git_config } +// NewConfig creates a new empty configuration object +func NewConfig() (*Config, error) { + config := new(Config) + + ret := C.git_config_new(&config.ptr) + if ret < 0 { + return nil, LastError() + } + + 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)) + + ret := C.git_config_add_file_ondisk(c.ptr, cpath, C.git_config_level_t(level), cbool(force)) + if ret < 0 { + return LastError() + } + + return nil +} + func (c *Config) LookupInt32(name string) (int32, error) { var out C.int32_t cname := C.CString(name) @@ -165,3 +216,38 @@ func (c *Config) Delete(name string) error { 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) + ret := C.git_config_open_level(&config.ptr, parent.ptr, C.git_config_level_t(level)) + if ret < 0 { + return nil, LastError() + } + + return config, nil +} + +// OpenOndisk creates a new config instance containing a single on-disk file +func OpenOndisk(parent *Config, path string) (*Config, error) { + cpath := C.CString(path) + defer C.free(unsafe.Pointer(cpath)) + + config := new(Config) + ret := C.git_config_open_ondisk(&config.ptr, cpath) + if ret < 0 { + return nil, LastError() + } + + return config, nil +} + +// Refresh refreshes the configuration to reflect any changes made externally e.g. on disk +func (c *Config) Refresh() error { + ret := C.git_config_refresh(c.ptr) + if ret < 0 { + return LastError() + } + + return nil +} -- 2.45.2 From af2446b1da13a86df348e8f167a220a849619b54 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Mart=C3=ADn=20Nieto?= Date: Thu, 12 Sep 2013 01:39:35 +0200 Subject: [PATCH 3/4] Add iterators and ConfigEntry --- config.go | 84 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 84 insertions(+) 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(¢ry, 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)) +} + -- 2.45.2 From fc0a2f56e892d9175289c89c627de66edcb7f778 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Mart=C3=ADn=20Nieto?= Date: Sat, 21 Sep 2013 23:01:37 +0200 Subject: [PATCH 4/4] Lock the thread so we can get the error message --- config.go | 56 +++++++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 48 insertions(+), 8 deletions(-) diff --git a/config.go b/config.go index f1fafde..7665b20 100644 --- a/config.go +++ b/config.go @@ -57,8 +57,10 @@ type Config struct { func NewConfig() (*Config, error) { config := new(Config) - ret := C.git_config_new(&config.ptr) - if ret < 0 { + runtime.LockOSThread() + defer runtime.UnlockOSThread() + + if ret := C.git_config_new(&config.ptr); ret < 0 { return nil, LastError() } @@ -70,6 +72,10 @@ 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), cbool(force)) if ret < 0 { return LastError() @@ -118,8 +124,7 @@ func (c *Config) LookupString(name string) (string, error) { runtime.LockOSThread() defer runtime.UnlockOSThread() - ret := C.git_config_get_string(&ptr, c.ptr, cname) - if ret < 0 { + if ret := C.git_config_get_string(&ptr, c.ptr, cname); ret < 0 { return "", LastError() } @@ -132,6 +137,9 @@ func (c *Config) LookupBool(name string) (bool, error) { 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) if ret < 0 { return false, LastError() @@ -153,6 +161,10 @@ func (c *Config) NewMultivarIterator(name, regexp string) (*ConfigIterator, erro } iter := new(ConfigIterator) + + runtime.LockOSThread() + defer runtime.UnlockOSThread() + ret := C.git_config_multivar_iterator_new(&iter.ptr, c.ptr, cname, cregexp) if ret < 0 { return nil, LastError() @@ -166,6 +178,10 @@ func (c *Config) NewMultivarIterator(name, regexp string) (*ConfigIterator, erro // configuration func (c *Config) NewIterator() (*ConfigIterator, error) { iter := new(ConfigIterator) + + runtime.LockOSThread() + defer runtime.UnlockOSThread() + ret := C.git_config_iterator_new(&iter.ptr, c.ptr) if ret < 0 { return nil, LastError() @@ -181,6 +197,9 @@ func (c *Config) NewIteratorGlob(regexp string) (*ConfigIterator, error) { 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, LastError() @@ -228,6 +247,9 @@ 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)) if ret < 0 { return LastError() @@ -240,6 +262,9 @@ 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)) if ret < 0 { return LastError() @@ -258,6 +283,9 @@ func (c *Config) SetMultivar(name, regexp, value string) (err error) { 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) if ret < 0 { return LastError() @@ -270,6 +298,9 @@ 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) if ret < 0 { @@ -282,6 +313,10 @@ func (c *Config) Delete(name string) error { // 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)) if ret < 0 { return nil, LastError() @@ -296,8 +331,11 @@ func OpenOndisk(parent *Config, path string) (*Config, error) { defer C.free(unsafe.Pointer(cpath)) config := new(Config) - ret := C.git_config_open_ondisk(&config.ptr, cpath) - if ret < 0 { + + runtime.LockOSThread() + defer runtime.UnlockOSThread() + + if ret := C.git_config_open_ondisk(&config.ptr, cpath); ret < 0 { return nil, LastError() } @@ -306,8 +344,10 @@ func OpenOndisk(parent *Config, path string) (*Config, error) { // Refresh refreshes the configuration to reflect any changes made externally e.g. on disk func (c *Config) Refresh() error { - ret := C.git_config_refresh(c.ptr) - if ret < 0 { + runtime.LockOSThread() + defer runtime.UnlockOSThread() + + if ret := C.git_config_refresh(c.ptr); ret < 0 { return LastError() } -- 2.45.2