summaryrefslogtreecommitdiff
path: root/object.go
diff options
context:
space:
mode:
authorVicent Marti <[email protected]>2013-04-16 23:04:35 +0200
committerVicent Marti <[email protected]>2013-04-16 23:04:35 +0200
commitd190d8a6b3717402744902d060be57195f27d604 (patch)
treebbaa99449afa98545eb7833831d085e5875377dd /object.go
parentc7286515b83bae368245b5cf2dc92971e16b21ff (diff)
Take 2 on polymorphism
Diffstat (limited to 'object.go')
-rw-r--r--object.go49
1 files changed, 49 insertions, 0 deletions
diff --git a/object.go b/object.go
new file mode 100644
index 0000000..0d55409
--- /dev/null
+++ b/object.go
@@ -0,0 +1,49 @@
+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
+}
+
+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)
+
+ case OBJ_TREE:
+ object = &Tree{cobj}
+ runtime.SetFinalizer(object, (*Tree).Free)
+
+ case OBJ_BLOB:
+ object = &Blob{cobj}
+ runtime.SetFinalizer(object, (*Blob).Free)
+
+ default:
+ return nil
+ }
+
+ return object
+}