-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathharvester.go
144 lines (122 loc) · 3.61 KB
/
harvester.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
package inspector
import (
"github.com/libgit2/git2go"
"log"
"strings"
"net/http"
)
func Harvest(repoName string) *Everything {
resp, _ := http.Get("https://github.com/" + repoName)
if resp.StatusCode == 404 {
return nil
}
repo, _ := GetRepo(repoName)
defer repo.Free()
defer CleanTempDir()
var everything *Everything = &Everything{}
var files map[string]*File = make(map[string]*File)
var commits map[string]*Commit = make(map[string]*Commit)
everything.Files = files
everything.Commits = commits
numberOfCommits, _ := GetNumberOfCommits(repo)
count := 0
WalkCommits(repo, func(previousCommit *git.Commit, currentCommit *git.Commit) bool {
if (len(files) == 0) {
//Initially populate the files map with the whole file structure at the specific commit
log.Printf("[START] Create base tree")
diff, _ := GetDiff(repo, previousCommit, nil)
WalkFiles(diff, func(file git.DiffDelta, process float64) {
if !strings.HasSuffix(strings.ToLower(file.OldFile.Path), ".go") {
return
}
blob, err := repo.LookupBlob(file.OldFile.Oid)
if err != nil {
return
}
astFile := ParseFileContents(file.OldFile.Path, string(blob.Contents()))
files[file.OldFile.Path] = astFile
})
log.Printf("[Success] Create base tree")
return true
}
count++
diff, _ := GetDiff(repo, previousCommit, currentCommit)
newestCommit := &Commit{
Hash: currentCommit.Id().String(),
Contributor: nil,
Message: currentCommit.Message(),
Time: currentCommit.Author().When,
}
commits[newestCommit.Hash] = newestCommit
var commitFiles map[string]*File = make(map[string]*File)
var checked map[string]bool = map[string]bool{}
WalkHunks(diff, func(file git.DiffDelta, hunk git.DiffHunk) {
if !strings.HasSuffix(strings.ToLower(file.OldFile.Path), ".go") {
return
}
var newFile *File
if commitFiles[file.NewFile.Path] != nil {
newFile = commitFiles[file.NewFile.Path]
} else {
blob, err := repo.LookupBlob(file.NewFile.Oid)
if err != nil {
return
}
newFile = ParseFileContents(file.NewFile.Path, string(blob.Contents()))
}
if files[newFile.Path] != nil {
files[file.OldFile.Path] = newFile
}
if !checked[newFile.Path] {
checked[newFile.Path] = true
if files[newFile.Path] != nil {
remove := []int{}
for i, u := range files[newFile.Path].Units {
for _, un := range newFile.Units {
if u.Type == un.Type && u.Name == un.Name {
remove = append(remove, i)
}
}
}
newUnits := []*Unit{}
for i, unit := range files[newFile.Path].Units {
ff := false
for _, j := range remove {
if i == j {
ff = true
}
}
if !ff {
newUnits = append(newUnits, unit)
}
files[newFile.Path].Units = newUnits
}
} else {
files[newFile.Path] = newFile
}
}
for _, unit := range newFile.Units {
unit.RatioSum += 0.5
unit.TimesChanged++
if unit.IntersectsInt(hunk.NewStart, hunk.NewStart + hunk.NewLines) {
magicNumber := unit.numberOfLinesIntersected(hunk.NewStart, hunk.NewStart + hunk.NewLines) * len(commits)
unit.RatioSum += float64(magicNumber) * float64(count) / float64(numberOfCommits)
unit.Commits = append(unit.Commits, newestCommit)
if files[file.NewFile.Path] != nil {
for _, u := range files[file.NewFile.Path].Units {
if u.Type == unit.Type && u.Name == unit.Name {
u.LineStart = unit.LineStart
u.LineEnd = unit.LineEnd
}
}
} else {
files[file.NewFile.Path] = newFile
}
} else {
}
}
})
return true
})
return everything
}