Address issue #108 #109

Merged
bnavetta merged 15 commits from git_status into master 2014-09-11 02:16:39 -05:00
3 changed files with 55 additions and 1 deletions
Showing only changes of commit 1cb654e4f2 - Show all commits

View File

@ -3,11 +3,14 @@ package git
/*
#include <git2.h>
#include <git2/errors.h>
int _go_git_status_foreach(git_repository *repo, void *data);
*/
import "C"
import (
"runtime"
"unsafe"
)
type Status int
@ -168,3 +171,26 @@ func (v *Repository) StatusFile(path string) (Status, error) {
}
return Status(statusFlags), nil
}
type StatusCallback func(path string, status Status) int
//export fileStatusForeach
func fileStatusForeach(_path *C.char, _flags C.uint, _payload unsafe.Pointer) C.int {
path := C.GoString(_path)
flags := Status(_flags)
cb := (*StatusCallback)(_payload)
return C.int((*cb)(path, flags))
}
func (v *Repository) StatusForeach(callback StatusCallback) error {
runtime.LockOSThread()
defer runtime.UnlockOSThread()
ret := C._go_git_status_foreach(v.ptr, unsafe.Pointer(&callback))
if ret < 0 {
return MakeGitError(ret)
}
return nil
}

View File

@ -23,6 +23,29 @@ func TestStatusFile(t *testing.T) {
}
}
func TestStatusForeach(t *testing.T) {
repo := createTestRepo(t)
defer repo.Free()
defer os.RemoveAll(repo.Workdir())
err := ioutil.WriteFile(path.Join(path.Dir(repo.Workdir()), "hello.txt"), []byte("Hello, World"), 0644)
checkFatal(t, err)
statusFound := false
err = repo.StatusForeach(func (path string, statusFlags Status) int {
if path == "hello.txt" && statusFlags & StatusWtNew != 0 {
statusFound = true
}
return 0
});
checkFatal(t, err)
if !statusFound {
t.Fatal("Status callback not called with the new file")
}
}
func TestEntryCount(t *testing.T) {
repo := createTestRepo(t)
defer repo.Free()

View File

@ -45,7 +45,7 @@ void _go_git_refdb_backend_free(git_refdb_backend *backend)
int _go_git_diff_foreach(git_diff *diff, int eachFile, int eachHunk, int eachLine, void *payload)
{
git_diff_file_cb fcb = NULL;
git_diff_file_cb fcb = NULL;
git_diff_hunk_cb hcb = NULL;
git_diff_line_cb lcb = NULL;
@ -105,4 +105,9 @@ int _go_git_blob_create_fromchunks(git_oid *id,
return git_blob_create_fromchunks(id, repo, hintpath, _go_blob_chunk_cb, payload);
}
int _go_git_status_foreach(git_repository *repo, void *data)
{
return git_status_foreach(repo, (git_status_cb)fileStatusForeach, data);
}
/* EOF */