2013-04-16 16:04:35 -05:00
|
|
|
package git
|
|
|
|
|
|
|
|
/*
|
|
|
|
#include <git2.h>
|
|
|
|
#include <git2/errors.h>
|
|
|
|
*/
|
|
|
|
import "C"
|
|
|
|
import "runtime"
|
|
|
|
|
|
|
|
type ObjectType int
|
|
|
|
|
2013-09-11 16:01:27 -05:00
|
|
|
const (
|
2013-09-12 03:40:57 -05:00
|
|
|
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
|
2014-05-25 02:06:18 -05:00
|
|
|
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
|
2014-05-26 02:28:07 -05:00
|
|
|
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) {
|
2013-09-12 03:40:57 -05:00
|
|
|
case ObjectAny:
|
2013-04-25 19:06:47 -05:00
|
|
|
return "Any"
|
2013-09-12 03:40:57 -05:00
|
|
|
case ObjectBad:
|
2013-04-25 19:06:47 -05:00
|
|
|
return "Bad"
|
2013-09-12 03:40:57 -05:00
|
|
|
case ObjectCommit:
|
2013-04-25 19:06:47 -05:00
|
|
|
return "Commit"
|
2013-09-12 03:40:57 -05:00
|
|
|
case ObjectTree:
|
2013-04-25 19:06:47 -05:00
|
|
|
return "Tree"
|
2013-09-12 03:40:57 -05:00
|
|
|
case ObjectBlob:
|
2013-04-25 19:06:47 -05:00
|
|
|
return "Blob"
|
2013-09-12 03:40:57 -05:00
|
|
|
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 {
|
2014-04-01 05:13:37 -05:00
|
|
|
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))
|
|
|
|
}
|
|
|
|
|
2014-05-25 02:06:18 -05:00
|
|
|
// 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),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-04-25 19:32:41 -05:00
|
|
|
func (o *gitObject) Free() {
|
2013-04-17 17:54:46 -05:00
|
|
|
runtime.SetFinalizer(o, nil)
|
2014-04-01 05:13:37 -05:00
|
|
|
C.git_object_free(o.ptr)
|
2013-04-17 17:54:46 -05:00
|
|
|
}
|
|
|
|
|
2014-05-26 02:28:07 -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)) {
|
2013-09-12 03:40:57 -05:00
|
|
|
case ObjectCommit:
|
2014-04-01 05:13:37 -05:00
|
|
|
commit := &Commit{
|
2014-05-26 02:28:07 -05:00
|
|
|
gitObject: obj,
|
2014-04-01 05:13:37 -05:00
|
|
|
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
|
|
|
|
2013-09-12 03:40:57 -05:00
|
|
|
case ObjectTree:
|
2014-04-01 05:13:37 -05:00
|
|
|
tree := &Tree{
|
2014-05-26 02:28:07 -05:00
|
|
|
gitObject: obj,
|
2014-04-01 05:13:37 -05:00
|
|
|
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
|
|
|
|
2013-09-12 03:40:57 -05:00
|
|
|
case ObjectBlob:
|
2014-04-01 05:13:37 -05:00
|
|
|
blob := &Blob{
|
2014-05-26 02:28:07 -05:00
|
|
|
gitObject: obj,
|
2014-04-01 05:13:37 -05:00
|
|
|
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
|
|
|
}
|