From db17135a30100c698d3efa1c98ba717201f426ce Mon Sep 17 00:00:00 2001 From: Jose Alvarez Date: Fri, 5 Dec 2014 16:52:15 -0500 Subject: Export PatchFromBuffers function. This change also factor out diffOptionsToC function to remove duplicated code. --- patch.go | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) (limited to 'patch.go') diff --git a/patch.go b/patch.go index 0fe1c04..ea01b87 100644 --- a/patch.go +++ b/patch.go @@ -6,6 +6,7 @@ package git import "C" import ( "runtime" + "unsafe" ) type Patch struct { @@ -50,3 +51,34 @@ func (patch *Patch) String() (string, error) { } return C.GoString(buf.ptr), nil } + +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) + newPtr := (*C.char)(toPointer(newBuf)) + + cOldPath := C.CString(oldPath) + defer C.free(unsafe.Pointer(cOldPath)) + + cNewPath := C.CString(newPath) + defer C.free(unsafe.Pointer(cNewPath)) + + copts, _ := diffOptionsToC(opts) + defer freeDiffOptions(copts) + + ecode := C.git_patch_from_buffers(&patchPtr, oldPtr, C.size_t(len(oldBuf)), cOldPath, newPtr, C.size_t(len(newBuf)), cNewPath, copts) + if ecode < 0 { + return nil, MakeGitError(ecode) + } + return newPatchFromC(patchPtr), nil +} -- cgit v1.2.3