/
RepositoryManagerAnalytics.go
341 lines (320 loc) · 10.2 KB
/
RepositoryManagerAnalytics.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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
package git
import (
"fmt"
"github.com/devtron-labs/git-sensor/internals"
"github.com/devtron-labs/git-sensor/util"
"github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/plumbing"
"github.com/go-git/go-git/v5/plumbing/object"
"go.uber.org/zap"
"io"
"strings"
"time"
)
type RepositoryManagerAnalytics interface {
ChangesSinceByRepositoryForAnalytics(gitCtx GitContext, checkoutPath string, Old string, New string) (*GitChanges, error)
}
type RepositoryManagerAnalyticsImpl struct {
repoManager RepositoryManager
gitManager GitManager
configuration *internals.Configuration
logger *zap.SugaredLogger
}
func NewRepositoryManagerAnalyticsImpl(repoManager RepositoryManager, gitManager GitManager,
configuration *internals.Configuration, logger *zap.SugaredLogger) *RepositoryManagerAnalyticsImpl {
return &RepositoryManagerAnalyticsImpl{
repoManager: repoManager,
gitManager: gitManager,
configuration: configuration,
logger: logger,
}
}
func computeDiff(r *git.Repository, newHash *plumbing.Hash, oldHash *plumbing.Hash) ([]*object.Commit, error) {
processed := make(map[string]*object.Commit, 0)
//t := time.Now()
h := newHash //plumbing.NewHash(newHash)
h2 := oldHash //plumbing.NewHash(oldHash)
c1, err := r.CommitObject(*h)
if err != nil {
return nil, fmt.Errorf("not found commit %s", h.String())
}
c2, err := r.CommitObject(*h2)
if err != nil {
return nil, fmt.Errorf("not found commit %s", h2.String())
}
var parents, ancestorStack []*object.Commit
ps := c1.Parents()
for {
n, err := ps.Next()
if err == io.EOF {
break
}
if n.Hash.String() != c2.Hash.String() {
parents = append(parents, n)
}
}
ancestorStack = append(ancestorStack, parents...)
processed[c1.Hash.String()] = c1
for len(ancestorStack) > 0 {
lastIndex := len(ancestorStack) - 1
//dont process already processed in this algorithm path is not important
if _, ok := processed[ancestorStack[lastIndex].Hash.String()]; ok {
ancestorStack = ancestorStack[:lastIndex]
continue
}
//if this is old commit provided for processing then ignore it
if ancestorStack[lastIndex].Hash.String() == c2.Hash.String() {
ancestorStack = ancestorStack[:lastIndex]
continue
}
m, err := ancestorStack[lastIndex].MergeBase(c2)
//fmt.Printf("mergebase between %s and %s is %s length %d\n", ancestorStack[lastIndex].Hash.String(), c2.Hash.String(), m[0].Hash.String(), len(m))
if err != nil {
return nil, fmt.Errorf("error in mergebase %s %s - err %w", ancestorStack[lastIndex].Hash.String(), c2.Hash.String(), err)
}
// if commit being analyzed is itself merge commit then dont process as it is common in both old and new
if in(ancestorStack[lastIndex], m) {
ancestorStack = ancestorStack[:lastIndex]
continue
}
d, p := getDiffTillBranchingOrDest(ancestorStack[lastIndex], m)
//fmt.Printf("length of diff %d\n", len(d))
for _, v := range d {
processed[v.Hash.String()] = v
}
curNodes := make(map[string]bool, 0)
for _, v := range ancestorStack {
curNodes[v.Hash.String()] = true
}
processed[ancestorStack[lastIndex].Hash.String()] = ancestorStack[lastIndex]
ancestorStack = ancestorStack[:lastIndex]
for _, v := range p {
if ok2, _ := curNodes[v.Hash.String()]; !ok2 {
ancestorStack = append(ancestorStack, v)
}
}
}
var commits []*object.Commit
for _, d := range processed {
commits = append(commits, d)
}
return commits, nil
}
func getDiffTillBranchingOrDest(src *object.Commit, dst []*object.Commit) (diff, parents []*object.Commit) {
if in(src, dst) {
return
}
new := src
for {
ps := new.Parents()
parents = make([]*object.Commit, 0)
for {
n, err := ps.Next()
if err == io.EOF {
break
}
parents = append(parents, n)
}
if len(parents) > 1 || len(parents) == 0 {
return
}
if in(parents[0], dst) {
parents = nil
return
} else {
//fmt.Printf("added %s when child is %s and merge base is %s", parents[0].Hash.String(), src.Hash.String(), dst[0].Hash.String())
diff = append(diff, parents[0])
}
new = parents[0]
}
}
func in(obj *object.Commit, list []*object.Commit) bool {
for _, v := range list {
if v.Hash.String() == obj.Hash.String() {
return true
}
}
return false
}
func transform(src *object.Commit, tag *object.Tag) (dst *Commit) {
if src == nil {
return nil
}
dst = &Commit{
Hash: &Hash{
Long: src.Hash.String(),
Short: src.Hash.String()[:8],
},
Tree: &Tree{
Long: src.TreeHash.String(),
Short: src.TreeHash.String()[:8],
},
Author: &Author{
Name: src.Author.Name,
Email: src.Author.Email,
Date: src.Author.When,
},
Committer: &Committer{
Name: src.Committer.Name,
Email: src.Committer.Email,
Date: src.Committer.When,
},
Subject: src.Message,
Body: "",
}
if tag != nil {
dst.Tag = &Tag{
Name: tag.Name,
Date: tag.Tagger.When,
}
}
return
}
// from -> old commit
// to -> new commit
func (impl RepositoryManagerAnalyticsImpl) ChangesSinceByRepositoryForAnalytics(gitCtx GitContext, checkoutPath string, Old string, New string) (*GitChanges, error) {
var err error
start := time.Now()
defer func() {
util.TriggerGitOperationMetrics("changesSinceByRepositoryForAnalytics", start, err)
}()
GitChanges := &GitChanges{}
repository, err := impl.gitManager.OpenRepoPlain(checkoutPath)
if err != nil {
return nil, err
}
newHash := plumbing.NewHash(New)
oldHash := plumbing.NewHash(Old)
var fileStats FileStats
if strings.Contains(checkoutPath, "/.git") || impl.configuration.UseGitCli {
oldHashString := oldHash.String()
newHashString := newHash.String()
fileStats, err = impl.gitManager.FetchDiffStatBetweenCommits(gitCtx, oldHashString, newHashString, checkoutPath)
if err != nil {
impl.logger.Errorw("error in fetching fileStat diff between commits and converting git diff into fileStats ", "err", err)
}
} else {
patch, err := impl.getPatchObject(gitCtx, repository.Repository, oldHash, newHash)
if err != nil {
impl.logger.Errorw("can't get patch: ", "err", err)
return nil, err
}
fileStats = transformFileStats(patch.Stats())
}
GitChanges.FileStats = fileStats
impl.logger.Debugw("computed files stats", "filestats", fileStats)
commits, err := impl.computeCommitDiff(gitCtx, checkoutPath, oldHash, newHash, repository)
if err != nil {
return nil, err
}
GitChanges.Commits = commits
return GitChanges, nil
}
func (impl RepositoryManagerAnalyticsImpl) computeCommitDiff(gitCtx GitContext, checkoutPath string, oldHash plumbing.Hash, newHash plumbing.Hash, repository *GitRepository) ([]*Commit, error) {
var commitsCli, commitsGoGit []*Commit
var err error
if impl.configuration.UseGitCli || impl.configuration.AnalyticsDebug {
commitsCli, err = impl.gitManager.LogMergeBase(gitCtx, checkoutPath, oldHash.String(), newHash.String())
if err != nil {
impl.logger.Errorw("error in fetching commits for analytics through CLI: ", "err", err)
return nil, err
}
}
if !impl.configuration.UseGitCli || impl.configuration.AnalyticsDebug {
ctx, cancel := gitCtx.WithTimeout(impl.configuration.GoGitTimeout)
defer cancel()
commitsGoGit, err = RunWithTimeout(ctx, func() ([]*Commit, error) {
return impl.getCommitDiff(repository, newHash, oldHash)
})
if err != nil {
impl.logger.Errorw("error in fetching commits for analytics through gogit: ", "err", err)
return nil, err
}
}
if impl.configuration.AnalyticsDebug {
impl.logOldestCommitComparison(commitsGoGit, commitsCli, checkoutPath, oldHash.String(), newHash.String())
}
if !impl.configuration.UseGitCli {
return commitsGoGit, nil
}
return commitsCli, nil
}
func (impl RepositoryManagerAnalyticsImpl) logOldestCommitComparison(commitsGoGit []*Commit, commitsCli []*Commit, checkoutPath string, old string, new string) {
impl.logger.Infow("analysing CLI diff for analytics flow", "checkoutPath", checkoutPath, "old", old, "new", new)
if len(commitsGoGit) == 0 || len(commitsCli) == 0 {
return
}
oldestHashGoGit := getOldestCommit(commitsGoGit).Hash.Long
oldestHashCli := getOldestCommit(commitsCli).Hash.Long
if oldestHashGoGit != oldestHashCli {
impl.logger.Infow("oldest commit did not match for analytics flow", "checkoutPath", checkoutPath, "old", oldestHashGoGit, "new", oldestHashCli)
} else {
impl.logger.Infow("oldest commit matched for analytics flow", "checkoutPath", checkoutPath, "old", oldestHashGoGit, "new", oldestHashCli)
}
}
func getOldestCommit(commits []*Commit) *Commit {
oldest := commits[0]
for _, commit := range commits {
if oldest.Author.Date.After(commit.Author.Date) {
oldest = commit
}
}
return oldest
}
func (impl RepositoryManagerAnalyticsImpl) getCommitDiff(repository *GitRepository, newHash plumbing.Hash, oldHash plumbing.Hash) ([]*Commit, error) {
commits, err := computeDiff(repository.Repository, &newHash, &oldHash)
if err != nil {
impl.logger.Errorw("can't get commits: ", "err", err)
return nil, err
}
var serializableCommits []*Commit
for _, c := range commits {
t, err := repository.TagObject(c.Hash)
if err != nil && err != plumbing.ErrObjectNotFound {
impl.logger.Errorw("can't get tag: ", "err", err)
return nil, err
}
serializableCommits = append(serializableCommits, transform(c, t))
}
return serializableCommits, nil
}
func (impl RepositoryManagerAnalyticsImpl) getPatchObject(gitCtx GitContext, repository *git.Repository, oldHash, newHash plumbing.Hash) (*object.Patch, error) {
patch := &object.Patch{}
old, err := repository.CommitObject(newHash)
if err != nil {
return nil, err
}
new, err := repository.CommitObject(oldHash)
if err != nil {
return nil, err
}
oldTree, err := old.Tree()
if err != nil {
return nil, err
}
newTree, err := new.Tree()
if err != nil {
return nil, err
}
ctx, cancel := gitCtx.WithTimeout(impl.configuration.GoGitTimeout)
defer cancel()
patch, err = oldTree.PatchContext(ctx, newTree)
if err != nil {
return nil, err
}
return patch, nil
}
func processGitLogOutputForAnalytics(out string) ([]*Commit, error) {
gitCommits := make([]*Commit, 0)
if len(out) == 0 {
return gitCommits, nil
}
gitCommitFormattedList, err := parseFormattedLogOutput(out)
if err != nil {
return gitCommits, err
}
for _, formattedCommit := range gitCommitFormattedList {
gitCommits = append(gitCommits, formattedCommit.transformToCommit())
}
return gitCommits, nil
}