Skip to content

Commit

Permalink
Third round of keep-alive aditions
Browse files Browse the repository at this point in the history
  • Loading branch information
carlosmn committed Jul 8, 2017
1 parent 7d29d68 commit 55a1096
Show file tree
Hide file tree
Showing 17 changed files with 353 additions and 122 deletions.
66 changes: 40 additions & 26 deletions merge.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,11 @@ import (

type AnnotatedCommit struct {
ptr *C.git_annotated_commit
r *Repository
}

func newAnnotatedCommitFromC(c *C.git_annotated_commit) *AnnotatedCommit {
mh := &AnnotatedCommit{ptr: c}
func newAnnotatedCommitFromC(ptr *C.git_annotated_commit, r *Repository) *AnnotatedCommit {
mh := &AnnotatedCommit{ptr: ptr, r: r}
runtime.SetFinalizer(mh, (*AnnotatedCommit).Free)
return mh
}
Expand All @@ -32,8 +33,6 @@ func (mh *AnnotatedCommit) Free() {
}

func (r *Repository) AnnotatedCommitFromFetchHead(branchName string, remoteURL string, oid *Oid) (*AnnotatedCommit, error) {
mh := &AnnotatedCommit{}

cbranchName := C.CString(branchName)
defer C.free(unsafe.Pointer(cbranchName))

Expand All @@ -43,40 +42,41 @@ func (r *Repository) AnnotatedCommitFromFetchHead(branchName string, remoteURL s
runtime.LockOSThread()
defer runtime.UnlockOSThread()

ret := C.git_annotated_commit_from_fetchhead(&mh.ptr, r.ptr, cbranchName, cremoteURL, oid.toC())
var ptr *C.git_annotated_commit
ret := C.git_annotated_commit_from_fetchhead(&ptr, r.ptr, cbranchName, cremoteURL, oid.toC())
runtime.KeepAlive(oid)
if ret < 0 {
return nil, MakeGitError(ret)
}
runtime.SetFinalizer(mh, (*AnnotatedCommit).Free)
return mh, nil

return newAnnotatedCommitFromC(ptr, r), nil
}

func (r *Repository) LookupAnnotatedCommit(oid *Oid) (*AnnotatedCommit, error) {
mh := &AnnotatedCommit{}

runtime.LockOSThread()
defer runtime.UnlockOSThread()

ret := C.git_annotated_commit_lookup(&mh.ptr, r.ptr, oid.toC())
var ptr *C.git_annotated_commit
ret := C.git_annotated_commit_lookup(&ptr, r.ptr, oid.toC())
runtime.KeepAlive(oid)
if ret < 0 {
return nil, MakeGitError(ret)
}
runtime.SetFinalizer(mh, (*AnnotatedCommit).Free)
return mh, nil
return newAnnotatedCommitFromC(ptr, r), nil
}

func (r *Repository) AnnotatedCommitFromRef(ref *Reference) (*AnnotatedCommit, error) {
mh := &AnnotatedCommit{}

runtime.LockOSThread()
defer runtime.UnlockOSThread()

ret := C.git_annotated_commit_from_ref(&mh.ptr, r.ptr, ref.ptr)
var ptr *C.git_annotated_commit
ret := C.git_annotated_commit_from_ref(&ptr, r.ptr, ref.ptr)
runtime.KeepAlive(r)
runtime.KeepAlive(ref)
if ret < 0 {
return nil, MakeGitError(ret)
}
runtime.SetFinalizer(mh, (*AnnotatedCommit).Free)
return mh, nil
return newAnnotatedCommitFromC(ptr, r), nil
}

type MergeTreeFlag int
Expand Down Expand Up @@ -162,6 +162,7 @@ func (r *Repository) Merge(theirHeads []*AnnotatedCommit, mergeOptions *MergeOpt
}
ptr := unsafe.Pointer(&gmerge_head_array[0])
err := C.git_merge(r.ptr, (**C.git_annotated_commit)(ptr), C.size_t(len(theirHeads)), cMergeOpts, cCheckoutOpts)
runtime.KeepAlive(theirHeads)
if err < 0 {
return MakeGitError(err)
}
Expand Down Expand Up @@ -201,6 +202,7 @@ func (r *Repository) MergeAnalysis(theirHeads []*AnnotatedCommit) (MergeAnalysis
var analysis C.git_merge_analysis_t
var preference C.git_merge_preference_t
err := C.git_merge_analysis(&analysis, &preference, r.ptr, (**C.git_annotated_commit)(ptr), C.size_t(len(theirHeads)))
runtime.KeepAlive(theirHeads)
if err < 0 {
return MergeAnalysisNone, MergePreferenceNone, MakeGitError(err)
}
Expand All @@ -214,14 +216,15 @@ func (r *Repository) MergeCommits(ours *Commit, theirs *Commit, options *MergeOp

copts := options.toC()

idx := &Index{}

ret := C.git_merge_commits(&idx.ptr, r.ptr, ours.cast_ptr, theirs.cast_ptr, copts)
var ptr *C.git_index
ret := C.git_merge_commits(&ptr, r.ptr, ours.cast_ptr, theirs.cast_ptr, copts)
runtime.KeepAlive(ours)
runtime.KeepAlive(theirs)
if ret < 0 {
return nil, MakeGitError(ret)
}
runtime.SetFinalizer(idx, (*Index).Free)
return idx, nil

return newIndexFromC(ptr, r), nil
}

func (r *Repository) MergeTrees(ancestor *Tree, ours *Tree, theirs *Tree, options *MergeOptions) (*Index, error) {
Expand All @@ -230,17 +233,20 @@ func (r *Repository) MergeTrees(ancestor *Tree, ours *Tree, theirs *Tree, option

copts := options.toC()

idx := &Index{}
var ancestor_ptr *C.git_tree
if ancestor != nil {
ancestor_ptr = ancestor.cast_ptr
}
ret := C.git_merge_trees(&idx.ptr, r.ptr, ancestor_ptr, ours.cast_ptr, theirs.cast_ptr, copts)
var ptr *C.git_index
ret := C.git_merge_trees(&ptr, r.ptr, ancestor_ptr, ours.cast_ptr, theirs.cast_ptr, copts)
runtime.KeepAlive(ancestor)
runtime.KeepAlive(ours)
runtime.KeepAlive(theirs)
if ret < 0 {
return nil, MakeGitError(ret)
}
runtime.SetFinalizer(idx, (*Index).Free)
return idx, nil

return newIndexFromC(ptr, r), nil
}

func (r *Repository) MergeBase(one *Oid, two *Oid) (*Oid, error) {
Expand All @@ -249,6 +255,9 @@ func (r *Repository) MergeBase(one *Oid, two *Oid) (*Oid, error) {

var oid C.git_oid
ret := C.git_merge_base(&oid, r.ptr, one.toC(), two.toC())
runtime.KeepAlive(one)
runtime.KeepAlive(two)
runtime.KeepAlive(r)
if ret < 0 {
return nil, MakeGitError(ret)
}
Expand All @@ -265,6 +274,8 @@ func (r *Repository) MergeBases(one, two *Oid) ([]*Oid, error) {

var coids C.git_oidarray
ret := C.git_merge_bases(&coids, r.ptr, one.toC(), two.toC())
runtime.KeepAlive(one)
runtime.KeepAlive(two)
if ret < 0 {
return make([]*Oid, 0), MakeGitError(ret)
}
Expand Down Expand Up @@ -413,6 +424,9 @@ func MergeFile(ancestor MergeFileInput, ours MergeFileInput, theirs MergeFileInp
(*C.char)(unsafe.Pointer(oursContents)), C.size_t(len(ours.Contents)), oursPath, C.uint(ours.Mode),
(*C.char)(unsafe.Pointer(theirsContents)), C.size_t(len(theirs.Contents)), theirsPath, C.uint(theirs.Mode),
copts)
runtime.KeepAlive(ancestor)
runtime.KeepAlive(ours)
runtime.KeepAlive(theirs)
if ecode < 0 {
return nil, MakeGitError(ecode)
}
Expand Down
48 changes: 36 additions & 12 deletions note.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ func (c *NoteCollection) Create(
ret := C.git_note_create(
oid.toC(), c.repo.ptr, cref, authorSig,
committerSig, id.toC(), cnote, cbool(force))

runtime.KeepAlive(c)
runtime.KeepAlive(id)
if ret < 0 {
return nil, MakeGitError(ret)
}
Expand All @@ -69,17 +70,18 @@ func (c *NoteCollection) Read(ref string, id *Oid) (*Note, error) {
defer C.free(unsafe.Pointer(cref))
}

note := new(Note)

runtime.LockOSThread()
defer runtime.UnlockOSThread()

if ret := C.git_note_read(&note.ptr, c.repo.ptr, cref, id.toC()); ret < 0 {
var ptr *C.git_note
ret := C.git_note_read(&ptr, c.repo.ptr, cref, id.toC())
runtime.KeepAlive(c)
runtime.KeepAlive(id)
if ret < 0 {
return nil, MakeGitError(ret)
}

runtime.SetFinalizer(note, (*Note).Free)
return note, nil
return newNoteFromC(ptr, c.repo), nil
}

// Remove removes the note for an object
Expand Down Expand Up @@ -108,6 +110,8 @@ func (c *NoteCollection) Remove(ref string, author, committer *Signature, id *Oi
defer runtime.UnlockOSThread()

ret := C.git_note_remove(c.repo.ptr, cref, authorSig, committerSig, id.toC())
runtime.KeepAlive(c)
runtime.KeepAlive(id)
if ret < 0 {
return MakeGitError(ret)
}
Expand All @@ -121,8 +125,10 @@ func (c *NoteCollection) DefaultRef() (string, error) {
runtime.LockOSThread()
defer runtime.UnlockOSThread()

if ret := C.git_note_default_ref(&buf, c.repo.ptr); ret < 0 {
return "", MakeGitError(ret)
ecode := C.git_note_default_ref(&buf, c.repo.ptr)
runtime.KeepAlive(c)
if ecode < 0 {
return "", MakeGitError(ecode)
}

ret := C.GoString(buf.ptr)
Expand All @@ -134,6 +140,13 @@ func (c *NoteCollection) DefaultRef() (string, error) {
// Note
type Note struct {
ptr *C.git_note
r *Repository
}

func newNoteFromC(ptr *C.git_note, r *Repository) *Note {
note := &Note{ptr: ptr, r: r}
runtime.SetFinalizer(note, (*Note).Free)
return note
}

// Free frees a git_note object
Expand All @@ -156,23 +169,28 @@ func (n *Note) Author() *Signature {
// Id returns the note object's id
func (n *Note) Id() *Oid {
ptr := C.git_note_id(n.ptr)
runtime.KeepAlive(n)
return newOidFromC(ptr)
}

// Committer returns the signature of the note committer
func (n *Note) Committer() *Signature {
ptr := C.git_note_committer(n.ptr)
runtime.KeepAlive(n)
return newSignatureFromC(ptr)
}

// Message returns the note message
func (n *Note) Message() string {
return C.GoString(C.git_note_message(n.ptr))
ret := C.GoString(C.git_note_message(n.ptr))
runtime.KeepAlive(n)
return ret
}

// NoteIterator
type NoteIterator struct {
ptr *C.git_note_iterator
r *Repository
}

// NewNoteIterator creates a new iterator for notes
Expand All @@ -190,11 +208,13 @@ func (repo *Repository) NewNoteIterator(ref string) (*NoteIterator, error) {
runtime.LockOSThread()
defer runtime.UnlockOSThread()

if ret := C.git_note_iterator_new(&ptr, repo.ptr, cref); ret < 0 {
ret := C.git_note_iterator_new(&ptr, repo.ptr, cref)
runtime.KeepAlive(repo)
if ret < 0 {
return nil, MakeGitError(ret)
}

iter := &NoteIterator{ptr: ptr}
iter := &NoteIterator{ptr: ptr, r: repo}
runtime.SetFinalizer(iter, (*NoteIterator).Free)
return iter, nil
}
Expand All @@ -213,7 +233,11 @@ func (it *NoteIterator) Next() (noteId, annotatedId *Oid, err error) {
runtime.LockOSThread()
defer runtime.UnlockOSThread()

if ret := C.git_note_next(noteId.toC(), annotatedId.toC(), it.ptr); ret < 0 {
ret := C.git_note_next(noteId.toC(), annotatedId.toC(), it.ptr)
runtime.KeepAlive(noteId)
runtime.KeepAlive(annotatedId)
runtime.KeepAlive(it)
if ret < 0 {
err = MakeGitError(ret)
}
return
Expand Down
21 changes: 16 additions & 5 deletions object.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,9 @@ func (t ObjectType) String() string {
}

func (o *Object) Id() *Oid {
return newOidFromC(C.git_object_id(o.ptr))
ret := newOidFromC(C.git_object_id(o.ptr))
runtime.KeepAlive(o)
return ret
}

func (o *Object) ShortId() (string, error) {
Expand All @@ -56,6 +58,7 @@ func (o *Object) ShortId() (string, error) {
defer runtime.UnlockOSThread()

ecode := C.git_object_short_id(&resultBuf, o.ptr)
runtime.KeepAlive(o)
if ecode < 0 {
return "", MakeGitError(ecode)
}
Expand All @@ -64,15 +67,19 @@ func (o *Object) ShortId() (string, error) {
}

func (o *Object) Type() ObjectType {
return ObjectType(C.git_object_type(o.ptr))
ret := ObjectType(C.git_object_type(o.ptr))
runtime.KeepAlive(o)
return ret
}

// Owner returns a weak reference to the repository which owns this
// object. This won't keep the underlying repository alive.
func (o *Object) Owner() *Repository {
return &Repository{
ret := &Repository{
ptr: C.git_object_owner(o.ptr),
}
runtime.KeepAlive(o)
return ret
}

func dupObject(obj *Object, kind ObjectType) (*C.git_object, error) {
Expand All @@ -85,7 +92,9 @@ func dupObject(obj *Object, kind ObjectType) (*C.git_object, error) {
runtime.LockOSThread()
defer runtime.UnlockOSThread()

if err := C.git_object_dup(&cobj, obj.ptr); err < 0 {
err := C.git_object_dup(&cobj, obj.ptr)
runtime.KeepAlive(obj)
if err < 0 {
return nil, MakeGitError(err)
}

Expand Down Expand Up @@ -203,7 +212,9 @@ func (o *Object) Peel(t ObjectType) (*Object, error) {
runtime.LockOSThread()
defer runtime.UnlockOSThread()

if err := C.git_object_peel(&cobj, o.ptr, C.git_otype(t)); err < 0 {
err := C.git_object_peel(&cobj, o.ptr, C.git_otype(t))
runtime.KeepAlive(o)
if err < 0 {
return nil, MakeGitError(err)
}

Expand Down
Loading

0 comments on commit 55a1096

Please sign in to comment.