Skip to content

Commit 65f4026

Browse files
author
jguerreiro
committed
fix(codestyle): implement changes suggested by golangci
1 parent 0f7791f commit 65f4026

File tree

12 files changed

+249
-167
lines changed

12 files changed

+249
-167
lines changed

.golangci.yml

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,8 @@ linters:
99
- dogsled
1010
- dupl
1111
- errcheck
12-
- funlen
1312
- gochecknoinits
1413
- goconst
15-
- gocritic
16-
- gocyclo
1714
- gofmt
1815
- goimports
1916
- golint
@@ -41,6 +38,11 @@ linters:
4138
- wrapcheck
4239
- exhaustivestruct
4340
- errorlint
41+
# temporary disables
42+
- gocritic
43+
- gofumpt
44+
- funlen
45+
- gocyclo
4446
disable-all: false
4547
presets:
4648
- bugs

analyzer.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@ import (
44
git "gopkg.in/src-d/go-git.v4/plumbing/object"
55
)
66

7-
// Analyzer analyzer a commit to extract its author
7+
// Analyzer analyzer a commit to extract its author.
88
type Analyzer struct {
99
}
1010

11-
// AnalyzeCommit extracts author and commiter from a commit
12-
func (a *Analyzer) AnalyzeCommit(commit *git.Commit) (author git.Signature, commiter git.Signature) {
11+
// AnalyzeCommit extracts author and commiter from a commit.
12+
func (a *Analyzer) AnalyzeCommit(commit *git.Commit) (author, commiter git.Signature) {
1313
return commit.Author, commit.Committer
1414
}

cloner.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,15 @@ import (
1111
"gopkg.in/src-d/go-git.v4/storage/memory"
1212
)
1313

14-
// Cloner represents a cloner of git repository
14+
// Cloner represents a cloner of git repository.
1515
type Cloner interface {
1616
CloneRepository(url string, auth transport.AuthMethod) (*git.Repository, error)
1717
}
1818

19-
// MemoryCloner clones a git repository in memory
19+
// MemoryCloner clones a git repository in memory.
2020
type MemoryCloner struct{}
2121

22-
// CloneRepository clones a git repository given its information
22+
// CloneRepository clones a git repository given its information.
2323
func (*MemoryCloner) CloneRepository(url string, auth transport.AuthMethod) (*git.Repository, error) {
2424
return git.Clone(memory.NewStorage(), nil, &git.CloneOptions{
2525
URL: url,
@@ -28,16 +28,18 @@ func (*MemoryCloner) CloneRepository(url string, auth transport.AuthMethod) (*gi
2828
})
2929
}
3030

31-
// DiskCloner closes a git repository on disk in a temporary file
31+
// DiskCloner closes a git repository on disk in a temporary file.
3232
type DiskCloner struct{}
3333

34-
// CloneRepository clones a git repository given its information
34+
// CloneRepository clones a git repository given its information.
3535
func (*DiskCloner) CloneRepository(url string, auth transport.AuthMethod) (*git.Repository, error) {
3636
tmpDir, err := ioutil.TempDir("", "fs-")
3737
if err != nil {
3838
return nil, err
3939
}
40+
4041
fs := osfs.New(tmpDir)
42+
4143
return git.Clone(filesystem.NewStorage(fs, cache.NewObjectLRUDefault()), nil, &git.CloneOptions{
4244
URL: url,
4345
Progress: ioutil.Discard,

cmd/dna-collector-test/main.go

Lines changed: 51 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,22 @@ import (
44
"dnacollector"
55
"encoding/json"
66
"errors"
7-
"fmt"
7+
"io"
8+
89
"github.com/caarlos0/env"
910
log "github.com/sirupsen/logrus"
1011
git2 "gopkg.in/src-d/go-git.v4"
1112
"gopkg.in/src-d/go-git.v4/plumbing/format/diff"
1213
git "gopkg.in/src-d/go-git.v4/plumbing/object"
13-
"io"
1414
)
1515

1616
type config struct {
1717
GithubToken string `env:"GITHUB_TOKEN"`
1818
GitlabToken string `env:"GITLAB_TOKEN"`
1919
}
2020

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")
2523

2624
type GitFileSimplified struct {
2725
Name string `json:"name"`
@@ -40,11 +38,18 @@ type CommitSimplified struct {
4038

4139
func NewFromGitFile(file *git.File) *GitFileSimplified {
4240
isBinary, _ := file.IsBinary()
41+
4342
return &GitFileSimplified{Name: file.Name, Sha: file.Hash.String(), IsBinary: isBinary, Size: file.Size}
4443
}
4544

4645
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+
}
4853
}
4954

5055
func NewFromFilePatch(filePatch diff.FilePatch) (*GitFileSimplified, error) {
@@ -54,12 +59,13 @@ func NewFromFilePatch(filePatch diff.FilePatch) (*GitFileSimplified, error) {
5459
// If the patch deletes a file, "to" will be nil.
5560

5661
// Rare usecase
57-
if to == nil && from == nil {
62+
switch {
63+
case to == nil && from == nil:
5864
return nil, ErrFileSimplifiedCreation
59-
} else if to != nil {
65+
case to != nil:
6066
// File creation
6167
return &GitFileSimplified{Name: to.Path(), Sha: to.Hash().String(), IsBinary: isBinary, Size: 0}, nil
62-
} else {
68+
default:
6369
// File deletion
6470
return &GitFileSimplified{Name: from.Path(), Sha: from.Hash().String(), IsBinary: isBinary, Size: 0}, nil
6571
}
@@ -85,22 +91,23 @@ func (a *Analyzer) GetFilesFromCommit(commit *git.Commit) ([]*GitFileSimplified,
8591
return nil, err
8692
}
8793

88-
filesIter.ForEach(func(file *git.File) error {
94+
_ = filesIter.ForEach(func(file *git.File) error {
8995
fileSimplified := NewFromGitFile(file)
9096
log.Debugf("Appending file %s", fileSimplified.Name)
9197
if fileSimplified.Size > 0 {
92-
/* fileSimplifiedJson, _ := json.Marshal(fileSimplified)*/
98+
// fileSimplifiedJson, _ := json.Marshal(fileSimplified)
9399
log.Info(fileSimplified)
94100
}
95101

96102
files = append(files, fileSimplified)
103+
97104
return nil
98105
})
99-
// There is a parent, so we consider only the diff
100106
} else {
107+
// There is a parent, so we consider only the diff
101108
patch, _ := commit.Patch(parent)
102109
filePatches := patch.FilePatches()
103-
//log.Info(patch.Stats())
110+
// log.Info(patch.Stats())
104111
for _, fp := range filePatches {
105112
fileSimplified, err := NewFromFilePatch(fp)
106113
//for _, chunk := range fp.Chunks() {
@@ -112,33 +119,36 @@ func (a *Analyzer) GetFilesFromCommit(commit *git.Commit) ([]*GitFileSimplified,
112119
files = append(files, fileSimplified)
113120
} else {
114121
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)
119125
}
120126
}
121127
}
122128

123129
return files, nil
124130
}
125131

126-
// AnalyzeCommit extracts author and committer from a commit
132+
// AnalyzeCommit extracts author and committer from a commit.
127133
func (a *Analyzer) AnalyzeCommit(commit *git.Commit) string {
128-
// Store commmit sha
134+
// Store commmit sha
129135
files, _ := a.GetFilesFromCommit(commit)
130136
a.CommitsList = append(a.CommitsList, NewFromCommit(commit, files))
137+
131138
return commit.Hash.String()
132139
}
133140

134141
func (a *Analyzer) GetStats() map[string]int {
135142
res := make(map[string]int)
136143
res["nb_commits"] = len(a.CommitsList)
137-
nb_files_shas := 0
144+
nbFilesSHAs := 0
145+
138146
for _, commit := range a.CommitsList {
139-
nb_files_shas += len(commit.Files)
147+
nbFilesSHAs += len(commit.Files)
140148
}
141-
res["nb_files_shas"] = nb_files_shas
149+
150+
res["nb_files_shas"] = nbFilesSHAs
151+
142152
return res
143153
}
144154

@@ -164,7 +174,8 @@ func main() {
164174
if err := env.Parse(&conf); err != nil {
165175
log.Fatalf("Could not parse env: %v\n", err)
166176
}
167-
log.Debug(conf)
177+
178+
log.Debugln(conf)
168179
//var cloner dnacollector.Cloner = &dnacollector.MemoryCloner{}
169180
//auth := &http.BasicAuth{
170181
// Username: "ericfourrier",
@@ -173,23 +184,36 @@ func main() {
173184

174185
repository, err := git2.PlainOpen("/Users/ericfourrier/Documents/GGCode/dna-collector/testdata/react-vis")
175186
if err != nil {
176-
fmt.Print(err)
187+
log.Warnln(err)
177188
}
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())
180197
extractor, err := dnacollector.NewExtractor(repository)
198+
if err != nil {
199+
log.Warnln(err)
200+
}
201+
181202
analyzer := NewAnalyzer()
203+
182204
for {
183205
commit, err := extractor.ExtractNextCommit()
184206
if err != nil && err != io.EOF {
185207
log.Panic(err)
186208
}
209+
187210
if commit == nil {
188211
break
189212
}
190213

191214
analyzer.AnalyzeCommit(commit)
192215
}
216+
193217
res2, _ := json.Marshal(analyzer.CommitsList)
194218
log.Debug(string(res2))
195219
//fmt.Print(analyzer.SetCommitsSha)
@@ -200,5 +224,4 @@ func main() {
200224
//}
201225
log.Info(analyzer.GetStats())
202226
log.Infof("Done extracting %v\n", repository)
203-
204227
}

0 commit comments

Comments
 (0)