From 295ec8894c0e96330a106d4854bea5c1e9d37f20 Mon Sep 17 00:00:00 2001 From: Artiom Di Date: Thu, 14 Nov 2013 13:08:34 +0200 Subject: [PATCH 1/4] Fix memleak, free TreeBuilder --- repository.go | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/repository.go b/repository.go index 34df0aa..3ba3e5e 100644 --- a/repository.go +++ b/repository.go @@ -6,8 +6,8 @@ package git */ import "C" import ( - "unsafe" "runtime" + "unsafe" ) // Repository @@ -180,7 +180,7 @@ func (v *Repository) CreateCommit( var cparents []*C.git_commit = nil var parentsarg **C.git_commit = nil - nparents:= len(parents) + nparents := len(parents) if nparents > 0 { cparents = make([]*C.git_commit, nparents) for i, v := range parents { @@ -226,7 +226,7 @@ func (repo *Repository) Path() string { return C.GoString(C.git_repository_path(repo.ptr)) } -func (repo *Repository) IsBare() (bool) { +func (repo *Repository) IsBare() bool { return C.git_repository_is_bare(repo.ptr) != 0 } @@ -249,6 +249,7 @@ func (v *Repository) TreeBuilder() (*TreeBuilder, error) { if ret := C.git_treebuilder_create(&bld.ptr, nil); ret < 0 { return nil, LastError() } + runtime.SetFinalizer(bld, (*TreeBuilder).Free) bld.repo = v return bld, nil -- 2.45.2 From 5e30c192e9f7219322887da7252c344d5be1ec05 Mon Sep 17 00:00:00 2001 From: Artiom Di Date: Thu, 14 Nov 2013 15:24:43 +0200 Subject: [PATCH 2/4] Fix memleak for Config and parent commit objects --- commit.go | 13 +++++++------ config.go | 6 ++++++ repository.go | 1 + 3 files changed, 14 insertions(+), 6 deletions(-) diff --git a/commit.go b/commit.go index cacaa33..c633e2d 100644 --- a/commit.go +++ b/commit.go @@ -9,8 +9,8 @@ extern int _go_git_treewalk(git_tree *tree, git_treewalk_mode mode, void *ptr); import "C" import ( - "unsafe" "time" + "unsafe" ) // Commit @@ -48,12 +48,13 @@ func (c Commit) Committer() *Signature { } func (c *Commit) Parent(n uint) *Commit { - par := &Commit{} - ret := C.git_commit_parent(&par.ptr, c.ptr, C.uint(n)) + var cobj *C.git_object + ret := C.git_commit_parent(&cobj, c.ptr, C.uint(n)) if ret != 0 { return nil } - return par + + return allocObject(cobj).(*Commit) } func (c *Commit) ParentId(n uint) *Oid { @@ -85,10 +86,10 @@ func newSignatureFromC(sig *C.git_signature) *Signature { // the offset in mintes, which is what git wants func (v *Signature) Offset() int { _, offset := v.When.Zone() - return offset/60 + return offset / 60 } -func (sig *Signature) toC() (*C.git_signature) { +func (sig *Signature) toC() *C.git_signature { var out *C.git_signature name := C.CString(sig.Name) diff --git a/config.go b/config.go index dcad2a7..7235716 100644 --- a/config.go +++ b/config.go @@ -6,6 +6,7 @@ package git */ import "C" import ( + "runtime" "unsafe" ) @@ -66,3 +67,8 @@ func (c *Config) Set(name, value string) (err error) { return nil } + +func (c *Config) Free() { + runtime.SetFinalizer(c, nil) + C.git_config_free(c.ptr) +} diff --git a/repository.go b/repository.go index 3ba3e5e..5540a0c 100644 --- a/repository.go +++ b/repository.go @@ -58,6 +58,7 @@ func (v *Repository) Config() (*Config, error) { return nil, LastError() } + runtime.SetFinalizer(config, (*Config).Free) return config, nil } -- 2.45.2 From a40bdfd4202db244bfc5da348a0f0c528ef122cd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Mart=C3=ADn=20Nieto?= Date: Wed, 18 Sep 2013 09:23:47 +0200 Subject: [PATCH 3/4] Lock the OS thread when acessing errors The library stores error information in thread-local storage, which means we need to make sure that the Go runtime doesn't switch OS threads between the time we call a function and th time we attempt to retrieve the error information. --- checkout.go | 7 +++++++ commit.go | 4 ++++ config.go | 12 ++++++++++++ git.go | 11 +++++++++++ index.go | 7 +++++++ odb.go | 8 ++++++++ packbuilder.go | 18 ++++++++++++++++++ reference.go | 27 +++++++++++++++++++++++++++ repository.go | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ submodule.go | 43 +++++++++++++++++++++++++++++++++++++++++++ tree.go | 13 +++++++++++++ walk.go | 7 +++++++ 12 files changed, 205 insertions(+) diff --git a/checkout.go b/checkout.go index 63637e2..f4c1d4e 100644 --- a/checkout.go +++ b/checkout.go @@ -9,6 +9,7 @@ git_checkout_opts git_checkout_opts_init() { */ import "C" import ( + "runtime" "os" ) @@ -59,6 +60,9 @@ func (v *Repository) Checkout(opts *CheckoutOpts) error { var copts C.git_checkout_opts populateCheckoutOpts(&copts, opts) + runtime.LockOSThread() + defer runtime.UnlockOSThread() + ret := C.git_checkout_head(v.ptr, &copts) if ret < 0 { return LastError() @@ -72,6 +76,9 @@ func (v *Repository) CheckoutIndex(index *Index, opts *CheckoutOpts) error { var copts C.git_checkout_opts populateCheckoutOpts(&copts, opts) + runtime.LockOSThread() + defer runtime.UnlockOSThread() + ret := C.git_checkout_index(v.ptr, index.ptr, &copts) if ret < 0 { return LastError() diff --git a/commit.go b/commit.go index c633e2d..498669e 100644 --- a/commit.go +++ b/commit.go @@ -9,6 +9,7 @@ extern int _go_git_treewalk(git_tree *tree, git_treewalk_mode mode, void *ptr); import "C" import ( + "runtime" "time" "unsafe" ) @@ -25,6 +26,9 @@ func (c Commit) Message() string { func (c Commit) Tree() (*Tree, error) { var ptr *C.git_object + runtime.LockOSThread() + defer runtime.UnlockOSThread() + err := C.git_commit_tree(&ptr, c.ptr) if err < 0 { return nil, LastError() diff --git a/config.go b/config.go index 7235716..2aa073a 100644 --- a/config.go +++ b/config.go @@ -19,6 +19,9 @@ func (c *Config) LookupInt32(name string) (v int32, err error) { cname := C.CString(name) defer C.free(unsafe.Pointer(cname)) + runtime.LockOSThread() + defer runtime.UnlockOSThread() + ret := C.git_config_get_int32(&out, c.ptr, cname) if ret < 0 { return 0, LastError() @@ -32,6 +35,9 @@ func (c *Config) LookupInt64(name string) (v int64, err error) { cname := C.CString(name) defer C.free(unsafe.Pointer(cname)) + runtime.LockOSThread() + defer runtime.UnlockOSThread() + ret := C.git_config_get_int64(&out, c.ptr, cname) if ret < 0 { return 0, LastError() @@ -45,6 +51,9 @@ func (c *Config) LookupString(name string) (v string, err error) { cname := C.CString(name) defer C.free(unsafe.Pointer(cname)) + runtime.LockOSThread() + defer runtime.UnlockOSThread() + ret := C.git_config_get_string(&ptr, c.ptr, cname) if ret < 0 { return "", LastError() @@ -60,6 +69,9 @@ func (c *Config) Set(name, value string) (err error) { cvalue := C.CString(value) defer C.free(unsafe.Pointer(cvalue)) + runtime.LockOSThread() + defer runtime.UnlockOSThread() + ret := C.git_config_set_string(c.ptr, cname, cvalue) if ret < 0 { return LastError() diff --git a/git.go b/git.go index 6f81293..28196c8 100644 --- a/git.go +++ b/git.go @@ -9,6 +9,7 @@ import "C" import ( "bytes" "errors" + "runtime" "unsafe" "strings" ) @@ -57,6 +58,9 @@ func NewOidFromString(s string) (*Oid, error) { cs := C.CString(s) defer C.free(unsafe.Pointer(cs)) + runtime.LockOSThread() + defer runtime.UnlockOSThread() + if C.git_oid_fromstr(o.toC(), cs) < 0 { return nil, LastError() } @@ -109,6 +113,10 @@ func ShortenOids(ids []*Oid, minlen int) (int, error) { defer C.git_oid_shorten_free(shorten) var ret C.int + + runtime.LockOSThread() + defer runtime.UnlockOSThread() + for _, id := range ids { buf := make([]byte, 41) C.git_oid_fmt((*C.char)(unsafe.Pointer(&buf[0])), id.toC()) @@ -162,6 +170,9 @@ func Discover(start string, across_fs bool, ceiling_dirs []string) (string, erro retpath := (*C.char)(C.malloc(C.GIT_PATH_MAX)) defer C.free(unsafe.Pointer(retpath)) + runtime.LockOSThread() + defer runtime.UnlockOSThread() + r := C.git_repository_discover(retpath, C.GIT_PATH_MAX, cstart, cbool(across_fs), ceildirs) if r == 0 { diff --git a/index.go b/index.go index 4a69c1e..ac5148c 100644 --- a/index.go +++ b/index.go @@ -24,6 +24,9 @@ func (v *Index) AddByPath(path string) error { cstr := C.CString(path) defer C.free(unsafe.Pointer(cstr)) + runtime.LockOSThread() + defer runtime.UnlockOSThread() + ret := C.git_index_add_bypath(v.ptr, cstr) if ret < 0 { return LastError() @@ -34,6 +37,10 @@ func (v *Index) AddByPath(path string) error { func (v *Index) WriteTree() (*Oid, error) { oid := new(Oid) + + runtime.LockOSThread() + defer runtime.UnlockOSThread() + ret := C.git_index_write_tree(oid.toC(), v.ptr) if ret < 0 { return nil, LastError() diff --git a/odb.go b/odb.go index eaa7872..638ef74 100644 --- a/odb.go +++ b/odb.go @@ -25,6 +25,10 @@ func (v *Odb) Exists(oid *Oid) bool { func (v *Odb) Write(data []byte, otype ObjectType) (oid *Oid, err error) { oid = new(Oid) hdr := (*reflect.SliceHeader)(unsafe.Pointer(&data)) + + runtime.LockOSThread() + defer runtime.UnlockOSThread() + 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 { @@ -36,6 +40,10 @@ func (v *Odb) Write(data []byte, otype ObjectType) (oid *Oid, err error) { func (v *Odb) Read(oid *Oid) (obj *OdbObject, err error) { obj = new(OdbObject) + + runtime.LockOSThread() + defer runtime.UnlockOSThread() + ret := C.git_odb_read(&obj.ptr, v.ptr, oid.toC()) if ret < 0 { return nil, LastError() diff --git a/packbuilder.go b/packbuilder.go index c57b0d4..333f183 100644 --- a/packbuilder.go +++ b/packbuilder.go @@ -22,6 +22,10 @@ type Packbuilder struct { func (repo *Repository) NewPackbuilder() (*Packbuilder, error) { builder := &Packbuilder{} + + runtime.LockOSThread() + defer runtime.UnlockOSThread() + ret := C.git_packbuilder_new(&builder.ptr, repo.ptr) if ret != 0 { return nil, LastError() @@ -38,6 +42,10 @@ func (pb *Packbuilder) Free() { func (pb *Packbuilder) Insert(id *Oid, name string) error { cname := C.CString(name) defer C.free(unsafe.Pointer(cname)) + + runtime.LockOSThread() + defer runtime.UnlockOSThread() + ret := C.git_packbuilder_insert(pb.ptr, id.toC(), cname) if ret != 0 { return LastError() @@ -46,6 +54,9 @@ func (pb *Packbuilder) Insert(id *Oid, name string) error { } func (pb *Packbuilder) InsertCommit(id *Oid) error { + runtime.LockOSThread() + defer runtime.UnlockOSThread() + ret := C.git_packbuilder_insert_commit(pb.ptr, id.toC()) if ret != 0 { return LastError() @@ -54,6 +65,9 @@ func (pb *Packbuilder) InsertCommit(id *Oid) error { } func (pb *Packbuilder) InsertTree(id *Oid) error { + runtime.LockOSThread() + defer runtime.UnlockOSThread() + ret := C.git_packbuilder_insert_tree(pb.ptr, id.toC()) if ret != 0 { return LastError() @@ -68,6 +82,10 @@ func (pb *Packbuilder) ObjectCount() uint32 { func (pb *Packbuilder) WriteToFile(name string, mode os.FileMode) error { cname := C.CString(name) defer C.free(unsafe.Pointer(cname)) + + runtime.LockOSThread() + defer runtime.UnlockOSThread() + ret := C.git_packbuilder_write(pb.ptr, cname, C.uint(mode.Perm()), nil, nil) if ret != 0 { return LastError() diff --git a/reference.go b/reference.go index 525e092..8e33354 100644 --- a/reference.go +++ b/reference.go @@ -32,6 +32,9 @@ func (v *Reference) SetSymbolicTarget(target string) (*Reference, error) { ctarget := C.CString(target) defer C.free(unsafe.Pointer(ctarget)) + runtime.LockOSThread() + defer runtime.UnlockOSThread() + ret := C.git_reference_symbolic_set_target(&ptr, v.ptr, ctarget) if ret < 0 { return nil, LastError() @@ -43,6 +46,9 @@ func (v *Reference) SetSymbolicTarget(target string) (*Reference, error) { func (v *Reference) SetTarget(target *Oid) (*Reference, error) { var ptr *C.git_reference + runtime.LockOSThread() + defer runtime.UnlockOSThread() + ret := C.git_reference_set_target(&ptr, v.ptr, target.toC()) if ret < 0 { return nil, LastError() @@ -54,6 +60,9 @@ func (v *Reference) SetTarget(target *Oid) (*Reference, error) { func (v *Reference) Resolve() (*Reference, error) { var ptr *C.git_reference + runtime.LockOSThread() + defer runtime.UnlockOSThread() + ret := C.git_reference_resolve(&ptr, v.ptr) if ret < 0 { return nil, LastError() @@ -67,6 +76,9 @@ func (v *Reference) Rename(name string, force bool) (*Reference, error) { cname := C.CString(name) defer C.free(unsafe.Pointer(cname)) + runtime.LockOSThread() + defer runtime.UnlockOSThread() + ret := C.git_reference_rename(&ptr, v.ptr, cname, cbool(force)) if ret < 0 { @@ -90,6 +102,9 @@ func (v *Reference) SymbolicTarget() string { } func (v *Reference) Delete() error { + runtime.LockOSThread() + defer runtime.UnlockOSThread() + ret := C.git_reference_delete(v.ptr) if ret < 0 { @@ -120,6 +135,10 @@ type ReferenceIterator struct { // NewReferenceIterator creates a new iterator over reference names func (repo *Repository) NewReferenceIterator() (*ReferenceIterator, error) { var ptr *C.git_reference_iterator + + runtime.LockOSThread() + defer runtime.UnlockOSThread() + ret := C.git_reference_iterator_new(&ptr, repo.ptr) if ret < 0 { return nil, LastError() @@ -137,6 +156,10 @@ func (repo *Repository) NewReferenceIteratorGlob(glob string) (*ReferenceIterato cstr := C.CString(glob) defer C.free(unsafe.Pointer(cstr)) var ptr *C.git_reference_iterator + + runtime.LockOSThread() + defer runtime.UnlockOSThread() + ret := C.git_reference_iterator_glob_new(&ptr, repo.ptr, cstr) if ret < 0 { return nil, LastError() @@ -151,6 +174,10 @@ func (repo *Repository) NewReferenceIteratorGlob(glob string) (*ReferenceIterato // the returned error is git.ErrIterOver func (v *ReferenceIterator) NextName() (string, error) { var ptr *C.char + + runtime.LockOSThread() + defer runtime.UnlockOSThread() + ret := C.git_reference_next_name(&ptr, v.ptr) if ret == ITEROVER { return "", ErrIterOver diff --git a/repository.go b/repository.go index 5540a0c..3a9068d 100644 --- a/repository.go +++ b/repository.go @@ -21,6 +21,9 @@ func OpenRepository(path string) (*Repository, error) { cpath := C.CString(path) defer C.free(unsafe.Pointer(cpath)) + runtime.LockOSThread() + defer runtime.UnlockOSThread() + ret := C.git_repository_open(&repo.ptr, cpath) if ret < 0 { return nil, LastError() @@ -36,6 +39,9 @@ func InitRepository(path string, isbare bool) (*Repository, error) { cpath := C.CString(path) defer C.free(unsafe.Pointer(cpath)) + runtime.LockOSThread() + defer runtime.UnlockOSThread() + ret := C.git_repository_init(&repo.ptr, cpath, ucbool(isbare)) if ret < 0 { return nil, LastError() @@ -53,6 +59,9 @@ func (v *Repository) Free() { func (v *Repository) Config() (*Config, error) { config := new(Config) + runtime.LockOSThread() + defer runtime.UnlockOSThread() + ret := C.git_repository_config(&config.ptr, v.ptr) if ret < 0 { return nil, LastError() @@ -64,6 +73,10 @@ func (v *Repository) Config() (*Config, error) { func (v *Repository) Index() (*Index, error) { var ptr *C.git_index + + runtime.LockOSThread() + defer runtime.UnlockOSThread() + ret := C.git_repository_index(&ptr, v.ptr) if ret < 0 { return nil, LastError() @@ -74,6 +87,10 @@ func (v *Repository) Index() (*Index, error) { func (v *Repository) lookupType(oid *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)) if ret < 0 { return nil, LastError() @@ -118,6 +135,9 @@ func (v *Repository) LookupReference(name string) (*Reference, error) { defer C.free(unsafe.Pointer(cname)) var ptr *C.git_reference + runtime.LockOSThread() + defer runtime.UnlockOSThread() + ecode := C.git_reference_lookup(&ptr, v.ptr, cname) if ecode < 0 { return nil, LastError() @@ -131,6 +151,9 @@ func (v *Repository) CreateReference(name string, oid *Oid, force bool) (*Refere defer C.free(unsafe.Pointer(cname)) var ptr *C.git_reference + runtime.LockOSThread() + defer runtime.UnlockOSThread() + ecode := C.git_reference_create(&ptr, v.ptr, cname, oid.toC(), cbool(force)) if ecode < 0 { return nil, LastError() @@ -146,6 +169,9 @@ func (v *Repository) CreateSymbolicReference(name, target string, force bool) (* defer C.free(unsafe.Pointer(ctarget)) var ptr *C.git_reference + runtime.LockOSThread() + defer runtime.UnlockOSThread() + ecode := C.git_reference_symbolic_create(&ptr, v.ptr, cname, ctarget, cbool(force)) if ecode < 0 { return nil, LastError() @@ -156,6 +182,10 @@ func (v *Repository) CreateSymbolicReference(name, target string, force bool) (* func (v *Repository) Walk() (*RevWalk, error) { walk := new(RevWalk) + + runtime.LockOSThread() + defer runtime.UnlockOSThread() + ecode := C.git_revwalk_new(&walk.ptr, v.ptr) if ecode < 0 { return nil, LastError() @@ -196,6 +226,9 @@ func (v *Repository) CreateCommit( committerSig := committer.toC() defer C.git_signature_free(committerSig) + runtime.LockOSThread() + defer runtime.UnlockOSThread() + ret := C.git_commit_create( oid.toC(), v.ptr, cref, authorSig, committerSig, @@ -215,6 +248,10 @@ func (v *Odb) Free() { func (v *Repository) Odb() (odb *Odb, err error) { odb = new(Odb) + + runtime.LockOSThread() + defer runtime.UnlockOSThread() + if ret := C.git_repository_odb(&odb.ptr, v.ptr); ret < 0 { return nil, LastError() } @@ -239,6 +276,9 @@ func (repo *Repository) SetWorkdir(workdir string, updateGitlink bool) error { cstr := C.CString(workdir) defer C.free(unsafe.Pointer(cstr)) + runtime.LockOSThread() + defer runtime.UnlockOSThread() + if C.git_repository_set_workdir(repo.ptr, cstr, cbool(updateGitlink)) < 0 { return LastError() } @@ -247,6 +287,10 @@ func (repo *Repository) SetWorkdir(workdir string, updateGitlink bool) error { func (v *Repository) TreeBuilder() (*TreeBuilder, error) { bld := new(TreeBuilder) + + runtime.LockOSThread() + defer runtime.UnlockOSThread() + if ret := C.git_treebuilder_create(&bld.ptr, nil); ret < 0 { return nil, LastError() } @@ -261,6 +305,10 @@ func (v *Repository) RevparseSingle(spec string) (Object, error) { defer C.free(unsafe.Pointer(cspec)) var ptr *C.git_object + + runtime.LockOSThread() + defer runtime.UnlockOSThread() + ecode := C.git_revparse_single(&ptr, v.ptr, cspec) if ecode < 0 { return nil, LastError() diff --git a/submodule.go b/submodule.go index 9819b87..48ea151 100644 --- a/submodule.go +++ b/submodule.go @@ -8,6 +8,7 @@ extern int _go_git_visit_submodule(git_repository *repo, void *fct); */ import "C" import ( + "runtime" "unsafe" ) @@ -66,6 +67,10 @@ func (repo *Repository) LookupSubmodule(name string) (*Submodule, error) { defer C.free(unsafe.Pointer(cname)) sub := new(Submodule) + + runtime.LockOSThread() + defer runtime.UnlockOSThread() + ret := C.git_submodule_lookup(&sub.ptr, repo.ptr, cname) if ret < 0 { return nil, LastError() @@ -84,6 +89,9 @@ func SubmoduleVisitor(csub unsafe.Pointer, name string, cfct unsafe.Pointer) int } func (repo *Repository) ForeachSubmodule(cbk SubmoduleCbk) error { + runtime.LockOSThread() + defer runtime.UnlockOSThread() + ret := C._go_git_visit_submodule(repo.ptr, unsafe.Pointer(&cbk)) if ret < 0 { return LastError() @@ -98,6 +106,10 @@ func (repo *Repository) AddSubmodule(url, path string, use_git_link bool) (*Subm defer C.free(unsafe.Pointer(cpath)) sub := new(Submodule) + + runtime.LockOSThread() + defer runtime.UnlockOSThread() + ret := C.git_submodule_add_setup(&sub.ptr, repo.ptr, curl, cpath, cbool(use_git_link)) if ret < 0 { return nil, LastError() @@ -106,6 +118,9 @@ func (repo *Repository) AddSubmodule(url, path string, use_git_link bool) (*Subm } func (sub *Submodule) FinalizeAdd() error { + runtime.LockOSThread() + defer runtime.UnlockOSThread() + ret := C.git_submodule_add_finalize(sub.ptr) if ret < 0 { return LastError() @@ -114,6 +129,9 @@ func (sub *Submodule) FinalizeAdd() error { } func (sub *Submodule) AddToIndex(write_index bool) error { + runtime.LockOSThread() + defer runtime.UnlockOSThread() + ret := C.git_submodule_add_to_index(sub.ptr, cbool(write_index)) if ret < 0 { return LastError() @@ -122,6 +140,9 @@ func (sub *Submodule) AddToIndex(write_index bool) error { } func (sub *Submodule) Save() error { + runtime.LockOSThread() + defer runtime.UnlockOSThread() + ret := C.git_submodule_save(sub.ptr) if ret < 0 { return LastError() @@ -154,6 +175,9 @@ func (sub *Submodule) SetUrl(url string) error { curl := C.CString(url) defer C.free(unsafe.Pointer(curl)) + runtime.LockOSThread() + defer runtime.UnlockOSThread() + ret := C.git_submodule_set_url(sub.ptr, curl) if ret < 0 { return LastError() @@ -213,6 +237,9 @@ func (sub *Submodule) FetchRecurseSubmodules() bool { } func (sub *Submodule) SetFetchRecurseSubmodules(v bool) error { + runtime.LockOSThread() + defer runtime.UnlockOSThread() + ret := C.git_submodule_set_fetch_recurse_submodules(sub.ptr, cbool(v)) if ret < 0 { return LastError() @@ -221,6 +248,9 @@ func (sub *Submodule) SetFetchRecurseSubmodules(v bool) error { } func (sub *Submodule) Init(overwrite bool) error { + runtime.LockOSThread() + defer runtime.UnlockOSThread() + ret := C.git_submodule_init(sub.ptr, cbool(overwrite)) if ret < 0 { return LastError() @@ -229,6 +259,9 @@ func (sub *Submodule) Init(overwrite bool) error { } func (sub *Submodule) Sync() error { + runtime.LockOSThread() + defer runtime.UnlockOSThread() + ret := C.git_submodule_sync(sub.ptr) if ret < 0 { return LastError() @@ -238,6 +271,10 @@ func (sub *Submodule) Sync() error { func (sub *Submodule) Open() (*Repository, error) { repo := new(Repository) + + runtime.LockOSThread() + defer runtime.UnlockOSThread() + ret := C.git_submodule_open(&repo.ptr, sub.ptr) if ret < 0 { return nil, LastError() @@ -246,6 +283,9 @@ func (sub *Submodule) Open() (*Repository, error) { } func (sub *Submodule) Reload() error { + runtime.LockOSThread() + defer runtime.UnlockOSThread() + ret := C.git_submodule_reload(sub.ptr) if ret < 0 { return LastError() @@ -254,6 +294,9 @@ func (sub *Submodule) Reload() error { } func (repo *Repository) ReloadAllSubmodules() error { + runtime.LockOSThread() + defer runtime.UnlockOSThread() + ret := C.git_submodule_reload_all(repo.ptr) if ret < 0 { return LastError() diff --git a/tree.go b/tree.go index 3abd31c..8c74e5d 100644 --- a/tree.go +++ b/tree.go @@ -62,6 +62,9 @@ func (t Tree) EntryByPath(path string) (*TreeEntry, error) { defer C.free(unsafe.Pointer(cpath)) var entry *C.git_tree_entry + runtime.LockOSThread() + defer runtime.UnlockOSThread() + ret := C.git_tree_entry_bypath(&entry, t.ptr, cpath) if ret < 0 { return nil, LastError() @@ -96,6 +99,9 @@ func CallbackGitTreeWalk(_root unsafe.Pointer, _entry unsafe.Pointer, ptr unsafe } func (t Tree) Walk(callback TreeWalkCallback) error { + runtime.LockOSThread() + defer runtime.UnlockOSThread() + err := C._go_git_treewalk( t.ptr, C.GIT_TREEWALK_PRE, @@ -123,6 +129,9 @@ func (v *TreeBuilder) Insert(filename string, id *Oid, filemode int) (error) { cfilename := C.CString(filename) defer C.free(unsafe.Pointer(cfilename)) + runtime.LockOSThread() + defer runtime.UnlockOSThread() + err := C.git_treebuilder_insert(nil, v.ptr, cfilename, id.toC(), C.git_filemode_t(filemode)) if err < 0 { return LastError() @@ -133,6 +142,10 @@ func (v *TreeBuilder) Insert(filename string, id *Oid, filemode int) (error) { func (v *TreeBuilder) Write() (*Oid, error) { oid := new(Oid) + + runtime.LockOSThread() + defer runtime.UnlockOSThread() + err := C.git_treebuilder_write(oid.toC(), v.repo.ptr, v.ptr) if err < 0 { diff --git a/walk.go b/walk.go index 9fc4094..6979b6b 100644 --- a/walk.go +++ b/walk.go @@ -8,6 +8,7 @@ import "C" import ( "io" + "runtime" ) // RevWalk @@ -34,6 +35,9 @@ func (v *RevWalk) Push(id *Oid) { } func (v *RevWalk) PushHead() (err error) { + runtime.LockOSThread() + defer runtime.UnlockOSThread() + ecode := C.git_revwalk_push_head(v.ptr) if ecode < 0 { err = LastError() @@ -43,6 +47,9 @@ func (v *RevWalk) PushHead() (err error) { } func (v *RevWalk) Next(oid *Oid) (err error) { + runtime.LockOSThread() + defer runtime.UnlockOSThread() + ret := C.git_revwalk_next(oid.toC(), v.ptr) switch { case ret == ITEROVER: -- 2.45.2 From f66502aaf44862a8671285e80327d808afee155f Mon Sep 17 00:00:00 2001 From: Aidan Nulman Date: Wed, 29 Jan 2014 18:01:26 -0500 Subject: [PATCH 4/4] update git2go to support latest libgit2 development commit (id: 66af84) --- git.go | 20 ++++++++++---------- reference.go | 22 ++++++++++++++++++---- reference_test.go | 17 +++++++++++------ repository.go | 29 ++++++++++++++++++++++------- submodule.go | 12 ++++++++++-- 5 files changed, 71 insertions(+), 29 deletions(-) diff --git a/git.go b/git.go index 6f81293..c663d2e 100644 --- a/git.go +++ b/git.go @@ -9,8 +9,8 @@ import "C" import ( "bytes" "errors" - "unsafe" "strings" + "unsafe" ) const ( @@ -89,7 +89,7 @@ func (oid *Oid) Equal(oid2 *Oid) bool { } func (oid *Oid) IsZero() bool { - for _, a := range(oid.bytes) { + for _, a := range oid.bytes { if a != '0' { return false } @@ -123,10 +123,10 @@ func ShortenOids(ids []*Oid, minlen int) (int, error) { type GitError struct { Message string - Code int + Code int } -func (e GitError) Error() string{ +func (e GitError) Error() string { return e.Message } @@ -139,14 +139,14 @@ func LastError() error { } func cbool(b bool) C.int { - if (b) { + if b { return C.int(1) } return C.int(0) } func ucbool(b bool) C.uint { - if (b) { + if b { return C.uint(1) } return C.uint(0) @@ -159,13 +159,13 @@ func Discover(start string, across_fs bool, ceiling_dirs []string) (string, erro cstart := C.CString(start) defer C.free(unsafe.Pointer(cstart)) - retpath := (*C.char)(C.malloc(C.GIT_PATH_MAX)) - defer C.free(unsafe.Pointer(retpath)) + retpath := (*C.git_buf)(C.malloc(C.GIT_PATH_MAX)) + defer C.git_buf_free(retpath) - r := C.git_repository_discover(retpath, C.GIT_PATH_MAX, cstart, cbool(across_fs), ceildirs) + r := C.git_repository_discover(retpath, cstart, cbool(across_fs), ceildirs) if r == 0 { - return C.GoString(retpath), nil + return C.GoString(retpath.ptr), nil } return "", LastError() diff --git a/reference.go b/reference.go index 525e092..8060146 100644 --- a/reference.go +++ b/reference.go @@ -11,6 +11,7 @@ import ( ) type ReferenceType int + const ( ReferenceSymbolic ReferenceType = C.GIT_REF_SYMBOLIC ReferenceOid = C.GIT_REF_OID @@ -27,12 +28,19 @@ func newReferenceFromC(ptr *C.git_reference) *Reference { return ref } -func (v *Reference) SetSymbolicTarget(target string) (*Reference, error) { +func (v *Reference) SetSymbolicTarget(target string, sig *Signature, msg string) (*Reference, error) { var ptr *C.git_reference + ctarget := C.CString(target) defer C.free(unsafe.Pointer(ctarget)) - ret := C.git_reference_symbolic_set_target(&ptr, v.ptr, ctarget) + csig := sig.toC() + defer C.free(unsafe.Pointer(csig)) + + cmsg := C.CString(msg) + defer C.free(unsafe.Pointer(cmsg)) + + ret := C.git_reference_symbolic_set_target(&ptr, v.ptr, ctarget, csig, cmsg) if ret < 0 { return nil, LastError() } @@ -40,10 +48,16 @@ func (v *Reference) SetSymbolicTarget(target string) (*Reference, error) { return newReferenceFromC(ptr), nil } -func (v *Reference) SetTarget(target *Oid) (*Reference, error) { +func (v *Reference) SetTarget(target *Oid, sig *Signature, msg string) (*Reference, error) { var ptr *C.git_reference - ret := C.git_reference_set_target(&ptr, v.ptr, target.toC()) + csig := sig.toC() + defer C.free(unsafe.Pointer(csig)) + + cmsg := C.CString(msg) + defer C.free(unsafe.Pointer(cmsg)) + + ret := C.git_reference_set_target(&ptr, v.ptr, target.toC(), csig, cmsg) if ret < 0 { return nil, LastError() } diff --git a/reference_test.go b/reference_test.go index f955a2c..ab62522 100644 --- a/reference_test.go +++ b/reference_test.go @@ -14,7 +14,14 @@ func TestRefModification(t *testing.T) { commitId, treeId := seedTestRepo(t, repo) - _, err := repo.CreateReference("refs/tags/tree", treeId, true) + loc, err := time.LoadLocation("Europe/Berlin") + checkFatal(t, err) + sig := &Signature{ + Name: "Rand Om Hacker", + Email: "random@hacker.com", + When: time.Date(2013, 03, 06, 14, 30, 0, 0, loc), + } + _, err = repo.CreateReference("refs/tags/tree", treeId, true, sig, "testTreeTag") checkFatal(t, err) tag, err := repo.LookupReference("refs/tags/tree") @@ -78,13 +85,13 @@ func TestIterator(t *testing.T) { commitId, err := repo.CreateCommit("HEAD", sig, sig, message, tree) checkFatal(t, err) - _, err = repo.CreateReference("refs/heads/one", commitId, true) + _, err = repo.CreateReference("refs/heads/one", commitId, true, sig, "headOne") checkFatal(t, err) - _, err = repo.CreateReference("refs/heads/two", commitId, true) + _, err = repo.CreateReference("refs/heads/two", commitId, true, sig, "headTwo") checkFatal(t, err) - _, err = repo.CreateReference("refs/heads/three", commitId, true) + _, err = repo.CreateReference("refs/heads/three", commitId, true, sig, "headThree") checkFatal(t, err) iter, err := repo.NewReferenceIterator() @@ -108,7 +115,6 @@ func TestIterator(t *testing.T) { t.Fatal("Iteration not over") } - sort.Strings(list) compareStringList(t, expected, list) @@ -129,7 +135,6 @@ func TestIterator(t *testing.T) { t.Fatalf("Wrong number of references returned %v", count) } - // test the channel iteration list = []string{} iter, err = repo.NewReferenceIterator() diff --git a/repository.go b/repository.go index 34df0aa..31a2d05 100644 --- a/repository.go +++ b/repository.go @@ -6,8 +6,8 @@ package git */ import "C" import ( - "unsafe" "runtime" + "unsafe" ) // Repository @@ -125,12 +125,19 @@ func (v *Repository) LookupReference(name string) (*Reference, error) { return newReferenceFromC(ptr), nil } -func (v *Repository) CreateReference(name string, oid *Oid, force bool) (*Reference, error) { +func (v *Repository) CreateReference(name string, oid *Oid, force bool, sig *Signature, msg string) (*Reference, error) { cname := C.CString(name) defer C.free(unsafe.Pointer(cname)) + + csig := sig.toC() + defer C.free(unsafe.Pointer(csig)) + + cmsg := C.CString(msg) + defer C.free(unsafe.Pointer(cmsg)) + var ptr *C.git_reference - ecode := C.git_reference_create(&ptr, v.ptr, cname, oid.toC(), cbool(force)) + ecode := C.git_reference_create(&ptr, v.ptr, cname, oid.toC(), cbool(force), csig, cmsg) if ecode < 0 { return nil, LastError() } @@ -138,14 +145,22 @@ func (v *Repository) CreateReference(name string, oid *Oid, force bool) (*Refere return newReferenceFromC(ptr), nil } -func (v *Repository) CreateSymbolicReference(name, target string, force bool) (*Reference, error) { +func (v *Repository) CreateSymbolicReference(name, target string, force bool, sig *Signature, msg string) (*Reference, error) { cname := C.CString(name) defer C.free(unsafe.Pointer(cname)) + ctarget := C.CString(target) defer C.free(unsafe.Pointer(ctarget)) + + csig := sig.toC() + defer C.free(unsafe.Pointer(csig)) + + cmsg := C.CString(msg) + defer C.free(unsafe.Pointer(cmsg)) + var ptr *C.git_reference - ecode := C.git_reference_symbolic_create(&ptr, v.ptr, cname, ctarget, cbool(force)) + ecode := C.git_reference_symbolic_create(&ptr, v.ptr, cname, ctarget, cbool(force), csig, cmsg) if ecode < 0 { return nil, LastError() } @@ -180,7 +195,7 @@ func (v *Repository) CreateCommit( var cparents []*C.git_commit = nil var parentsarg **C.git_commit = nil - nparents:= len(parents) + nparents := len(parents) if nparents > 0 { cparents = make([]*C.git_commit, nparents) for i, v := range parents { @@ -226,7 +241,7 @@ func (repo *Repository) Path() string { return C.GoString(C.git_repository_path(repo.ptr)) } -func (repo *Repository) IsBare() (bool) { +func (repo *Repository) IsBare() bool { return C.git_repository_is_bare(repo.ptr) != 0 } diff --git a/submodule.go b/submodule.go index 9819b87..903d784 100644 --- a/submodule.go +++ b/submodule.go @@ -55,6 +55,14 @@ const ( SubmoduleStatusWdUntracked = C.GIT_SUBMODULE_STATUS_WD_UNTRACKED ) +type SubmoduleRecurse int + +const ( + SubmoduleRecurseNo SubmoduleRecurse = C.GIT_SUBMODULE_RECURSE_NO + SubmoduleRecurseYes = C.GIT_SUBMODULE_RECURSE_YES + SubmoduleRecurseOndemand = C.GIT_SUBMODULE_RECURSE_ONDEMAND +) + func SubmoduleStatusIsUnmodified(status int) bool { o := SubmoduleStatus(status) & ^(SubmoduleStatusInHead | SubmoduleStatusInIndex | SubmoduleStatusInConfig | SubmoduleStatusInWd) @@ -212,8 +220,8 @@ func (sub *Submodule) FetchRecurseSubmodules() bool { return true } -func (sub *Submodule) SetFetchRecurseSubmodules(v bool) error { - ret := C.git_submodule_set_fetch_recurse_submodules(sub.ptr, cbool(v)) +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() } -- 2.45.2