-
Notifications
You must be signed in to change notification settings - Fork 0
/
avl.go
221 lines (197 loc) · 4.29 KB
/
avl.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
package main
import (
"bufio"
"fmt"
"github.com/urfave/cli"
"math"
"os/exec"
"strings"
)
type FileOwnership struct {
Files map[string]map[string]bool
}
func NewFileOwnership() FileOwnership {
return FileOwnership{
Files: make(map[string]map[string]bool),
}
}
func (f *FileOwnership) AddOwner(file string, owner string) {
if _, found := f.Files[file]; !found {
f.Files[file] = make(map[string]bool)
}
f.Files[file][owner] = true
}
func (f *FileOwnership) TopOwners() []string {
h := NewHistogram()
for _, owners := range f.Files {
for owner, _ := range owners {
h.Increment(owner)
}
}
result := make([]string, 0)
for _, entry := range h.SortedView() {
result = append(result, entry.Key)
}
return result
}
func (f *FileOwnership) FilesWithoutOwners() int {
res := 0
for _, owners := range f.Files {
k := 0
for _, role := range owners {
if role {
k++
}
}
if k == 0 {
res++
}
}
return res
}
func (f *FileOwnership) AllFiles() int {
return len(f.Files)
}
func (f *FileOwnership) RemoveOwner(owner string) {
for file, owners := range f.Files {
if _, found := owners[owner]; found {
f.Files[file][owner] = false
}
}
}
type DOARepo struct {
Files map[string]*HistogramWithLast
Deleted map[string]bool
Renames map[string]string
}
func (r *DOARepo) UpdateFile(name string, email string) {
if _, found := r.Files[name]; !found {
r.Files[name] = NewHistogramWithLast()
}
r.Files[name].Increment(email)
}
func (r *DOARepo) Normalize(name string) string {
t := name
for {
des, found := r.Renames[t]
if !found {
return t
} else {
t = des
}
}
return t
}
type HistogramWithLast struct {
Histogram
Last string
}
func (h *HistogramWithLast) Increment(key string) {
h.Histogram.Increment(key)
h.Last = key
}
func NewHistogramWithLast() *HistogramWithLast {
return &HistogramWithLast{
NewHistogram(),
"",
}
}
func AVL(repo string) ([]string, error) {
codeOwners := make([]string, 0)
result := NewDOARepo()
command := exec.Command("git", "log", "--name-status", "--find-renames", "--pretty=format:commit %H %ae")
command.Dir = repo
r, _ := command.StdoutPipe()
done := make(chan struct{})
scanner := bufio.NewScanner(r)
go func() {
currentAuthor := ""
for scanner.Scan() {
line := scanner.Text()
parts := strings.Split(strings.TrimSpace(line), " ")
if parts[0] == "commit" {
currentAuthor = parts[2]
} else {
parts = strings.Split(strings.TrimSpace(line), "\t")
if parts[0] == "D" {
result.Deleted[result.Normalize(parts[1])] = true
} else if parts[0] == "M" || parts[0] == "A" {
result.UpdateFile(result.Normalize(parts[1]), currentAuthor)
} else if strings.HasPrefix(parts[0], "R") {
if result.Renames[parts[2]] == parts[1] {
delete(result.Renames, parts[2])
} else {
result.Renames[parts[1]] = result.Normalize(parts[2])
}
}
}
}
done <- struct{}{}
}()
err := command.Start()
if err != nil {
return codeOwners, err
}
<-done
err = command.Wait()
if err != nil {
return codeOwners, err
}
owners := NewFileOwnership()
for file, hist := range result.Files {
sum := hist.Histogram.Sum()
max := float64(0)
doas := make(map[string]float64)
for _, entry := range hist.Histogram.SortedView() {
doa := 3.293 + 0.164*float64(entry.Occurrence) - 0.321*math.Log(float64(1+sum-entry.Occurrence))
if entry.Key == hist.Last {
doa += 1.098
}
if doa > max {
max = doa
}
doas[entry.Key] = doa
}
for k, v := range doas {
doas[k] = v / max
if doas[k] > 0.5 && v > 3.293 {
owners.AddOwner(file, k)
}
}
}
factor := 0
for _, owner := range owners.TopOwners() {
if owners.FilesWithoutOwners() > owners.AllFiles()/2 {
return codeOwners, nil
}
owners.RemoveOwner(owner)
codeOwners = append(codeOwners, owner)
factor++
}
return codeOwners, nil
}
func NewDOARepo() DOARepo {
return DOARepo{
Deleted: make(map[string]bool),
Files: make(map[string]*HistogramWithLast),
Renames: make(map[string]string),
}
}
func init() {
App.Commands = append(App.Commands, cli.Command{
Name: "avl",
Flags: []cli.Flag{
cli.StringFlag{
Name: "repo",
},
},
Action: func(c *cli.Context) error {
res, err := AVL(repo(c.String("repo")))
if err != nil {
return err
}
fmt.Printf("%d", len(res))
return nil
},
})
}