@@ -4,24 +4,22 @@ import (
4
4
"dnacollector"
5
5
"encoding/json"
6
6
"errors"
7
- "fmt"
7
+ "io"
8
+
8
9
"github.com/caarlos0/env"
9
10
log "github.com/sirupsen/logrus"
10
11
git2 "gopkg.in/src-d/go-git.v4"
11
12
"gopkg.in/src-d/go-git.v4/plumbing/format/diff"
12
13
git "gopkg.in/src-d/go-git.v4/plumbing/object"
13
- "io"
14
14
)
15
15
16
16
type config struct {
17
17
GithubToken string `env:"GITHUB_TOKEN"`
18
18
GitlabToken string `env:"GITLAB_TOKEN"`
19
19
}
20
20
21
- var (
22
- // ErrGroupNotFound is the error returned when group can not be found
23
- ErrFileSimplifiedCreation = errors .New ("we could not instantiate GitFileSimplified from GitFile" )
24
- )
21
+ // ErrFileSimplifiedCreation is the error returned when GitFileSimplified can not be instantiated.
22
+ var ErrFileSimplifiedCreation = errors .New ("we could not instantiate GitFileSimplified from GitFile" )
25
23
26
24
type GitFileSimplified struct {
27
25
Name string `json:"name"`
@@ -40,11 +38,18 @@ type CommitSimplified struct {
40
38
41
39
func NewFromGitFile (file * git.File ) * GitFileSimplified {
42
40
isBinary , _ := file .IsBinary ()
41
+
43
42
return & GitFileSimplified {Name : file .Name , Sha : file .Hash .String (), IsBinary : isBinary , Size : file .Size }
44
43
}
45
44
46
45
func NewFromCommit (commit * git.Commit , files []* GitFileSimplified ) * CommitSimplified {
47
- return & CommitSimplified {Message : commit .Message , Sha : commit .Hash .String (), Author : commit .Author , Committer : commit .Committer , Files : files }
46
+ return & CommitSimplified {
47
+ Message : commit .Message ,
48
+ Sha : commit .Hash .String (),
49
+ Author : commit .Author ,
50
+ Committer : commit .Committer ,
51
+ Files : files ,
52
+ }
48
53
}
49
54
50
55
func NewFromFilePatch (filePatch diff.FilePatch ) (* GitFileSimplified , error ) {
@@ -54,12 +59,13 @@ func NewFromFilePatch(filePatch diff.FilePatch) (*GitFileSimplified, error) {
54
59
// If the patch deletes a file, "to" will be nil.
55
60
56
61
// Rare usecase
57
- if to == nil && from == nil {
62
+ switch {
63
+ case to == nil && from == nil :
58
64
return nil , ErrFileSimplifiedCreation
59
- } else if to != nil {
65
+ case to != nil :
60
66
// File creation
61
67
return & GitFileSimplified {Name : to .Path (), Sha : to .Hash ().String (), IsBinary : isBinary , Size : 0 }, nil
62
- } else {
68
+ default :
63
69
// File deletion
64
70
return & GitFileSimplified {Name : from .Path (), Sha : from .Hash ().String (), IsBinary : isBinary , Size : 0 }, nil
65
71
}
@@ -85,22 +91,23 @@ func (a *Analyzer) GetFilesFromCommit(commit *git.Commit) ([]*GitFileSimplified,
85
91
return nil , err
86
92
}
87
93
88
- filesIter .ForEach (func (file * git.File ) error {
94
+ _ = filesIter .ForEach (func (file * git.File ) error {
89
95
fileSimplified := NewFromGitFile (file )
90
96
log .Debugf ("Appending file %s" , fileSimplified .Name )
91
97
if fileSimplified .Size > 0 {
92
- /* fileSimplifiedJson, _ := json.Marshal(fileSimplified)*/
98
+ // fileSimplifiedJson, _ := json.Marshal(fileSimplified)
93
99
log .Info (fileSimplified )
94
100
}
95
101
96
102
files = append (files , fileSimplified )
103
+
97
104
return nil
98
105
})
99
- // There is a parent, so we consider only the diff
100
106
} else {
107
+ // There is a parent, so we consider only the diff
101
108
patch , _ := commit .Patch (parent )
102
109
filePatches := patch .FilePatches ()
103
- //log.Info(patch.Stats())
110
+ // log.Info(patch.Stats())
104
111
for _ , fp := range filePatches {
105
112
fileSimplified , err := NewFromFilePatch (fp )
106
113
//for _, chunk := range fp.Chunks() {
@@ -112,33 +119,36 @@ func (a *Analyzer) GetFilesFromCommit(commit *git.Commit) ([]*GitFileSimplified,
112
119
files = append (files , fileSimplified )
113
120
} else {
114
121
continue
115
- //log.Error(ErrFileSimplifiedCreation)
116
- //log.Warn(commit)
117
- //log.Warn(fp)
118
-
122
+ // log.Error(ErrFileSimplifiedCreation)
123
+ // log.Warn(commit)
124
+ // log.Warn(fp)
119
125
}
120
126
}
121
127
}
122
128
123
129
return files , nil
124
130
}
125
131
126
- // AnalyzeCommit extracts author and committer from a commit
132
+ // AnalyzeCommit extracts author and committer from a commit.
127
133
func (a * Analyzer ) AnalyzeCommit (commit * git.Commit ) string {
128
- // Store commmit sha
134
+ // Store commmit sha
129
135
files , _ := a .GetFilesFromCommit (commit )
130
136
a .CommitsList = append (a .CommitsList , NewFromCommit (commit , files ))
137
+
131
138
return commit .Hash .String ()
132
139
}
133
140
134
141
func (a * Analyzer ) GetStats () map [string ]int {
135
142
res := make (map [string ]int )
136
143
res ["nb_commits" ] = len (a .CommitsList )
137
- nb_files_shas := 0
144
+ nbFilesSHAs := 0
145
+
138
146
for _ , commit := range a .CommitsList {
139
- nb_files_shas += len (commit .Files )
147
+ nbFilesSHAs += len (commit .Files )
140
148
}
141
- res ["nb_files_shas" ] = nb_files_shas
149
+
150
+ res ["nb_files_shas" ] = nbFilesSHAs
151
+
142
152
return res
143
153
}
144
154
@@ -164,7 +174,8 @@ func main() {
164
174
if err := env .Parse (& conf ); err != nil {
165
175
log .Fatalf ("Could not parse env: %v\n " , err )
166
176
}
167
- log .Debug (conf )
177
+
178
+ log .Debugln (conf )
168
179
//var cloner dnacollector.Cloner = &dnacollector.MemoryCloner{}
169
180
//auth := &http.BasicAuth{
170
181
// Username: "ericfourrier",
@@ -173,23 +184,36 @@ func main() {
173
184
174
185
repository , err := git2 .PlainOpen ("/Users/ericfourrier/Documents/GGCode/dna-collector/testdata/react-vis" )
175
186
if err != nil {
176
- fmt . Print (err )
187
+ log . Warnln (err )
177
188
}
178
- repository .Config ()
179
- //log.Infof("Cloned repo %v (size: %v)\n", repository.n, repository.GetStorageSize())
189
+
190
+ _ , err = repository .Config ()
191
+ // config is not used?
192
+ if err != nil {
193
+ log .Warnln (err )
194
+ }
195
+
196
+ // log.Infof("Cloned repo %v (size: %v)\n", repository.n, repository.GetStorageSize())
180
197
extractor , err := dnacollector .NewExtractor (repository )
198
+ if err != nil {
199
+ log .Warnln (err )
200
+ }
201
+
181
202
analyzer := NewAnalyzer ()
203
+
182
204
for {
183
205
commit , err := extractor .ExtractNextCommit ()
184
206
if err != nil && err != io .EOF {
185
207
log .Panic (err )
186
208
}
209
+
187
210
if commit == nil {
188
211
break
189
212
}
190
213
191
214
analyzer .AnalyzeCommit (commit )
192
215
}
216
+
193
217
res2 , _ := json .Marshal (analyzer .CommitsList )
194
218
log .Debug (string (res2 ))
195
219
//fmt.Print(analyzer.SetCommitsSha)
@@ -200,5 +224,4 @@ func main() {
200
224
//}
201
225
log .Info (analyzer .GetStats ())
202
226
log .Infof ("Done extracting %v\n " , repository )
203
-
204
227
}
0 commit comments