diff options
| author | Carlos Martín Nieto <[email protected]> | 2014-12-11 02:42:29 +0100 |
|---|---|---|
| committer | Carlos Martín Nieto <[email protected]> | 2014-12-11 02:42:29 +0100 |
| commit | 1198f829b1f1e2dc907445769a2a1ea30f5df571 (patch) | |
| tree | 89933385ba4c47beab8eab88929bf748f68db3b3 /patch.go | |
| parent | 7762e1a3a963d3e00c4709b284d30ff843ea30ff (diff) | |
| parent | db17135a30100c698d3efa1c98ba717201f426ce (diff) | |
Merge pull request #148 from joseferminj/export-patch-from-branches
Export PatchFromBuffers function.
Diffstat (limited to 'patch.go')
| -rw-r--r-- | patch.go | 32 |
1 files changed, 32 insertions, 0 deletions
@@ -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 +} |
