diff --git a/status.go b/status.go index ab0469f..4523cb8 100644 --- a/status.go +++ b/status.go @@ -3,11 +3,14 @@ package git /* #include #include + +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 +} diff --git a/status_test.go b/status_test.go index 228ee0e..a0ff1d3 100644 --- a/status_test.go +++ b/status_test.go @@ -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() diff --git a/wrapper.c b/wrapper.c index 2fd8fb7..09dba46 100644 --- a/wrapper.c +++ b/wrapper.c @@ -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 */