Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

KeepAlive all the things #393

Merged
merged 5 commits into from
Jul 8, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion blame.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ func (v *Repository) BlameFile(path string, opts *BlameOptions) (*Blame, error)
defer runtime.UnlockOSThread()

ecode := C.git_blame_file(&blamePtr, v.ptr, cpath, copts)
runtime.KeepAlive(v)
if ecode < 0 {
return nil, MakeGitError(ecode)
}
Expand All @@ -88,11 +89,15 @@ type Blame struct {
}

func (blame *Blame) HunkCount() int {
return int(C.git_blame_get_hunk_count(blame.ptr))
ret := int(C.git_blame_get_hunk_count(blame.ptr))
runtime.KeepAlive(blame)

return ret
}

func (blame *Blame) HunkByIndex(index int) (BlameHunk, error) {
ptr := C.git_blame_get_hunk_byindex(blame.ptr, C.uint32_t(index))
runtime.KeepAlive(blame)
if ptr == nil {
return BlameHunk{}, ErrInvalid
}
Expand All @@ -101,6 +106,7 @@ func (blame *Blame) HunkByIndex(index int) (BlameHunk, error) {

func (blame *Blame) HunkByLine(lineno int) (BlameHunk, error) {
ptr := C.git_blame_get_hunk_byline(blame.ptr, C.size_t(lineno))
runtime.KeepAlive(blame)
if ptr == nil {
return BlameHunk{}, ErrInvalid
}
Expand Down
23 changes: 17 additions & 6 deletions blob.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,19 @@ type Blob struct {
}

func (v *Blob) Size() int64 {
return int64(C.git_blob_rawsize(v.cast_ptr))
ret := int64(C.git_blob_rawsize(v.cast_ptr))
runtime.KeepAlive(v)
return ret
}

func (v *Blob) Contents() []byte {
size := C.int(C.git_blob_rawsize(v.cast_ptr))
buffer := unsafe.Pointer(C.git_blob_rawcontent(v.cast_ptr))
return C.GoBytes(buffer, size)

goBytes := C.GoBytes(buffer, size)
runtime.KeepAlive(v)

return goBytes
}

func (repo *Repository) CreateBlobFromBuffer(data []byte) (*Oid, error) {
Expand All @@ -53,6 +59,7 @@ func (repo *Repository) CreateBlobFromBuffer(data []byte) (*Oid, error) {
}

ecode := C.git_blob_create_frombuffer(&id, repo.ptr, unsafe.Pointer(&data[0]), size)
runtime.KeepAlive(repo)
if ecode < 0 {
return nil, MakeGitError(ecode)
}
Expand Down Expand Up @@ -102,16 +109,18 @@ func (repo *Repository) CreateFromStream(hintPath string) (*BlobWriteStream, err
return nil, MakeGitError(ecode)
}

return newBlobWriteStreamFromC(stream), nil
return newBlobWriteStreamFromC(stream, repo), nil
}

type BlobWriteStream struct {
ptr *C.git_writestream
ptr *C.git_writestream
repo *Repository
}

func newBlobWriteStreamFromC(ptr *C.git_writestream) *BlobWriteStream {
func newBlobWriteStreamFromC(ptr *C.git_writestream, repo *Repository) *BlobWriteStream {
stream := &BlobWriteStream{
ptr: ptr,
ptr: ptr,
repo: repo,
}

runtime.SetFinalizer(stream, (*BlobWriteStream).Free)
Expand All @@ -128,6 +137,7 @@ func (stream *BlobWriteStream) Write(p []byte) (int, error) {
defer runtime.UnlockOSThread()

ecode := C._go_git_writestream_write(stream.ptr, ptr, size)
runtime.KeepAlive(stream)
if ecode < 0 {
return 0, MakeGitError(ecode)
}
Expand All @@ -147,6 +157,7 @@ func (stream *BlobWriteStream) Commit() (*Oid, error) {
defer runtime.UnlockOSThread()

ecode := C.git_blob_create_fromstream_commit(&oid, stream.ptr)
runtime.KeepAlive(stream)
if ecode < 0 {
return nil, MakeGitError(ecode)
}
Expand Down
12 changes: 12 additions & 0 deletions branch.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ func (repo *Repository) NewBranchIterator(flags BranchType) (*BranchIterator, er
defer runtime.UnlockOSThread()

ecode := C.git_branch_iterator_new(&ptr, repo.ptr, refType)
runtime.KeepAlive(repo)
if ecode < 0 {
return nil, MakeGitError(ecode)
}
Expand All @@ -106,6 +107,8 @@ func (repo *Repository) CreateBranch(branchName string, target *Commit, force bo
defer runtime.UnlockOSThread()

ret := C.git_branch_create(&ptr, repo.ptr, cBranchName, target.cast_ptr, cForce)
runtime.KeepAlive(repo)
runtime.KeepAlive(target)
if ret < 0 {
return nil, MakeGitError(ret)
}
Expand All @@ -117,6 +120,7 @@ func (b *Branch) Delete() error {
runtime.LockOSThread()
defer runtime.UnlockOSThread()
ret := C.git_branch_delete(b.Reference.ptr)
runtime.KeepAlive(b.Reference)
if ret < 0 {
return MakeGitError(ret)
}
Expand All @@ -133,6 +137,7 @@ func (b *Branch) Move(newBranchName string, force bool) (*Branch, error) {
defer runtime.UnlockOSThread()

ret := C.git_branch_move(&ptr, b.Reference.ptr, cNewBranchName, cForce)
runtime.KeepAlive(b.Reference)
if ret < 0 {
return nil, MakeGitError(ret)
}
Expand All @@ -145,6 +150,7 @@ func (b *Branch) IsHead() (bool, error) {
defer runtime.UnlockOSThread()

ret := C.git_branch_is_head(b.Reference.ptr)
runtime.KeepAlive(b.Reference)
switch ret {
case 1:
return true, nil
Expand All @@ -165,6 +171,7 @@ func (repo *Repository) LookupBranch(branchName string, bt BranchType) (*Branch,
defer runtime.UnlockOSThread()

ret := C.git_branch_lookup(&ptr, repo.ptr, cName, C.git_branch_t(bt))
runtime.KeepAlive(repo)
if ret < 0 {
return nil, MakeGitError(ret)
}
Expand All @@ -179,6 +186,7 @@ func (b *Branch) Name() (string, error) {
defer runtime.UnlockOSThread()

ret := C.git_branch_name(&cName, b.Reference.ptr)
runtime.KeepAlive(b.Reference)
if ret < 0 {
return "", MakeGitError(ret)
}
Expand All @@ -196,6 +204,7 @@ func (repo *Repository) RemoteName(canonicalBranchName string) (string, error) {
defer runtime.UnlockOSThread()

ret := C.git_branch_remote_name(&nameBuf, repo.ptr, cName)
runtime.KeepAlive(repo)
if ret < 0 {
return "", MakeGitError(ret)
}
Expand All @@ -212,6 +221,7 @@ func (b *Branch) SetUpstream(upstreamName string) error {
defer runtime.UnlockOSThread()

ret := C.git_branch_set_upstream(b.Reference.ptr, cName)
runtime.KeepAlive(b.Reference)
if ret < 0 {
return MakeGitError(ret)
}
Expand All @@ -225,6 +235,7 @@ func (b *Branch) Upstream() (*Reference, error) {
defer runtime.UnlockOSThread()

ret := C.git_branch_upstream(&ptr, b.Reference.ptr)
runtime.KeepAlive(b.Reference)
if ret < 0 {
return nil, MakeGitError(ret)
}
Expand All @@ -241,6 +252,7 @@ func (repo *Repository) UpstreamName(canonicalBranchName string) (string, error)
defer runtime.UnlockOSThread()

ret := C.git_branch_upstream_name(&nameBuf, repo.ptr, cName)
runtime.KeepAlive(repo)
if ret < 0 {
return "", MakeGitError(ret)
}
Expand Down
3 changes: 3 additions & 0 deletions checkout.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,7 @@ func (v *Repository) CheckoutHead(opts *CheckoutOpts) error {
defer freeCheckoutOpts(cOpts)

ret := C.git_checkout_head(v.ptr, cOpts)
runtime.KeepAlive(v)
if ret < 0 {
return MakeGitError(ret)
}
Expand All @@ -211,6 +212,7 @@ func (v *Repository) CheckoutIndex(index *Index, opts *CheckoutOpts) error {
defer freeCheckoutOpts(cOpts)

ret := C.git_checkout_index(v.ptr, iptr, cOpts)
runtime.KeepAlive(v)
if ret < 0 {
return MakeGitError(ret)
}
Expand All @@ -226,6 +228,7 @@ func (v *Repository) CheckoutTree(tree *Tree, opts *CheckoutOpts) error {
defer freeCheckoutOpts(cOpts)

ret := C.git_checkout_tree(v.ptr, tree.ptr, cOpts)
runtime.KeepAlive(v)
if ret < 0 {
return MakeGitError(ret)
}
Expand Down
2 changes: 2 additions & 0 deletions cherrypick.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ func (v *Repository) Cherrypick(commit *Commit, opts CherrypickOptions) error {
defer freeCherrypickOpts(cOpts)

ecode := C.git_cherrypick(v.ptr, commit.cast_ptr, cOpts)
runtime.KeepAlive(v)
runtime.KeepAlive(commit)
if ecode < 0 {
return MakeGitError(ecode)
}
Expand Down
66 changes: 45 additions & 21 deletions commit.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,19 @@ type Commit struct {
cast_ptr *C.git_commit
}

func (c Commit) Message() string {
return C.GoString(C.git_commit_message(c.cast_ptr))
func (c *Commit) Message() string {
ret := C.GoString(C.git_commit_message(c.cast_ptr))
runtime.KeepAlive(c)
return ret
}

func (c Commit) RawMessage() string {
return C.GoString(C.git_commit_message_raw(c.cast_ptr))
func (c *Commit) RawMessage() string {
ret := C.GoString(C.git_commit_message_raw(c.cast_ptr))
runtime.KeepAlive(c)
return ret
}

func (c Commit) ExtractSignature() (string, string, error) {
func (c *Commit) ExtractSignature() (string, string, error) {

var c_signed C.git_buf
defer C.git_buf_free(&c_signed)
Expand All @@ -35,48 +39,59 @@ func (c Commit) ExtractSignature() (string, string, error) {
defer C.git_buf_free(&c_signature)

oid := c.Id()

repo := C.git_commit_owner(c.cast_ptr)

runtime.LockOSThread()
defer runtime.UnlockOSThread()
ret := C.git_commit_extract_signature(&c_signature, &c_signed, repo, oid.toC(), nil)

runtime.KeepAlive(oid)
if ret < 0 {
return "", "", MakeGitError(ret)
return "", "", MakeGitError(ret)
} else {
return C.GoString(c_signature.ptr), C.GoString(c_signed.ptr), nil
}

}

func (c Commit) Summary() string {
return C.GoString(C.git_commit_summary(c.cast_ptr))
func (c *Commit) Summary() string {
ret := C.GoString(C.git_commit_summary(c.cast_ptr))
runtime.KeepAlive(c)
return ret
}

func (c Commit) Tree() (*Tree, error) {
func (c *Commit) Tree() (*Tree, error) {
var ptr *C.git_tree

runtime.LockOSThread()
defer runtime.UnlockOSThread()

err := C.git_commit_tree(&ptr, c.cast_ptr)
runtime.KeepAlive(c)
if err < 0 {
return nil, MakeGitError(err)
}

return allocTree(ptr, c.repo), nil
}

func (c Commit) TreeId() *Oid {
return newOidFromC(C.git_commit_tree_id(c.cast_ptr))
func (c *Commit) TreeId() *Oid {
ret := newOidFromC(C.git_commit_tree_id(c.cast_ptr))
runtime.KeepAlive(c)
return ret
}

func (c Commit) Author() *Signature {
func (c *Commit) Author() *Signature {
cast_ptr := C.git_commit_author(c.cast_ptr)
return newSignatureFromC(cast_ptr)
ret := newSignatureFromC(cast_ptr)
runtime.KeepAlive(c)
return ret
}

func (c Commit) Committer() *Signature {
func (c *Commit) Committer() *Signature {
cast_ptr := C.git_commit_committer(c.cast_ptr)
return newSignatureFromC(cast_ptr)
ret := newSignatureFromC(cast_ptr)
runtime.KeepAlive(c)
return ret
}

func (c *Commit) Parent(n uint) *Commit {
Expand All @@ -86,15 +101,21 @@ func (c *Commit) Parent(n uint) *Commit {
return nil
}

return allocCommit(cobj, c.repo)
parent := allocCommit(cobj, c.repo)
runtime.KeepAlive(c)
return parent
}

func (c *Commit) ParentId(n uint) *Oid {
return newOidFromC(C.git_commit_parent_id(c.cast_ptr, C.uint(n)))
ret := newOidFromC(C.git_commit_parent_id(c.cast_ptr, C.uint(n)))
runtime.KeepAlive(c)
return ret
}

func (c *Commit) ParentCount() uint {
return uint(C.git_commit_parentcount(c.cast_ptr))
ret := uint(C.git_commit_parentcount(c.cast_ptr))
runtime.KeepAlive(c)
return ret
}

func (c *Commit) Amend(refname string, author, committer *Signature, message string, tree *Tree) (*Oid, error) {
Expand Down Expand Up @@ -127,6 +148,9 @@ func (c *Commit) Amend(refname string, author, committer *Signature, message str
oid := new(Oid)

cerr := C.git_commit_amend(oid.toC(), c.cast_ptr, cref, authorSig, committerSig, nil, cmsg, tree.cast_ptr)
runtime.KeepAlive(oid)
runtime.KeepAlive(c)
runtime.KeepAlive(tree)
if cerr < 0 {
return nil, MakeGitError(cerr)
}
Expand Down
Loading