git2go/object.go

107 lines
1.8 KiB
Go
Raw Normal View History

2013-04-16 16:04:35 -05:00
package git
/*
#include <git2.h>
#include <git2/errors.h>
*/
import "C"
import "runtime"
type ObjectType int
const (
ObjectAny ObjectType = C.GIT_OBJ_ANY
ObjectBad = C.GIT_OBJ_BAD
ObjectCommit = C.GIT_OBJ_COMMIT
ObjectTree = C.GIT_OBJ_TREE
ObjectBlob = C.GIT_OBJ_BLOB
ObjectTag = C.GIT_OBJ_TAG
2013-04-16 16:04:35 -05:00
)
type Object interface {
Free()
Id() *Oid
Type() ObjectType
Owner() *Repository
2013-04-16 16:04:35 -05:00
}
2013-04-17 17:54:46 -05:00
type gitObject struct {
ptr *C.git_object
repo *Repository
2013-04-17 17:54:46 -05:00
}
2013-04-25 19:06:47 -05:00
func (t ObjectType) String() (string) {
switch (t) {
case ObjectAny:
2013-04-25 19:06:47 -05:00
return "Any"
case ObjectBad:
2013-04-25 19:06:47 -05:00
return "Bad"
case ObjectCommit:
2013-04-25 19:06:47 -05:00
return "Commit"
case ObjectTree:
2013-04-25 19:06:47 -05:00
return "Tree"
case ObjectBlob:
2013-04-25 19:06:47 -05:00
return "Blob"
case ObjectTag:
2013-11-13 17:24:44 -06:00
return "Tag"
2013-04-25 19:06:47 -05:00
}
// Never reached
return ""
}
2013-04-17 17:54:46 -05:00
func (o gitObject) Id() *Oid {
return newOidFromC(C.git_object_id(o.ptr))
2013-04-17 17:54:46 -05:00
}
func (o gitObject) Type() ObjectType {
return ObjectType(C.git_object_type(o.ptr))
}
// Owner returns a weak reference to the repository which owns this
// object
func (o gitObject) Owner() *Repository {
return &Repository{
ptr: C.git_object_owner(o.ptr),
}
}
func (o *gitObject) Free() {
2013-04-17 17:54:46 -05:00
runtime.SetFinalizer(o, nil)
C.git_object_free(o.ptr)
2013-04-17 17:54:46 -05:00
}
func allocObject(cobj *C.git_object, repo *Repository) Object {
obj := gitObject{
ptr: cobj,
repo: repo,
}
2013-04-16 16:04:35 -05:00
switch ObjectType(C.git_object_type(cobj)) {
case ObjectCommit:
commit := &Commit{
gitObject: obj,
cast_ptr: (*C.git_commit)(cobj),
}
2013-04-17 17:54:46 -05:00
runtime.SetFinalizer(commit, (*Commit).Free)
return commit
2013-04-16 16:04:35 -05:00
case ObjectTree:
tree := &Tree{
gitObject: obj,
cast_ptr: (*C.git_tree)(cobj),
}
2013-04-17 17:54:46 -05:00
runtime.SetFinalizer(tree, (*Tree).Free)
return tree
2013-04-16 16:04:35 -05:00
case ObjectBlob:
blob := &Blob{
gitObject: obj,
cast_ptr: (*C.git_blob)(cobj),
}
2013-04-17 17:54:46 -05:00
runtime.SetFinalizer(blob, (*Blob).Free)
return blob
2013-04-16 16:04:35 -05:00
}
2013-04-17 17:54:46 -05:00
return nil
2013-04-16 16:04:35 -05:00
}