-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support excluding issues by source line regexp
See issues.exclude-rules[i].source. Also introduced file data and file lines cache.
- Loading branch information
Showing
13 changed files
with
365 additions
and
203 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package fsutils | ||
|
||
import ( | ||
"fmt" | ||
"io/ioutil" | ||
|
||
"github.com/golangci/golangci-lint/pkg/logutils" | ||
|
||
"github.com/pkg/errors" | ||
) | ||
|
||
type FileCache struct { | ||
files map[string][]byte | ||
} | ||
|
||
func NewFileCache() *FileCache { | ||
return &FileCache{ | ||
files: map[string][]byte{}, | ||
} | ||
} | ||
|
||
func (fc *FileCache) GetFileBytes(filePath string) ([]byte, error) { | ||
cachedBytes := fc.files[filePath] | ||
if cachedBytes != nil { | ||
return cachedBytes, nil | ||
} | ||
|
||
fileBytes, err := ioutil.ReadFile(filePath) | ||
if err != nil { | ||
return nil, errors.Wrapf(err, "can't read file %s", filePath) | ||
} | ||
|
||
fc.files[filePath] = fileBytes | ||
return fileBytes, nil | ||
} | ||
|
||
func prettifyBytesCount(n int) string { | ||
const ( | ||
Multiplexer = 1024 | ||
KiB = 1 * Multiplexer | ||
MiB = KiB * Multiplexer | ||
GiB = MiB * Multiplexer | ||
) | ||
|
||
if n >= GiB { | ||
return fmt.Sprintf("%.1fGiB", float64(n)/GiB) | ||
} | ||
if n >= MiB { | ||
return fmt.Sprintf("%.1fMiB", float64(n)/MiB) | ||
} | ||
if n >= KiB { | ||
return fmt.Sprintf("%.1fKiB", float64(n)/KiB) | ||
} | ||
return fmt.Sprintf("%dB", n) | ||
} | ||
|
||
func (fc *FileCache) PrintStats(log logutils.Log) { | ||
var size int | ||
for _, fileBytes := range fc.files { | ||
size += len(fileBytes) | ||
} | ||
|
||
log.Infof("File cache stats: %d entries of total size %s", len(fc.files), prettifyBytesCount(size)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package fsutils | ||
|
||
import ( | ||
"bytes" | ||
"fmt" | ||
|
||
"github.com/pkg/errors" | ||
) | ||
|
||
type fileLinesCache [][]byte | ||
|
||
type LineCache struct { | ||
files map[string]fileLinesCache | ||
fileCache *FileCache | ||
} | ||
|
||
func NewLineCache(fc *FileCache) *LineCache { | ||
return &LineCache{ | ||
files: map[string]fileLinesCache{}, | ||
fileCache: fc, | ||
} | ||
} | ||
|
||
// GetLine returns a index1-th (1-based index) line from the file on filePath | ||
func (lc *LineCache) GetLine(filePath string, index1 int) (string, error) { | ||
if index1 == 0 { // some linters, e.g. gosec can do it: it really means first line | ||
index1 = 1 | ||
} | ||
|
||
rawLine, err := lc.getRawLine(filePath, index1-1) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
return string(bytes.Trim(rawLine, "\r")), nil | ||
} | ||
|
||
func (lc *LineCache) getRawLine(filePath string, index0 int) ([]byte, error) { | ||
fc, err := lc.getFileCache(filePath) | ||
if err != nil { | ||
return nil, errors.Wrapf(err, "failed to get file %s lines cache", filePath) | ||
} | ||
|
||
if index0 < 0 { | ||
return nil, fmt.Errorf("invalid file line index0 < 0: %d", index0) | ||
} | ||
|
||
if index0 >= len(fc) { | ||
return nil, fmt.Errorf("invalid file line index0 (%d) >= len(fc) (%d)", index0, len(fc)) | ||
} | ||
|
||
return fc[index0], nil | ||
} | ||
|
||
func (lc *LineCache) getFileCache(filePath string) (fileLinesCache, error) { | ||
fc := lc.files[filePath] | ||
if fc != nil { | ||
return fc, nil | ||
} | ||
|
||
fileBytes, err := lc.fileCache.GetFileBytes(filePath) | ||
if err != nil { | ||
return nil, errors.Wrapf(err, "can't get file %s bytes from cache", filePath) | ||
} | ||
|
||
fc = bytes.Split(fileBytes, []byte("\n")) | ||
lc.files[filePath] = fc | ||
return fc, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.