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
|
package git
/*
#include <git2.h>
extern int _go_git_diff_foreach(git_diff *diff, int eachFile, int eachHunk, int eachLine, void *payload);
*/
import "C"
import (
"runtime"
"unsafe"
)
type Delta int
type DiffFlag int
const (
DiffFlagBinary = DiffFlag(C.GIT_DIFF_FLAG_BINARY)
DiffFlagNotBinary = DiffFlag(C.GIT_DIFF_FLAG_NOT_BINARY)
DiffFlagValidOid = DiffFlag(C.GIT_DIFF_FLAG_VALID_OID)
DeltaUnmodified = Delta(C.GIT_DELTA_UNMODIFIED)
DeltaAdded = Delta(C.GIT_DELTA_ADDED)
DeltaDeleted = Delta(C.GIT_DELTA_DELETED)
DeltaModified = Delta(C.GIT_DELTA_MODIFIED)
DeltaRenamed = Delta(C.GIT_DELTA_RENAMED)
DeltaCopied = Delta(C.GIT_DELTA_COPIED)
DeltaIgnored = Delta(C.GIT_DELTA_IGNORED)
DeltaUntracked = Delta(C.GIT_DELTA_UNTRACKED)
DeltaTypeChange = Delta(C.GIT_DELTA_TYPECHANGE)
DiffLineContext = C.GIT_DIFF_LINE_CONTEXT
DiffLineAddition = C.GIT_DIFF_LINE_ADDITION
DiffLineDeletion = C.GIT_DIFF_LINE_DELETION
DiffLineContextEOFNL = C.GIT_DIFF_LINE_CONTEXT_EOFNL
DiffLineAddEOFNL = C.GIT_DIFF_LINE_ADD_EOFNL
DiffLineDelEOFNL = C.GIT_DIFF_LINE_DEL_EOFNL
DiffLineFileHdr = C.GIT_DIFF_LINE_FILE_HDR
DiffLineHunkHdr = C.GIT_DIFF_LINE_HUNK_HDR
DiffLineBinary = C.GIT_DIFF_LINE_BINARY
)
type DiffFile struct {
file C.git_diff_file
Path string
}
func newDiffFile(file *C.git_diff_file) *DiffFile {
return &DiffFile{
file: *file,
Path: C.GoString(file.path),
}
}
func (df *DiffFile) Oid() *Oid {
return newOidFromC(&df.file.oid)
}
func (df *DiffFile) Size() int {
return int(df.file.size)
}
func (df *DiffFile) Flags() uint32 {
return uint32(df.file.flags)
}
func (df *DiffFile) Mode() uint16 {
return uint16(df.file.mode)
}
type DiffDelta struct {
delta C.git_diff_delta
OldFile *DiffFile
NewFile *DiffFile
}
func newDiffDelta(delta *C.git_diff_delta) *DiffDelta {
return &DiffDelta{
delta: *delta,
OldFile: newDiffFile(&delta.old_file),
NewFile: newDiffFile(&delta.new_file),
}
}
func (dd *DiffDelta) Status() Delta {
return Delta(dd.delta.status)
}
func (dd *DiffDelta) Flags() DiffFlag {
return DiffFlag(dd.delta.flags)
}
func (dd *DiffDelta) Similarity() uint16 {
return uint16(dd.delta.similarity)
}
type DiffHunk struct {
hunk C.git_diff_hunk
Header string
DiffDelta
}
func newDiffHunk(delta *C.git_diff_delta, hunk *C.git_diff_hunk) *DiffHunk {
return &DiffHunk{
hunk: *hunk,
Header: C.GoStringN(&hunk.header[0], C.int(hunk.header_len)),
DiffDelta: *newDiffDelta(delta),
}
}
func (dh *DiffHunk) OldStart() int {
return int(dh.hunk.old_start)
}
func (dh *DiffHunk) OldLines() int {
return int(dh.hunk.old_lines)
}
func (dh *DiffHunk) NewStart() int {
return int(dh.hunk.new_start)
}
func (dh *DiffHunk) NewLines() int {
return int(dh.hunk.new_lines)
}
type DiffLine struct {
Origin byte
OldLineno int
NewLineno int
NumLines int
Content string
DiffHunk
}
func newDiffLine(delta *C.git_diff_delta, hunk *C.git_diff_hunk, line *C.git_diff_line) *DiffLine {
return &DiffLine{
Origin: byte(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)),
DiffHunk: *newDiffHunk(delta, hunk),
}
}
type Diff struct {
ptr *C.git_diff
}
func newDiff(ptr *C.git_diff) *Diff {
if ptr == nil {
return nil
}
diff := &Diff{
ptr: ptr,
}
runtime.SetFinalizer(diff, (*Diff).Free)
return diff
}
func (diff *Diff) Free() {
runtime.SetFinalizer(diff, nil)
C.git_diff_free(diff.ptr)
}
func (diff *Diff) forEachFileWrap(ch chan *DiffDelta) {
C._go_git_diff_foreach(diff.ptr, 1, 0, 0, unsafe.Pointer(&ch))
close(ch)
}
func (diff *Diff) ForEachFile() chan *DiffDelta {
ch := make(chan *DiffDelta, 0)
go diff.forEachFileWrap(ch)
return ch
}
//export diffForEachFileCb
func diffForEachFileCb(delta *C.git_diff_delta, progress C.float, payload unsafe.Pointer) int {
ch := *(*chan *DiffDelta)(payload)
select {
case ch <-newDiffDelta(delta):
case <-ch:
return -1
}
return 0
}
func (diff *Diff) forEachHunkWrap(ch chan *DiffHunk) {
C._go_git_diff_foreach(diff.ptr, 0, 1, 0, unsafe.Pointer(&ch))
close(ch)
}
func (diff *Diff) ForEachHunk() chan *DiffHunk {
ch := make(chan *DiffHunk, 0)
go diff.forEachHunkWrap(ch)
return ch
}
//export diffForEachHunkCb
func diffForEachHunkCb(delta *C.git_diff_delta, hunk *C.git_diff_hunk, payload unsafe.Pointer) int {
ch := *(*chan *DiffHunk)(payload)
select {
case ch <-newDiffHunk(delta, hunk):
case <-ch:
return -1
}
return 0
}
func (diff *Diff) forEachLineWrap(ch chan *DiffLine) {
C._go_git_diff_foreach(diff.ptr, 0, 0, 1, unsafe.Pointer(&ch))
close(ch)
}
func (diff *Diff) ForEachLine() chan *DiffLine {
ch := make(chan *DiffLine, 0)
go diff.forEachLineWrap(ch)
return ch
}
//export diffForEachLineCb
func diffForEachLineCb(delta *C.git_diff_delta, hunk *C.git_diff_hunk, line *C.git_diff_line, payload unsafe.Pointer) int {
ch := *(*chan *DiffLine)(payload)
select {
case ch <-newDiffLine(delta, hunk, line):
case <-ch:
return -1
}
return 0
}
func (diff *Diff) NumDeltas() int {
return int(C.git_diff_num_deltas(diff.ptr))
}
func (diff *Diff) GetDelta(index int) *DiffDelta {
ptr := C.git_diff_get_delta(diff.ptr, C.size_t(index))
if ptr == nil {
return nil
}
return newDiffDelta(ptr)
}
func (diff *Diff) Patch(deltaIndex int) *Patch {
var patchPtr *C.git_patch
C.git_patch_from_diff(&patchPtr, diff.ptr, C.size_t(deltaIndex))
return newPatch(patchPtr)
}
|