-
-
Notifications
You must be signed in to change notification settings - Fork 402
/
diff_filter.go
215 lines (191 loc) 路 5.45 KB
/
diff_filter.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
package filter
import (
"fmt"
"path/filepath"
"strings"
"github.com/reviewdog/reviewdog/diff"
"github.com/reviewdog/reviewdog/service/serviceutil"
)
// Mode represents enumeration of available filter modes
type Mode int
const (
// ModeDefault represents default mode, which means users doesn't specify
// filter-mode. The behavior can be changed depending on reporters/context
// later if we want. Basically, it's same as ModeAdded because it's most safe
// and basic mode for reporters implementation.
ModeDefault Mode = iota
// ModeAdded represents filtering by added/changed diff lines.
ModeAdded
// ModeDiffContext represents filtering by diff context.
// i.e. changed lines +-N lines (e.g. N=3 for default git diff).
ModeDiffContext
// ModeFile represents filtering by changed files.
ModeFile
// ModeNoFilter doesn't filter out any results.
ModeNoFilter
)
// String implements the flag.Value interface
func (mode *Mode) String() string {
names := [...]string{
"default",
"added",
"diff_context",
"file",
"nofilter",
}
if *mode < ModeDefault || *mode > ModeNoFilter {
return "Unknown mode"
}
return names[*mode]
}
// Set implements the flag.Value interface
func (mode *Mode) Set(value string) error {
switch value {
case "default", "":
*mode = ModeDefault
case "added":
*mode = ModeAdded
case "diff_context":
*mode = ModeDiffContext
case "file":
*mode = ModeFile
case "nofilter":
*mode = ModeNoFilter
default:
return fmt.Errorf("invalid mode name: %s", value)
}
return nil
}
// DiffFilter filters lines by diff.
type DiffFilter struct {
// Current working directory (workdir).
cwd string
// Relative path to the project root (e.g. git) directory from current workdir.
// It can be empty if it doesn't find any project root directory.
projectRelPath string
strip int
mode Mode
difflines difflines
difffiles difffiles
}
// difflines is a hash table of normalizedPath to line number to *diff.Line.
type difflines map[normalizedPath]map[int]*diff.Line
// difffiles is a hash table of normalizedPath to *diff.FileDiff.
type difffiles map[normalizedPath]*diff.FileDiff
// NewDiffFilter creates a new DiffFilter.
func NewDiffFilter(diff []*diff.FileDiff, strip int, cwd string, mode Mode) *DiffFilter {
df := &DiffFilter{
strip: strip,
cwd: cwd,
mode: mode,
difflines: make(difflines),
difffiles: make(difffiles),
}
// If cwd is empty, projectRelPath should not have any meaningful data too.
if cwd != "" {
df.projectRelPath, _ = serviceutil.GitRelWorkdir()
}
df.addDiff(diff)
return df
}
func (df *DiffFilter) addDiff(filediffs []*diff.FileDiff) {
for _, filediff := range filediffs {
path := df.normalizeDiffPath(filediff)
df.difffiles[path] = filediff
lines, ok := df.difflines[path]
if !ok {
lines = make(map[int]*diff.Line)
}
for _, hunk := range filediff.Hunks {
for _, line := range hunk.Lines {
if line.LnumNew > 0 {
lines[line.LnumNew] = line
}
}
}
df.difflines[path] = lines
}
}
// ShouldReport returns true, if the given path should be reported depending on
// the filter Mode. It also optionally return diff file/line.
func (df *DiffFilter) ShouldReport(path string, lnum int) (bool, *diff.FileDiff, *diff.Line) {
npath := df.normalizePath(path)
file := df.difffiles[npath]
lines, ok := df.difflines[npath]
if !ok {
return df.mode == ModeNoFilter, file, nil
}
line, ok := lines[lnum]
if !ok {
return df.mode == ModeNoFilter || df.mode == ModeFile, file, nil
}
return df.isSignificantLine(line), file, line
}
// DiffLine returns diff data from given new path and lnum. Returns nil if not
// found.
func (df *DiffFilter) DiffLine(path string, lnum int) *diff.Line {
npath := df.normalizePath(path)
lines, ok := df.difflines[npath]
if !ok {
return nil
}
line, ok := lines[lnum]
if !ok {
return nil
}
return line
}
func (df *DiffFilter) isSignificantLine(line *diff.Line) bool {
switch df.mode {
case ModeDiffContext, ModeFile, ModeNoFilter:
return true // any lines in diff are significant.
case ModeAdded, ModeDefault:
return line.Type == diff.LineAdded
}
return false
}
// normalizedPath is file path which is relative to **project root dir** or
// to current dir if project root not found.
type normalizedPath struct{ p string }
func (df *DiffFilter) normalizePath(path string) normalizedPath {
return normalizedPath{p: NormalizePath(path, df.cwd, df.projectRelPath)}
}
func contains(path, base string) bool {
ps := splitPathList(path)
bs := splitPathList(base)
if len(ps) < len(bs) {
return false
}
for i := range bs {
if bs[i] != ps[i] {
return false
}
}
return true
}
// Assuming diff path should be relative path to the project root dir by
// default (e.g. git diff).
//
// `git diff --relative` can returns relative path to current workdir, so we
// ask users not to use it for reviewdog command.
func (df *DiffFilter) normalizeDiffPath(filediff *diff.FileDiff) normalizedPath {
return normalizedPath{p: NormalizeDiffPath(filediff.PathNew, df.strip)}
}
// NormalizeDiffPath return path normalized path from given path in diff with
// strip.
func NormalizeDiffPath(diffpath string, strip int) string {
if diffpath == "/dev/null" {
return ""
}
path := diffpath
if strip > 0 && !filepath.IsAbs(path) {
ps := splitPathList(path)
if len(ps) > strip {
path = filepath.Join(ps[strip:]...)
}
}
return filepath.ToSlash(filepath.Clean(path))
}
func splitPathList(path string) []string {
return strings.Split(filepath.ToSlash(path), "/")
}