-
Notifications
You must be signed in to change notification settings - Fork 0
/
evaluator.go
205 lines (177 loc) · 3.98 KB
/
evaluator.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
package patch_evaluator
import (
"io"
"strings"
"github.com/bluekeyes/go-gitdiff/gitdiff"
"github.com/sergi/go-diff/diffmatchpatch"
)
type Evaluator struct {
}
func (e Evaluator) Evaluate(r io.Reader, lowValue []Filterer, noValue []Filterer) ([]*gitdiff.File, []*Reasons, []*Reasons, error) {
files, _, err := gitdiff.Parse(r)
if err != nil {
return nil, nil, nil, err
}
filtered := []*gitdiff.File{}
reasonsLowValue := []*Reasons{}
reasonsNoValue := []*Reasons{}
loop:
for _, file := range files {
for _, filterer := range noValue {
if rea := filterer.Filter(file); rea != nil {
reasonsNoValue = append(reasonsNoValue, rea)
continue loop
}
}
for _, filterer := range lowValue {
if rea := filterer.Filter(file); rea != nil {
reasonsLowValue = append(reasonsLowValue, rea)
continue loop
}
}
filtered = append(filtered, file)
}
return filtered, reasonsLowValue, reasonsNoValue, nil
}
var contentDiff = diffmatchpatch.New()
type Filterer interface {
Filter(file *gitdiff.File) *Reasons
}
type Reasons struct {
File string
Message string
}
type StringsModifyFilterer struct{}
func (s StringsModifyFilterer) Filter(file *gitdiff.File) *Reasons {
total := 0
stringModiry := 0
for _, fragments := range file.TextFragments {
for i, line := range fragments.Lines {
if line.Op != gitdiff.OpAdd {
continue
}
if i == 0 {
continue
}
prvLine := fragments.Lines[i-1]
if prvLine.Op != gitdiff.OpDelete {
continue
}
diffs := contentDiff.DiffMain(prvLine.Line, line.Line, false)
if len(diffs) != 3 {
continue
}
total++
if strings.ContainsAny(diffs[0].Text, `"'`) && strings.ContainsAny(diffs[2].Text, `"'`) {
stringModiry++
}
}
}
if total != 0 && total == stringModiry {
return &Reasons{
File: file.NewName,
Message: "only string modified",
}
}
return nil
}
type EmptyLineFilterer struct{}
func (s EmptyLineFilterer) Filter(file *gitdiff.File) *Reasons {
if file.IsNew {
return nil
}
for _, fragments := range file.TextFragments {
for _, line := range fragments.Lines {
if line.Op != gitdiff.OpContext {
continue
}
if strings.TrimSpace(line.Line) == "" {
continue
}
return nil
}
}
return &Reasons{
File: file.NewName,
Message: "not modified",
}
}
type CommentFilterer struct{}
func (s CommentFilterer) Filter(file *gitdiff.File) *Reasons {
for _, fragments := range file.TextFragments {
for _, line := range fragments.Lines {
if line.Op != gitdiff.OpAdd {
continue
}
if strings.TrimSpace(line.Line) == "" {
continue
}
if strings.HasPrefix(strings.TrimSpace(line.Line), "//") {
continue
}
if strings.HasPrefix(strings.TrimSpace(line.Line), "#") {
continue
}
return nil
}
}
return &Reasons{
File: file.NewName,
Message: "All lines are comments",
}
}
type FocusSuffixFilterer []string
func (s FocusSuffixFilterer) Filter(file *gitdiff.File) *Reasons {
for _, v := range s {
if strings.HasSuffix(file.NewName, v) {
return nil
}
}
return &Reasons{
File: file.NewName,
Message: "not focused",
}
}
type SuffixFilterer []string
func (s SuffixFilterer) Filter(file *gitdiff.File) *Reasons {
for _, v := range s {
if strings.HasSuffix(file.NewName, v) {
return &Reasons{
File: file.NewName,
Message: "suffix " + v,
}
}
}
return nil
}
type PrefixFilterer []string
func (s PrefixFilterer) Filter(file *gitdiff.File) *Reasons {
for _, v := range s {
if strings.HasPrefix(file.NewName, v) {
return &Reasons{
File: file.NewName,
Message: "prefix " + v,
}
}
}
return nil
}
type ContainsFilterer []string
func (s ContainsFilterer) Filter(file *gitdiff.File) *Reasons {
for _, v := range s {
if strings.Contains(file.NewName, v) {
return &Reasons{
File: file.NewName,
Message: "contains " + v,
}
}
}
return nil
}
type NotFilter struct {
Filterer Filterer
}
func (s NotFilter) Filter(file *gitdiff.File) *Reasons {
s.Filterer.Filter(file)
return nil
}