forked from nielsing/yar
/
git.go
208 lines (187 loc) · 5.2 KB
/
git.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
package robber
import (
"os"
"strings"
"gopkg.in/src-d/go-git.v4"
"gopkg.in/src-d/go-git.v4/plumbing/format/diff"
"gopkg.in/src-d/go-git.v4/plumbing/object"
"gopkg.in/src-d/go-git.v4/plumbing/transport/http"
)
// DiffObject holds everything that is needed to analyze a diff.
type DiffObject struct {
Commit *object.Commit
Diff *string
Reponame *string
Filepath *string
}
// NewDiffObject returns a new DiffObject.
func NewDiffObject(commit *object.Commit, diff, reponame, filepath *string) *DiffObject {
return &DiffObject{
Commit: commit,
Diff: diff,
Reponame: reponame,
Filepath: filepath,
}
}
// getCloneOptions returns either an authenticated clone of a repo or an
// anonymous clone of a repo based on whether an AccessToken was given or not.
func getCloneOptions(m *Middleware, url string) *git.CloneOptions {
if m.AccessToken != "" {
return &git.CloneOptions{
URL: url,
Depth: *m.Flags.CommitDepth + 1, // There is an off by one error in Depth field.
Auth: &http.BasicAuth{
Username: "NotEmpty", // https://godoc.org/gopkg.in/src-d/go-git.v4#PlainClone
Password: m.AccessToken,
},
}
}
return &git.CloneOptions{
URL: url,
Depth: *m.Flags.CommitDepth + 1, // There is an off by one error in Depth field.
}
}
// cloneRepo creates a temp directory in the OS's temp directory
// and clones the given URL into it.
func cloneRepo(m *Middleware, url string, cloneFolder string) (*git.Repository, error) {
opt := getCloneOptions(m, url)
repo, err := git.PlainClone(cloneFolder, !*m.Flags.NoBare, opt)
if err != nil {
return nil, err
}
return repo, nil
}
// OpenRepo opens a repository found at the given path.
// If the path points to a nonexistant repository it assumes that an URL
// was given and tries to clone it instead.
func OpenRepo(m *Middleware, path string) (*git.Repository, error) {
dir, exists := GetDir(path)
if !*m.Flags.NoCache && !*m.Flags.NoBare && exists {
repo, err := git.PlainOpen(dir)
if err != nil {
return nil, err
}
return repo, nil
}
if *m.Flags.NoBare || *m.Flags.NoCache {
os.RemoveAll(dir)
}
repo, err := cloneRepo(m, path, dir)
if err != nil {
return nil, err
}
return repo, nil
}
// GetCommits simply traverses a given repository, gathering all commits
// and then returns a list of them.
func GetCommits(m *Middleware, repo *git.Repository, reponame string) ([]*object.Commit, error) {
defer func() {
if r := recover(); r != nil {
m.Logger.LogFail("%s is corrupted please run yar --cleanup %s and try again\n",
reponame[9:], reponame[9:])
}
}()
var commits []*object.Commit
ref, err := repo.Head()
commitIter, err := repo.Log(&git.LogOptions{From: ref.Hash(), Order: git.LogOrderCommitterTime})
if err != nil {
return nil, err
}
count := 0
commitIter.ForEach(func(c *object.Commit) error {
if count == *m.Flags.CommitDepth {
return nil
}
commits = append(commits, c)
count++
return nil
})
return commits, nil
}
func getParentTree(commit *object.Commit) (*object.Tree, error) {
// Bit of a hack to handle the edge case of 0 parents.
var emptyTree *object.Tree
if commit.NumParents() == 0 {
emptyTree = &object.Tree{Entries: []object.TreeEntry{}}
return emptyTree, nil
}
parent, err := commit.Parents().Next()
if err != nil {
return nil, err
}
parentTree, err := parent.Tree()
if err != nil {
return nil, err
}
return parentTree, nil
}
// GetCommitChanges gets the changes of a commit by comparing it to its'
// parent commit tree.
func GetCommitChanges(commit *object.Commit) (object.Changes, error) {
commitTree, err := commit.Tree()
if err != nil {
return nil, err
}
parentTree, err := getParentTree(commit)
if err != nil {
return nil, err
}
changes, err := object.DiffTree(commitTree, parentTree)
if err != nil {
return nil, err
}
return changes, nil
}
// GetDiffs gets all diffs which are either of type addage or removal
// for a change in a commit.
func GetDiffs(m *Middleware, change *object.Change, reponame string) ([]string, string, error) {
// This is done to handle the following inevitable error https://github.com/sergi/go-diff/issues/89
// If you run into this error a bunch of times then please take a look at the issue and see if you can
// contribute a fix :).
defer func() {
if r := recover(); r != nil {
m.Logger.LogWarn("Encountered a file that is too large to handle in %s!\n", reponame)
}
}()
patch, err := change.Patch()
if err != nil {
return nil, "", err
}
var diffs []string
var filename string
for _, file := range patch.FilePatches() {
if file.IsBinary() {
continue
}
filename = getFilepath(file)
if blacklistedFile(m, filename) {
continue
}
for _, chunk := range file.Chunks() {
// Only look at diffs that add/remove something
if chunk.Type() == 0 {
continue
}
diff := strings.Trim(chunk.Content(), " \n")
diffs = append(diffs, diff)
}
}
return diffs, filename, nil
}
// GetDiffs helper
func getFilepath(file diff.FilePatch) string {
from, to := file.Files()
if from != nil {
return from.Path()
}
return to.Path()
}
// GetDiffs helper
func blacklistedFile(m *Middleware, filename string) bool {
for _, rule := range m.Blacklist {
if rule.Match([]byte(filename)) {
return true
}
}
return false
}