diff --git a/README.md b/README.md index 315032f..a5e6100 100644 --- a/README.md +++ b/README.md @@ -8,7 +8,7 @@ Go bindings for [libgit2](http://libgit2.github.com/). The `master` branch follo Installing ---------- -This project wraps the functionality provided by libgit2. If you're using a stable version, install it to your system via your system's package manager and then install git2go as usual. +This project wraps the functionality provided by libgit2. If you're using a stable version, install it to your system via your system's package manaager and then install git2go as usual. Otherwise (`next` which tracks an unstable version), we need to build libgit2 as well. In order to build it, you need `cmake`, `pkg-config` and a C compiler. You will also need the development packages for OpenSSL and LibSSH2 installed if you want libgit2 to support HTTPS and SSH respectively. diff --git a/config.go b/config.go index 9d25e35..c4c4028 100644 --- a/config.go +++ b/config.go @@ -115,18 +115,20 @@ func (c *Config) LookupInt64(name string) (int64, error) { } func (c *Config) LookupString(name string) (string, error) { - var ptr *C.char cname := C.CString(name) defer C.free(unsafe.Pointer(cname)) + valBuf := C.git_buf{} + runtime.LockOSThread() defer runtime.UnlockOSThread() - if ret := C.git_config_get_string(&ptr, c.ptr, cname); ret < 0 { + if ret := C.git_config_get_string_buf(&valBuf, c.ptr, cname); ret < 0 { return "", MakeGitError(ret) } + defer C.git_buf_free(&valBuf) - return C.GoString(ptr), nil + return C.GoString(valBuf.ptr), nil } func (c *Config) LookupBool(name string) (bool, error) { diff --git a/config_test.go b/config_test.go new file mode 100644 index 0000000..8c2decc --- /dev/null +++ b/config_test.go @@ -0,0 +1,95 @@ +package git + +import ( + "os" + "testing" +) + +var tempConfig = "./temp.gitconfig" + +func setupConfig() (*Config, error) { + var ( + c *Config + err error + ) + + c, err = OpenOndisk(nil, tempConfig) + if err != nil { + return nil, err + } + + c.SetString("foo.bar", "baz") + c.SetBool("foo.bool", true) + c.SetInt32("foo.int32", 32) + c.SetInt64("foo.int64", 64) + + return c, err +} + +func cleanupConfig() { + os.Remove(tempConfig) +} + +type TestRunner func(*Config, *testing.T) + +var tests = []TestRunner{ + // LookupString + func(c *Config, t *testing.T) { + val, err := c.LookupString("foo.bar") + if err != nil { + t.Errorf("Got LookupString error: '%v', expected none\n", err) + } + if val != "baz" { + t.Errorf("Got '%s' from LookupString, expected 'bar'\n", val) + } + }, + // LookupBool + func(c *Config, t *testing.T) { + val, err := c.LookupBool("foo.bool") + if err != nil { + t.Errorf("Got LookupBool error: '%v', expected none\n", err) + } + if !val { + t.Errorf("Got %b from LookupBool, expected 'false'\n", val) + } + }, + // LookupInt32 + func(c *Config, t *testing.T) { + val, err := c.LookupInt32("foo.int32") + if err != nil { + t.Errorf("Got LookupInt32 error: '%v', expected none\n", err) + } + if val != 32 { + t.Errorf("Got %v, expected 32\n", val) + } + }, + // LookupInt64 + func(c *Config, t *testing.T) { + val, err := c.LookupInt64("foo.int64") + if err != nil { + t.Errorf("Got LookupInt64 error: '%v', expected none\n", err) + } + if val != 64 { + t.Errorf("Got %v, expected 64\n", val) + } + }, +} + +func TestConfigLookups(t *testing.T) { + var ( + err error + c *Config + ) + + c, err = setupConfig() + defer cleanupConfig() + + if err != nil { + t.Errorf("Setup error: '%v'. Expected none\n", err) + } + defer c.Free() + + for _, test := range tests { + test(c, t) + } +}