- Notifications
You must be signed in to change notification settings - Fork 324
/
Copy pathdiff.go
1103 lines (906 loc) · 30 KB
/
diff.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package git
/*
#include <git2.h>
extern void _go_git_populate_apply_callbacks(git_apply_options *options);
extern int _go_git_diff_foreach(git_diff *diff, int eachFile, int eachHunk, int eachLine, void *payload);
extern void _go_git_setup_diff_notify_callbacks(git_diff_options* opts);
extern int _go_git_diff_blobs(git_blob *old, const char *old_path, git_blob *new, const char *new_path, git_diff_options *opts, int eachFile, int eachHunk, int eachLine, void *payload);
*/
import"C"
import (
"errors"
"runtime"
"unsafe"
)
typeDiffFlaguint32
const (
DiffFlagBinaryDiffFlag=C.GIT_DIFF_FLAG_BINARY
DiffFlagNotBinaryDiffFlag=C.GIT_DIFF_FLAG_NOT_BINARY
DiffFlagValidOidDiffFlag=C.GIT_DIFF_FLAG_VALID_ID
DiffFlagExistsDiffFlag=C.GIT_DIFF_FLAG_EXISTS
)
typeDeltaint
const (
DeltaUnmodifiedDelta=C.GIT_DELTA_UNMODIFIED
DeltaAddedDelta=C.GIT_DELTA_ADDED
DeltaDeletedDelta=C.GIT_DELTA_DELETED
DeltaModifiedDelta=C.GIT_DELTA_MODIFIED
DeltaRenamedDelta=C.GIT_DELTA_RENAMED
DeltaCopiedDelta=C.GIT_DELTA_COPIED
DeltaIgnoredDelta=C.GIT_DELTA_IGNORED
DeltaUntrackedDelta=C.GIT_DELTA_UNTRACKED
DeltaTypeChangeDelta=C.GIT_DELTA_TYPECHANGE
DeltaUnreadableDelta=C.GIT_DELTA_UNREADABLE
DeltaConflictedDelta=C.GIT_DELTA_CONFLICTED
)
//go:generate stringer -type Delta -trimprefix Delta -tags static
typeDiffLineTypeint
const (
DiffLineContextDiffLineType=C.GIT_DIFF_LINE_CONTEXT
DiffLineAdditionDiffLineType=C.GIT_DIFF_LINE_ADDITION
DiffLineDeletionDiffLineType=C.GIT_DIFF_LINE_DELETION
DiffLineContextEOFNLDiffLineType=C.GIT_DIFF_LINE_CONTEXT_EOFNL
DiffLineAddEOFNLDiffLineType=C.GIT_DIFF_LINE_ADD_EOFNL
DiffLineDelEOFNLDiffLineType=C.GIT_DIFF_LINE_DEL_EOFNL
DiffLineFileHdrDiffLineType=C.GIT_DIFF_LINE_FILE_HDR
DiffLineHunkHdrDiffLineType=C.GIT_DIFF_LINE_HUNK_HDR
DiffLineBinaryDiffLineType=C.GIT_DIFF_LINE_BINARY
)
//go:generate stringer -type DiffLineType -trimprefix DiffLine -tags static
typeDiffFilestruct {
Pathstring
Oid*Oid
Sizeint
FlagsDiffFlag
Modeuint16
}
funcdiffFileFromC(file*C.git_diff_file) DiffFile {
returnDiffFile{
Path: C.GoString(file.path),
Oid: newOidFromC(&file.id),
Size: int(file.size),
Flags: DiffFlag(file.flags),
Mode: uint16(file.mode),
}
}
typeDiffDeltastruct {
StatusDelta
FlagsDiffFlag
Similarityuint16
OldFileDiffFile
NewFileDiffFile
}
funcdiffDeltaFromC(delta*C.git_diff_delta) DiffDelta {
returnDiffDelta{
Status: Delta(delta.status),
Flags: DiffFlag(delta.flags),
Similarity: uint16(delta.similarity),
OldFile: diffFileFromC(&delta.old_file),
NewFile: diffFileFromC(&delta.new_file),
}
}
typeDiffHunkstruct {
OldStartint
OldLinesint
NewStartint
NewLinesint
Headerstring
}
funcdiffHunkFromC(hunk*C.git_diff_hunk) DiffHunk {
returnDiffHunk{
OldStart: int(hunk.old_start),
OldLines: int(hunk.old_lines),
NewStart: int(hunk.new_start),
NewLines: int(hunk.new_lines),
Header: C.GoStringN(&hunk.header[0], C.int(hunk.header_len)),
}
}
typeDiffLinestruct {
OriginDiffLineType
OldLinenoint
NewLinenoint
NumLinesint
Contentstring
}
funcdiffLineFromC(line*C.git_diff_line) DiffLine {
returnDiffLine{
Origin: DiffLineType(line.origin),
OldLineno: int(line.old_lineno),
NewLineno: int(line.new_lineno),
NumLines: int(line.num_lines),
Content: C.GoStringN(line.content, C.int(line.content_len)),
}
}
typeDiffstruct {
doNotCompare
ptr*C.git_diff
repo*Repository
runFinalizerbool
}
func (diff*Diff) NumDeltas() (int, error) {
ifdiff.ptr==nil {
return-1, ErrInvalid
}
ret:=int(C.git_diff_num_deltas(diff.ptr))
runtime.KeepAlive(diff)
returnret, nil
}
func (diff*Diff) Delta(indexint) (DiffDelta, error) {
ifdiff.ptr==nil {
returnDiffDelta{}, ErrInvalid
}
ptr:=C.git_diff_get_delta(diff.ptr, C.size_t(index))
ret:=diffDeltaFromC(ptr)
runtime.KeepAlive(diff)
returnret, nil
}
// deprecated: You should use `Diff.Delta()` instead.
func (diff*Diff) GetDelta(indexint) (DiffDelta, error) {
returndiff.Delta(index)
}
funcnewDiffFromC(ptr*C.git_diff, repo*Repository) *Diff {
ifptr==nil {
returnnil
}
diff:=&Diff{
ptr: ptr,
repo: repo,
runFinalizer: true,
}
runtime.SetFinalizer(diff, (*Diff).Free)
returndiff
}
func (diff*Diff) Free() error {
ifdiff.ptr==nil {
returnErrInvalid
}
if!diff.runFinalizer {
// This is the case with the Diff objects that are involved in the DiffNotifyCallback.
diff.ptr=nil
returnnil
}
runtime.SetFinalizer(diff, nil)
C.git_diff_free(diff.ptr)
diff.ptr=nil
returnnil
}
func (diff*Diff) FindSimilar(opts*DiffFindOptions) error {
varcopts*C.git_diff_find_options
ifopts!=nil {
copts=&C.git_diff_find_options{
version: C.GIT_DIFF_FIND_OPTIONS_VERSION,
flags: C.uint32_t(opts.Flags),
rename_threshold: C.uint16_t(opts.RenameThreshold),
copy_threshold: C.uint16_t(opts.CopyThreshold),
rename_from_rewrite_threshold: C.uint16_t(opts.RenameFromRewriteThreshold),
break_rewrite_threshold: C.uint16_t(opts.BreakRewriteThreshold),
rename_limit: C.size_t(opts.RenameLimit),
}
}
runtime.LockOSThread()
deferruntime.UnlockOSThread()
ecode:=C.git_diff_find_similar(diff.ptr, copts)
runtime.KeepAlive(diff)
ifecode<0 {
returnMakeGitError(ecode)
}
returnnil
}
typeDiffStatsstruct {
doNotCompare
ptr*C.git_diff_stats
}
func (stats*DiffStats) Free() error {
ifstats.ptr==nil {
returnErrInvalid
}
runtime.SetFinalizer(stats, nil)
C.git_diff_stats_free(stats.ptr)
stats.ptr=nil
returnnil
}
func (stats*DiffStats) Insertions() int {
ret:=int(C.git_diff_stats_insertions(stats.ptr))
runtime.KeepAlive(stats)
returnret
}
func (stats*DiffStats) Deletions() int {
ret:=int(C.git_diff_stats_deletions(stats.ptr))
runtime.KeepAlive(stats)
returnret
}
func (stats*DiffStats) FilesChanged() int {
ret:=int(C.git_diff_stats_files_changed(stats.ptr))
runtime.KeepAlive(stats)
returnret
}
typeDiffStatsFormatint
const (
DiffStatsNoneDiffStatsFormat=C.GIT_DIFF_STATS_NONE
DiffStatsFullDiffStatsFormat=C.GIT_DIFF_STATS_FULL
DiffStatsShortDiffStatsFormat=C.GIT_DIFF_STATS_SHORT
DiffStatsNumberDiffStatsFormat=C.GIT_DIFF_STATS_NUMBER
DiffStatsIncludeSummaryDiffStatsFormat=C.GIT_DIFF_STATS_INCLUDE_SUMMARY
)
func (stats*DiffStats) String(formatDiffStatsFormat,
widthuint) (string, error) {
buf:= C.git_buf{}
deferC.git_buf_dispose(&buf)
runtime.LockOSThread()
deferruntime.UnlockOSThread()
ret:=C.git_diff_stats_to_buf(&buf,
stats.ptr, C.git_diff_stats_format_t(format), C.size_t(width))
runtime.KeepAlive(stats)
ifret<0 {
return"", MakeGitError(ret)
}
returnC.GoString(buf.ptr), nil
}
func (diff*Diff) Stats() (*DiffStats, error) {
stats:=new(DiffStats)
runtime.LockOSThread()
deferruntime.UnlockOSThread()
ecode:=C.git_diff_get_stats(&stats.ptr, diff.ptr)
runtime.KeepAlive(diff)
ifecode<0 {
returnnil, MakeGitError(ecode)
}
runtime.SetFinalizer(stats, (*DiffStats).Free)
returnstats, nil
}
typediffForEachCallbackDatastruct {
fileCallbackDiffForEachFileCallback
hunkCallbackDiffForEachHunkCallback
lineCallbackDiffForEachLineCallback
errorTarget*error
}
typeDiffForEachFileCallbackfunc(deltaDiffDelta, progressfloat64) (DiffForEachHunkCallback, error)
typeDiffDetailint
const (
DiffDetailFilesDiffDetail=iota
DiffDetailHunks
DiffDetailLines
)
func (diff*Diff) ForEach(cbFileDiffForEachFileCallback, detailDiffDetail) error {
ifdiff.ptr==nil {
returnErrInvalid
}
intHunks:=C.int(0)
ifdetail>=DiffDetailHunks {
intHunks=C.int(1)
}
intLines:=C.int(0)
ifdetail>=DiffDetailLines {
intLines=C.int(1)
}
varerrerror
data:=&diffForEachCallbackData{
fileCallback: cbFile,
errorTarget: &err,
}
handle:=pointerHandles.Track(data)
deferpointerHandles.Untrack(handle)
runtime.LockOSThread()
deferruntime.UnlockOSThread()
ret:=C._go_git_diff_foreach(diff.ptr, 1, intHunks, intLines, handle)
runtime.KeepAlive(diff)
ifret==C.int(ErrorCodeUser) &&err!=nil {
returnerr
}
ifret<0 {
returnMakeGitError(ret)
}
returnnil
}
//export diffForEachFileCallback
funcdiffForEachFileCallback(delta*C.git_diff_delta, progress C.float, handle unsafe.Pointer) C.int {
payload:=pointerHandles.Get(handle)
data, ok:=payload.(*diffForEachCallbackData)
if!ok {
panic("could not retrieve data for handle")
}
data.hunkCallback=nil
ifdata.fileCallback!=nil {
cb, err:=data.fileCallback(diffDeltaFromC(delta), float64(progress))
iferr!=nil {
*data.errorTarget=err
returnC.int(ErrorCodeUser)
}
data.hunkCallback=cb
}
returnC.int(ErrorCodeOK)
}
typeDiffForEachHunkCallbackfunc(DiffHunk) (DiffForEachLineCallback, error)
//export diffForEachHunkCallback
funcdiffForEachHunkCallback(delta*C.git_diff_delta, hunk*C.git_diff_hunk, handle unsafe.Pointer) C.int {
payload:=pointerHandles.Get(handle)
data, ok:=payload.(*diffForEachCallbackData)
if!ok {
panic("could not retrieve data for handle")
}
data.lineCallback=nil
ifdata.hunkCallback!=nil {
cb, err:=data.hunkCallback(diffHunkFromC(hunk))
iferr!=nil {
*data.errorTarget=err
returnC.int(ErrorCodeUser)
}
data.lineCallback=cb
}
returnC.int(ErrorCodeOK)
}
typeDiffForEachLineCallbackfunc(DiffLine) error
//export diffForEachLineCallback
funcdiffForEachLineCallback(delta*C.git_diff_delta, hunk*C.git_diff_hunk, line*C.git_diff_line, handle unsafe.Pointer) C.int {
payload:=pointerHandles.Get(handle)
data, ok:=payload.(*diffForEachCallbackData)
if!ok {
panic("could not retrieve data for handle")
}
err:=data.lineCallback(diffLineFromC(line))
iferr!=nil {
*data.errorTarget=err
returnC.int(ErrorCodeUser)
}
returnC.int(ErrorCodeOK)
}
func (diff*Diff) Patch(deltaIndexint) (*Patch, error) {
ifdiff.ptr==nil {
returnnil, ErrInvalid
}
varpatchPtr*C.git_patch
runtime.LockOSThread()
deferruntime.UnlockOSThread()
ecode:=C.git_patch_from_diff(&patchPtr, diff.ptr, C.size_t(deltaIndex))
runtime.KeepAlive(diff)
ifecode<0 {
returnnil, MakeGitError(ecode)
}
returnnewPatchFromC(patchPtr), nil
}
typeDiffFormatint
const (
DiffFormatPatchDiffFormat=C.GIT_DIFF_FORMAT_PATCH
DiffFormatPatchHeaderDiffFormat=C.GIT_DIFF_FORMAT_PATCH_HEADER
DiffFormatRawDiffFormat=C.GIT_DIFF_FORMAT_RAW
DiffFormatNameOnlyDiffFormat=C.GIT_DIFF_FORMAT_NAME_ONLY
DiffFormatNameStatusDiffFormat=C.GIT_DIFF_FORMAT_NAME_STATUS
)
func (diff*Diff) ToBuf(formatDiffFormat) ([]byte, error) {
ifdiff.ptr==nil {
returnnil, ErrInvalid
}
diffBuf:= C.git_buf{}
runtime.LockOSThread()
deferruntime.UnlockOSThread()
ecode:=C.git_diff_to_buf(&diffBuf, diff.ptr, C.git_diff_format_t(format))
runtime.KeepAlive(diff)
ifecode<0 {
returnnil, MakeGitError(ecode)
}
deferC.git_buf_dispose(&diffBuf)
returnC.GoBytes(unsafe.Pointer(diffBuf.ptr), C.int(diffBuf.size)), nil
}
typeDiffOptionsFlagint
const (
DiffNormalDiffOptionsFlag=C.GIT_DIFF_NORMAL
DiffReverseDiffOptionsFlag=C.GIT_DIFF_REVERSE
DiffIncludeIgnoredDiffOptionsFlag=C.GIT_DIFF_INCLUDE_IGNORED
DiffRecurseIgnoredDirsDiffOptionsFlag=C.GIT_DIFF_RECURSE_IGNORED_DIRS
DiffIncludeUntrackedDiffOptionsFlag=C.GIT_DIFF_INCLUDE_UNTRACKED
DiffRecurseUntrackedDiffOptionsFlag=C.GIT_DIFF_RECURSE_UNTRACKED_DIRS
DiffIncludeUnmodifiedDiffOptionsFlag=C.GIT_DIFF_INCLUDE_UNMODIFIED
DiffIncludeTypeChangeDiffOptionsFlag=C.GIT_DIFF_INCLUDE_TYPECHANGE
DiffIncludeTypeChangeTreesDiffOptionsFlag=C.GIT_DIFF_INCLUDE_TYPECHANGE_TREES
DiffIgnoreFilemodeDiffOptionsFlag=C.GIT_DIFF_IGNORE_FILEMODE
DiffIgnoreSubmodulesDiffOptionsFlag=C.GIT_DIFF_IGNORE_SUBMODULES
DiffIgnoreCaseDiffOptionsFlag=C.GIT_DIFF_IGNORE_CASE
DiffIncludeCaseChangeDiffOptionsFlag=C.GIT_DIFF_INCLUDE_CASECHANGE
DiffDisablePathspecMatchDiffOptionsFlag=C.GIT_DIFF_DISABLE_PATHSPEC_MATCH
DiffSkipBinaryCheckDiffOptionsFlag=C.GIT_DIFF_SKIP_BINARY_CHECK
DiffEnableFastUntrackedDirsDiffOptionsFlag=C.GIT_DIFF_ENABLE_FAST_UNTRACKED_DIRS
DiffForceTextDiffOptionsFlag=C.GIT_DIFF_FORCE_TEXT
DiffForceBinaryDiffOptionsFlag=C.GIT_DIFF_FORCE_BINARY
DiffIgnoreWhitespaceDiffOptionsFlag=C.GIT_DIFF_IGNORE_WHITESPACE
DiffIgnoreWhitespaceChangeDiffOptionsFlag=C.GIT_DIFF_IGNORE_WHITESPACE_CHANGE
DiffIgnoreWhitespaceEOLDiffOptionsFlag=C.GIT_DIFF_IGNORE_WHITESPACE_EOL
DiffShowUntrackedContentDiffOptionsFlag=C.GIT_DIFF_SHOW_UNTRACKED_CONTENT
DiffShowUnmodifiedDiffOptionsFlag=C.GIT_DIFF_SHOW_UNMODIFIED
DiffPatienceDiffOptionsFlag=C.GIT_DIFF_PATIENCE
DiffMinimalDiffOptionsFlag=C.GIT_DIFF_MINIMAL
DiffShowBinaryDiffOptionsFlag=C.GIT_DIFF_SHOW_BINARY
DiffIndentHeuristicDiffOptionsFlag=C.GIT_DIFF_INDENT_HEURISTIC
)
typeDiffNotifyCallbackfunc(diffSoFar*Diff, deltaToAddDiffDelta, matchedPathspecstring) error
typeDiffOptionsstruct {
FlagsDiffOptionsFlag
IgnoreSubmodulesSubmoduleIgnore
Pathspec []string
NotifyCallbackDiffNotifyCallback
ContextLinesuint32
InterhunkLinesuint32
IdAbbrevuint16
MaxSizeint
OldPrefixstring
NewPrefixstring
}
funcDefaultDiffOptions() (DiffOptions, error) {
opts:= C.git_diff_options{}
runtime.LockOSThread()
deferruntime.UnlockOSThread()
ecode:=C.git_diff_options_init(&opts, C.GIT_DIFF_OPTIONS_VERSION)
ifecode<0 {
returnDiffOptions{}, MakeGitError(ecode)
}
returnDiffOptions{
Flags: DiffOptionsFlag(opts.flags),
IgnoreSubmodules: SubmoduleIgnore(opts.ignore_submodules),
Pathspec: makeStringsFromCStrings(opts.pathspec.strings, int(opts.pathspec.count)),
ContextLines: uint32(opts.context_lines),
InterhunkLines: uint32(opts.interhunk_lines),
IdAbbrev: uint16(opts.id_abbrev),
MaxSize: int(opts.max_size),
OldPrefix: "a",
NewPrefix: "b",
}, nil
}
typeDiffFindOptionsFlagint
const (
DiffFindByConfigDiffFindOptionsFlag=C.GIT_DIFF_FIND_BY_CONFIG
DiffFindRenamesDiffFindOptionsFlag=C.GIT_DIFF_FIND_RENAMES
DiffFindRenamesFromRewritesDiffFindOptionsFlag=C.GIT_DIFF_FIND_RENAMES_FROM_REWRITES
DiffFindCopiesDiffFindOptionsFlag=C.GIT_DIFF_FIND_COPIES
DiffFindCopiesFromUnmodifiedDiffFindOptionsFlag=C.GIT_DIFF_FIND_COPIES_FROM_UNMODIFIED
DiffFindRewritesDiffFindOptionsFlag=C.GIT_DIFF_FIND_REWRITES
DiffFindBreakRewritesDiffFindOptionsFlag=C.GIT_DIFF_BREAK_REWRITES
DiffFindAndBreakRewritesDiffFindOptionsFlag=C.GIT_DIFF_FIND_AND_BREAK_REWRITES
DiffFindForUntrackedDiffFindOptionsFlag=C.GIT_DIFF_FIND_FOR_UNTRACKED
DiffFindAllDiffFindOptionsFlag=C.GIT_DIFF_FIND_ALL
DiffFindIgnoreLeadingWhitespaceDiffFindOptionsFlag=C.GIT_DIFF_FIND_IGNORE_LEADING_WHITESPACE
DiffFindIgnoreWhitespaceDiffFindOptionsFlag=C.GIT_DIFF_FIND_IGNORE_WHITESPACE
DiffFindDontIgnoreWhitespaceDiffFindOptionsFlag=C.GIT_DIFF_FIND_DONT_IGNORE_WHITESPACE
DiffFindExactMatchOnlyDiffFindOptionsFlag=C.GIT_DIFF_FIND_EXACT_MATCH_ONLY
DiffFindBreakRewritesForRenamesOnlyDiffFindOptionsFlag=C.GIT_DIFF_BREAK_REWRITES_FOR_RENAMES_ONLY
DiffFindRemoveUnmodifiedDiffFindOptionsFlag=C.GIT_DIFF_FIND_REMOVE_UNMODIFIED
)
// TODO implement git_diff_similarity_metric
typeDiffFindOptionsstruct {
FlagsDiffFindOptionsFlag
RenameThresholduint16
CopyThresholduint16
RenameFromRewriteThresholduint16
BreakRewriteThresholduint16
RenameLimituint
}
funcDefaultDiffFindOptions() (DiffFindOptions, error) {
opts:= C.git_diff_find_options{}
runtime.LockOSThread()
deferruntime.UnlockOSThread()
ecode:=C.git_diff_find_options_init(&opts, C.GIT_DIFF_FIND_OPTIONS_VERSION)
ifecode<0 {
returnDiffFindOptions{}, MakeGitError(ecode)
}
returnDiffFindOptions{
Flags: DiffFindOptionsFlag(opts.flags),
RenameThreshold: uint16(opts.rename_threshold),
CopyThreshold: uint16(opts.copy_threshold),
RenameFromRewriteThreshold: uint16(opts.rename_from_rewrite_threshold),
BreakRewriteThreshold: uint16(opts.break_rewrite_threshold),
RenameLimit: uint(opts.rename_limit),
}, nil
}
var (
ErrDeltaSkip=errors.New("Skip delta")
)
typediffNotifyCallbackDatastruct {
callbackDiffNotifyCallback
repository*Repository
errorTarget*error
}
//export diffNotifyCallback
funcdiffNotifyCallback(_diff_so_far unsafe.Pointer, delta_to_add*C.git_diff_delta, matched_pathspec*C.char, handle unsafe.Pointer) C.int {
diff_so_far:= (*C.git_diff)(_diff_so_far)
payload:=pointerHandles.Get(handle)
data, ok:=payload.(*diffNotifyCallbackData)
if!ok {
panic("could not retrieve data for handle")
}
ifdata==nil {
returnC.int(ErrorCodeOK)
}
// We are not taking ownership of this diff pointer, so no finalizer is set.
diff:=&Diff{
ptr: diff_so_far,
repo: data.repository,
runFinalizer: false,
}
err:=data.callback(diff, diffDeltaFromC(delta_to_add), C.GoString(matched_pathspec))
// Since the callback could theoretically keep a reference to the diff
// (which could be freed by libgit2 if an error occurs later during the
// diffing process), this converts a use-after-free (terrible!) into a nil
// dereference ("just" pretty bad).
diff.ptr=nil
iferr==ErrDeltaSkip {
return1
}
iferr!=nil {
*data.errorTarget=err
returnC.int(ErrorCodeUser)
}
returnC.int(ErrorCodeOK)
}
funcpopulateDiffOptions(copts*C.git_diff_options, opts*DiffOptions, repo*Repository, errorTarget*error) *C.git_diff_options {
C.git_diff_options_init(copts, C.GIT_DIFF_OPTIONS_VERSION)
ifopts==nil {
returnnil
}
copts.flags=C.uint32_t(opts.Flags)
copts.ignore_submodules=C.git_submodule_ignore_t(opts.IgnoreSubmodules)
iflen(opts.Pathspec) >0 {
copts.pathspec.count=C.size_t(len(opts.Pathspec))
copts.pathspec.strings=makeCStringsFromStrings(opts.Pathspec)
}
copts.context_lines=C.uint32_t(opts.ContextLines)
copts.interhunk_lines=C.uint32_t(opts.InterhunkLines)
copts.id_abbrev=C.uint16_t(opts.IdAbbrev)
copts.max_size=C.git_off_t(opts.MaxSize)
copts.old_prefix=C.CString(opts.OldPrefix)
copts.new_prefix=C.CString(opts.NewPrefix)
ifopts.NotifyCallback!=nil {
notifyData:=&diffNotifyCallbackData{
callback: opts.NotifyCallback,
repository: repo,
errorTarget: errorTarget,
}
C._go_git_setup_diff_notify_callbacks(copts)
copts.payload=pointerHandles.Track(notifyData)
}
returncopts
}
funcfreeDiffOptions(copts*C.git_diff_options) {
ifcopts==nil {
return
}
freeStrarray(&copts.pathspec)
C.free(unsafe.Pointer(copts.old_prefix))
C.free(unsafe.Pointer(copts.new_prefix))
ifcopts.payload!=nil {
pointerHandles.Untrack(copts.payload)
}
}
func (v*Repository) DiffTreeToTree(oldTree, newTree*Tree, opts*DiffOptions) (*Diff, error) {
vardiffPtr*C.git_diff
varoldPtr, newPtr*C.git_tree
ifoldTree!=nil {
oldPtr=oldTree.cast_ptr
}
ifnewTree!=nil {
newPtr=newTree.cast_ptr
}
varerrerror
copts:=populateDiffOptions(&C.git_diff_options{}, opts, v, &err)
deferfreeDiffOptions(copts)
runtime.LockOSThread()
deferruntime.UnlockOSThread()
ret:=C.git_diff_tree_to_tree(&diffPtr, v.ptr, oldPtr, newPtr, copts)
runtime.KeepAlive(oldTree)
runtime.KeepAlive(newTree)
ifret==C.int(ErrorCodeUser) &&err!=nil {
returnnil, err
}
ifret<0 {
returnnil, MakeGitError(ret)
}
returnnewDiffFromC(diffPtr, v), nil
}
func (v*Repository) DiffTreeToWorkdir(oldTree*Tree, opts*DiffOptions) (*Diff, error) {
vardiffPtr*C.git_diff
varoldPtr*C.git_tree
ifoldTree!=nil {
oldPtr=oldTree.cast_ptr
}
varerrerror
copts:=populateDiffOptions(&C.git_diff_options{}, opts, v, &err)
deferfreeDiffOptions(copts)
runtime.LockOSThread()
deferruntime.UnlockOSThread()
ret:=C.git_diff_tree_to_workdir(&diffPtr, v.ptr, oldPtr, copts)
runtime.KeepAlive(oldTree)
ifret==C.int(ErrorCodeUser) &&err!=nil {
returnnil, err
}
ifret<0 {
returnnil, MakeGitError(ret)
}
returnnewDiffFromC(diffPtr, v), nil
}
func (v*Repository) DiffTreeToIndex(oldTree*Tree, index*Index, opts*DiffOptions) (*Diff, error) {
vardiffPtr*C.git_diff
varoldPtr*C.git_tree
varindexPtr*C.git_index
ifoldTree!=nil {
oldPtr=oldTree.cast_ptr
}
ifindex!=nil {
indexPtr=index.ptr
}
varerrerror
copts:=populateDiffOptions(&C.git_diff_options{}, opts, v, &err)
deferfreeDiffOptions(copts)
runtime.LockOSThread()
deferruntime.UnlockOSThread()
ret:=C.git_diff_tree_to_index(&diffPtr, v.ptr, oldPtr, indexPtr, copts)
runtime.KeepAlive(oldTree)
runtime.KeepAlive(index)
ifret==C.int(ErrorCodeUser) &&err!=nil {
returnnil, err
}
ifret<0 {
returnnil, MakeGitError(ret)
}
returnnewDiffFromC(diffPtr, v), nil
}
func (v*Repository) DiffTreeToWorkdirWithIndex(oldTree*Tree, opts*DiffOptions) (*Diff, error) {
vardiffPtr*C.git_diff
varoldPtr*C.git_tree
ifoldTree!=nil {
oldPtr=oldTree.cast_ptr
}
varerrerror
copts:=populateDiffOptions(&C.git_diff_options{}, opts, v, &err)
deferfreeDiffOptions(copts)
runtime.LockOSThread()
deferruntime.UnlockOSThread()
ret:=C.git_diff_tree_to_workdir_with_index(&diffPtr, v.ptr, oldPtr, copts)
runtime.KeepAlive(oldTree)
ifret==C.int(ErrorCodeUser) &&err!=nil {
returnnil, err
}
ifret<0 {
returnnil, MakeGitError(ret)
}
returnnewDiffFromC(diffPtr, v), nil
}
func (v*Repository) DiffIndexToWorkdir(index*Index, opts*DiffOptions) (*Diff, error) {
vardiffPtr*C.git_diff
varindexPtr*C.git_index
ifindex!=nil {
indexPtr=index.ptr
}
varerrerror
copts:=populateDiffOptions(&C.git_diff_options{}, opts, v, &err)
deferfreeDiffOptions(copts)
runtime.LockOSThread()
deferruntime.UnlockOSThread()
ret:=C.git_diff_index_to_workdir(&diffPtr, v.ptr, indexPtr, copts)
runtime.KeepAlive(index)
ifret==C.int(ErrorCodeUser) &&err!=nil {
returnnil, err
}
ifret<0 {
returnnil, MakeGitError(ret)
}
returnnewDiffFromC(diffPtr, v), nil
}
// DiffBlobs performs a diff between two arbitrary blobs. You can pass
// whatever file names you'd like for them to appear as in the diff.
funcDiffBlobs(oldBlob*Blob, oldAsPathstring, newBlob*Blob, newAsPathstring, opts*DiffOptions, fileCallbackDiffForEachFileCallback, detailDiffDetail) error {
varerrerror
data:=&diffForEachCallbackData{
fileCallback: fileCallback,
errorTarget: &err,
}
intHunks:=C.int(0)
ifdetail>=DiffDetailHunks {
intHunks=C.int(1)
}
intLines:=C.int(0)
ifdetail>=DiffDetailLines {
intLines=C.int(1)
}
handle:=pointerHandles.Track(data)
deferpointerHandles.Untrack(handle)
varrepo*Repository
varoldBlobPtr, newBlobPtr*C.git_blob
ifoldBlob!=nil {
oldBlobPtr=oldBlob.cast_ptr
repo=oldBlob.repo
}
ifnewBlob!=nil {
newBlobPtr=newBlob.cast_ptr
repo=newBlob.repo
}
oldBlobPath:=C.CString(oldAsPath)
deferC.free(unsafe.Pointer(oldBlobPath))
newBlobPath:=C.CString(newAsPath)
deferC.free(unsafe.Pointer(newBlobPath))
copts:=populateDiffOptions(&C.git_diff_options{}, opts, repo, &err)
deferfreeDiffOptions(copts)
runtime.LockOSThread()
deferruntime.UnlockOSThread()
ret:=C._go_git_diff_blobs(oldBlobPtr, oldBlobPath, newBlobPtr, newBlobPath, copts, 1, intHunks, intLines, handle)
runtime.KeepAlive(oldBlob)
runtime.KeepAlive(newBlob)
ifret==C.int(ErrorCodeUser) &&err!=nil {
returnerr
}
ifret<0 {
returnMakeGitError(ret)
}
returnnil
}
// ApplyHunkCallback is a callback that will be made per delta (file) when applying a patch.
typeApplyHunkCallbackfunc(*DiffHunk) (applybool, errerror)
// ApplyDeltaCallback is a callback that will be made per hunk when applying a patch.
typeApplyDeltaCallbackfunc(*DiffDelta) (applybool, errerror)
// ApplyOptions has 2 callbacks that are called for hunks or deltas
// If these functions return an error, abort the apply process immediately.
// If the first return value is true, the delta/hunk will be applied. If it is false, the delta/hunk will not be applied. In either case, the rest of the apply process will continue.
typeApplyOptionsstruct {
ApplyHunkCallbackApplyHunkCallback
ApplyDeltaCallbackApplyDeltaCallback
Flagsuint
}
typeapplyCallbackDatastruct {
options*ApplyOptions
errorTarget*error
}
//export hunkApplyCallback
funchunkApplyCallback(_hunk*C.git_diff_hunk, _payload unsafe.Pointer) C.int {
data, ok:=pointerHandles.Get(_payload).(*applyCallbackData)
if!ok {
panic("invalid apply options payload")
}
ifdata.options.ApplyHunkCallback==nil {
returnC.int(ErrorCodeOK)
}
hunk:=diffHunkFromC(_hunk)
apply, err:=data.options.ApplyHunkCallback(&hunk)
iferr!=nil {
*data.errorTarget=err
returnC.int(ErrorCodeUser)
}
if!apply {
return1
}
returnC.int(ErrorCodeOK)
}
//export deltaApplyCallback
funcdeltaApplyCallback(_delta*C.git_diff_delta, _payload unsafe.Pointer) C.int {
data, ok:=pointerHandles.Get(_payload).(*applyCallbackData)
if!ok {
panic("invalid apply options payload")
}
ifdata.options.ApplyDeltaCallback==nil {
returnC.int(ErrorCodeOK)
}
delta:=diffDeltaFromC(_delta)
apply, err:=data.options.ApplyDeltaCallback(&delta)
iferr!=nil {
*data.errorTarget=err
returnC.int(ErrorCodeUser)
}
if!apply {
return1
}
returnC.int(ErrorCodeOK)
}
// DefaultApplyOptions returns default options for applying diffs or patches.
funcDefaultApplyOptions() (*ApplyOptions, error) {
opts:= C.git_apply_options{}
runtime.LockOSThread()
deferruntime.UnlockOSThread()
ecode:=C.git_apply_options_init(&opts, C.GIT_APPLY_OPTIONS_VERSION)
ifint(ecode) !=0 {
returnnil, MakeGitError(ecode)
}
returnapplyOptionsFromC(&opts), nil
}
funcpopulateApplyOptions(copts*C.git_apply_options, opts*ApplyOptions, errorTarget*error) *C.git_apply_options {
C.git_apply_options_init(copts, C.GIT_APPLY_OPTIONS_VERSION)
ifopts==nil {
returnnil
}
copts.flags=C.uint(opts.Flags)
ifopts.ApplyDeltaCallback!=nil||opts.ApplyHunkCallback!=nil {
data:=&applyCallbackData{
options: opts,
errorTarget: errorTarget,
}
C._go_git_populate_apply_callbacks(copts)
copts.payload=pointerHandles.Track(data)
}
returncopts
}
funcfreeApplyOptions(copts*C.git_apply_options) {
ifcopts==nil {
return
}
ifcopts.payload!=nil {