Ok, now with shared base object
This commit is contained in:
parent
7292cafac2
commit
2bf17ba2f1
20
blob.go
20
blob.go
|
@ -8,31 +8,17 @@ package git
|
|||
import "C"
|
||||
import (
|
||||
"unsafe"
|
||||
"runtime"
|
||||
)
|
||||
|
||||
type Blob struct {
|
||||
ptr *C.git_blob
|
||||
gitObject
|
||||
}
|
||||
|
||||
func (o *Blob) Id() *Oid {
|
||||
return newOidFromC(C.git_blob_id(o.ptr))
|
||||
}
|
||||
|
||||
func (o *Blob) Type() ObjectType {
|
||||
return OBJ_BLOB
|
||||
}
|
||||
|
||||
func (o *Blob) Free() {
|
||||
runtime.SetFinalizer(o, nil)
|
||||
C.git_blob_free(o.ptr)
|
||||
}
|
||||
|
||||
func (v *Blob) Size() int64 {
|
||||
func (v Blob) Size() int64 {
|
||||
return int64(C.git_blob_rawsize(v.ptr))
|
||||
}
|
||||
|
||||
func (v *Blob) Contents() []byte {
|
||||
func (v Blob) Contents() []byte {
|
||||
size := C.int(C.git_blob_rawsize(v.ptr))
|
||||
buffer := unsafe.Pointer(C.git_blob_rawcontent(v.ptr))
|
||||
return C.GoBytes(buffer, size)
|
||||
|
|
26
commit.go
26
commit.go
|
@ -9,34 +9,20 @@ extern int _go_git_treewalk(git_tree *tree, git_treewalk_mode mode, void *ptr);
|
|||
import "C"
|
||||
|
||||
import (
|
||||
"runtime"
|
||||
"unsafe"
|
||||
"time"
|
||||
)
|
||||
|
||||
// Commit
|
||||
type Commit struct {
|
||||
ptr *C.git_commit
|
||||
gitObject
|
||||
}
|
||||
|
||||
func (o *Commit) Id() *Oid {
|
||||
return newOidFromC(C.git_commit_id(o.ptr))
|
||||
}
|
||||
|
||||
func (o *Commit) Type() ObjectType {
|
||||
return OBJ_COMMIT
|
||||
}
|
||||
|
||||
func (o *Commit) Free() {
|
||||
runtime.SetFinalizer(o, nil)
|
||||
C.git_commit_free(o.ptr)
|
||||
}
|
||||
|
||||
func (c *Commit) Message() string {
|
||||
func (c Commit) Message() string {
|
||||
return C.GoString(C.git_commit_message(c.ptr))
|
||||
}
|
||||
|
||||
func (c *Commit) Tree() (*Tree, error) {
|
||||
func (c Commit) Tree() (*Tree, error) {
|
||||
var ptr *C.git_object
|
||||
|
||||
err := C.git_commit_tree(&ptr, c.ptr)
|
||||
|
@ -47,16 +33,16 @@ func (c *Commit) Tree() (*Tree, error) {
|
|||
return allocObject(ptr).(*Tree), nil
|
||||
}
|
||||
|
||||
func (c *Commit) TreeId() *Oid {
|
||||
func (c Commit) TreeId() *Oid {
|
||||
return newOidFromC(C.git_commit_tree_id(c.ptr))
|
||||
}
|
||||
|
||||
func (c *Commit) Author() *Signature {
|
||||
func (c Commit) Author() *Signature {
|
||||
ptr := C.git_commit_author(c.ptr)
|
||||
return newSignatureFromC(ptr)
|
||||
}
|
||||
|
||||
func (c *Commit) Committer() *Signature {
|
||||
func (c Commit) Committer() *Signature {
|
||||
ptr := C.git_commit_committer(c.ptr)
|
||||
return newSignatureFromC(ptr)
|
||||
}
|
||||
|
|
38
object.go
38
object.go
|
@ -25,25 +25,41 @@ type Object interface {
|
|||
Type() ObjectType
|
||||
}
|
||||
|
||||
type gitObject struct {
|
||||
ptr *C.git_object
|
||||
}
|
||||
|
||||
func (o gitObject) Id() *Oid {
|
||||
return newOidFromC(C.git_commit_id(o.ptr))
|
||||
}
|
||||
|
||||
func (o gitObject) Type() ObjectType {
|
||||
return ObjectType(C.git_object_type(o.ptr))
|
||||
}
|
||||
|
||||
func (o gitObject) Free() {
|
||||
runtime.SetFinalizer(o, nil)
|
||||
C.git_commit_free(o.ptr)
|
||||
}
|
||||
|
||||
func allocObject(cobj *C.git_object) Object {
|
||||
var object Object
|
||||
|
||||
switch ObjectType(C.git_object_type(cobj)) {
|
||||
case OBJ_COMMIT:
|
||||
object = &Commit{cobj}
|
||||
runtime.SetFinalizer(object, (*Commit).Free)
|
||||
commit := &Commit{gitObject{cobj}}
|
||||
runtime.SetFinalizer(commit, (*Commit).Free)
|
||||
return commit
|
||||
|
||||
case OBJ_TREE:
|
||||
object = &Tree{cobj}
|
||||
runtime.SetFinalizer(object, (*Tree).Free)
|
||||
tree := &Tree{gitObject{cobj}}
|
||||
runtime.SetFinalizer(tree, (*Tree).Free)
|
||||
return tree
|
||||
|
||||
case OBJ_BLOB:
|
||||
object = &Blob{cobj}
|
||||
runtime.SetFinalizer(object, (*Blob).Free)
|
||||
|
||||
default:
|
||||
return nil
|
||||
blob := &Blob{gitObject{cobj}}
|
||||
runtime.SetFinalizer(blob, (*Blob).Free)
|
||||
return blob
|
||||
}
|
||||
|
||||
return object
|
||||
return nil
|
||||
}
|
||||
|
|
23
tree.go
23
tree.go
|
@ -14,20 +14,7 @@ import (
|
|||
)
|
||||
|
||||
type Tree struct {
|
||||
ptr *C.git_tree
|
||||
}
|
||||
|
||||
func (o *Tree) Id() *Oid {
|
||||
return newOidFromC(C.git_tree_id(o.ptr))
|
||||
}
|
||||
|
||||
func (o *Tree) Type() ObjectType {
|
||||
return OBJ_TREE
|
||||
}
|
||||
|
||||
func (o *Tree) Free() {
|
||||
runtime.SetFinalizer(o, nil)
|
||||
C.git_tree_free(o.ptr)
|
||||
gitObject
|
||||
}
|
||||
|
||||
type TreeEntry struct {
|
||||
|
@ -44,7 +31,7 @@ func newTreeEntry(entry *C.git_tree_entry) *TreeEntry {
|
|||
}
|
||||
}
|
||||
|
||||
func (t *Tree) EntryByName(filename string) *TreeEntry {
|
||||
func (t Tree) EntryByName(filename string) *TreeEntry {
|
||||
cname := C.CString(filename)
|
||||
defer C.free(unsafe.Pointer(cname))
|
||||
|
||||
|
@ -56,7 +43,7 @@ func (t *Tree) EntryByName(filename string) *TreeEntry {
|
|||
return newTreeEntry(entry)
|
||||
}
|
||||
|
||||
func (t *Tree) EntryByIndex(index uint64) *TreeEntry {
|
||||
func (t Tree) EntryByIndex(index uint64) *TreeEntry {
|
||||
entry := C.git_tree_entry_byindex(t.ptr, C.size_t(index))
|
||||
if entry == nil {
|
||||
return nil
|
||||
|
@ -65,7 +52,7 @@ func (t *Tree) EntryByIndex(index uint64) *TreeEntry {
|
|||
return newTreeEntry(entry)
|
||||
}
|
||||
|
||||
func (t *Tree) EntryCount() uint64 {
|
||||
func (t Tree) EntryCount() uint64 {
|
||||
num := C.git_tree_entrycount(t.ptr)
|
||||
return uint64(num)
|
||||
}
|
||||
|
@ -81,7 +68,7 @@ func CallbackGitTreeWalk(_root unsafe.Pointer, _entry unsafe.Pointer, ptr unsafe
|
|||
return C.int(callback(root, newTreeEntry(entry)))
|
||||
}
|
||||
|
||||
func (t *Tree) Walk(callback TreeWalkCallback) error {
|
||||
func (t Tree) Walk(callback TreeWalkCallback) error {
|
||||
err := C._go_git_treewalk(
|
||||
t.ptr,
|
||||
C.GIT_TREEWALK_PRE,
|
||||
|
|
Loading…
Reference in New Issue