generated from axetroy/go-cli-boilerplate
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy path2_extractor.go
120 lines (93 loc) · 2.43 KB
/
2_extractor.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
package whatchanged
import (
"io"
git "github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/plumbing"
"github.com/go-git/go-git/v5/plumbing/object"
"github.com/pkg/errors"
"github.com/release-lab/whatchanged/internal/client"
)
type ExtractSplice struct {
Name string // The name of splice
Commit []*object.Commit // The commits including in this splice
Tag *client.Tag // If this splice is a tag splice
}
func getTagOfCommit(tags []*client.Tag, commit *object.Commit) (result []*client.Tag) {
for _, tag := range tags {
if tag.Commit.Hash.String() == commit.Hash.String() {
result = append(result, tag)
}
}
return
}
func Extract(g *client.GitClient, scopes []*Scope) ([]*ExtractSplice, error) {
splices := make([]*ExtractSplice, 0)
for _, scope := range scopes {
options := git.LogOptions{From: plumbing.NewHash(scope.Start.Commit.Hash.String())}
cIter, err := g.Repository.Log(&options)
if err != nil {
return nil, errors.WithStack(err)
}
commits := make([]*object.Commit, 0)
for {
if c, err := cIter.Next(); err == io.EOF {
break
} else if err != nil {
return nil, errors.WithStack(err)
} else if c == nil {
break
} else if c.Hash.String() == scope.End.Commit.Hash.String() {
commits = append(commits, c)
break
} else {
commits = append(commits, c)
}
}
if err != nil {
return nil, errors.WithStack(err)
}
index := 0
tagIndex := 0
for {
if index == len(commits) {
break
}
currentCommit := commits[index]
item := &ExtractSplice{
Name: "Unreleased",
Commit: make([]*object.Commit, 0),
}
if tags := getTagOfCommit(scope.Tags, currentCommit); len(tags) != 0 {
if tagIndex+1 == len(tags) {
item.Tag = tags[tagIndex]
item.Name = tags[tagIndex].Name
item.Commit = append(item.Commit, currentCommit)
tagIndex = 0 // reset tag index to zero
} else {
item.Tag = tags[tagIndex]
item.Name = tags[tagIndex].Name
splices = append(splices, item)
tagIndex++
continue
}
} else {
item.Commit = append(item.Commit, currentCommit)
}
index++
loop:
for {
if index == len(commits) {
break loop
}
nextCommit := commits[index]
if t := getTagOfCommit(scope.Tags, nextCommit); t != nil {
break loop
}
item.Commit = append(item.Commit, nextCommit)
index++
}
splices = append(splices, item)
}
}
return splices, nil
}