forked from golangci/golangci-lint
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdiff.go
96 lines (77 loc) · 2.07 KB
/
diff.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package processors
import (
"bytes"
"context"
"fmt"
"io"
"os"
"strings"
"github.com/golangci/revgrep"
"github.com/golangci/golangci-lint/pkg/config"
"github.com/golangci/golangci-lint/pkg/result"
)
const envGolangciDiffProcessorPatch = "GOLANGCI_DIFF_PROCESSOR_PATCH"
var _ Processor = (*Diff)(nil)
// Diff filters issues based on options `new`, `new-from-rev`, etc.
//
// Uses `git`.
// The paths inside the patch are relative to the path where git is run (the same location where golangci-lint is run).
//
// Warning: it doesn't use `path-prefix` option.
type Diff struct {
onlyNew bool
fromRev string
patchFilePath string
wholeFiles bool
patch string
}
func NewDiff(cfg *config.Issues) *Diff {
return &Diff{
onlyNew: cfg.Diff,
fromRev: cfg.DiffFromRevision,
patchFilePath: cfg.DiffPatchFilePath,
wholeFiles: cfg.WholeFiles,
patch: os.Getenv(envGolangciDiffProcessorPatch),
}
}
func (*Diff) Name() string {
return "diff"
}
func (p *Diff) Process(issues []result.Issue) ([]result.Issue, error) {
if !p.onlyNew && p.fromRev == "" && p.patchFilePath == "" && p.patch == "" {
return issues, nil
}
var patchReader io.Reader
switch {
case p.patchFilePath != "":
patch, err := os.ReadFile(p.patchFilePath)
if err != nil {
return nil, fmt.Errorf("can't read from patch file %s: %w", p.patchFilePath, err)
}
patchReader = bytes.NewReader(patch)
case p.patch != "":
patchReader = strings.NewReader(p.patch)
}
checker := revgrep.Checker{
Patch: patchReader,
RevisionFrom: p.fromRev,
WholeFiles: p.wholeFiles,
}
if err := checker.Prepare(context.Background()); err != nil {
return nil, fmt.Errorf("can't prepare diff by revgrep: %w", err)
}
return transformIssues(issues, func(issue *result.Issue) *result.Issue {
if issue.FromLinter == typeCheckName {
// Never hide typechecking errors.
return issue
}
hunkPos, isNew := checker.IsNewIssue(issue)
if !isNew {
return nil
}
newIssue := *issue
newIssue.HunkPos = hunkPos
return &newIssue
}), nil
}
func (*Diff) Finish() {}