github.com/vanstinator/golangci-lint@v0.0.0-20240223191551-cc572f00d9d1/pkg/result/processors/diff.go (about) 1 package processors 2 3 import ( 4 "bytes" 5 "fmt" 6 "io" 7 "os" 8 "strings" 9 10 "github.com/golangci/revgrep" 11 12 "github.com/vanstinator/golangci-lint/pkg/result" 13 ) 14 15 const envGolangciDiffProcessorPatch = "GOLANGCI_DIFF_PROCESSOR_PATCH" 16 17 type Diff struct { 18 onlyNew bool 19 fromRev string 20 patchFilePath string 21 wholeFiles bool 22 patch string 23 } 24 25 var _ Processor = Diff{} 26 27 func NewDiff(onlyNew bool, fromRev, patchFilePath string, wholeFiles bool) *Diff { 28 return &Diff{ 29 onlyNew: onlyNew, 30 fromRev: fromRev, 31 patchFilePath: patchFilePath, 32 wholeFiles: wholeFiles, 33 patch: os.Getenv(envGolangciDiffProcessorPatch), 34 } 35 } 36 37 func (p Diff) Name() string { 38 return "diff" 39 } 40 41 func (p Diff) Process(issues []result.Issue) ([]result.Issue, error) { 42 if !p.onlyNew && p.fromRev == "" && p.patchFilePath == "" && p.patch == "" { // no need to work 43 return issues, nil 44 } 45 46 var patchReader io.Reader 47 if p.patchFilePath != "" { 48 patch, err := os.ReadFile(p.patchFilePath) 49 if err != nil { 50 return nil, fmt.Errorf("can't read from patch file %s: %w", p.patchFilePath, err) 51 } 52 patchReader = bytes.NewReader(patch) 53 } else if p.patch != "" { 54 patchReader = strings.NewReader(p.patch) 55 } 56 57 c := revgrep.Checker{ 58 Patch: patchReader, 59 RevisionFrom: p.fromRev, 60 WholeFiles: p.wholeFiles, 61 } 62 if err := c.Prepare(); err != nil { 63 return nil, fmt.Errorf("can't prepare diff by revgrep: %w", err) 64 } 65 66 return transformIssues(issues, func(i *result.Issue) *result.Issue { 67 hunkPos, isNew := c.IsNewIssue(i) 68 if !isNew { 69 return nil 70 } 71 72 newI := *i 73 newI.HunkPos = hunkPos 74 return &newI 75 }), nil 76 } 77 78 func (Diff) Finish() {}