Skip to content

Commit

Permalink
Adjust to Go tip changes
Browse files Browse the repository at this point in the history
It does not like breaking aliasing rules, so let's keep a casted pointer
for when libgit2 wants that.
  • Loading branch information
carlosmn committed Apr 1, 2014
1 parent 286ff62 commit a06f4a0
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 31 deletions.
7 changes: 4 additions & 3 deletions blob.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,16 @@ import (

type Blob struct {
gitObject
cast_ptr *C.git_blob
}

func (v *Blob) Size() int64 {
return int64(C.git_blob_rawsize(v.ptr))
return int64(C.git_blob_rawsize(v.cast_ptr))
}

func (v *Blob) Contents() []byte {
size := C.int(C.git_blob_rawsize(v.ptr))
buffer := unsafe.Pointer(C.git_blob_rawcontent(v.ptr))
size := C.int(C.git_blob_rawsize(v.cast_ptr))
buffer := unsafe.Pointer(C.git_blob_rawcontent(v.cast_ptr))
return C.GoBytes(buffer, size)
}

Expand Down
2 changes: 1 addition & 1 deletion branch.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func (repo *Repository) CreateBranch(branchName string, target *Commit, force bo
runtime.LockOSThread()
defer runtime.UnlockOSThread()

ret := C.git_branch_create(&ref.ptr, repo.ptr, cBranchName, target.ptr, cForce, cSignature, cmsg)
ret := C.git_branch_create(&ref.ptr, repo.ptr, cBranchName, target.cast_ptr, cForce, cSignature, cmsg)
if ret < 0 {
return nil, MakeGitError(ret)
}
Expand Down
29 changes: 15 additions & 14 deletions commit.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,56 +17,57 @@ import (
// Commit
type Commit struct {
gitObject
cast_ptr *C.git_commit
}

func (c Commit) Message() string {
return C.GoString(C.git_commit_message(c.ptr))
return C.GoString(C.git_commit_message(c.cast_ptr))
}

func (c Commit) Tree() (*Tree, error) {
var ptr *C.git_object
var ptr *C.git_tree

runtime.LockOSThread()
defer runtime.UnlockOSThread()

err := C.git_commit_tree(&ptr, c.ptr)
err := C.git_commit_tree(&ptr, c.cast_ptr)
if err < 0 {
return nil, MakeGitError(err)
}

return allocObject(ptr).(*Tree), nil
return allocObject((*C.git_object)(ptr)).(*Tree), nil
}

func (c Commit) TreeId() *Oid {
return newOidFromC(C.git_commit_tree_id(c.ptr))
return newOidFromC(C.git_commit_tree_id(c.cast_ptr))
}

func (c Commit) Author() *Signature {
ptr := C.git_commit_author(c.ptr)
return newSignatureFromC(ptr)
cast_ptr := C.git_commit_author(c.cast_ptr)
return newSignatureFromC(cast_ptr)
}

func (c Commit) Committer() *Signature {
ptr := C.git_commit_committer(c.ptr)
return newSignatureFromC(ptr)
cast_ptr := C.git_commit_committer(c.cast_ptr)
return newSignatureFromC(cast_ptr)
}

func (c *Commit) Parent(n uint) *Commit {
var cobj *C.git_object
ret := C.git_commit_parent(&cobj, c.ptr, C.uint(n))
var cobj *C.git_commit
ret := C.git_commit_parent(&cobj, c.cast_ptr, C.uint(n))
if ret != 0 {
return nil
}

return allocObject(cobj).(*Commit)
return allocObject((*C.git_object)(cobj)).(*Commit)
}

func (c *Commit) ParentId(n uint) *Oid {
return newOidFromC(C.git_commit_parent_id(c.ptr, C.uint(n)))
return newOidFromC(C.git_commit_parent_id(c.cast_ptr, C.uint(n)))
}

func (c *Commit) ParentCount() uint {
return uint(C.git_commit_parentcount(c.ptr))
return uint(C.git_commit_parentcount(c.cast_ptr))
}

// Signature
Expand Down
19 changes: 14 additions & 5 deletions object.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func (t ObjectType) String() (string) {
}

func (o gitObject) Id() *Oid {
return newOidFromC(C.git_commit_id(o.ptr))
return newOidFromC(C.git_object_id(o.ptr))
}

func (o gitObject) Type() ObjectType {
Expand All @@ -57,24 +57,33 @@ func (o gitObject) Type() ObjectType {

func (o *gitObject) Free() {
runtime.SetFinalizer(o, nil)
C.git_commit_free(o.ptr)
C.git_object_free(o.ptr)
}

func allocObject(cobj *C.git_object) Object {

switch ObjectType(C.git_object_type(cobj)) {
case ObjectCommit:
commit := &Commit{gitObject{cobj}}
commit := &Commit{
gitObject: gitObject{cobj},
cast_ptr: (*C.git_commit)(cobj),
}
runtime.SetFinalizer(commit, (*Commit).Free)
return commit

case ObjectTree:
tree := &Tree{gitObject{cobj}}
tree := &Tree{
gitObject: gitObject{cobj},
cast_ptr: (*C.git_tree)(cobj),
}
runtime.SetFinalizer(tree, (*Tree).Free)
return tree

case ObjectBlob:
blob := &Blob{gitObject{cobj}}
blob := &Blob{
gitObject: gitObject{cobj},
cast_ptr: (*C.git_blob)(cobj),
}
runtime.SetFinalizer(blob, (*Blob).Free)
return blob
}
Expand Down
6 changes: 3 additions & 3 deletions repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ func (v *Repository) CreateCommit(
if nparents > 0 {
cparents = make([]*C.git_commit, nparents)
for i, v := range parents {
cparents[i] = v.ptr
cparents[i] = v.cast_ptr
}
parentsarg = &cparents[0]
}
Expand All @@ -256,7 +256,7 @@ func (v *Repository) CreateCommit(
ret := C.git_commit_create(
oid.toC(), v.ptr, cref,
authorSig, committerSig,
nil, cmsg, tree.ptr, C.size_t(nparents), parentsarg)
nil, cmsg, tree.cast_ptr, C.size_t(nparents), parentsarg)

if ret < 0 {
return nil, MakeGitError(ret)
Expand Down Expand Up @@ -331,7 +331,7 @@ func (v *Repository) TreeBuilderFromTree(tree *Tree) (*TreeBuilder, error) {
runtime.LockOSThread()
defer runtime.UnlockOSThread()

if ret := C.git_treebuilder_create(&bld.ptr, tree.ptr); ret < 0 {
if ret := C.git_treebuilder_create(&bld.ptr, tree.cast_ptr); ret < 0 {
return nil, MakeGitError(ret)
}
runtime.SetFinalizer(bld, (*TreeBuilder).Free)
Expand Down
11 changes: 6 additions & 5 deletions tree.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ const (

type Tree struct {
gitObject
cast_ptr *C.git_tree
}

type TreeEntry struct {
Expand All @@ -48,7 +49,7 @@ func (t Tree) EntryByName(filename string) *TreeEntry {
cname := C.CString(filename)
defer C.free(unsafe.Pointer(cname))

entry := C.git_tree_entry_byname(t.ptr, cname)
entry := C.git_tree_entry_byname(t.cast_ptr, cname)
if entry == nil {
return nil
}
Expand All @@ -66,7 +67,7 @@ func (t Tree) EntryByPath(path string) (*TreeEntry, error) {
runtime.LockOSThread()
defer runtime.UnlockOSThread()

ret := C.git_tree_entry_bypath(&entry, t.ptr, cpath)
ret := C.git_tree_entry_bypath(&entry, t.cast_ptr, cpath)
if ret < 0 {
return nil, MakeGitError(ret)
}
Expand All @@ -75,7 +76,7 @@ func (t Tree) EntryByPath(path string) (*TreeEntry, error) {
}

func (t Tree) EntryByIndex(index uint64) *TreeEntry {
entry := C.git_tree_entry_byindex(t.ptr, C.size_t(index))
entry := C.git_tree_entry_byindex(t.cast_ptr, C.size_t(index))
if entry == nil {
return nil
}
Expand All @@ -84,7 +85,7 @@ func (t Tree) EntryByIndex(index uint64) *TreeEntry {
}

func (t Tree) EntryCount() uint64 {
num := C.git_tree_entrycount(t.ptr)
num := C.git_tree_entrycount(t.cast_ptr)
return uint64(num)
}

Expand All @@ -104,7 +105,7 @@ func (t Tree) Walk(callback TreeWalkCallback) error {
defer runtime.UnlockOSThread()

err := C._go_git_treewalk(
t.ptr,
t.cast_ptr,
C.GIT_TREEWALK_PRE,
unsafe.Pointer(&callback),
)
Expand Down

0 comments on commit a06f4a0

Please sign in to comment.