From 179b69ce21fd1742c4f51e4ae0f2c1ef6929019e Mon Sep 17 00:00:00 2001 From: Axel Wagner Date: Wed, 22 May 2013 14:41:42 +0200 Subject: [PATCH 01/10] Support for index-entries --- index.go | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/index.go b/index.go index 72b1d5b..90a49de 100644 --- a/index.go +++ b/index.go @@ -7,7 +7,9 @@ package git */ import "C" import ( + "fmt" "runtime" + "time" "unsafe" ) @@ -15,6 +17,18 @@ type Index struct { ptr *C.git_index } +type IndexEntry struct { + ptr *C.git_index_entry + Ctime time.Time + Mtime time.Time + Mode uint + Uid uint + Gid uint + Size uint + Oid *Oid + Path string +} + func newIndexFromC(ptr *C.git_index) *Index { idx := &Index{ptr} runtime.SetFinalizer(idx, (*Index).Free) @@ -47,3 +61,29 @@ func (v *Index) Free() { runtime.SetFinalizer(v, nil) C.git_index_free(v.ptr) } + +func (v *Index) EntryCount() uint { + return uint(C.git_index_entrycount(v.ptr)) +} + +func newIndexEntryFromC(entry *C.git_index_entry) *IndexEntry { + return &IndexEntry{ + entry, + time.Unix(int64(entry.ctime.seconds), int64(entry.ctime.nanoseconds)), + time.Unix(int64(entry.mtime.seconds), int64(entry.mtime.nanoseconds)), + uint(entry.mode), + uint(entry.uid), + uint(entry.gid), + uint(entry.file_size), + newOidFromC(&entry.oid), + C.GoString(entry.path), + } +} + +func (v *Index) EntryByIndex(index uint) (*IndexEntry, error) { + centry := C.git_index_get_byindex(v.ptr, C.size_t(index)) + if centry == nil { + return nil, fmt.Errorf("Index out of Bounds") + } + return newIndexEntryFromC(centry), nil +} 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 02/10] 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 +} 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 03/10] 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 +} 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 04/10] 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)) +} + 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 05/10] 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() } From 1e01cae286652885432c3d65d0693363e1b4a05c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Mart=C3=ADn=20Nieto?= Date: Wed, 26 Feb 2014 15:18:47 +0100 Subject: [PATCH 06/10] Remove pointer to git_index_entry We have all the data --- index.go | 2 -- 1 file changed, 2 deletions(-) diff --git a/index.go b/index.go index 90a49de..2bf3d04 100644 --- a/index.go +++ b/index.go @@ -18,7 +18,6 @@ type Index struct { } type IndexEntry struct { - ptr *C.git_index_entry Ctime time.Time Mtime time.Time Mode uint @@ -68,7 +67,6 @@ func (v *Index) EntryCount() uint { func newIndexEntryFromC(entry *C.git_index_entry) *IndexEntry { return &IndexEntry{ - entry, time.Unix(int64(entry.ctime.seconds), int64(entry.ctime.nanoseconds)), time.Unix(int64(entry.mtime.seconds), int64(entry.mtime.nanoseconds)), uint(entry.mode), From 14f902afed482faefc58aa3af005cb8f2a0b050d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Mart=C3=ADn=20Nieto?= Date: Wed, 26 Feb 2014 15:22:48 +0100 Subject: [PATCH 07/10] Adjust to oid -> id --- index.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/index.go b/index.go index 2bf3d04..3ebb605 100644 --- a/index.go +++ b/index.go @@ -24,7 +24,7 @@ type IndexEntry struct { Uid uint Gid uint Size uint - Oid *Oid + Id *Oid Path string } @@ -73,7 +73,7 @@ func newIndexEntryFromC(entry *C.git_index_entry) *IndexEntry { uint(entry.uid), uint(entry.gid), uint(entry.file_size), - newOidFromC(&entry.oid), + newOidFromC(&entry.id), C.GoString(entry.path), } } From 3e5586bd8d532c929aecf778fc094e4f86588d37 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Mart=C3=ADn=20Nieto?= Date: Wed, 26 Feb 2014 15:30:16 +0100 Subject: [PATCH 08/10] Remove 'oid' as id name Following the cleanup from libgit2, let's not use 'oid' unless we mean the name of the data type. In the other cases, we mean an identifier, hence the name 'id'. --- repository.go | 24 ++++++++++++------------ walk.go | 4 ++-- 2 files changed, 14 insertions(+), 14 deletions(-) diff --git a/repository.go b/repository.go index 48c2b46..1d58d7e 100644 --- a/repository.go +++ b/repository.go @@ -85,13 +85,13 @@ func (v *Repository) Index() (*Index, error) { return newIndexFromC(ptr), nil } -func (v *Repository) lookupType(oid *Oid, t ObjectType) (Object, error) { +func (v *Repository) lookupType(id *Oid, t ObjectType) (Object, error) { var ptr *C.git_object runtime.LockOSThread() defer runtime.UnlockOSThread() - ret := C.git_object_lookup(&ptr, v.ptr, oid.toC(), C.git_otype(t)) + ret := C.git_object_lookup(&ptr, v.ptr, id.toC(), C.git_otype(t)) if ret < 0 { return nil, LastError() } @@ -99,12 +99,12 @@ func (v *Repository) lookupType(oid *Oid, t ObjectType) (Object, error) { return allocObject(ptr), nil } -func (v *Repository) Lookup(oid *Oid) (Object, error) { - return v.lookupType(oid, ObjectAny) +func (v *Repository) Lookup(id *Oid) (Object, error) { + return v.lookupType(id, ObjectAny) } -func (v *Repository) LookupTree(oid *Oid) (*Tree, error) { - obj, err := v.lookupType(oid, ObjectTree) +func (v *Repository) LookupTree(id *Oid) (*Tree, error) { + obj, err := v.lookupType(id, ObjectTree) if err != nil { return nil, err } @@ -112,8 +112,8 @@ func (v *Repository) LookupTree(oid *Oid) (*Tree, error) { return obj.(*Tree), nil } -func (v *Repository) LookupCommit(oid *Oid) (*Commit, error) { - obj, err := v.lookupType(oid, ObjectCommit) +func (v *Repository) LookupCommit(id *Oid) (*Commit, error) { + obj, err := v.lookupType(id, ObjectCommit) if err != nil { return nil, err } @@ -121,8 +121,8 @@ func (v *Repository) LookupCommit(oid *Oid) (*Commit, error) { return obj.(*Commit), nil } -func (v *Repository) LookupBlob(oid *Oid) (*Blob, error) { - obj, err := v.lookupType(oid, ObjectBlob) +func (v *Repository) LookupBlob(id *Oid) (*Blob, error) { + obj, err := v.lookupType(id, ObjectBlob) if err != nil { return nil, err } @@ -146,7 +146,7 @@ func (v *Repository) LookupReference(name string) (*Reference, error) { return newReferenceFromC(ptr), nil } -func (v *Repository) CreateReference(name string, oid *Oid, force bool, sig *Signature, msg string) (*Reference, error) { +func (v *Repository) CreateReference(name string, id *Oid, force bool, sig *Signature, msg string) (*Reference, error) { cname := C.CString(name) defer C.free(unsafe.Pointer(cname)) @@ -161,7 +161,7 @@ func (v *Repository) CreateReference(name string, oid *Oid, force bool, sig *Sig runtime.LockOSThread() defer runtime.UnlockOSThread() - ecode := C.git_reference_create(&ptr, v.ptr, cname, oid.toC(), cbool(force), csig, cmsg) + ecode := C.git_reference_create(&ptr, v.ptr, cname, id.toC(), cbool(force), csig, cmsg) if ecode < 0 { return nil, LastError() } diff --git a/walk.go b/walk.go index 6979b6b..619188d 100644 --- a/walk.go +++ b/walk.go @@ -46,11 +46,11 @@ func (v *RevWalk) PushHead() (err error) { return } -func (v *RevWalk) Next(oid *Oid) (err error) { +func (v *RevWalk) Next(id *Oid) (err error) { runtime.LockOSThread() defer runtime.UnlockOSThread() - ret := C.git_revwalk_next(oid.toC(), v.ptr) + ret := C.git_revwalk_next(id.toC(), v.ptr) switch { case ret == ITEROVER: err = io.EOF From 499f52a3549503604f30663211361e2fbd3cf202 Mon Sep 17 00:00:00 2001 From: Jesper Hansen Date: Sun, 7 Jul 2013 16:43:44 +0200 Subject: [PATCH 09/10] Added git error code to the error object. --- checkout.go | 4 ++-- commit.go | 2 +- config.go | 8 ++++---- git.go | 27 ++++++++++++++++++--------- index.go | 4 ++-- odb.go | 4 ++-- packbuilder.go | 10 +++++----- reference.go | 16 ++++++++-------- repository.go | 31 ++++++++++++++++--------------- submodule.go | 26 +++++++++++++------------- tree.go | 6 +++--- walk.go | 4 ++-- 12 files changed, 76 insertions(+), 66 deletions(-) diff --git a/checkout.go b/checkout.go index f4c1d4e..d3cd47b 100644 --- a/checkout.go +++ b/checkout.go @@ -65,7 +65,7 @@ func (v *Repository) Checkout(opts *CheckoutOpts) error { ret := C.git_checkout_head(v.ptr, &copts) if ret < 0 { - return LastError() + return MakeGitError(ret) } return nil @@ -81,7 +81,7 @@ func (v *Repository) CheckoutIndex(index *Index, opts *CheckoutOpts) error { ret := C.git_checkout_index(v.ptr, index.ptr, &copts) if ret < 0 { - return LastError() + return MakeGitError(ret) } return nil diff --git a/commit.go b/commit.go index 0c64c76..0edebb6 100644 --- a/commit.go +++ b/commit.go @@ -31,7 +31,7 @@ func (c Commit) Tree() (*Tree, error) { err := C.git_commit_tree(&ptr, c.ptr) if err < 0 { - return nil, LastError() + return nil, MakeGitError(err) } return allocObject(ptr).(*Tree), nil diff --git a/config.go b/config.go index 7665b20..2234a43 100644 --- a/config.go +++ b/config.go @@ -94,7 +94,7 @@ func (c *Config) LookupInt32(name string) (int32, error) { ret := C.git_config_get_int32(&out, c.ptr, cname) if ret < 0 { - return 0, LastError() + return 0, MakeGitError(ret) } return int32(out), nil @@ -110,7 +110,7 @@ func (c *Config) LookupInt64(name string) (int64, error) { ret := C.git_config_get_int64(&out, c.ptr, cname) if ret < 0 { - return 0, LastError() + return 0, MakeGitError(ret) } return int64(out), nil @@ -125,7 +125,7 @@ func (c *Config) LookupString(name string) (string, error) { defer runtime.UnlockOSThread() if ret := C.git_config_get_string(&ptr, c.ptr, cname); ret < 0 { - return "", LastError() + return "", MakeGitError(ret) } return C.GoString(ptr), nil @@ -220,7 +220,7 @@ func (c *Config) SetString(name, value string) (err error) { ret := C.git_config_set_string(c.ptr, cname, cvalue) if ret < 0 { - return LastError() + return MakeGitError(ret) } return nil diff --git a/git.go b/git.go index 72ea33e..7c76c30 100644 --- a/git.go +++ b/git.go @@ -61,8 +61,8 @@ func NewOidFromString(s string) (*Oid, error) { runtime.LockOSThread() defer runtime.UnlockOSThread() - if C.git_oid_fromstr(o.toC(), cs) < 0 { - return nil, LastError() + if ret := C.git_oid_fromstr(o.toC(), cs); ret < 0 { + return nil, MakeGitError(ret) } return o, nil @@ -123,27 +123,36 @@ func ShortenOids(ids []*Oid, minlen int) (int, error) { buf[40] = 0 ret = C.git_oid_shorten_add(shorten, (*C.char)(unsafe.Pointer(&buf[0]))) if ret < 0 { - return int(ret), LastError() + return int(ret), MakeGitError(ret) } } return int(ret), nil } type GitError struct { - Message string - Code int + Message string + Class int + ErrorCode int } func (e GitError) Error() string { return e.Message } -func LastError() error { +func IsNotExist(err error) bool { + return err.(*GitError).ErrorCode == C.GIT_ENOTFOUND +} + +func IsExist(err error) bool { + return err.(*GitError).ErrorCode == C.GIT_EEXISTS +} + +func MakeGitError(errorCode C.int) error { err := C.giterr_last() if err == nil { - return &GitError{"No message", 0} + return &GitError{"No message", C.GITERR_INVALID, C.GIT_ERROR} } - return &GitError{C.GoString(err.message), int(err.klass)} + return &GitError{C.GoString(err.message), int(err.klass), int(errorCode)} } func cbool(b bool) C.int { @@ -175,7 +184,7 @@ func Discover(start string, across_fs bool, ceiling_dirs []string) (string, erro ret := C.git_repository_discover(&buf, cstart, cbool(across_fs), ceildirs) if ret < 0 { - return "", LastError() + return "", MakeGitError(ret) } return C.GoString(buf.ptr), nil diff --git a/index.go b/index.go index bc11025..cbb28c4 100644 --- a/index.go +++ b/index.go @@ -42,7 +42,7 @@ func (v *Index) AddByPath(path string) error { ret := C.git_index_add_bypath(v.ptr, cstr) if ret < 0 { - return LastError() + return MakeGitError(ret) } return nil @@ -56,7 +56,7 @@ func (v *Index) WriteTree() (*Oid, error) { ret := C.git_index_write_tree(oid.toC(), v.ptr) if ret < 0 { - return nil, LastError() + return nil, MakeGitError(ret) } return oid, nil diff --git a/odb.go b/odb.go index 953791c..3daf3a4 100644 --- a/odb.go +++ b/odb.go @@ -32,7 +32,7 @@ func (v *Odb) Write(data []byte, otype ObjectType) (oid *Oid, err error) { ret := C.git_odb_write(oid.toC(), v.ptr, unsafe.Pointer(hdr.Data), C.size_t(hdr.Len), C.git_otype(otype)) if ret < 0 { - err = LastError() + err = MakeGitError(ret) } return @@ -46,7 +46,7 @@ func (v *Odb) Read(oid *Oid) (obj *OdbObject, err error) { ret := C.git_odb_read(&obj.ptr, v.ptr, oid.toC()) if ret < 0 { - return nil, LastError() + return nil, MakeGitError(ret) } runtime.SetFinalizer(obj, (*OdbObject).Free) diff --git a/packbuilder.go b/packbuilder.go index 333f183..70c4530 100644 --- a/packbuilder.go +++ b/packbuilder.go @@ -28,7 +28,7 @@ func (repo *Repository) NewPackbuilder() (*Packbuilder, error) { ret := C.git_packbuilder_new(&builder.ptr, repo.ptr) if ret != 0 { - return nil, LastError() + return nil, MakeGitError(ret) } runtime.SetFinalizer(builder, (*Packbuilder).Free) return builder, nil @@ -48,7 +48,7 @@ func (pb *Packbuilder) Insert(id *Oid, name string) error { ret := C.git_packbuilder_insert(pb.ptr, id.toC(), cname) if ret != 0 { - return LastError() + return MakeGitError(ret) } return nil } @@ -59,7 +59,7 @@ func (pb *Packbuilder) InsertCommit(id *Oid) error { ret := C.git_packbuilder_insert_commit(pb.ptr, id.toC()) if ret != 0 { - return LastError() + return MakeGitError(ret) } return nil } @@ -70,7 +70,7 @@ func (pb *Packbuilder) InsertTree(id *Oid) error { ret := C.git_packbuilder_insert_tree(pb.ptr, id.toC()) if ret != 0 { - return LastError() + return MakeGitError(ret) } return nil } @@ -88,7 +88,7 @@ func (pb *Packbuilder) WriteToFile(name string, mode os.FileMode) error { ret := C.git_packbuilder_write(pb.ptr, cname, C.uint(mode.Perm()), nil, nil) if ret != 0 { - return LastError() + return MakeGitError(ret) } return nil } diff --git a/reference.go b/reference.go index a2f1636..39d328e 100644 --- a/reference.go +++ b/reference.go @@ -45,7 +45,7 @@ func (v *Reference) SetSymbolicTarget(target string, sig *Signature, msg string) ret := C.git_reference_symbolic_set_target(&ptr, v.ptr, ctarget, csig, cmsg) if ret < 0 { - return nil, LastError() + return nil, MakeGitError(ret) } return newReferenceFromC(ptr), nil @@ -65,7 +65,7 @@ func (v *Reference) SetTarget(target *Oid, sig *Signature, msg string) (*Referen ret := C.git_reference_set_target(&ptr, v.ptr, target.toC(), csig, cmsg) if ret < 0 { - return nil, LastError() + return nil, MakeGitError(ret) } return newReferenceFromC(ptr), nil @@ -79,7 +79,7 @@ func (v *Reference) Resolve() (*Reference, error) { ret := C.git_reference_resolve(&ptr, v.ptr) if ret < 0 { - return nil, LastError() + return nil, MakeGitError(ret) } return newReferenceFromC(ptr), nil @@ -102,7 +102,7 @@ func (v *Reference) Rename(name string, force bool, sig *Signature, msg string) ret := C.git_reference_rename(&ptr, v.ptr, cname, cbool(force), csig, cmsg) if ret < 0 { - return nil, LastError() + return nil, MakeGitError(ret) } return newReferenceFromC(ptr), nil @@ -128,7 +128,7 @@ func (v *Reference) Delete() error { ret := C.git_reference_delete(v.ptr) if ret < 0 { - return LastError() + return MakeGitError(ret) } return nil @@ -173,7 +173,7 @@ func (repo *Repository) NewReferenceIterator() (*ReferenceIterator, error) { ret := C.git_reference_iterator_new(&ptr, repo.ptr) if ret < 0 { - return nil, LastError() + return nil, MakeGitError(ret) } iter := &ReferenceIterator{repo: repo, ptr: ptr} @@ -194,7 +194,7 @@ func (repo *Repository) NewReferenceIteratorGlob(glob string) (*ReferenceIterato ret := C.git_reference_iterator_glob_new(&ptr, repo.ptr, cstr) if ret < 0 { - return nil, LastError() + return nil, MakeGitError(ret) } iter := &ReferenceIterator{repo: repo, ptr: ptr} @@ -215,7 +215,7 @@ func (v *ReferenceIterator) NextName() (string, error) { return "", ErrIterOver } if ret < 0 { - return "", LastError() + return "", MakeGitError(ret) } return C.GoString(ptr), nil diff --git a/repository.go b/repository.go index 1d58d7e..e78422e 100644 --- a/repository.go +++ b/repository.go @@ -26,7 +26,7 @@ func OpenRepository(path string) (*Repository, error) { ret := C.git_repository_open(&repo.ptr, cpath) if ret < 0 { - return nil, LastError() + return nil, MakeGitError(ret) } runtime.SetFinalizer(repo, (*Repository).Free) @@ -44,7 +44,7 @@ func InitRepository(path string, isbare bool) (*Repository, error) { ret := C.git_repository_init(&repo.ptr, cpath, ucbool(isbare)) if ret < 0 { - return nil, LastError() + return nil, MakeGitError(ret) } runtime.SetFinalizer(repo, (*Repository).Free) @@ -64,7 +64,7 @@ func (v *Repository) Config() (*Config, error) { ret := C.git_repository_config(&config.ptr, v.ptr) if ret < 0 { - return nil, LastError() + return nil, MakeGitError(ret) } runtime.SetFinalizer(config, (*Config).Free) @@ -79,7 +79,7 @@ func (v *Repository) Index() (*Index, error) { ret := C.git_repository_index(&ptr, v.ptr) if ret < 0 { - return nil, LastError() + return nil, MakeGitError(ret) } return newIndexFromC(ptr), nil @@ -93,7 +93,7 @@ func (v *Repository) lookupType(id *Oid, t ObjectType) (Object, error) { ret := C.git_object_lookup(&ptr, v.ptr, id.toC(), C.git_otype(t)) if ret < 0 { - return nil, LastError() + return nil, MakeGitError(ret) } return allocObject(ptr), nil @@ -140,7 +140,7 @@ func (v *Repository) LookupReference(name string) (*Reference, error) { ecode := C.git_reference_lookup(&ptr, v.ptr, cname) if ecode < 0 { - return nil, LastError() + return nil, MakeGitError(ecode) } return newReferenceFromC(ptr), nil @@ -163,7 +163,7 @@ func (v *Repository) CreateReference(name string, id *Oid, force bool, sig *Sign ecode := C.git_reference_create(&ptr, v.ptr, cname, id.toC(), cbool(force), csig, cmsg) if ecode < 0 { - return nil, LastError() + return nil, MakeGitError(ecode) } return newReferenceFromC(ptr), nil @@ -189,7 +189,7 @@ func (v *Repository) CreateSymbolicReference(name, target string, force bool, si ecode := C.git_reference_symbolic_create(&ptr, v.ptr, cname, ctarget, cbool(force), csig, cmsg) if ecode < 0 { - return nil, LastError() + return nil, MakeGitError(ecode) } return newReferenceFromC(ptr), nil @@ -203,7 +203,7 @@ func (v *Repository) Walk() (*RevWalk, error) { ecode := C.git_revwalk_new(&walk.ptr, v.ptr) if ecode < 0 { - return nil, LastError() + return nil, MakeGitError(ecode) } walk.repo = v @@ -250,7 +250,7 @@ func (v *Repository) CreateCommit( nil, cmsg, tree.ptr, C.size_t(nparents), parentsarg) if ret < 0 { - return nil, LastError() + return nil, MakeGitError(ret) } return oid, nil @@ -268,7 +268,7 @@ func (v *Repository) Odb() (odb *Odb, err error) { defer runtime.UnlockOSThread() if ret := C.git_repository_odb(&odb.ptr, v.ptr); ret < 0 { - return nil, LastError() + return nil, MakeGitError(ret) } runtime.SetFinalizer(odb, (*Odb).Free) @@ -294,9 +294,10 @@ func (repo *Repository) SetWorkdir(workdir string, updateGitlink bool) error { runtime.LockOSThread() defer runtime.UnlockOSThread() - if C.git_repository_set_workdir(repo.ptr, cstr, cbool(updateGitlink)) < 0 { - return LastError() + if ret := C.git_repository_set_workdir(repo.ptr, cstr, cbool(updateGitlink)); ret < 0 { + return MakeGitError(ret) } + return nil } @@ -307,7 +308,7 @@ func (v *Repository) TreeBuilder() (*TreeBuilder, error) { defer runtime.UnlockOSThread() if ret := C.git_treebuilder_create(&bld.ptr, nil); ret < 0 { - return nil, LastError() + return nil, MakeGitError(ret) } runtime.SetFinalizer(bld, (*TreeBuilder).Free) @@ -326,7 +327,7 @@ func (v *Repository) RevparseSingle(spec string) (Object, error) { ecode := C.git_revparse_single(&ptr, v.ptr, cspec) if ecode < 0 { - return nil, LastError() + return nil, MakeGitError(ecode) } return allocObject(ptr), nil diff --git a/submodule.go b/submodule.go index 9abf333..8390e36 100644 --- a/submodule.go +++ b/submodule.go @@ -81,7 +81,7 @@ func (repo *Repository) LookupSubmodule(name string) (*Submodule, error) { ret := C.git_submodule_lookup(&sub.ptr, repo.ptr, cname) if ret < 0 { - return nil, LastError() + return nil, MakeGitError(ret) } return sub, nil @@ -102,7 +102,7 @@ func (repo *Repository) ForeachSubmodule(cbk SubmoduleCbk) error { ret := C._go_git_visit_submodule(repo.ptr, unsafe.Pointer(&cbk)) if ret < 0 { - return LastError() + return MakeGitError(ret) } return nil } @@ -120,7 +120,7 @@ func (repo *Repository) AddSubmodule(url, path string, use_git_link bool) (*Subm ret := C.git_submodule_add_setup(&sub.ptr, repo.ptr, curl, cpath, cbool(use_git_link)) if ret < 0 { - return nil, LastError() + return nil, MakeGitError(ret) } return sub, nil } @@ -131,7 +131,7 @@ func (sub *Submodule) FinalizeAdd() error { ret := C.git_submodule_add_finalize(sub.ptr) if ret < 0 { - return LastError() + return MakeGitError(ret) } return nil } @@ -142,7 +142,7 @@ func (sub *Submodule) AddToIndex(write_index bool) error { ret := C.git_submodule_add_to_index(sub.ptr, cbool(write_index)) if ret < 0 { - return LastError() + return MakeGitError(ret) } return nil } @@ -153,7 +153,7 @@ func (sub *Submodule) Save() error { ret := C.git_submodule_save(sub.ptr) if ret < 0 { - return LastError() + return MakeGitError(ret) } return nil } @@ -188,7 +188,7 @@ func (sub *Submodule) SetUrl(url string) error { ret := C.git_submodule_set_url(sub.ptr, curl) if ret < 0 { - return LastError() + return MakeGitError(ret) } return nil } @@ -247,7 +247,7 @@ func (sub *Submodule) SetFetchRecurseSubmodules(recurse SubmoduleRecurse) error ret := C.git_submodule_set_fetch_recurse_submodules(sub.ptr, C.git_submodule_recurse_t(recurse)) if ret < 0 { - return LastError() + return MakeGitError(ret) } return nil } @@ -258,7 +258,7 @@ func (sub *Submodule) Init(overwrite bool) error { ret := C.git_submodule_init(sub.ptr, cbool(overwrite)) if ret < 0 { - return LastError() + return MakeGitError(ret) } return nil } @@ -269,7 +269,7 @@ func (sub *Submodule) Sync() error { ret := C.git_submodule_sync(sub.ptr) if ret < 0 { - return LastError() + return MakeGitError(ret) } return nil } @@ -282,7 +282,7 @@ func (sub *Submodule) Open() (*Repository, error) { ret := C.git_submodule_open(&repo.ptr, sub.ptr) if ret < 0 { - return nil, LastError() + return nil, MakeGitError(ret) } return repo, nil } @@ -293,7 +293,7 @@ func (sub *Submodule) Reload() error { ret := C.git_submodule_reload(sub.ptr) if ret < 0 { - return LastError() + return MakeGitError(ret) } return nil } @@ -304,7 +304,7 @@ func (repo *Repository) ReloadAllSubmodules() error { ret := C.git_submodule_reload_all(repo.ptr) if ret < 0 { - return LastError() + return MakeGitError(ret) } return nil } diff --git a/tree.go b/tree.go index 8c74e5d..4efb589 100644 --- a/tree.go +++ b/tree.go @@ -109,7 +109,7 @@ func (t Tree) Walk(callback TreeWalkCallback) error { ) if err < 0 { - return LastError() + return MakeGitError(err) } return nil @@ -134,7 +134,7 @@ func (v *TreeBuilder) Insert(filename string, id *Oid, filemode int) (error) { err := C.git_treebuilder_insert(nil, v.ptr, cfilename, id.toC(), C.git_filemode_t(filemode)) if err < 0 { - return LastError() + return MakeGitError(err) } return nil @@ -149,7 +149,7 @@ func (v *TreeBuilder) Write() (*Oid, error) { err := C.git_treebuilder_write(oid.toC(), v.repo.ptr, v.ptr) if err < 0 { - return nil, LastError() + return nil, MakeGitError(err) } return oid, nil diff --git a/walk.go b/walk.go index 619188d..cdc1a20 100644 --- a/walk.go +++ b/walk.go @@ -40,7 +40,7 @@ func (v *RevWalk) PushHead() (err error) { ecode := C.git_revwalk_push_head(v.ptr) if ecode < 0 { - err = LastError() + err = MakeGitError(ecode) } return @@ -55,7 +55,7 @@ func (v *RevWalk) Next(id *Oid) (err error) { case ret == ITEROVER: err = io.EOF case ret < 0: - err = LastError() + err = MakeGitError(ret) } return From 00ea11691b574b8372cb216427d98038e107e358 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Mart=C3=ADn=20Nieto?= Date: Wed, 26 Feb 2014 16:14:31 +0100 Subject: [PATCH 10/10] Convert the rest of the errors --- config.go | 30 +++++++++++++++--------------- index.go | 2 +- odb.go | 12 ++++++------ reference.go | 2 +- submodule.go | 2 +- tree.go | 2 +- 6 files changed, 25 insertions(+), 25 deletions(-) diff --git a/config.go b/config.go index 2234a43..6b1f093 100644 --- a/config.go +++ b/config.go @@ -61,7 +61,7 @@ func NewConfig() (*Config, error) { defer runtime.UnlockOSThread() if ret := C.git_config_new(&config.ptr); ret < 0 { - return nil, LastError() + return nil, MakeGitError(ret) } return config, nil @@ -78,7 +78,7 @@ func (c *Config) AddFile(path string, level ConfigLevel, force bool) error { ret := C.git_config_add_file_ondisk(c.ptr, cpath, C.git_config_level_t(level), cbool(force)) if ret < 0 { - return LastError() + return MakeGitError(ret) } return nil @@ -142,7 +142,7 @@ func (c *Config) LookupBool(name string) (bool, error) { ret := C.git_config_get_bool(&out, c.ptr, cname) if ret < 0 { - return false, LastError() + return false, MakeGitError(ret) } return out != 0, nil @@ -167,7 +167,7 @@ func (c *Config) NewMultivarIterator(name, regexp string) (*ConfigIterator, erro ret := C.git_config_multivar_iterator_new(&iter.ptr, c.ptr, cname, cregexp) if ret < 0 { - return nil, LastError() + return nil, MakeGitError(ret) } runtime.SetFinalizer(iter, (*ConfigIterator).Free) @@ -184,7 +184,7 @@ func (c *Config) NewIterator() (*ConfigIterator, error) { ret := C.git_config_iterator_new(&iter.ptr, c.ptr) if ret < 0 { - return nil, LastError() + return nil, MakeGitError(ret) } return iter, nil @@ -202,7 +202,7 @@ func (c *Config) NewIteratorGlob(regexp string) (*ConfigIterator, error) { ret := C.git_config_iterator_glob_new(&iter.ptr, c.ptr, cregexp) if ret < 0 { - return nil, LastError() + return nil, MakeGitError(ret) } return iter, nil @@ -237,7 +237,7 @@ func (c *Config) SetInt32(name string, value int32) (err error) { ret := C.git_config_set_int32(c.ptr, cname, C.int32_t(value)) if ret < 0 { - return LastError() + return MakeGitError(ret) } return nil @@ -252,7 +252,7 @@ func (c *Config) SetInt64(name string, value int64) (err error) { ret := C.git_config_set_int64(c.ptr, cname, C.int64_t(value)) if ret < 0 { - return LastError() + return MakeGitError(ret) } return nil @@ -267,7 +267,7 @@ func (c *Config) SetBool(name string, value bool) (err error) { ret := C.git_config_set_bool(c.ptr, cname, cbool(value)) if ret < 0 { - return LastError() + return MakeGitError(ret) } return nil @@ -288,7 +288,7 @@ func (c *Config) SetMultivar(name, regexp, value string) (err error) { ret := C.git_config_set_multivar(c.ptr, cname, cregexp, cvalue) if ret < 0 { - return LastError() + return MakeGitError(ret) } return nil @@ -304,7 +304,7 @@ func (c *Config) Delete(name string) error { ret := C.git_config_delete_entry(c.ptr, cname) if ret < 0 { - return LastError() + return MakeGitError(ret) } return nil @@ -319,7 +319,7 @@ func (c *Config) OpenLevel(parent *Config, level ConfigLevel) (*Config, error) { ret := C.git_config_open_level(&config.ptr, parent.ptr, C.git_config_level_t(level)) if ret < 0 { - return nil, LastError() + return nil, MakeGitError(ret) } return config, nil @@ -336,7 +336,7 @@ func OpenOndisk(parent *Config, path string) (*Config, error) { defer runtime.UnlockOSThread() if ret := C.git_config_open_ondisk(&config.ptr, cpath); ret < 0 { - return nil, LastError() + return nil, MakeGitError(ret) } return config, nil @@ -348,7 +348,7 @@ func (c *Config) Refresh() error { defer runtime.UnlockOSThread() if ret := C.git_config_refresh(c.ptr); ret < 0 { - return LastError() + return MakeGitError(ret) } return nil @@ -364,7 +364,7 @@ func (iter *ConfigIterator) Next() (*ConfigEntry, error) { ret := C.git_config_next(¢ry, iter.ptr) if ret < 0 { - return nil, LastError() + return nil, MakeGitError(ret) } return newConfigEntryFromC(centry), nil diff --git a/index.go b/index.go index cbb28c4..6da3c98 100644 --- a/index.go +++ b/index.go @@ -68,7 +68,7 @@ func (v *Index) Write() (error) { ret := C.git_index_write(v.ptr) if ret < 0 { - return LastError() + return MakeGitError(ret) } return nil diff --git a/odb.go b/odb.go index 3daf3a4..daf63dd 100644 --- a/odb.go +++ b/odb.go @@ -90,7 +90,7 @@ func (v *Odb) Hash(data []byte, otype ObjectType) (oid *Oid, err error) { ret := C.git_odb_hash(oid.toC(), ptr, C.size_t(header.Len), C.git_otype(otype)); if ret < 0 { - err = LastError() + err = MakeGitError(ret) } return } @@ -101,7 +101,7 @@ func (v *Odb) NewReadStream(id *Oid) (*OdbReadStream, error) { stream := new(OdbReadStream) ret := C.git_odb_open_rstream(&stream.ptr, v.ptr, id.toC()) if ret < 0 { - return nil, LastError() + return nil, MakeGitError(ret) } runtime.SetFinalizer(stream, (*OdbReadStream).Free) @@ -115,7 +115,7 @@ func (v *Odb) NewWriteStream(size int, otype ObjectType) (*OdbWriteStream, error stream := new(OdbWriteStream) ret := C.git_odb_open_wstream(&stream.ptr, v.ptr, C.size_t(size), C.git_otype(otype)) if ret < 0 { - return nil, LastError() + return nil, MakeGitError(ret) } runtime.SetFinalizer(stream, (*OdbWriteStream).Free) @@ -164,7 +164,7 @@ func (stream *OdbReadStream) Read(data []byte) (int, error) { size := C.size_t(header.Cap) ret := C.git_odb_stream_read(stream.ptr, ptr, size) if ret < 0 { - return 0, LastError() + return 0, MakeGitError(ret) } header.Len = int(ret) @@ -196,7 +196,7 @@ func (stream *OdbWriteStream) Write(data []byte) (int, error) { ret := C.git_odb_stream_write(stream.ptr, ptr, size) if ret < 0 { - return 0, LastError() + return 0, MakeGitError(ret) } return len(data), nil @@ -207,7 +207,7 @@ func (stream *OdbWriteStream) Write(data []byte) (int, error) { func (stream *OdbWriteStream) Close() error { ret := C.git_odb_stream_finalize_write(stream.Id.toC(), stream.ptr) if ret < 0 { - return LastError() + return MakeGitError(ret) } return nil diff --git a/reference.go b/reference.go index 39d328e..45a3b22 100644 --- a/reference.go +++ b/reference.go @@ -247,7 +247,7 @@ func (v *ReferenceIterator) Next() (*Reference, error) { return nil, ErrIterOver } if ret < 0 { - return nil, LastError() + return nil, MakeGitError(ret) } return newReferenceFromC(ptr), nil diff --git a/submodule.go b/submodule.go index 8390e36..dcc4723 100644 --- a/submodule.go +++ b/submodule.go @@ -247,7 +247,7 @@ func (sub *Submodule) SetFetchRecurseSubmodules(recurse SubmoduleRecurse) error ret := C.git_submodule_set_fetch_recurse_submodules(sub.ptr, C.git_submodule_recurse_t(recurse)) if ret < 0 { - return MakeGitError(ret) + return MakeGitError(C.int(ret)) } return nil } diff --git a/tree.go b/tree.go index 4efb589..b3340d6 100644 --- a/tree.go +++ b/tree.go @@ -67,7 +67,7 @@ func (t Tree) EntryByPath(path string) (*TreeEntry, error) { ret := C.git_tree_entry_bypath(&entry, t.ptr, cpath) if ret < 0 { - return nil, LastError() + return nil, MakeGitError(ret) } return newTreeEntry(entry), nil