-
Notifications
You must be signed in to change notification settings - Fork 5
/
reference_history.go
234 lines (173 loc) · 4.98 KB
/
reference_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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
package reference
import (
"bytes"
"fmt"
"strings"
"gopkg.in/src-d/go-git.v4"
"gopkg.in/src-d/go-git.v4/plumbing"
"gopkg.in/src-d/go-git.v4/plumbing/object"
)
// refSolver match a SymbolicRefPathStmt against
// a repository to resolve commit refSolver and commit reference
type refSolver struct {
stmt *symbolicRefPathStmt
commitRef *object.Commit
}
// newRefSolver creates a new RefSolver object
func newRefSolver(stmt *symbolicRefPathStmt, repo *git.Repository) (*refSolver, error) {
hash, err := resolveHash(stmt.branchName, repo)
if err != nil {
return nil, err
}
commitRef, _ := repo.Commit(hash)
for _, path := range stmt.refPath {
parents := commitRef.Parents()
parentCount := commitRef.NumParents()
if parentCount == 0 {
return nil, fmt.Errorf(`Can't find reference`)
}
for i := 1; i <= parentCount; i++ {
commit, err := parents.Next()
if err != nil {
return nil, fmt.Errorf("Can't find parent")
}
if path == i {
commitRef = commit
}
}
}
return &refSolver{stmt, commitRef}, nil
}
// resolveHash give hash commit for a given string reference
func resolveHash(refCommit string, repository *git.Repository) (plumbing.Hash, error) {
hash := plumbing.Hash{}
if strings.ToLower(refCommit) == "head" {
head, err := repository.Head()
if err == nil {
return head.Hash(), nil
}
}
iter, err := repository.References()
if err != nil {
return plumbing.Hash{}, err
}
err = iter.ForEach(func(ref *plumbing.Reference) error {
if ref.Name().Short() == refCommit {
hash = ref.Hash()
}
return nil
})
if err == nil && !hash.IsZero() {
return hash, err
}
hash = plumbing.NewHash(refCommit)
_, err = repository.Commit(hash)
if err == nil && !hash.IsZero() {
return hash, nil
}
return hash, fmt.Errorf(`Can't find reference "%s"`, refCommit)
}
// retrieveCommitPath fetch all commits between 2 references
func retrieveCommitPath(from *object.Commit, to *object.Commit) (*[]*object.Commit, error) {
results := []*object.Commit{}
parents := []*object.Commit{}
fmt.Println(to)
err := to.Parents().ForEach(
func(c *object.Commit) error {
parents = append(parents, c)
return nil
})
if err != nil {
return nil, err
}
if to.ID() == from.ID() {
results = append(results, to)
return &results, nil
}
for i := 0; i < to.NumParents(); i++ {
cs, err := retrieveCommitPath(from, parents[i])
if err != nil {
return nil, err
}
if len(*cs) > 0 {
results = append(results, to)
results = append(results, *cs...)
return &results, nil
}
}
return &results, nil
}
// parseCommitHistory return commits between two intervals
func parseCommitHistory(from *refSolver, to *refSolver) (*[]*object.Commit, error) {
results := []*object.Commit{}
commits, err := retrieveCommitPath(from.commitRef, to.commitRef)
if err != nil {
return nil, err
}
for i := 0; i < len(*commits)-1; i++ {
cs, errs := parseTree((*commits)[i], (*commits)[i+1])
if len(errs) > 0 {
return nil, fmt.Errorf("An error occurred when retrieving commits between %s and %s", from.commitRef.ID(), to.commitRef.ID())
}
results = append(results, cs...)
}
return &results, nil
}
// parseTree recursively parse a given tree to extract commits till boundary is reached
func parseTree(commit *object.Commit, bound *object.Commit) ([]*object.Commit, []error) {
commits := []*object.Commit{}
errors := []error{}
if commit.ID() == bound.ID() || commit.NumParents() == 0 {
return commits, errors
}
commits = append(commits, commit)
parents := []*object.Commit{}
err := commit.Parents().ForEach(
func(c *object.Commit) error {
parents = append(parents, c)
return nil
})
if err != nil {
errors = append(errors, err)
return commits, errors
}
if len(parents) == 2 {
cs, errs := parseTree(parents[1], bound)
errors = append(errors, errs...)
commits = append(commits, cs...)
}
if len(parents) == 1 {
cs, errs := parseTree(parents[0], bound)
errors = append(errors, errs...)
commits = append(commits, cs...)
}
return commits, errors
}
// FetchCommitInterval retrieves commit refSolver in a given interval for a repository
func FetchCommitInterval(repo *git.Repository, from string, to string) (*[]*object.Commit, error) {
refRefSolverFrom := newParser(bytes.NewBufferString(from))
fromStmt, err := refRefSolverFrom.parseSymbolicReferencePath()
commits := []*object.Commit{}
if err != nil {
return &commits, err
}
refRefSolverTo := newParser(bytes.NewBufferString(to))
toStmt, err := refRefSolverTo.parseSymbolicReferencePath()
if err != nil {
return &commits, err
}
fromRefSolver, err := newRefSolver(fromStmt, repo)
if err != nil {
return &commits, err
}
toRefSolver, err := newRefSolver(toStmt, repo)
if err != nil {
return &commits, err
}
return parseCommitHistory(fromRefSolver, toRefSolver)
}
// FetchCommitByID retrieves a single commit from a repository
func FetchCommitByID(repo *git.Repository, ID string) (*object.Commit, error) {
hash := plumbing.NewHash(ID)
return repo.Commit(hash)
}