From 9d37f817648cac252194816751cd383ae3586883 Mon Sep 17 00:00:00 2001 From: Lucas Clemente Date: Wed, 22 Oct 2014 00:23:12 +0200 Subject: implemented Index.AddAll, Index.RemoveAll, Index.UpdateAll --- index.go | 98 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 98 insertions(+) (limited to 'index.go') diff --git a/index.go b/index.go index b1542d5..e4ca9d6 100644 --- a/index.go +++ b/index.go @@ -3,6 +3,11 @@ package git /* #include #include + +extern int _go_git_index_add_all(git_index*, const git_strarray*, unsigned int, void*); +extern int _go_git_index_update_all(git_index*, const git_strarray*, void*); +extern int _go_git_index_remove_all(git_index*, const git_strarray*, void*); + */ import "C" import ( @@ -12,6 +17,17 @@ import ( "unsafe" ) +type IndexMatchedPathCallback func(string, string) int + +type IndexAddOpts uint + +const ( + IndexAddDefault IndexAddOpts = C.GIT_INDEX_ADD_DEFAULT + IndexAddForce IndexAddOpts = C.GIT_INDEX_ADD_FORCE + IndexAddDisablePathspecMatch IndexAddOpts = C.GIT_INDEX_ADD_DISABLE_PATHSPEC_MATCH + IndexAddCheckPathspec IndexAddOpts = C.GIT_INDEX_ADD_CHECK_PATHSPEC +) + type Index struct { ptr *C.git_index } @@ -114,6 +130,88 @@ func (v *Index) AddByPath(path string) error { return nil } +func (v *Index) AddAll(pathspecs []string, flags IndexAddOpts, callback IndexMatchedPathCallback) error { + cpathspecs := C.git_strarray{} + cpathspecs.count = C.size_t(len(pathspecs)) + cpathspecs.strings = makeCStringsFromStrings(pathspecs) + defer freeStrarray(&cpathspecs) + + runtime.LockOSThread() + defer runtime.UnlockOSThread() + + var cb *IndexMatchedPathCallback + if callback != nil { + cb = &callback + } + + ret := C._go_git_index_add_all( + v.ptr, + &cpathspecs, + C.uint(flags), + unsafe.Pointer(cb), + ) + if ret < 0 { + return MakeGitError(ret) + } + return nil +} + +func (v *Index) UpdateAll(pathspecs []string, callback IndexMatchedPathCallback) error { + cpathspecs := C.git_strarray{} + cpathspecs.count = C.size_t(len(pathspecs)) + cpathspecs.strings = makeCStringsFromStrings(pathspecs) + defer freeStrarray(&cpathspecs) + + runtime.LockOSThread() + defer runtime.UnlockOSThread() + + var cb *IndexMatchedPathCallback + if callback != nil { + cb = &callback + } + + ret := C._go_git_index_update_all( + v.ptr, + &cpathspecs, + unsafe.Pointer(cb), + ) + if ret < 0 { + return MakeGitError(ret) + } + return nil +} + +func (v *Index) RemoveAll(pathspecs []string, callback IndexMatchedPathCallback) error { + cpathspecs := C.git_strarray{} + cpathspecs.count = C.size_t(len(pathspecs)) + cpathspecs.strings = makeCStringsFromStrings(pathspecs) + defer freeStrarray(&cpathspecs) + + runtime.LockOSThread() + defer runtime.UnlockOSThread() + + var cb *IndexMatchedPathCallback + if callback != nil { + cb = &callback + } + + ret := C._go_git_index_remove_all( + v.ptr, + &cpathspecs, + unsafe.Pointer(cb), + ) + if ret < 0 { + return MakeGitError(ret) + } + return nil +} + +//export indexMatchedPathCallback +func indexMatchedPathCallback(cPath, cMatchedPathspec *C.char, payload unsafe.Pointer) int { + callback := (*IndexMatchedPathCallback)(payload) + return (*callback)(C.GoString(cPath), C.GoString(cMatchedPathspec)) +} + func (v *Index) RemoveByPath(path string) error { cstr := C.CString(path) defer C.free(unsafe.Pointer(cstr)) -- cgit v1.2.3