-
Notifications
You must be signed in to change notification settings - Fork 25
/
git_log.go
94 lines (75 loc) · 1.78 KB
/
git_log.go
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
package vcs
import (
"time"
git "github.com/libgit2/git2go/v33"
)
type LogEntry struct {
Time time.Time
RawCommitMessage string
ID string // This is a Commit ID
Name string
Email string
// If this commits has been landed on sturdytrunk (potentially with another commit ID)
IsLanded bool
}
func (repo *repository) LogHead(limit int) ([]*LogEntry, error) {
defer getMeterFunc("LogHead")()
revwalk, err := repo.r.Walk()
if err != nil {
return nil, err
}
defer revwalk.Free()
err = revwalk.PushHead()
if err != nil {
return nil, err
}
return repo.log(revwalk, limit)
}
func (repo *repository) LogBranch(branchName string, limit int) ([]*LogEntry, error) {
defer getMeterFunc("LogBranch")()
branch, err := repo.r.LookupBranch(branchName, git.BranchLocal)
if err != nil {
return nil, err
}
defer branch.Free()
revwalk, err := repo.r.Walk()
if err != nil {
return nil, err
}
defer revwalk.Free()
err = revwalk.Push(branch.Target())
if err != nil {
return nil, err
}
return repo.log(revwalk, limit)
}
func CommitLogEntry(commit *git.Commit) *LogEntry {
id := commit.Id().String()
committer := commit.Committer()
return &LogEntry{
Time: committer.When,
RawCommitMessage: commit.Message(),
ID: id,
Name: committer.Name,
Email: committer.Email,
}
}
func (repo *repository) log(revwalk *git.RevWalk, limit int) ([]*LogEntry, error) {
defer getMeterFunc("log")()
var out []*LogEntry
var i int
revwalk.Iterate(func(commit *git.Commit) bool {
// If merge commit, skip it from the list
if commit.ParentCount() >= 2 {
return true
}
out = append(out, CommitLogEntry(commit))
// Break at limit
i++
if i == limit {
return false
}
return true
})
return out, nil
}