diff options
| author | Vicent Martà <[email protected]> | 2013-06-13 10:15:36 -0700 |
|---|---|---|
| committer | Vicent Martà <[email protected]> | 2013-06-13 10:15:36 -0700 |
| commit | 62f65d071d0671fb53aaca54a2d59a636267c2b0 (patch) | |
| tree | e03dd9af8fb0e7287abfc0597c1a325013ee0073 /object.go | |
| parent | 01d1a5c5d5fede6f054e50a1154ff747e3879cf8 (diff) | |
| parent | 5766c4accf913bb4a98189177261e1db939397e2 (diff) | |
Merge pull request #13 from libgit2/polymorphism-take-2
My take on polymorphism
Diffstat (limited to 'object.go')
| -rw-r--r-- | object.go | 84 |
1 files changed, 84 insertions, 0 deletions
diff --git a/object.go b/object.go new file mode 100644 index 0000000..c6cd8a8 --- /dev/null +++ b/object.go @@ -0,0 +1,84 @@ +package git + +/* +#cgo pkg-config: libgit2 +#include <git2.h> +#include <git2/errors.h> +*/ +import "C" +import "runtime" + +type ObjectType int + +var ( + OBJ_ANY ObjectType = C.GIT_OBJ_ANY + OBJ_BAD ObjectType = C.GIT_OBJ_BAD + OBJ_COMMIT ObjectType = C.GIT_OBJ_COMMIT + OBJ_TREE ObjectType = C.GIT_OBJ_TREE + OBJ_BLOB ObjectType = C.GIT_OBJ_BLOB + OBJ_TAG ObjectType = C.GIT_OBJ_TAG +) + +type Object interface { + Free() + Id() *Oid + Type() ObjectType +} + +type gitObject struct { + ptr *C.git_object +} + +func (t ObjectType) String() (string) { + switch (t) { + case OBJ_ANY: + return "Any" + case OBJ_BAD: + return "Bad" + case OBJ_COMMIT: + return "Commit" + case OBJ_TREE: + return "Tree" + case OBJ_BLOB: + return "Blob" + case OBJ_TAG: + return "tag" + } + // Never reached + return "" +} + +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 { + + switch ObjectType(C.git_object_type(cobj)) { + case OBJ_COMMIT: + commit := &Commit{gitObject{cobj}} + runtime.SetFinalizer(commit, (*Commit).Free) + return commit + + case OBJ_TREE: + tree := &Tree{gitObject{cobj}} + runtime.SetFinalizer(tree, (*Tree).Free) + return tree + + case OBJ_BLOB: + blob := &Blob{gitObject{cobj}} + runtime.SetFinalizer(blob, (*Blob).Free) + return blob + } + + return nil +} |
