diff --git a/diff.go b/diff.go index f762a56..271d692 100644 --- a/diff.go +++ b/diff.go @@ -374,21 +374,8 @@ func diffNotifyCb(_diff_so_far unsafe.Pointer, delta_to_add *C.git_diff_delta, m return 0 } -func (v *Repository) DiffTreeToTree(oldTree, newTree *Tree, opts *DiffOptions) (*Diff, error) { - var diffPtr *C.git_diff - var oldPtr, newPtr *C.git_tree - - if oldTree != nil { - oldPtr = oldTree.cast_ptr - } - - if newTree != nil { - newPtr = newTree.cast_ptr - } - +func diffOptionsToC(opts *DiffOptions) (copts *C.git_diff_options, notifyData *diffNotifyData) { cpathspec := C.git_strarray{} - var copts *C.git_diff_options - var notifyData *diffNotifyData if opts != nil { notifyData = &diffNotifyData{ Callback: opts.NotifyCallback, @@ -396,7 +383,6 @@ func (v *Repository) DiffTreeToTree(oldTree, newTree *Tree, opts *DiffOptions) ( if opts.Pathspec != nil { cpathspec.count = C.size_t(len(opts.Pathspec)) cpathspec.strings = makeCStringsFromStrings(opts.Pathspec) - defer freeStrarray(&cpathspec) } copts = &C.git_diff_options{ @@ -415,6 +401,30 @@ func (v *Repository) DiffTreeToTree(oldTree, newTree *Tree, opts *DiffOptions) ( copts.notify_payload = unsafe.Pointer(notifyData) } } + return +} + +func freeDiffOptions(copts *C.git_diff_options) { + if copts != nil { + cpathspec := copts.pathspec + freeStrarray(&cpathspec) + } +} + +func (v *Repository) DiffTreeToTree(oldTree, newTree *Tree, opts *DiffOptions) (*Diff, error) { + var diffPtr *C.git_diff + var oldPtr, newPtr *C.git_tree + + if oldTree != nil { + oldPtr = oldTree.cast_ptr + } + + if newTree != nil { + newPtr = newTree.cast_ptr + } + + copts, notifyData := diffOptionsToC(opts) + defer freeDiffOptions(copts) ecode := C.git_diff_tree_to_tree(&diffPtr, v.ptr, oldPtr, newPtr, copts) if ecode < 0 { @@ -435,35 +445,8 @@ func (v *Repository) DiffTreeToWorkdir(oldTree *Tree, opts *DiffOptions) (*Diff, oldPtr = oldTree.cast_ptr } - cpathspec := C.git_strarray{} - var copts *C.git_diff_options - var notifyData *diffNotifyData - if opts != nil { - notifyData = &diffNotifyData{ - Callback: opts.NotifyCallback, - } - if opts.Pathspec != nil { - cpathspec.count = C.size_t(len(opts.Pathspec)) - cpathspec.strings = makeCStringsFromStrings(opts.Pathspec) - defer freeStrarray(&cpathspec) - } - - copts = &C.git_diff_options{ - version: C.GIT_DIFF_OPTIONS_VERSION, - flags: C.uint32_t(opts.Flags), - ignore_submodules: C.git_submodule_ignore_t(opts.IgnoreSubmodules), - pathspec: cpathspec, - context_lines: C.uint16_t(opts.ContextLines), - interhunk_lines: C.uint16_t(opts.InterhunkLines), - id_abbrev: C.uint16_t(opts.IdAbbrev), - max_size: C.git_off_t(opts.MaxSize), - } - - if opts.NotifyCallback != nil { - C._go_git_setup_diff_notify_callbacks(copts) - copts.notify_payload = unsafe.Pointer(notifyData) - } - } + copts, notifyData := diffOptionsToC(opts) + defer freeDiffOptions(copts) ecode := C.git_diff_tree_to_workdir(&diffPtr, v.ptr, oldPtr, copts) if ecode < 0 { @@ -474,5 +457,4 @@ func (v *Repository) DiffTreeToWorkdir(oldTree *Tree, opts *DiffOptions) (*Diff, return notifyData.Diff, nil } return newDiffFromC(diffPtr), nil - } diff --git a/patch.go b/patch.go index 0665501..3e96d36 100644 --- a/patch.go +++ b/patch.go @@ -6,6 +6,7 @@ package git import "C" import ( "runtime" + "unsafe" ) type Patch struct { @@ -46,3 +47,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 +}