summaryrefslogtreecommitdiff
path: root/commit.go
blob: 855ed875e563367349180f1d7bbc09bd0e574f96 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
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 (
	"time"
)

// Commit
type Commit struct {
	ptr *C.git_commit
}

func (c *Commit) Id() *Oid {
	return newOidFromC(C.git_commit_id(c.ptr))
}

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

func (c *Commit) Tree() (*Tree, error) {
	tree := new(Tree)

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

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)
	return newSignatureFromC(ptr)
}

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

// Signature

type Signature struct {
	Name string
	Email string
	UnixTime int64
	Offset int
}

func newSignatureFromC(sig *C.git_signature) *Signature {
	return &Signature{
		C.GoString(sig.name),
		C.GoString(sig.email),
		int64(sig.when.time),
		int(sig.when.offset),
	}
}

func (sig *Signature) Time() time.Time {
	loc := time.FixedZone("", sig.Offset*60)
	return time.Unix(sig.UnixTime, 0).In(loc)
}