-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig.go
More file actions
276 lines (251 loc) · 8.21 KB
/
Copy pathconfig.go
File metadata and controls
276 lines (251 loc) · 8.21 KB
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
package repomap
import (
"fmt"
"os"
"path"
"path/filepath"
"regexp"
"strings"
"gopkg.in/yaml.v3"
)
// configFileName is the on-disk config file name loaded from the project root.
const configFileName = ".repomap.yaml"
// fileDetailOverride is the compiled form of one file_overrides entry.
type fileDetailOverride struct {
// glob is the path.Match pattern; used when prefix == "".
glob string
// prefix is set instead of glob when the original pattern contained "**".
// Match succeeds when the relative path starts with this prefix.
prefix string
// level is the forced DetailLevel: -1 (omit) or 2 (full).
level int
}
// BlocklistConfig holds loaded-from-disk repomap settings that filter parsed
// symbols and file paths. Safe for concurrent reads after Load returns.
type BlocklistConfig struct {
// MethodBlocklist lists symbol-name patterns to drop at parse time.
// Each entry is either:
// - a regex wrapped in forward slashes, e.g. "/^pb_/"
// - a glob matched with path.Match, e.g. "Test*" or "*Mock"
MethodBlocklist []string `yaml:"method_blocklist"`
// ExcludePaths lists path glob patterns (relative to project root) to drop
// at scan time. Any file whose relative path matches is excluded.
// Example: ["internal/gen/*", "vendor/*"]
ExcludePaths []string `yaml:"exclude_paths"`
// IncludePaths lists path glob patterns (relative to project root) to keep
// at scan time. When non-empty, only matching files are included.
// Example: ["cmd/*", "internal/cli/*"]
IncludePaths []string `yaml:"include_paths"`
// FileOverrides maps relative-path globs to forced detail levels.
// Accepted values: "full" (DetailLevel 2) and "omit" (DetailLevel -1).
// Example:
// file_overrides:
// "cmd/main.go": full
// "internal/gen/**": omit
FileOverrides map[string]string `yaml:"file_overrides"`
// compiled symbol patterns. Populated by compile(); zero value = match nothing.
compiled []blocklistMatcher
// compiledExclude and compiledInclude are compiled path patterns.
compiledExclude []string
compiledInclude []string
// compiledOverrides holds the sorted/compiled file override rules.
compiledOverrides []fileDetailOverride
}
// blocklistMatcher is the compiled form of a single pattern entry.
type blocklistMatcher struct {
raw string
glob string // path.Match pattern; empty if regex
regex *regexp.Regexp // compiled regex; nil if glob
}
// LoadBlocklistConfig reads <root>/.repomap.yaml. Returns zero-value config
// when the file is absent. Returns a wrapped error only when the file exists
// but is malformed or has invalid patterns.
func LoadBlocklistConfig(root string) (*BlocklistConfig, error) {
p := filepath.Join(root, configFileName)
data, err := os.ReadFile(p)
if err != nil {
if os.IsNotExist(err) {
return &BlocklistConfig{}, nil
}
return nil, fmt.Errorf("read %s: %w", configFileName, err)
}
var c BlocklistConfig
if err := yaml.Unmarshal(data, &c); err != nil {
return nil, fmt.Errorf("parse %s: %w", configFileName, err)
}
if err := c.compile(); err != nil {
return nil, fmt.Errorf("%s: %w", configFileName, err)
}
return &c, nil
}
// compile pre-compiles all patterns in MethodBlocklist, ExcludePaths,
// IncludePaths, and FileOverrides. Invalid patterns return an error; all-or-nothing semantics.
func (c *BlocklistConfig) compile() error {
c.compiled = make([]blocklistMatcher, 0, len(c.MethodBlocklist))
for _, raw := range c.MethodBlocklist {
raw = strings.TrimSpace(raw)
if raw == "" {
continue
}
m := blocklistMatcher{raw: raw}
if strings.HasPrefix(raw, "/") && strings.HasSuffix(raw, "/") && len(raw) >= 2 {
re, err := regexp.Compile(raw[1 : len(raw)-1])
if err != nil {
return fmt.Errorf("invalid regex %q: %w", raw, err)
}
m.regex = re
} else {
if _, err := path.Match(raw, "probe"); err != nil {
return fmt.Errorf("invalid glob %q: %w", raw, err)
}
m.glob = raw
}
c.compiled = append(c.compiled, m)
}
if err := validateGlobs("exclude_paths", c.ExcludePaths); err != nil {
return err
}
c.compiledExclude = compactPatterns(c.ExcludePaths)
if err := validateGlobs("include_paths", c.IncludePaths); err != nil {
return err
}
c.compiledInclude = compactPatterns(c.IncludePaths)
c.compiledOverrides = make([]fileDetailOverride, 0, len(c.FileOverrides))
for glob, val := range c.FileOverrides {
glob = strings.TrimSpace(glob)
if glob == "" {
continue
}
var level int
switch strings.TrimSpace(val) {
case "full":
level = 2
case "omit":
level = -1
default:
return fmt.Errorf("file_overrides: %q has unknown value %q (want \"full\" or \"omit\")", glob, val)
}
ov := fileDetailOverride{level: level}
if idx := strings.Index(glob, "**"); idx >= 0 {
// path.Match does not support **: treat everything before ** as a prefix.
ov.prefix = glob[:idx]
} else {
if _, err := path.Match(glob, "probe"); err != nil {
return fmt.Errorf("file_overrides: invalid glob %q: %w", glob, err)
}
ov.glob = glob
}
c.compiledOverrides = append(c.compiledOverrides, ov)
}
return nil
}
// validateGlobs returns an error if any pattern in ps is not a valid path.Match glob.
func validateGlobs(field string, ps []string) error {
for _, p := range ps {
p = strings.TrimSpace(p)
if p == "" {
continue
}
if _, err := path.Match(p, "probe"); err != nil {
return fmt.Errorf("%s: invalid glob %q: %w", field, p, err)
}
}
return nil
}
// compactPatterns trims whitespace and drops empty entries.
func compactPatterns(ps []string) []string {
out := make([]string, 0, len(ps))
for _, p := range ps {
if p = strings.TrimSpace(p); p != "" {
out = append(out, p)
}
}
return out
}
// MatchFileOverride reports whether a relative file path matches any file_overrides
// rule. Returns the forced DetailLevel (2 or -1) and true on match; 0 and false otherwise.
// A nil receiver returns (0, false) — no overrides.
// Globs use path.Match semantics; patterns containing "**" match any path with the
// corresponding prefix (everything before the first "**").
func (c *BlocklistConfig) MatchFileOverride(rel string) (level int, ok bool) {
if c == nil || len(c.compiledOverrides) == 0 {
return 0, false
}
for _, ov := range c.compiledOverrides {
if ov.prefix != "" {
if strings.HasPrefix(rel, ov.prefix) {
return ov.level, true
}
continue
}
if matched, _ := path.Match(ov.glob, rel); matched {
return ov.level, true
}
}
return 0, false
}
// ShouldSkipSymbol reports whether a symbol name matches any blocklist pattern.
// A nil receiver or empty blocklist returns false.
func (c *BlocklistConfig) ShouldSkipSymbol(name string) bool {
if c == nil || len(c.compiled) == 0 {
return false
}
for _, m := range c.compiled {
if m.regex != nil {
if m.regex.MatchString(name) {
return true
}
continue
}
if ok, _ := path.Match(m.glob, name); ok {
return true
}
}
return false
}
// filterSymbols removes blocklisted symbols from fs in place.
// No-op when fs is nil, the config is nil, or the blocklist is empty.
func (c *BlocklistConfig) filterSymbols(fs *FileSymbols) {
if fs == nil || c == nil || len(c.compiled) == 0 || len(fs.Symbols) == 0 {
return
}
kept := fs.Symbols[:0]
for _, s := range fs.Symbols {
if c.ShouldSkipSymbol(s.Name) {
continue
}
kept = append(kept, s)
}
fs.Symbols = kept
}
// ShouldExcludePath reports whether rel matches any ExcludePaths pattern.
// rel must be a slash-separated path relative to the project root.
// A nil receiver returns false (nothing excluded).
func (c *BlocklistConfig) ShouldExcludePath(rel string) bool {
if c == nil {
return false
}
rel = filepath.ToSlash(rel)
for _, pat := range c.compiledExclude {
if ok, _ := path.Match(pat, rel); ok {
return true
}
}
return false
}
// ShouldIncludePath reports whether rel passes the IncludePaths filter.
// When IncludePaths is empty, all paths are included (returns true).
// When non-empty, returns true only if rel matches at least one pattern.
// A nil receiver returns true (nothing excluded).
func (c *BlocklistConfig) ShouldIncludePath(rel string) bool {
if c == nil || len(c.compiledInclude) == 0 {
return true
}
rel = filepath.ToSlash(rel)
for _, pat := range c.compiledInclude {
if ok, _ := path.Match(pat, rel); ok {
return true
}
}
return false
}