From e066d24efba9587a66cf9eb6221b4a38c513c26e Mon Sep 17 00:00:00 2001 From: Carlos Martín Nieto Date: Fri, 12 Jun 2015 19:10:00 +0200 Subject: Add DiffBlobs This lets you diff two arbitrary blobs with arbitrary names. --- diff.go | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) (limited to 'diff.go') diff --git a/diff.go b/diff.go index aa422fc..de56374 100644 --- a/diff.go +++ b/diff.go @@ -5,6 +5,7 @@ package git extern int _go_git_diff_foreach(git_diff *diff, int eachFile, int eachHunk, int eachLine, void *payload); extern void _go_git_setup_diff_notify_callbacks(git_diff_options* opts); +extern int _go_git_diff_blobs(git_blob *old, const char *old_path, git_blob *new, const char *new_path, git_diff_options *opts, int eachFile, int eachHunk, int eachLine, void *payload); */ import "C" import ( @@ -670,3 +671,50 @@ func (v *Repository) DiffIndexToWorkdir(index *Index, opts *DiffOptions) (*Diff, } return newDiffFromC(diffPtr), nil } + +// DiffBlobs performs a diff between two arbitrary blobs. You can pass +// whatever file names you'd like for them to appear as in the diff. +func DiffBlobs(oldBlob *Blob, oldAsPath string, newBlob *Blob, newAsPath string, opts *DiffOptions, fileCallback DiffForEachFileCallback, detail DiffDetail) error { + data := &diffForEachData{ + FileCallback: fileCallback, + } + + intHunks := C.int(0) + if detail >= DiffDetailHunks { + intHunks = C.int(1) + } + + intLines := C.int(0) + if detail >= DiffDetailLines { + intLines = C.int(1) + } + + handle := pointerHandles.Track(data) + defer pointerHandles.Untrack(handle) + + var oldBlobPtr, newBlobPtr *C.git_blob + if oldBlob != nil { + oldBlobPtr = oldBlob.cast_ptr + } + if newBlob != nil { + newBlobPtr = newBlob.cast_ptr + } + + oldBlobPath := C.CString(oldAsPath) + defer C.free(unsafe.Pointer(oldBlobPath)) + newBlobPath := C.CString(newAsPath) + defer C.free(unsafe.Pointer(newBlobPath)) + + copts, _ := diffOptionsToC(opts) + defer freeDiffOptions(copts) + + runtime.LockOSThread() + defer runtime.UnlockOSThread() + + ecode := C._go_git_diff_blobs(oldBlobPtr, oldBlobPath, newBlobPtr, newBlobPath, copts, 1, intHunks, intLines, handle) + if ecode < 0 { + return MakeGitError(ecode) + } + + return nil +} -- cgit v1.2.3