git2go/object.go

139 lines
2.9 KiB
Go
Raw Normal View History

2013-04-16 16:04:35 -05:00
package git
/*
#include <git2.h>
*/
import "C"
import "runtime"
type ObjectType int
const (
ObjectAny ObjectType = C.GIT_OBJ_ANY
ObjectBad ObjectType = C.GIT_OBJ_BAD
ObjectCommit ObjectType = C.GIT_OBJ_COMMIT
ObjectTree ObjectType = C.GIT_OBJ_TREE
ObjectBlob ObjectType = C.GIT_OBJ_BLOB
ObjectTag ObjectType = C.GIT_OBJ_TAG
2013-04-16 16:04:35 -05:00
)
type Object interface {
Free()
Id() *Oid
Type() ObjectType
Owner() *Repository
Peel(t ObjectType) (Object, error)
2013-04-16 16:04:35 -05:00
}
2013-04-17 17:54:46 -05:00
type gitObject struct {
2015-03-04 13:40:26 -06:00
ptr *C.git_object
repo *Repository
2013-04-17 17:54:46 -05:00
}
2015-03-04 13:40:26 -06: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
}
// Peel recursively peels an object until an object of the specified type is met.
//
// If the query cannot be satisfied due to the object model, ErrInvalidSpec
// will be returned (e.g. trying to peel a blob to a tree).
//
// If you pass ObjectAny as the target type, then the object will be peeled
// until the type changes. A tag will be peeled until the referenced object
// is no longer a tag, and a commit will be peeled to a tree. Any other object
// type will return ErrInvalidSpec.
//
// If peeling a tag we discover an object which cannot be peeled to the target
// type due to the object model, an error will be returned.
func (o *gitObject) Peel(t ObjectType) (Object, error) {
var cobj *C.git_object
runtime.LockOSThread()
defer runtime.UnlockOSThread()
if err := C.git_object_peel(&cobj, o.ptr, C.git_otype(t)); err < 0 {
return nil, MakeGitError(err)
}
return allocObject(cobj, o.repo), nil
}
func allocObject(cobj *C.git_object, repo *Repository) Object {
obj := gitObject{
2015-03-04 13:40:26 -06:00
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
2014-06-09 16:19:17 -05:00
case ObjectTag:
tag := &Tag{
gitObject: obj,
cast_ptr: (*C.git_tag)(cobj),
}
runtime.SetFinalizer(tag, (*Tag).Free)
return tag
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
}