2013-03-05 13:53:04 -06:00
|
|
|
package git
|
|
|
|
|
|
|
|
/*
|
|
|
|
#include <git2.h>
|
|
|
|
#include <git2/errors.h>
|
|
|
|
|
|
|
|
extern int _go_git_treewalk(git_tree *tree, git_treewalk_mode mode, void *ptr);
|
|
|
|
*/
|
|
|
|
import "C"
|
|
|
|
|
|
|
|
import (
|
2013-03-05 18:43:48 -06:00
|
|
|
"runtime"
|
2013-03-06 09:59:45 -06:00
|
|
|
"unsafe"
|
2013-03-05 16:18:07 -06:00
|
|
|
"time"
|
2013-03-05 13:53:04 -06:00
|
|
|
)
|
|
|
|
|
|
|
|
// Commit
|
|
|
|
type Commit struct {
|
|
|
|
ptr *C.git_commit
|
|
|
|
}
|
|
|
|
|
2013-04-16 16:04:35 -05:00
|
|
|
func (o *Commit) Id() *Oid {
|
|
|
|
return newOidFromC(C.git_commit_id(o.ptr))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (o *Commit) Type() ObjectType {
|
|
|
|
return OBJ_COMMIT
|
|
|
|
}
|
|
|
|
|
|
|
|
func (o *Commit) Free() {
|
|
|
|
runtime.SetFinalizer(o, nil)
|
|
|
|
C.git_commit_free(o.ptr)
|
2013-03-05 13:53:04 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Commit) Message() string {
|
|
|
|
return C.GoString(C.git_commit_message(c.ptr))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Commit) Tree() (*Tree, error) {
|
2013-04-16 16:04:35 -05:00
|
|
|
var ptr *C.git_object
|
2013-03-05 13:53:04 -06:00
|
|
|
|
2013-04-16 16:04:35 -05:00
|
|
|
err := C.git_commit_tree(&ptr, c.ptr)
|
2013-03-05 13:53:04 -06:00
|
|
|
if err < 0 {
|
|
|
|
return nil, LastError()
|
|
|
|
}
|
2013-03-05 18:43:48 -06:00
|
|
|
|
2013-04-16 16:04:35 -05:00
|
|
|
return allocObject(ptr).(*Tree), nil
|
2013-03-05 13:53:04 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Commit) TreeId() *Oid {
|
|
|
|
return newOidFromC(C.git_commit_tree_id(c.ptr))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Commit) Author() *Signature {
|
|
|
|
ptr := C.git_commit_author(c.ptr)
|
2013-03-05 16:18:07 -06:00
|
|
|
return newSignatureFromC(ptr)
|
2013-03-05 13:53:04 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Commit) Committer() *Signature {
|
|
|
|
ptr := C.git_commit_committer(c.ptr)
|
2013-03-05 16:18:07 -06:00
|
|
|
return newSignatureFromC(ptr)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Signature
|
|
|
|
|
|
|
|
type Signature struct {
|
2013-03-06 13:28:39 -06:00
|
|
|
Name string
|
2013-03-05 16:18:07 -06:00
|
|
|
Email string
|
2013-03-06 13:28:39 -06:00
|
|
|
When time.Time
|
2013-03-05 16:18:07 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func newSignatureFromC(sig *C.git_signature) *Signature {
|
2013-03-06 13:28:39 -06:00
|
|
|
// git stores minutes, go wants seconds
|
|
|
|
loc := time.FixedZone("", int(sig.when.offset)*60)
|
2013-03-05 16:18:07 -06:00
|
|
|
return &Signature{
|
|
|
|
C.GoString(sig.name),
|
|
|
|
C.GoString(sig.email),
|
2013-03-06 13:28:39 -06:00
|
|
|
time.Unix(int64(sig.when.time), 0).In(loc),
|
2013-03-05 16:18:07 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-03-06 13:28:39 -06:00
|
|
|
// the offset in mintes, which is what git wants
|
|
|
|
func (v *Signature) Offset() int {
|
|
|
|
_, offset := v.When.Zone()
|
|
|
|
return offset/60
|
2013-03-05 13:53:04 -06:00
|
|
|
}
|
2013-03-06 09:59:45 -06:00
|
|
|
|
|
|
|
func (sig *Signature) toC() (*C.git_signature) {
|
|
|
|
var out *C.git_signature
|
|
|
|
|
|
|
|
name := C.CString(sig.Name)
|
|
|
|
defer C.free(unsafe.Pointer(name))
|
|
|
|
|
|
|
|
email := C.CString(sig.Email)
|
|
|
|
defer C.free(unsafe.Pointer(email))
|
|
|
|
|
2013-03-06 13:28:39 -06:00
|
|
|
ret := C.git_signature_new(&out, name, email, C.git_time_t(sig.When.Unix()), C.int(sig.Offset()))
|
2013-03-06 09:59:45 -06:00
|
|
|
if ret < 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return out
|
|
|
|
}
|