2014-02-20 00:25:30 -06:00
|
|
|
package git
|
|
|
|
|
|
|
|
/*
|
|
|
|
#include <git2.h>
|
|
|
|
*/
|
|
|
|
import "C"
|
|
|
|
import (
|
|
|
|
"runtime"
|
2014-12-05 15:52:15 -06:00
|
|
|
"unsafe"
|
2014-02-20 00:25:30 -06:00
|
|
|
)
|
|
|
|
|
|
|
|
type Patch struct {
|
|
|
|
ptr *C.git_patch
|
|
|
|
}
|
|
|
|
|
2014-03-20 23:56:41 -05:00
|
|
|
func newPatchFromC(ptr *C.git_patch) *Patch {
|
2014-02-20 00:25:30 -06:00
|
|
|
if ptr == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
patch := &Patch{
|
|
|
|
ptr: ptr,
|
|
|
|
}
|
|
|
|
|
|
|
|
runtime.SetFinalizer(patch, (*Patch).Free)
|
|
|
|
return patch
|
|
|
|
}
|
|
|
|
|
2014-03-20 23:56:41 -05:00
|
|
|
func (patch *Patch) Free() error {
|
|
|
|
if patch.ptr == nil {
|
|
|
|
return ErrInvalid
|
|
|
|
}
|
2014-02-20 00:25:30 -06:00
|
|
|
runtime.SetFinalizer(patch, nil)
|
|
|
|
C.git_patch_free(patch.ptr)
|
2014-03-21 01:19:22 -05:00
|
|
|
patch.ptr = nil
|
2014-03-20 23:56:41 -05:00
|
|
|
return nil
|
2014-02-20 00:25:30 -06:00
|
|
|
}
|
|
|
|
|
2014-03-20 23:56:41 -05:00
|
|
|
func (patch *Patch) String() (string, error) {
|
2014-03-21 00:54:18 -05:00
|
|
|
if patch.ptr == nil {
|
2014-03-20 23:56:41 -05:00
|
|
|
return "", ErrInvalid
|
|
|
|
}
|
2014-12-05 19:44:57 -06:00
|
|
|
|
|
|
|
runtime.LockOSThread()
|
|
|
|
defer runtime.UnlockOSThread()
|
|
|
|
|
2017-03-23 14:16:43 -05:00
|
|
|
var buf C.git_buf
|
|
|
|
|
2014-03-22 00:16:26 -05:00
|
|
|
ecode := C.git_patch_to_buf(&buf, patch.ptr)
|
2017-07-08 09:07:51 -05:00
|
|
|
runtime.KeepAlive(patch)
|
2014-03-22 00:16:26 -05:00
|
|
|
if ecode < 0 {
|
|
|
|
return "", MakeGitError(ecode)
|
|
|
|
}
|
2018-08-08 04:51:51 -05:00
|
|
|
defer C.git_buf_dispose(&buf)
|
2017-03-23 14:16:43 -05:00
|
|
|
|
2014-03-21 00:54:18 -05:00
|
|
|
return C.GoString(buf.ptr), nil
|
2014-02-20 00:25:30 -06:00
|
|
|
}
|
2014-12-05 15:52:15 -06:00
|
|
|
|
|
|
|
func toPointer(data []byte) (ptr unsafe.Pointer) {
|
|
|
|
if len(data) > 0 {
|
|
|
|
ptr = unsafe.Pointer(&data[0])
|
|
|
|
} else {
|
|
|
|
ptr = unsafe.Pointer(nil)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v *Repository) PatchFromBuffers(oldPath, newPath string, oldBuf, newBuf []byte, opts *DiffOptions) (*Patch, error) {
|
|
|
|
var patchPtr *C.git_patch
|
|
|
|
|
|
|
|
oldPtr := toPointer(oldBuf)
|
2017-09-29 02:49:21 -05:00
|
|
|
newPtr := toPointer(newBuf)
|
2014-12-05 15:52:15 -06:00
|
|
|
|
|
|
|
cOldPath := C.CString(oldPath)
|
|
|
|
defer C.free(unsafe.Pointer(cOldPath))
|
|
|
|
|
|
|
|
cNewPath := C.CString(newPath)
|
|
|
|
defer C.free(unsafe.Pointer(cNewPath))
|
|
|
|
|
2020-12-05 15:13:59 -06:00
|
|
|
var err error
|
2020-12-10 09:19:41 -06:00
|
|
|
copts := populateDiffOptions(&C.git_diff_options{}, opts, v, &err)
|
2014-12-05 15:52:15 -06:00
|
|
|
defer freeDiffOptions(copts)
|
|
|
|
|
2014-12-10 19:46:42 -06:00
|
|
|
runtime.LockOSThread()
|
|
|
|
defer runtime.UnlockOSThread()
|
|
|
|
|
2020-12-05 15:13:59 -06:00
|
|
|
ret := C.git_patch_from_buffers(&patchPtr, oldPtr, C.size_t(len(oldBuf)), cOldPath, newPtr, C.size_t(len(newBuf)), cNewPath, copts)
|
2017-07-08 09:07:51 -05:00
|
|
|
runtime.KeepAlive(oldBuf)
|
|
|
|
runtime.KeepAlive(newBuf)
|
2020-12-05 15:13:59 -06:00
|
|
|
if ret == C.int(ErrorCodeUser) && err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if ret < 0 {
|
|
|
|
return nil, MakeGitError(ret)
|
2014-12-05 15:52:15 -06:00
|
|
|
}
|
2020-12-05 15:13:59 -06:00
|
|
|
|
2014-12-05 15:52:15 -06:00
|
|
|
return newPatchFromC(patchPtr), nil
|
|
|
|
}
|