- Notifications
You must be signed in to change notification settings - Fork 324
/
Copy pathpatch.go
99 lines (79 loc) · 1.84 KB
/
patch.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
package git
/*
#include <git2.h>
*/
import"C"
import (
"runtime"
"unsafe"
)
typePatchstruct {
doNotCompare
ptr*C.git_patch
}
funcnewPatchFromC(ptr*C.git_patch) *Patch {
ifptr==nil {
returnnil
}
patch:=&Patch{
ptr: ptr,
}
runtime.SetFinalizer(patch, (*Patch).Free)
returnpatch
}
func (patch*Patch) Free() error {
ifpatch.ptr==nil {
returnErrInvalid
}
runtime.SetFinalizer(patch, nil)
C.git_patch_free(patch.ptr)
patch.ptr=nil
returnnil
}
func (patch*Patch) String() (string, error) {
ifpatch.ptr==nil {
return"", ErrInvalid
}
runtime.LockOSThread()
deferruntime.UnlockOSThread()
varbuf C.git_buf
ecode:=C.git_patch_to_buf(&buf, patch.ptr)
runtime.KeepAlive(patch)
ifecode<0 {
return"", MakeGitError(ecode)
}
deferC.git_buf_dispose(&buf)
returnC.GoString(buf.ptr), nil
}
functoPointer(data []byte) (ptr unsafe.Pointer) {
iflen(data) >0 {
ptr=unsafe.Pointer(&data[0])
} else {
ptr=unsafe.Pointer(nil)
}
return
}
func (v*Repository) PatchFromBuffers(oldPath, newPathstring, oldBuf, newBuf []byte, opts*DiffOptions) (*Patch, error) {
varpatchPtr*C.git_patch
oldPtr:=toPointer(oldBuf)
newPtr:=toPointer(newBuf)
cOldPath:=C.CString(oldPath)
deferC.free(unsafe.Pointer(cOldPath))
cNewPath:=C.CString(newPath)
deferC.free(unsafe.Pointer(cNewPath))
varerrerror
copts:=populateDiffOptions(&C.git_diff_options{}, opts, v, &err)
deferfreeDiffOptions(copts)
runtime.LockOSThread()
deferruntime.UnlockOSThread()
ret:=C.git_patch_from_buffers(&patchPtr, oldPtr, C.size_t(len(oldBuf)), cOldPath, newPtr, C.size_t(len(newBuf)), cNewPath, copts)
runtime.KeepAlive(oldBuf)
runtime.KeepAlive(newBuf)
ifret==C.int(ErrorCodeUser) &&err!=nil {
returnnil, err
}
ifret<0 {
returnnil, MakeGitError(ret)
}
returnnewPatchFromC(patchPtr), nil
}