git2go/commit.go

111 lines
2.2 KiB
Go
Raw Normal View History

2013-03-05 13:53:04 -06:00
package git
/*
#include <git2.h>
extern int _go_git_treewalk(git_tree *tree, git_treewalk_mode mode, void *ptr);
*/
import "C"
import (
"runtime"
2015-03-11 18:10:32 -05:00
"unsafe"
2013-03-05 13:53:04 -06:00
)
// Commit
type Commit struct {
2013-04-17 17:54:46 -05:00
gitObject
cast_ptr *C.git_commit
2013-03-05 13:53:04 -06:00
}
2013-04-17 17:54:46 -05:00
func (c Commit) Message() string {
return C.GoString(C.git_commit_message(c.cast_ptr))
2013-03-05 13:53:04 -06:00
}
2015-03-04 17:35:35 -06:00
func (c Commit) Summary() string {
return C.GoString(C.git_commit_summary(c.cast_ptr))
}
2013-04-17 17:54:46 -05:00
func (c Commit) Tree() (*Tree, error) {
var ptr *C.git_tree
2013-03-05 13:53:04 -06:00
runtime.LockOSThread()
defer runtime.UnlockOSThread()
err := C.git_commit_tree(&ptr, c.cast_ptr)
2013-03-05 13:53:04 -06:00
if err < 0 {
return nil, MakeGitError(err)
2013-03-05 13:53:04 -06:00
}
return allocObject((*C.git_object)(ptr), c.repo).(*Tree), nil
2013-03-05 13:53:04 -06:00
}
2013-04-17 17:54:46 -05:00
func (c Commit) TreeId() *Oid {
return newOidFromC(C.git_commit_tree_id(c.cast_ptr))
2013-03-05 13:53:04 -06:00
}
2013-04-17 17:54:46 -05:00
func (c Commit) Author() *Signature {
cast_ptr := C.git_commit_author(c.cast_ptr)
return newSignatureFromC(cast_ptr)
2013-03-05 13:53:04 -06:00
}
2013-04-17 17:54:46 -05:00
func (c Commit) Committer() *Signature {
cast_ptr := C.git_commit_committer(c.cast_ptr)
return newSignatureFromC(cast_ptr)
}
func (c *Commit) Parent(n uint) *Commit {
var cobj *C.git_commit
ret := C.git_commit_parent(&cobj, c.cast_ptr, C.uint(n))
if ret != 0 {
return nil
}
return allocObject((*C.git_object)(cobj), c.repo).(*Commit)
}
func (c *Commit) ParentId(n uint) *Oid {
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.cast_ptr))
}
2015-03-11 18:10:32 -05:00
func (c *Commit) Amend(refname string, author, committer *Signature, message string, tree *Tree) (*Oid, error) {
var cref *C.char
if refname == "" {
cref = nil
} else {
cref = C.CString(refname)
defer C.free(unsafe.Pointer(cref))
}
cmsg := C.CString(message)
defer C.free(unsafe.Pointer(cmsg))
runtime.LockOSThread()
defer runtime.UnlockOSThread()
authorSig, err := author.toC()
if err != nil {
return nil, err
}
defer C.git_signature_free(authorSig)
committerSig, err := committer.toC()
if err != nil {
return nil, err
}
defer C.git_signature_free(committerSig)
oid := new(Oid)
cerr := C.git_commit_amend(oid.toC(), c.cast_ptr, cref, authorSig, committerSig, nil, cmsg, tree.cast_ptr)
if cerr < 0 {
return nil, MakeGitError(cerr)
}
return oid, nil
}