2014-02-20 00:25:30 -06:00
|
|
|
package git
|
|
|
|
|
|
|
|
/*
|
|
|
|
#include <git2.h>
|
|
|
|
*/
|
|
|
|
import "C"
|
|
|
|
import (
|
|
|
|
"runtime"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Patch struct {
|
|
|
|
ptr *C.git_patch
|
|
|
|
}
|
|
|
|
|
2014-03-20 23:56:41 -05:00
|
|
|
func newPatchFromC(ptr *C.git_patch) *Patch {
|
2014-02-20 00:25:30 -06:00
|
|
|
if ptr == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
patch := &Patch{
|
|
|
|
ptr: ptr,
|
|
|
|
}
|
|
|
|
|
|
|
|
runtime.SetFinalizer(patch, (*Patch).Free)
|
|
|
|
return patch
|
|
|
|
}
|
|
|
|
|
2014-03-20 23:56:41 -05:00
|
|
|
func (patch *Patch) Free() error {
|
|
|
|
if patch.ptr == nil {
|
|
|
|
return ErrInvalid
|
|
|
|
}
|
2014-02-20 00:25:30 -06:00
|
|
|
runtime.SetFinalizer(patch, nil)
|
|
|
|
C.git_patch_free(patch.ptr)
|
2014-03-21 01:19:22 -05:00
|
|
|
patch.ptr = nil
|
2014-03-20 23:56:41 -05:00
|
|
|
return nil
|
2014-02-20 00:25:30 -06:00
|
|
|
}
|
|
|
|
|
2014-03-20 23:56:41 -05:00
|
|
|
func (patch *Patch) String() (string, error) {
|
2014-03-21 00:54:18 -05:00
|
|
|
if patch.ptr == nil {
|
2014-03-20 23:56:41 -05:00
|
|
|
return "", ErrInvalid
|
|
|
|
}
|
2014-03-21 00:54:18 -05:00
|
|
|
var buf C.git_buf
|
2014-03-22 00:16:26 -05:00
|
|
|
ecode := C.git_patch_to_buf(&buf, patch.ptr)
|
|
|
|
if ecode < 0 {
|
|
|
|
return "", MakeGitError(ecode)
|
|
|
|
}
|
2014-03-21 00:54:18 -05:00
|
|
|
return C.GoString(buf.ptr), nil
|
2014-02-20 00:25:30 -06:00
|
|
|
}
|