forked from speedata/gogit
-
Notifications
You must be signed in to change notification settings - Fork 32
/
repo_history.go
206 lines (168 loc) · 4.72 KB
/
repo_history.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
package git
import (
"container/list"
)
type HistoryWalkerAction int
const (
// drop commit and do not follow parents
HWDrop HistoryWalkerAction = 0
// take commit but not traverse parents
HWTakeCommit HistoryWalkerAction = 1 << iota
// drop commit but traverse parents
HWFollowParents
// stop traverse
HWStop
// take commit and follow parents
HWTakeAndFollow = HWTakeCommit | HWFollowParents
)
type CommitWalkCallback func(*Commit) (HistoryWalkerAction, error)
// CommitComparator defines callback type for checking commit equalty. If it returns true then
// commits are considered equal. See "History Simplification" chapter of git-log man for details
type CommitComparator func(current, parent *Commit) bool
func walkHistory(start *Commit, callback CommitWalkCallback) (*list.List, error) {
return walkHistoryLoop([]*Commit{start}, callback, nopComparator)
}
func walkFilteredHistory(start *Commit, callback CommitWalkCallback,
eq CommitComparator) (*list.List, error) {
return walkHistoryLoop([]*Commit{start}, callback, eq)
}
// roots must be not equal to each other
func walkHistoryLoop(roots []*Commit, callback CommitWalkCallback,
eq CommitComparator) (*list.List, error) {
results := list.New()
seen := make(map[sha1]struct{})
for {
if len(roots) == 0 {
return results, nil
}
var err error
roots, err = simplifyRoots(roots, eq, seen)
if err != nil {
return nil, err
}
if len(roots) == 0 {
return results, nil
}
var next *Commit
next, roots = extractNewestCommit(roots)
action, err := callback(next)
if err != nil {
return nil, err
}
if action&HWTakeCommit > 0 {
// witness commit
results.PushBack(next)
seen[next.Id] = struct{}{}
}
if action&HWFollowParents > 0 {
// follow all parents of commit
pars, err := parents(next)
if err != nil {
return nil, err
}
roots = mergeRoots(pars, roots, eq, seen)
}
if action&HWStop > 0 {
return results, nil
}
}
return results, nil
}
func parents(commit *Commit) ([]*Commit, error) {
parents := make([]*Commit, commit.ParentCount())
for idx := 0; idx < len(parents); idx++ {
var err error
parents[idx], err = commit.Parent(idx)
if err != nil {
return nil, err
}
}
return parents, nil
}
// mergeRoots will merge two sets of commits and ensure that they are not equal to each other
// the members of base and merging sets already nonequal to each other
func mergeRoots(base, merging []*Commit, eq CommitComparator, seen map[sha1]struct{}) []*Commit {
newRoots := append([]*Commit(nil), base...)
for _, needle := range merging {
found := false
for _, item := range base {
if eq(needle, item) {
// found equal commit in merging roots
// drop it
found = true
break
}
}
if !found {
// commit is unique enough
newRoots = append(newRoots, needle)
}
}
return newRoots
}
// skipEqualCommits compares commit to it parents. If it finds a parent
// that equals to current commit the current commit will be dropped and parent will be followed
// see "History Simplification" chapter of git-log man for full details.
func skipEqualCommits(commit *Commit, eq CommitComparator,
seen map[sha1]struct{}) (*Commit, error) {
for {
// we already seen that commit, no point to traverse further
if _, ok := seen[commit.Id]; ok {
return nil, nil
}
if len(commit.parents) == 0 {
return commit, nil
}
var found bool
for idx := 0; idx < commit.ParentCount(); idx++ {
parent, err := commit.Parent(idx)
if err != nil {
return nil, err
}
if eq(commit, parent) {
// we have parent that equals to given commit
// so take this parent as next commit (dropping current)
// but we will remember that we seen current commit in history
seen[commit.Id] = struct{}{}
commit = parent
found = true
break
}
}
// all parents of commit was not same to it, so return this commit
if !found {
return commit, nil
}
}
}
func simplifyRoots(roots []*Commit, eq CommitComparator,
seen map[sha1]struct{}) ([]*Commit, error) {
newRoots := []*Commit{}
for _, commit := range roots {
commit, err := skipEqualCommits(commit, eq, seen)
if err != nil {
return nil, err
}
if commit != nil {
newRoots = append(newRoots, commit)
}
}
return newRoots, nil
}
// extractNewestCommit will find newest commit, extract it and return resulting set
func extractNewestCommit(roots []*Commit) (*Commit, []*Commit) {
if len(roots) == 1 {
return roots[0], roots[:0]
}
target := roots[0]
targetIdx := 0
for idx, current := range roots[1:] {
if current.Committer.When.After(target.Committer.When) {
target = current
targetIdx = idx + 1
}
}
// remove picked commit
roots = append(roots[:targetIdx], roots[targetIdx+1:]...)
return target, roots
}