@@ -127,31 +127,37 @@ func diffLineFromC(line *C.git_diff_line) DiffLine {
127
127
}
128
128
129
129
type Diff struct {
130
- ptr * C.git_diff
130
+ ptr * C.git_diff
131
+ repo * Repository
131
132
}
132
133
133
134
func (diff * Diff ) NumDeltas () (int , error ) {
134
135
if diff .ptr == nil {
135
136
return - 1 , ErrInvalid
136
137
}
137
- return int (C .git_diff_num_deltas (diff .ptr )), nil
138
+ ret := int (C .git_diff_num_deltas (diff .ptr ))
139
+ runtime .KeepAlive (diff )
140
+ return ret , nil
138
141
}
139
142
140
143
func (diff * Diff ) GetDelta (index int ) (DiffDelta , error ) {
141
144
if diff .ptr == nil {
142
145
return DiffDelta {}, ErrInvalid
143
146
}
144
147
ptr := C .git_diff_get_delta (diff .ptr , C .size_t (index ))
145
- return diffDeltaFromC (ptr ), nil
148
+ ret := diffDeltaFromC (ptr )
149
+ runtime .KeepAlive (diff )
150
+ return ret , nil
146
151
}
147
152
148
- func newDiffFromC (ptr * C.git_diff ) * Diff {
153
+ func newDiffFromC (ptr * C.git_diff , repo * Repository ) * Diff {
149
154
if ptr == nil {
150
155
return nil
151
156
}
152
157
153
158
diff := & Diff {
154
- ptr : ptr ,
159
+ ptr : ptr ,
160
+ repo : repo ,
155
161
}
156
162
157
163
runtime .SetFinalizer (diff , (* Diff ).Free )
@@ -187,6 +193,7 @@ func (diff *Diff) FindSimilar(opts *DiffFindOptions) error {
187
193
defer runtime .UnlockOSThread ()
188
194
189
195
ecode := C .git_diff_find_similar (diff .ptr , copts )
196
+ runtime .KeepAlive (diff )
190
197
if ecode < 0 {
191
198
return MakeGitError (ecode )
192
199
}
@@ -209,15 +216,21 @@ func (stats *DiffStats) Free() error {
209
216
}
210
217
211
218
func (stats * DiffStats ) Insertions () int {
212
- return int (C .git_diff_stats_insertions (stats .ptr ))
219
+ ret := int (C .git_diff_stats_insertions (stats .ptr ))
220
+ runtime .KeepAlive (stats )
221
+ return ret
213
222
}
214
223
215
224
func (stats * DiffStats ) Deletions () int {
216
- return int (C .git_diff_stats_deletions (stats .ptr ))
225
+ ret := int (C .git_diff_stats_deletions (stats .ptr ))
226
+ runtime .KeepAlive (stats )
227
+ return ret
217
228
}
218
229
219
230
func (stats * DiffStats ) FilesChanged () int {
220
- return int (C .git_diff_stats_files_changed (stats .ptr ))
231
+ ret := int (C .git_diff_stats_files_changed (stats .ptr ))
232
+ runtime .KeepAlive (stats )
233
+ return ret
221
234
}
222
235
223
236
type DiffStatsFormat int
@@ -240,6 +253,7 @@ func (stats *DiffStats) String(format DiffStatsFormat,
240
253
241
254
ret := C .git_diff_stats_to_buf (& buf ,
242
255
stats .ptr , C .git_diff_stats_format_t (format ), C .size_t (width ))
256
+ runtime .KeepAlive (stats )
243
257
if ret < 0 {
244
258
return "" , MakeGitError (ret )
245
259
}
@@ -253,7 +267,9 @@ func (diff *Diff) Stats() (*DiffStats, error) {
253
267
runtime .LockOSThread ()
254
268
defer runtime .UnlockOSThread ()
255
269
256
- if ecode := C .git_diff_get_stats (& stats .ptr , diff .ptr ); ecode < 0 {
270
+ ecode := C .git_diff_get_stats (& stats .ptr , diff .ptr )
271
+ runtime .KeepAlive (diff )
272
+ if ecode < 0 {
257
273
return nil , MakeGitError (ecode )
258
274
}
259
275
runtime .SetFinalizer (stats , (* DiffStats ).Free )
@@ -301,6 +317,7 @@ func (diff *Diff) ForEach(cbFile DiffForEachFileCallback, detail DiffDetail) err
301
317
defer pointerHandles .Untrack (handle )
302
318
303
319
ecode := C ._go_git_diff_foreach (diff .ptr , 1 , intHunks , intLines , handle )
320
+ runtime .KeepAlive (diff )
304
321
if ecode < 0 {
305
322
return data .Error
306
323
}
@@ -380,6 +397,7 @@ func (diff *Diff) Patch(deltaIndex int) (*Patch, error) {
380
397
defer runtime .UnlockOSThread ()
381
398
382
399
ecode := C .git_patch_from_diff (& patchPtr , diff .ptr , C .size_t (deltaIndex ))
400
+ runtime .KeepAlive (diff )
383
401
if ecode < 0 {
384
402
return nil , MakeGitError (ecode )
385
403
}
@@ -537,7 +555,7 @@ func diffNotifyCb(_diff_so_far unsafe.Pointer, delta_to_add *C.git_diff_delta, m
537
555
538
556
if data != nil {
539
557
if data .Diff == nil {
540
- data .Diff = newDiffFromC (diff_so_far )
558
+ data .Diff = newDiffFromC (diff_so_far , nil )
541
559
}
542
560
543
561
err := data .Callback (data .Diff , diffDeltaFromC (delta_to_add ), C .GoString (matched_pathspec ))
@@ -618,14 +636,16 @@ func (v *Repository) DiffTreeToTree(oldTree, newTree *Tree, opts *DiffOptions) (
618
636
defer runtime .UnlockOSThread ()
619
637
620
638
ecode := C .git_diff_tree_to_tree (& diffPtr , v .ptr , oldPtr , newPtr , copts )
639
+ runtime .KeepAlive (oldTree )
640
+ runtime .KeepAlive (newTree )
621
641
if ecode < 0 {
622
642
return nil , MakeGitError (ecode )
623
643
}
624
644
625
645
if notifyData != nil && notifyData .Diff != nil {
626
646
return notifyData .Diff , nil
627
647
}
628
- return newDiffFromC (diffPtr ), nil
648
+ return newDiffFromC (diffPtr , v ), nil
629
649
}
630
650
631
651
func (v * Repository ) DiffTreeToWorkdir (oldTree * Tree , opts * DiffOptions ) (* Diff , error ) {
@@ -643,14 +663,15 @@ func (v *Repository) DiffTreeToWorkdir(oldTree *Tree, opts *DiffOptions) (*Diff,
643
663
defer runtime .UnlockOSThread ()
644
664
645
665
ecode := C .git_diff_tree_to_workdir (& diffPtr , v .ptr , oldPtr , copts )
666
+ runtime .KeepAlive (oldTree )
646
667
if ecode < 0 {
647
668
return nil , MakeGitError (ecode )
648
669
}
649
670
650
671
if notifyData != nil && notifyData .Diff != nil {
651
672
return notifyData .Diff , nil
652
673
}
653
- return newDiffFromC (diffPtr ), nil
674
+ return newDiffFromC (diffPtr , v ), nil
654
675
}
655
676
656
677
func (v * Repository ) DiffTreeToIndex (oldTree * Tree , index * Index , opts * DiffOptions ) (* Diff , error ) {
@@ -673,14 +694,16 @@ func (v *Repository) DiffTreeToIndex(oldTree *Tree, index *Index, opts *DiffOpti
673
694
defer runtime .UnlockOSThread ()
674
695
675
696
ecode := C .git_diff_tree_to_index (& diffPtr , v .ptr , oldPtr , indexPtr , copts )
697
+ runtime .KeepAlive (oldTree )
698
+ runtime .KeepAlive (index )
676
699
if ecode < 0 {
677
700
return nil , MakeGitError (ecode )
678
701
}
679
702
680
703
if notifyData != nil && notifyData .Diff != nil {
681
704
return notifyData .Diff , nil
682
705
}
683
- return newDiffFromC (diffPtr ), nil
706
+ return newDiffFromC (diffPtr , v ), nil
684
707
}
685
708
686
709
func (v * Repository ) DiffTreeToWorkdirWithIndex (oldTree * Tree , opts * DiffOptions ) (* Diff , error ) {
@@ -698,14 +721,15 @@ func (v *Repository) DiffTreeToWorkdirWithIndex(oldTree *Tree, opts *DiffOptions
698
721
defer runtime .UnlockOSThread ()
699
722
700
723
ecode := C .git_diff_tree_to_workdir_with_index (& diffPtr , v .ptr , oldPtr , copts )
724
+ runtime .KeepAlive (oldTree )
701
725
if ecode < 0 {
702
726
return nil , MakeGitError (ecode )
703
727
}
704
728
705
729
if notifyData != nil && notifyData .Diff != nil {
706
730
return notifyData .Diff , nil
707
731
}
708
- return newDiffFromC (diffPtr ), nil
732
+ return newDiffFromC (diffPtr , v ), nil
709
733
}
710
734
711
735
func (v * Repository ) DiffIndexToWorkdir (index * Index , opts * DiffOptions ) (* Diff , error ) {
@@ -723,14 +747,15 @@ func (v *Repository) DiffIndexToWorkdir(index *Index, opts *DiffOptions) (*Diff,
723
747
defer runtime .UnlockOSThread ()
724
748
725
749
ecode := C .git_diff_index_to_workdir (& diffPtr , v .ptr , indexPtr , copts )
750
+ runtime .KeepAlive (index )
726
751
if ecode < 0 {
727
752
return nil , MakeGitError (ecode )
728
753
}
729
754
730
755
if notifyData != nil && notifyData .Diff != nil {
731
756
return notifyData .Diff , nil
732
757
}
733
- return newDiffFromC (diffPtr ), nil
758
+ return newDiffFromC (diffPtr , v ), nil
734
759
}
735
760
736
761
// DiffBlobs performs a diff between two arbitrary blobs. You can pass
@@ -773,6 +798,8 @@ func DiffBlobs(oldBlob *Blob, oldAsPath string, newBlob *Blob, newAsPath string,
773
798
defer runtime .UnlockOSThread ()
774
799
775
800
ecode := C ._go_git_diff_blobs (oldBlobPtr , oldBlobPath , newBlobPtr , newBlobPath , copts , 1 , intHunks , intLines , handle )
801
+ runtime .KeepAlive (oldBlob )
802
+ runtime .KeepAlive (newBlob )
776
803
if ecode < 0 {
777
804
return MakeGitError (ecode )
778
805
}
0 commit comments