-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathignore.go
More file actions
186 lines (162 loc) · 4.01 KB
/
Copy pathignore.go
File metadata and controls
186 lines (162 loc) · 4.01 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
package contexting
import (
"path/filepath"
"strings"
)
const dotWhitelistKey = "__dot_whitelist__"
var defaultDotWhitelist = map[string]bool{
".prettierrc": true,
".eslintrc": true,
".editorconfig": true,
".env.example": true,
".env.sample": true,
".env.template": true,
".npmrc": true,
".nvmrc": true,
".node-version": true,
".python-version": true,
".ruby-version": true,
".tool-versions": true,
".dockerignore": true,
".stylelintrc": true,
".babelrc": true,
".postcssrc": true,
".lintstagedrc": true,
".huskyrc": true,
".gitattributes": true,
".npmignore": true,
".browserslistrc": true,
".commitlintrc": true,
}
var defaultIgnores = []string{
// Version control (ALWAYS ignore)
".git",
".svn",
".hg",
// Dependencies (ALWAYS ignore - can crash indexer)
"node_modules",
"vendor",
"bower_components",
// Virtual environments / caches
".venv",
".cache",
".pytest_cache",
"site-packages",
"__pycache__",
// Build outputs
"dist",
"build",
"out",
"tmp",
"temp",
// Database migrations (boilerplate, low search value)
"migrations",
"pb_migrations", // PocketBase
"db/migrate", // Rails
"alembic", // Python/Alembic
"flyway", // Java/Flyway
// IDE
".vscode",
".idea",
// OS junk
".DS_Store",
"Thumbs.db",
}
func BuildDotWhitelist(extra []string) map[string]bool {
merged := make(map[string]bool, len(defaultDotWhitelist)+len(extra))
for k, v := range defaultDotWhitelist {
merged[k] = v
}
for _, name := range extra {
trimmed := strings.TrimSpace(name)
if trimmed != "" {
merged[trimmed] = true
}
}
return merged
}
// EmbedDotWhitelist stores the dot file whitelist in the ignore map via a sentinel key.
func EmbedDotWhitelist(ignored map[string]bool, whitelist map[string]bool) {
for name := range whitelist {
ignored[dotWhitelistKey+name] = true
}
}
func BuildIgnoreMap(extra []string) map[string]bool {
ignored := make(map[string]bool, len(defaultIgnores)+len(extra))
for _, pattern := range defaultIgnores {
ignored[normalizeIgnorePattern(pattern)] = true
}
for _, pattern := range extra {
normalized := normalizeIgnorePattern(pattern)
if normalized != "" {
ignored[normalized] = true
}
}
return ignored
}
func BuildIgnoreMapForRoot(root string, extra []string) (map[string]bool, error) {
patterns, err := EnsureAndLoadGitignore(root)
if err != nil {
return nil, err
}
merged := make([]string, 0, len(patterns)+len(extra))
merged = append(merged, patterns...)
merged = append(merged, extra...)
return BuildIgnoreMap(merged), nil
}
func shouldIgnorePath(relPath string, baseName string, ignored map[string]bool) bool {
normalizedRel := normalizeIgnorePattern(relPath)
if normalizedRel == "" {
return false
}
// Check if baseName is a whitelisted dot file
if strings.HasPrefix(baseName, ".") {
if defaultDotWhitelist[baseName] || ignored[dotWhitelistKey+baseName] {
return false
}
}
if isDotPath(normalizedRel) || strings.HasPrefix(normalizeIgnorePattern(baseName), ".") {
return true
}
if len(ignored) == 0 {
return false
}
if ignored[normalizeIgnorePattern(baseName)] || ignored[normalizedRel] {
return true
}
for pattern := range ignored {
if !strings.Contains(pattern, "*") {
continue
}
if matched, _ := filepath.Match(pattern, normalizeIgnorePattern(baseName)); matched {
return true
}
if matched, _ := filepath.Match(pattern, normalizedRel); matched {
return true
}
}
segments := strings.Split(normalizedRel, "/")
for _, segment := range segments {
if ignored[segment] {
return true
}
}
return false
}
func normalizeIgnorePattern(path string) string {
trimmed := strings.TrimSpace(path)
if trimmed == "" {
return ""
}
normalized := filepath.ToSlash(trimmed)
return strings.Trim(normalized, "/")
}
func isDotPath(normalizedRel string) bool {
segments := strings.Split(normalizedRel, "/")
for _, segment := range segments {
if strings.HasPrefix(segment, ".") {
return true
}
}
return false
}