From 5539137e9a6da3276bc83ef47bbebca8bab56856 Mon Sep 17 00:00:00 2001 From: Ryan Dahl Date: Mon, 20 Oct 2014 11:53:10 -0400 Subject: [PATCH 1/3] Use Filemode type in TreeEntry and IndexEntry Fixes #121 --- index.go | 4 ++-- tree.go | 12 ++++++------ 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/index.go b/index.go index b1542d5..289239d 100644 --- a/index.go +++ b/index.go @@ -19,7 +19,7 @@ type Index struct { type IndexEntry struct { Ctime time.Time Mtime time.Time - Mode uint + Mode Filemode Uid uint Gid uint Size uint @@ -34,7 +34,7 @@ func newIndexEntryFromC(entry *C.git_index_entry) *IndexEntry { return &IndexEntry{ time.Unix(int64(entry.ctime.seconds), int64(entry.ctime.nanoseconds)), time.Unix(int64(entry.mtime.seconds), int64(entry.mtime.nanoseconds)), - uint(entry.mode), + Filemode(entry.mode), uint(entry.uid), uint(entry.gid), uint(entry.file_size), diff --git a/tree.go b/tree.go index e5fe361..45de9f1 100644 --- a/tree.go +++ b/tree.go @@ -17,10 +17,10 @@ type Filemode int const ( FilemodeTree Filemode = C.GIT_FILEMODE_TREE - FilemodeBlob = C.GIT_FILEMODE_BLOB - FilemodeBlobExecutable = C.GIT_FILEMODE_BLOB_EXECUTABLE - FilemodeLink = C.GIT_FILEMODE_LINK - FilemodeCommit = C.GIT_FILEMODE_COMMIT + FilemodeBlob Filemode = C.GIT_FILEMODE_BLOB + FilemodeBlobExecutable Filemode = C.GIT_FILEMODE_BLOB_EXECUTABLE + FilemodeLink Filemode = C.GIT_FILEMODE_LINK + FilemodeCommit Filemode = C.GIT_FILEMODE_COMMIT ) type Tree struct { @@ -32,7 +32,7 @@ type TreeEntry struct { Name string Id *Oid Type ObjectType - Filemode int + Filemode Filemode } func newTreeEntry(entry *C.git_tree_entry) *TreeEntry { @@ -40,7 +40,7 @@ func newTreeEntry(entry *C.git_tree_entry) *TreeEntry { C.GoString(C.git_tree_entry_name(entry)), newOidFromC(C.git_tree_entry_id(entry)), ObjectType(C.git_tree_entry_type(entry)), - int(C.git_tree_entry_filemode(entry)), + Filemode(C.git_tree_entry_filemode(entry)), } } -- 2.45.2 From 0af2a39b4e09d4f463fcf6779c072a36c6ab01f8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Mart=C3=ADn=20Nieto?= Date: Thu, 23 Oct 2014 18:51:51 +0200 Subject: [PATCH 2/3] Update to latest master --- vendor/libgit2 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/vendor/libgit2 b/vendor/libgit2 index e0383fa..d676af4 160000 --- a/vendor/libgit2 +++ b/vendor/libgit2 @@ -1 +1 @@ -Subproject commit e0383fa35f981c656043976a43c61bff059cb709 +Subproject commit d676af43da6603f1b31fb6d2d3eb02793b260ad0 -- 2.45.2 From 9d37f817648cac252194816751cd383ae3586883 Mon Sep 17 00:00:00 2001 From: Lucas Clemente Date: Wed, 22 Oct 2014 00:23:12 +0200 Subject: [PATCH 3/3] implemented Index.AddAll, Index.RemoveAll, Index.UpdateAll --- index.go | 98 +++++++++++++++++++++++++++++++++++++++++++++++++++ index_test.go | 54 ++++++++++++++++++++++++++-- wrapper.c | 15 ++++++++ 3 files changed, 165 insertions(+), 2 deletions(-) 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)) diff --git a/index_test.go b/index_test.go index cd178b9..98d9a31 100644 --- a/index_test.go +++ b/index_test.go @@ -1,6 +1,7 @@ package git import ( + "io/ioutil" "os" "runtime" "testing" @@ -54,9 +55,9 @@ func TestIndexAddAndWriteTreeTo(t *testing.T) { idx, err := NewIndex() checkFatal(t, err) - entry := IndexEntry { + entry := IndexEntry{ Path: "README", - Id: blobID, + Id: blobID, Mode: FilemodeBlob, } @@ -71,6 +72,55 @@ func TestIndexAddAndWriteTreeTo(t *testing.T) { } } +func TestIndexAddAllNoCallback(t *testing.T) { + repo := createTestRepo(t) + defer os.RemoveAll(repo.Workdir()) + + err := ioutil.WriteFile(repo.Workdir()+"/README", []byte("foo\n"), 0644) + checkFatal(t, err) + + idx, err := repo.Index() + checkFatal(t, err) + + err = idx.AddAll([]string{}, IndexAddDefault, nil) + checkFatal(t, err) + + treeId, err := idx.WriteTreeTo(repo) + checkFatal(t, err) + + if treeId.String() != "b7119b11e8ef7a1a5a34d3ac87f5b075228ac81e" { + t.Fatalf("%v", treeId.String()) + } +} + +func TestIndexAddAllCallback(t *testing.T) { + repo := createTestRepo(t) + defer os.RemoveAll(repo.Workdir()) + + err := ioutil.WriteFile(repo.Workdir()+"/README", []byte("foo\n"), 0644) + checkFatal(t, err) + + idx, err := repo.Index() + checkFatal(t, err) + + cbPath := "" + err = idx.AddAll([]string{}, IndexAddDefault, func(p, mP string) int { + cbPath = p + return 0 + }) + checkFatal(t, err) + if cbPath != "README" { + t.Fatalf("%v", cbPath) + } + + treeId, err := idx.WriteTreeTo(repo) + checkFatal(t, err) + + if treeId.String() != "b7119b11e8ef7a1a5a34d3ac87f5b075228ac81e" { + t.Fatalf("%v", treeId.String()) + } +} + func checkFatal(t *testing.T, err error) { if err == nil { return diff --git a/wrapper.c b/wrapper.c index 15e11ce..6e33fa2 100644 --- a/wrapper.c +++ b/wrapper.c @@ -104,4 +104,19 @@ 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_index_add_all(git_index *index, const git_strarray *pathspec, unsigned int flags, void *callback) { + git_index_matched_path_cb cb = callback ? (git_index_matched_path_cb) &indexMatchedPathCallback : NULL; + return git_index_add_all(index, pathspec, flags, cb, callback); +} + +int _go_git_index_update_all(git_index *index, const git_strarray *pathspec, void *callback) { + git_index_matched_path_cb cb = callback ? (git_index_matched_path_cb) &indexMatchedPathCallback : NULL; + return git_index_update_all(index, pathspec, cb, callback); +} + +int _go_git_index_remove_all(git_index *index, const git_strarray *pathspec, void *callback) { + git_index_matched_path_cb cb = callback ? (git_index_matched_path_cb) &indexMatchedPathCallback : NULL; + return git_index_remove_all(index, pathspec, cb, callback); +} + /* EOF */ -- 2.45.2