golangci-lint/pkg/result/processors/diff.go

77 lines
1.6 KiB
Go
Raw Normal View History

package processors
import (
2018-05-08 09:55:38 +03:00
"bytes"
"fmt"
2018-05-08 09:55:38 +03:00
"io"
2018-05-13 12:05:34 +03:00
"os"
"strings"
2018-05-08 09:55:38 +03:00
"github.com/golangci/revgrep"
"github.com/golangci/golangci-lint/pkg/result"
)
2018-05-08 09:55:38 +03:00
type Diff struct {
onlyNew bool
fromRev string
patchFilePath string
wholeFiles bool
2018-05-13 12:05:34 +03:00
patch string
}
2018-05-08 09:55:38 +03:00
var _ Processor = Diff{}
func NewDiff(onlyNew bool, fromRev, patchFilePath string, wholeFiles bool) *Diff {
2018-05-08 09:55:38 +03:00
return &Diff{
onlyNew: onlyNew,
fromRev: fromRev,
patchFilePath: patchFilePath,
wholeFiles: wholeFiles,
2018-05-13 12:05:34 +03:00
patch: os.Getenv("GOLANGCI_DIFF_PROCESSOR_PATCH"),
}
}
2018-05-08 09:55:38 +03:00
func (p Diff) Name() string {
return "diff"
}
2018-05-08 09:55:38 +03:00
func (p Diff) Process(issues []result.Issue) ([]result.Issue, error) {
2018-05-13 12:05:34 +03:00
if !p.onlyNew && p.fromRev == "" && p.patchFilePath == "" && p.patch == "" { // no need to work
2018-05-08 09:55:38 +03:00
return issues, nil
}
2018-05-08 09:55:38 +03:00
var patchReader io.Reader
if p.patchFilePath != "" {
patch, err := os.ReadFile(p.patchFilePath)
if err != nil {
return nil, fmt.Errorf("can't read from patch file %s: %s", p.patchFilePath, err)
}
2018-05-08 09:55:38 +03:00
patchReader = bytes.NewReader(patch)
2018-05-13 12:05:34 +03:00
} else if p.patch != "" {
patchReader = strings.NewReader(p.patch)
}
2018-05-13 12:05:34 +03:00
2018-05-08 09:55:38 +03:00
c := revgrep.Checker{
Patch: patchReader,
RevisionFrom: p.fromRev,
WholeFiles: p.wholeFiles,
}
2018-05-08 09:55:38 +03:00
if err := c.Prepare(); err != nil {
return nil, fmt.Errorf("can't prepare diff by revgrep: %s", err)
}
2018-05-08 09:55:38 +03:00
return transformIssues(issues, func(i *result.Issue) *result.Issue {
hunkPos, isNew := c.IsNewIssue(i)
if !isNew {
return nil
}
newI := *i
newI.HunkPos = hunkPos
return &newI
}), nil
}
2018-05-08 09:55:38 +03:00
func (Diff) Finish() {}