From 7f685a6ee64db446a8b2d05f98796a1bf63984d9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Mart=C3=ADn=20Nieto?= Date: Sat, 8 Jul 2017 22:53:50 +0200 Subject: [PATCH 1/2] Add Objecer interface We do want to be able to accept generic objects in functions. Add this interface so we can accept that instead of specific object types. --- blob.go | 4 ++++ commit.go | 4 ++++ object.go | 5 +++++ tag.go | 4 ++++ tree.go | 4 ++++ 5 files changed, 21 insertions(+) diff --git a/blob.go b/blob.go index 5235597..227e014 100644 --- a/blob.go +++ b/blob.go @@ -20,6 +20,10 @@ type Blob struct { cast_ptr *C.git_blob } +func (b *Blob) AsObject() *Object { + return &b.Object +} + func (v *Blob) Size() int64 { ret := int64(C.git_blob_rawsize(v.cast_ptr)) runtime.KeepAlive(v) diff --git a/commit.go b/commit.go index 5aa5f25..223b093 100644 --- a/commit.go +++ b/commit.go @@ -18,6 +18,10 @@ type Commit struct { cast_ptr *C.git_commit } +func (c *Commit) AsObject() *Object { + return &c.Object +} + func (c *Commit) Message() string { ret := C.GoString(C.git_commit_message(c.cast_ptr)) runtime.KeepAlive(c) diff --git a/object.go b/object.go index f4f1b3e..5505e35 100644 --- a/object.go +++ b/object.go @@ -26,6 +26,11 @@ type Object struct { repo *Repository } +// Objecter lets us accept any kind of Git object in functions. +type Objecter interface { + AsObject() *Object +} + func (t ObjectType) String() string { switch t { case ObjectAny: diff --git a/tag.go b/tag.go index a58b090..9e26174 100644 --- a/tag.go +++ b/tag.go @@ -17,6 +17,10 @@ type Tag struct { cast_ptr *C.git_tag } +func (t *Tag) AsObject() *Object { + return &t.Object +} + func (t Tag) Message() string { ret := C.GoString(C.git_tag_message(t.cast_ptr)) runtime.KeepAlive(t) diff --git a/tree.go b/tree.go index f5cbd78..ee14ec5 100644 --- a/tree.go +++ b/tree.go @@ -27,6 +27,10 @@ type Tree struct { cast_ptr *C.git_tree } +func (t *Tree) AsObject() *Object { + return &t.Object +} + type TreeEntry struct { Name string Id *Oid From 916d555644a241a55e1779d379618ab89108e850 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Mart=C3=ADn=20Nieto?= Date: Sat, 8 Jul 2017 22:55:44 +0200 Subject: [PATCH 2/2] tag: accept an Objecter for creating a tag This lets us create a tag for any kind of object. --- tag.go | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/tag.go b/tag.go index 9e26174..4debdb7 100644 --- a/tag.go +++ b/tag.go @@ -67,8 +67,7 @@ type TagsCollection struct { repo *Repository } -func (c *TagsCollection) Create( - name string, commit *Commit, tagger *Signature, message string) (*Oid, error) { +func (c *TagsCollection) Create(name string, obj Objecter, tagger *Signature, message string) (*Oid, error) { oid := new(Oid) @@ -84,13 +83,13 @@ func (c *TagsCollection) Create( } defer C.git_signature_free(taggerSig) - ctarget := commit.ptr - runtime.LockOSThread() defer runtime.UnlockOSThread() - ret := C.git_tag_create(oid.toC(), c.repo.ptr, cname, ctarget, taggerSig, cmessage, 0) + o := obj.AsObject() + ret := C.git_tag_create(oid.toC(), c.repo.ptr, cname, o.ptr, taggerSig, cmessage, 0) runtime.KeepAlive(c) + runtime.KeepAlive(obj) if ret < 0 { return nil, MakeGitError(ret) } @@ -114,7 +113,7 @@ func (c *TagsCollection) Remove(name string) error { return nil } -// CreateLightweight creates a new lightweight tag pointing to a commit +// CreateLightweight creates a new lightweight tag pointing to an object // and returns the id of the target object. // // The name of the tag is validated for consistency (see git_tag_create() for the rules @@ -126,20 +125,20 @@ func (c *TagsCollection) Remove(name string) error { // The created tag is a simple reference and can be queried using // repo.References.Lookup("refs/tags/"). The name of the tag (eg "v1.0.0") // is queried with ref.Shorthand(). -func (c *TagsCollection) CreateLightweight(name string, commit *Commit, force bool) (*Oid, error) { +func (c *TagsCollection) CreateLightweight(name string, obj Objecter, force bool) (*Oid, error) { oid := new(Oid) cname := C.CString(name) defer C.free(unsafe.Pointer(cname)) - ctarget := commit.ptr - runtime.LockOSThread() defer runtime.UnlockOSThread() - err := C.git_tag_create_lightweight(oid.toC(), c.repo.ptr, cname, ctarget, cbool(force)) + o := obj.AsObject() + err := C.git_tag_create_lightweight(oid.toC(), c.repo.ptr, cname, o.ptr, cbool(force)) runtime.KeepAlive(c) + runtime.KeepAlive(obj) if err < 0 { return nil, MakeGitError(err) }