Add Diff.ToBuf wrapping git_diff_to_buf #512

Merged
segevfiner merged 1 commits from diff-to-buf into master 2019-06-18 04:33:29 -05:00
1 changed files with 30 additions and 0 deletions

30
diff.go
View File

@ -405,6 +405,36 @@ func (diff *Diff) Patch(deltaIndex int) (*Patch, error) {
return newPatchFromC(patchPtr), nil
}
type DiffFormat int
const (
DiffFormatPatch DiffFormat = C.GIT_DIFF_FORMAT_PATCH
DiffFormatPatchHeader DiffFormat = C.GIT_DIFF_FORMAT_PATCH_HEADER
DiffFormatRaw DiffFormat = C.GIT_DIFF_FORMAT_RAW
DiffFormatNameOnly DiffFormat = C.GIT_DIFF_FORMAT_NAME_ONLY
DiffFormatNameStatus DiffFormat = C.GIT_DIFF_FORMAT_NAME_STATUS
)
func (diff *Diff) ToBuf(format DiffFormat) ([]byte, error) {
if diff.ptr == nil {
return nil, ErrInvalid
}
diffBuf := C.git_buf{}
runtime.LockOSThread()
defer runtime.UnlockOSThread()
ecode := C.git_diff_to_buf(&diffBuf, diff.ptr, C.git_diff_format_t(format))
runtime.KeepAlive(diff)
if ecode < 0 {
return nil, MakeGitError(ecode)
}
defer C.git_buf_free(&diffBuf)
return C.GoBytes(unsafe.Pointer(diffBuf.ptr), C.int(diffBuf.size)), nil
}
type DiffOptionsFlag int
const (