Skip to content

Commit 58334cf

Browse files
committed
First round of mass keep-alive additions
1 parent 0e9336b commit 58334cf

File tree

9 files changed

+120
-24
lines changed

9 files changed

+120
-24
lines changed

blame.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ func (v *Repository) BlameFile(path string, opts *BlameOptions) (*Blame, error)
7676
defer runtime.UnlockOSThread()
7777

7878
ecode := C.git_blame_file(&blamePtr, v.ptr, cpath, copts)
79+
runtime.KeepAlive(v)
7980
if ecode < 0 {
8081
return nil, MakeGitError(ecode)
8182
}
@@ -88,11 +89,15 @@ type Blame struct {
8889
}
8990

9091
func (blame *Blame) HunkCount() int {
91-
return int(C.git_blame_get_hunk_count(blame.ptr))
92+
ret := int(C.git_blame_get_hunk_count(blame.ptr))
93+
runtime.KeepAlive(blame)
94+
95+
return ret
9296
}
9397

9498
func (blame *Blame) HunkByIndex(index int) (BlameHunk, error) {
9599
ptr := C.git_blame_get_hunk_byindex(blame.ptr, C.uint32_t(index))
100+
runtime.KeepAlive(blame)
96101
if ptr == nil {
97102
return BlameHunk{}, ErrInvalid
98103
}
@@ -101,6 +106,7 @@ func (blame *Blame) HunkByIndex(index int) (BlameHunk, error) {
101106

102107
func (blame *Blame) HunkByLine(lineno int) (BlameHunk, error) {
103108
ptr := C.git_blame_get_hunk_byline(blame.ptr, C.size_t(lineno))
109+
runtime.KeepAlive(blame)
104110
if ptr == nil {
105111
return BlameHunk{}, ErrInvalid
106112
}

blob.go

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,19 @@ type Blob struct {
2121
}
2222

2323
func (v *Blob) Size() int64 {
24-
return int64(C.git_blob_rawsize(v.cast_ptr))
24+
ret := int64(C.git_blob_rawsize(v.cast_ptr))
25+
runtime.KeepAlive(v)
26+
return ret
2527
}
2628

2729
func (v *Blob) Contents() []byte {
2830
size := C.int(C.git_blob_rawsize(v.cast_ptr))
2931
buffer := unsafe.Pointer(C.git_blob_rawcontent(v.cast_ptr))
30-
return C.GoBytes(buffer, size)
32+
33+
goBytes := C.GoBytes(buffer, size)
34+
runtime.KeepAlive(v)
35+
36+
return goBytes
3137
}
3238

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

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

105-
return newBlobWriteStreamFromC(stream), nil
112+
return newBlobWriteStreamFromC(stream, repo), nil
106113
}
107114

108115
type BlobWriteStream struct {
109-
ptr *C.git_writestream
116+
ptr *C.git_writestream
117+
repo *Repository
110118
}
111119

112-
func newBlobWriteStreamFromC(ptr *C.git_writestream) *BlobWriteStream {
120+
func newBlobWriteStreamFromC(ptr *C.git_writestream, repo *Repository) *BlobWriteStream {
113121
stream := &BlobWriteStream{
114-
ptr: ptr,
122+
ptr: ptr,
123+
repo: repo,
115124
}
116125

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

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

149159
ecode := C.git_blob_create_fromstream_commit(&oid, stream.ptr)
160+
runtime.KeepAlive(stream)
150161
if ecode < 0 {
151162
return nil, MakeGitError(ecode)
152163
}

branch.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ func (repo *Repository) NewBranchIterator(flags BranchType) (*BranchIterator, er
8888
defer runtime.UnlockOSThread()
8989

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

108109
ret := C.git_branch_create(&ptr, repo.ptr, cBranchName, target.cast_ptr, cForce)
110+
runtime.KeepAlive(repo)
111+
runtime.KeepAlive(target)
109112
if ret < 0 {
110113
return nil, MakeGitError(ret)
111114
}
@@ -117,6 +120,7 @@ func (b *Branch) Delete() error {
117120
runtime.LockOSThread()
118121
defer runtime.UnlockOSThread()
119122
ret := C.git_branch_delete(b.Reference.ptr)
123+
runtime.KeepAlive(b.Reference)
120124
if ret < 0 {
121125
return MakeGitError(ret)
122126
}
@@ -133,6 +137,7 @@ func (b *Branch) Move(newBranchName string, force bool) (*Branch, error) {
133137
defer runtime.UnlockOSThread()
134138

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

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

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

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

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

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

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

243254
ret := C.git_branch_upstream_name(&nameBuf, repo.ptr, cName)
255+
runtime.KeepAlive(repo)
244256
if ret < 0 {
245257
return "", MakeGitError(ret)
246258
}

checkout.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,7 @@ func (v *Repository) CheckoutHead(opts *CheckoutOpts) error {
188188
defer freeCheckoutOpts(cOpts)
189189

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

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

228230
ret := C.git_checkout_tree(v.ptr, tree.ptr, cOpts)
231+
runtime.KeepAlive(v)
229232
if ret < 0 {
230233
return MakeGitError(ret)
231234
}

cherrypick.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,8 @@ func (v *Repository) Cherrypick(commit *Commit, opts CherrypickOptions) error {
6666
defer freeCherrypickOpts(cOpts)
6767

6868
ecode := C.git_cherrypick(v.ptr, commit.cast_ptr, cOpts)
69+
runtime.KeepAlive(v)
70+
runtime.KeepAlive(commit)
6971
if ecode < 0 {
7072
return MakeGitError(ecode)
7173
}

commit.go

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ func (c *Commit) Tree() (*Tree, error) {
7777
func (c *Commit) TreeId() *Oid {
7878
ret := newOidFromC(C.git_commit_tree_id(c.cast_ptr))
7979
runtime.KeepAlive(c)
80-
return c
80+
return ret
8181
}
8282

8383
func (c *Commit) Author() *Signature {
@@ -101,15 +101,21 @@ func (c *Commit) Parent(n uint) *Commit {
101101
return nil
102102
}
103103

104-
return allocCommit(cobj, c.repo)
104+
parent := allocCommit(cobj, c.repo)
105+
runtime.KeepAlive(c)
106+
return parent
105107
}
106108

107109
func (c *Commit) ParentId(n uint) *Oid {
108-
return newOidFromC(C.git_commit_parent_id(c.cast_ptr, C.uint(n)))
110+
ret := newOidFromC(C.git_commit_parent_id(c.cast_ptr, C.uint(n)))
111+
runtime.KeepAlive(c)
112+
return ret
109113
}
110114

111115
func (c *Commit) ParentCount() uint {
112-
return uint(C.git_commit_parentcount(c.cast_ptr))
116+
ret := uint(C.git_commit_parentcount(c.cast_ptr))
117+
runtime.KeepAlive(c)
118+
return ret
113119
}
114120

115121
func (c *Commit) Amend(refname string, author, committer *Signature, message string, tree *Tree) (*Oid, error) {
@@ -142,6 +148,9 @@ func (c *Commit) Amend(refname string, author, committer *Signature, message str
142148
oid := new(Oid)
143149

144150
cerr := C.git_commit_amend(oid.toC(), c.cast_ptr, cref, authorSig, committerSig, nil, cmsg, tree.cast_ptr)
151+
runtime.KeepAlive(oid)
152+
runtime.KeepAlive(c)
153+
runtime.KeepAlive(tree)
145154
if cerr < 0 {
146155
return nil, MakeGitError(cerr)
147156
}

0 commit comments

Comments
 (0)