2013-03-05 13:53:04 -06:00
|
|
|
package git
|
|
|
|
|
|
|
|
/*
|
|
|
|
#include <git2.h>
|
2014-03-07 18:43:20 -06:00
|
|
|
#include <string.h>
|
|
|
|
|
2016-04-21 09:34:51 -05:00
|
|
|
int _go_git_writestream_write(git_writestream *stream, const char *buffer, size_t len);
|
|
|
|
void _go_git_writestream_free(git_writestream *stream);
|
2013-03-05 13:53:04 -06:00
|
|
|
*/
|
|
|
|
import "C"
|
|
|
|
import (
|
2016-04-21 09:34:51 -05:00
|
|
|
"reflect"
|
2014-03-07 18:43:20 -06:00
|
|
|
"runtime"
|
2013-03-05 13:53:04 -06:00
|
|
|
"unsafe"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Blob struct {
|
2021-09-05 15:59:36 -05:00
|
|
|
doNotCompare
|
2015-08-04 07:47:10 -05:00
|
|
|
Object
|
2014-04-01 05:13:37 -05:00
|
|
|
cast_ptr *C.git_blob
|
2013-04-16 16:04:35 -05:00
|
|
|
}
|
|
|
|
|
2017-07-08 15:53:50 -05:00
|
|
|
func (b *Blob) AsObject() *Object {
|
|
|
|
return &b.Object
|
|
|
|
}
|
|
|
|
|
2014-03-07 18:43:20 -06:00
|
|
|
func (v *Blob) Size() int64 {
|
2017-07-07 16:45:09 -05:00
|
|
|
ret := int64(C.git_blob_rawsize(v.cast_ptr))
|
|
|
|
runtime.KeepAlive(v)
|
|
|
|
return ret
|
2013-03-06 16:59:33 -06:00
|
|
|
}
|
|
|
|
|
2014-03-07 18:43:20 -06:00
|
|
|
func (v *Blob) Contents() []byte {
|
2014-04-01 05:13:37 -05:00
|
|
|
size := C.int(C.git_blob_rawsize(v.cast_ptr))
|
|
|
|
buffer := unsafe.Pointer(C.git_blob_rawcontent(v.cast_ptr))
|
2017-07-07 16:45:09 -05:00
|
|
|
|
|
|
|
goBytes := C.GoBytes(buffer, size)
|
|
|
|
runtime.KeepAlive(v)
|
|
|
|
|
|
|
|
return goBytes
|
2013-03-05 13:53:04 -06:00
|
|
|
}
|
|
|
|
|
2020-07-30 07:07:05 -05:00
|
|
|
func (v *Blob) IsBinary() bool {
|
|
|
|
ret := C.git_blob_is_binary(v.cast_ptr) == 1
|
|
|
|
runtime.KeepAlive(v)
|
|
|
|
return ret
|
|
|
|
}
|
|
|
|
|
2014-03-07 18:43:20 -06:00
|
|
|
func (repo *Repository) CreateBlobFromBuffer(data []byte) (*Oid, error) {
|
|
|
|
runtime.LockOSThread()
|
|
|
|
defer runtime.UnlockOSThread()
|
2014-08-25 08:44:01 -05:00
|
|
|
|
|
|
|
var id C.git_oid
|
2016-08-27 13:51:13 -05:00
|
|
|
var size C.size_t
|
2014-08-25 08:44:01 -05:00
|
|
|
|
2016-08-27 13:51:13 -05:00
|
|
|
// Go 1.6 added some increased checking of passing pointer to
|
2017-07-11 22:52:13 -05:00
|
|
|
// C, but its check depends on its expectations of what we
|
2016-08-27 13:51:13 -05:00
|
|
|
// pass to the C function, so unless we take the address of
|
|
|
|
// its contents at the call site itself, it can fail when
|
|
|
|
// 'data' is a slice of a slice.
|
|
|
|
//
|
|
|
|
// When we're given an empty slice, create a dummy one where 0
|
|
|
|
// isn't out of bounds.
|
2014-08-25 08:44:01 -05:00
|
|
|
if len(data) > 0 {
|
2016-08-27 13:51:13 -05:00
|
|
|
size = C.size_t(len(data))
|
2014-08-25 08:44:01 -05:00
|
|
|
} else {
|
2016-08-27 13:51:13 -05:00
|
|
|
data = []byte{0}
|
|
|
|
size = C.size_t(0)
|
2014-08-25 08:44:01 -05:00
|
|
|
}
|
|
|
|
|
2020-12-05 09:23:44 -06:00
|
|
|
ecode := C.git_blob_create_from_buffer(&id, repo.ptr, unsafe.Pointer(&data[0]), size)
|
2017-07-07 16:45:09 -05:00
|
|
|
runtime.KeepAlive(repo)
|
2014-03-07 18:43:20 -06:00
|
|
|
if ecode < 0 {
|
|
|
|
return nil, MakeGitError(ecode)
|
|
|
|
}
|
2014-08-25 08:44:01 -05:00
|
|
|
return newOidFromC(&id), nil
|
2014-03-07 18:43:20 -06:00
|
|
|
}
|
|
|
|
|
2016-04-21 09:34:51 -05:00
|
|
|
func (repo *Repository) CreateFromStream(hintPath string) (*BlobWriteStream, error) {
|
2014-03-07 18:43:20 -06:00
|
|
|
var chintPath *C.char = nil
|
2016-04-21 09:34:51 -05:00
|
|
|
var stream *C.git_writestream
|
|
|
|
|
2014-03-07 18:43:20 -06:00
|
|
|
if len(hintPath) > 0 {
|
2016-01-04 08:02:21 -06:00
|
|
|
chintPath = C.CString(hintPath)
|
2014-03-07 18:43:20 -06:00
|
|
|
defer C.free(unsafe.Pointer(chintPath))
|
|
|
|
}
|
2015-04-24 03:13:52 -05:00
|
|
|
|
2016-04-21 09:34:51 -05:00
|
|
|
runtime.LockOSThread()
|
|
|
|
defer runtime.UnlockOSThread()
|
|
|
|
|
2020-12-05 09:23:44 -06:00
|
|
|
ecode := C.git_blob_create_from_stream(&stream, repo.ptr, chintPath)
|
2016-04-21 09:34:51 -05:00
|
|
|
if ecode < 0 {
|
|
|
|
return nil, MakeGitError(ecode)
|
|
|
|
}
|
|
|
|
|
2017-07-07 16:45:09 -05:00
|
|
|
return newBlobWriteStreamFromC(stream, repo), nil
|
2016-04-21 09:34:51 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
type BlobWriteStream struct {
|
2021-09-05 15:59:36 -05:00
|
|
|
doNotCompare
|
2017-07-07 16:45:09 -05:00
|
|
|
ptr *C.git_writestream
|
|
|
|
repo *Repository
|
2016-04-21 09:34:51 -05:00
|
|
|
}
|
|
|
|
|
2017-07-07 16:45:09 -05:00
|
|
|
func newBlobWriteStreamFromC(ptr *C.git_writestream, repo *Repository) *BlobWriteStream {
|
2016-04-21 09:34:51 -05:00
|
|
|
stream := &BlobWriteStream{
|
2017-07-07 16:45:09 -05:00
|
|
|
ptr: ptr,
|
|
|
|
repo: repo,
|
2016-04-21 09:34:51 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
runtime.SetFinalizer(stream, (*BlobWriteStream).Free)
|
|
|
|
return stream
|
|
|
|
}
|
|
|
|
|
|
|
|
// Implement io.Writer
|
|
|
|
func (stream *BlobWriteStream) Write(p []byte) (int, error) {
|
|
|
|
header := (*reflect.SliceHeader)(unsafe.Pointer(&p))
|
|
|
|
ptr := (*C.char)(unsafe.Pointer(header.Data))
|
|
|
|
size := C.size_t(header.Len)
|
2015-04-24 03:13:52 -05:00
|
|
|
|
2016-04-21 09:34:51 -05:00
|
|
|
runtime.LockOSThread()
|
|
|
|
defer runtime.UnlockOSThread()
|
|
|
|
|
|
|
|
ecode := C._go_git_writestream_write(stream.ptr, ptr, size)
|
2017-07-07 16:45:09 -05:00
|
|
|
runtime.KeepAlive(stream)
|
2016-04-21 09:34:51 -05:00
|
|
|
if ecode < 0 {
|
|
|
|
return 0, MakeGitError(ecode)
|
2014-03-07 18:43:20 -06:00
|
|
|
}
|
2016-04-21 09:34:51 -05:00
|
|
|
|
|
|
|
return len(p), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (stream *BlobWriteStream) Free() {
|
|
|
|
runtime.SetFinalizer(stream, nil)
|
|
|
|
C._go_git_writestream_free(stream.ptr)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (stream *BlobWriteStream) Commit() (*Oid, error) {
|
|
|
|
oid := C.git_oid{}
|
|
|
|
|
|
|
|
runtime.LockOSThread()
|
|
|
|
defer runtime.UnlockOSThread()
|
|
|
|
|
2020-12-05 09:23:44 -06:00
|
|
|
ecode := C.git_blob_create_from_stream_commit(&oid, stream.ptr)
|
2017-07-07 16:45:09 -05:00
|
|
|
runtime.KeepAlive(stream)
|
2014-03-07 18:43:20 -06:00
|
|
|
if ecode < 0 {
|
|
|
|
return nil, MakeGitError(ecode)
|
|
|
|
}
|
2016-04-21 09:34:51 -05:00
|
|
|
|
2014-03-07 18:43:20 -06:00
|
|
|
return newOidFromC(&oid), nil
|
|
|
|
}
|