-
Notifications
You must be signed in to change notification settings - Fork 3
/
FileChecker.cs
336 lines (244 loc) · 9.25 KB
/
FileChecker.cs
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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
using System.Text.RegularExpressions;
using CodeContext;
public class FileChecker
{
private const long MaxFileSizeBytes = 100 * 1024; // 100KB
private static readonly HashSet<string> IgnoredExtensions = new(StringComparer.OrdinalIgnoreCase)
{
// Executable and library files
".exe", ".dll", ".pdb", ".bin", ".obj", ".lib", ".so", ".dylib", ".a", ".o",
// Image files
".png", ".jpg", ".jpeg", ".gif", ".bmp", ".ico", ".svg", ".webp", ".tiff", ".tif", ".raw", ".psd", ".ai",
".eps", ".ps",
// Audio and video files
".mp3", ".mp4", ".wav", ".avi", ".mov", ".flv", ".wmv", ".m4a", ".m4v", ".mkv", ".webm", ".ogg",
// Compressed files
".zip", ".rar", ".7z", ".tar", ".gz", ".bz2", ".xz", ".tgz",
// Database files
".db", ".sqlite", ".mdf", ".ldf", ".bak", ".mdb", ".accdb",
// Document files
".docx", ".xlsx", ".pptx", ".pdf", ".doc", ".xls", ".ppt", ".rtf", ".odt", ".ods", ".odp",
// Log and temporary files
".log", ".cache", ".tmp", ".temp",
// Minified and source map files
".min.js", ".min.css", ".map", ".lock",
// Design files
".sketch", ".fig", ".xd",
// Deployment and settings files
".pub", ".pubxml", ".publishsettings", ".settings", ".suo", ".user", ".userosscache",
// Version control files
".vspscc", ".vssscc", ".pidb", ".scc",
// System files
".DS_Store", ".localized", ".manifest",
// Project-specific files
".csproj.user", ".sln.docstates", ".suo", ".user", ".vssscc",
// Compiler and build output
".pdb", ".ilk", ".msi", ".idb", ".pch", ".res",
// Font files
".eot", ".ttf", ".woff", ".woff2",
// 3D model files
".fbx", ".obj", ".3ds", ".max",
// Unity-specific files
".unity", ".unitypackage", ".asset",
// Certificate files
".pfx", ".cer", ".crt",
// Package manager files
".nupkg", ".snupkg",
// Java-specific files
".class", ".jar",
// Python-specific files
".pyc", ".pyo",
// Node.js-specific files
".node",
// Ruby-specific files
".gem",
// Rust-specific files
".rlib",
// Go-specific files
".a",
// Swift-specific files
".swiftmodule",
// Docker-specific files
".dockerignore",
// Kubernetes-specific files
".kubeconfig",
// Machine learning model files
".h5", ".pkl", ".onnx",
// Executable scripts (to be cautious)
".bat", ".sh", ".cmd", ".ps1"
};
private static readonly HashSet<string> IgnoredDirectories = new(StringComparer.OrdinalIgnoreCase)
{
// Version control systems
".git", ".svn", ".hg", ".bzr", ".cvs",
// IDE and editor-specific
".vs", ".idea", ".vscode", ".atom", ".sublime-project",
// Build output
"bin", "obj", "Debug", "Release", "x64", "x86", "AnyCPU",
// Package management
"packages", "node_modules", "bower_components", "jspm_packages",
// Python-specific
"__pycache__", "venv", "env", "virtualenv", ".venv", ".env", ".pytest_cache",
// Ruby-specific
".bundle", "vendor/bundle",
// Java-specific
"target", ".gradle", "build",
// JavaScript/TypeScript-specific
"dist", "out", "build", ".next", ".nuxt", ".cache",
// Testing and coverage
"coverage", "test-results", "reports", ".nyc_output",
// Logs and temporary files
"logs", "temp", "tmp", ".temp", ".tmp",
// Content and media
"uploads", "media", "static", "public", "assets",
// Third-party and dependencies
"vendor", "third-party", "external", "lib", "libs",
// WordPress-specific
"wp-content", "wp-includes", "wp-admin",
// Mobile development
"Pods", "DerivedData",
// Containerization
".docker",
// CI/CD
".github", ".gitlab", ".circleci", ".jenkins",
// Documentation
"docs", "_site", ".docusaurus",
// Caching
".cache", ".sass-cache", ".parcel-cache",
// Compiled languages
"__pycache__", ".mypy_cache", ".rpt2_cache", ".rts2_cache_cjs", ".rts2_cache_es", ".rts2_cache_umd",
// OS-specific
".DS_Store", "Thumbs.db",
// Dependency lock files directory
".pnpm-store",
// Serverless frameworks
".serverless",
// Terraform
".terraform",
// Yarn
".yarn",
// Expo (React Native)
".expo",
// Electron
"out",
// Flutter/Dart
".dart_tool", ".flutter-plugins", ".flutter-plugins-dependencies",
// Kubernetes
".kube",
// Ansible
".ansible",
// Chef
".chef",
// Vagrant
".vagrant",
// Unity
"Library", "Temp", "Obj", "Builds", "Logs",
// Unreal Engine
"Binaries", "Build", "Saved", "Intermediate",
// Godot Engine
".import", "export_presets.cfg",
// R language
".Rproj.user", ".Rhistory", ".RData",
// Jupyter Notebooks
".ipynb_checkpoints",
// LaTeX
"build", "out",
// Rust
"target",
// Go
"vendor",
// Elixir
"_build", ".elixir_ls",
// Helm Charts
"charts",
// Pipenv
".venv"
};
private static readonly HashSet<string> IgnoredFiles = new(StringComparer.OrdinalIgnoreCase)
{
".bzrignore", ".coveragerc", ".editorconfig", ".env", ".env.development",
".env.production", ".env.local", ".env.test", ".eslintrc", ".gitattributes",
"thumbs.db", "desktop.ini", ".DS_Store", "npm-debug.log", "yarn-error.log",
"package-lock.json", "yarn.lock", "composer.lock", ".gitignore"
};
private static List<string> gitIgnorePatterns;
public static bool ShouldSkip(FileSystemInfo info, string rootPath)
{
// Check if any parent directory is in the ignored list
var relativePath = Path.GetRelativePath(rootPath, info.FullName);
var pathParts = relativePath.Split(Path.DirectorySeparatorChar);
if (pathParts.Any(IgnoredDirectories.Contains))
{
return true;
}
if (info.Attributes.HasFlag(FileAttributes.Directory))
return false; // We've already checked if it's an ignored directory
// Check for ignored files
if (IgnoredFiles.Contains(info.Name))
return true;
// Improved extension checking
var fileName = info.Name;
var extension = Path.GetExtension(fileName);
if (IgnoredExtensions.Contains(extension))
return true;
// Check for compound extensions like .min.css
var lastDotIndex = fileName.LastIndexOf('.');
if (lastDotIndex > 0)
{
var secondLastDotIndex = fileName.LastIndexOf('.', lastDotIndex - 1);
if (secondLastDotIndex >= 0)
{
var compoundExtension = fileName.Substring(secondLastDotIndex);
if (IgnoredExtensions.Contains(compoundExtension))
return true;
}
}
if (info is FileInfo fileInfo && fileInfo.Length > MaxFileSizeBytes)
return true;
if (IsInGitRepository(rootPath))
{
if (gitIgnorePatterns == null)
LoadGitIgnore(rootPath);
if (IsIgnoredByGitIgnore(info.FullName, rootPath))
return true;
}
return FileUtils.IsBinaryFile(info.FullName) ||
IgnoredDirectories.Any(dir =>
info.FullName.Contains($"{Path.DirectorySeparatorChar}{dir}{Path.DirectorySeparatorChar}") ||
IsGeneratedCode(info.FullName));
}
private static bool IsGeneratedCode(string filePath)
{
const int linesToCheck = 10;
var lines = File.ReadLines(filePath).Take(linesToCheck);
return lines.Any(line => line.Contains("<auto-generated />"));
}
private static bool IsInGitRepository(string path)
{
while (!string.IsNullOrEmpty(path))
{
if (Directory.Exists(Path.Combine(path, ".git")))
return true;
path = Path.GetDirectoryName(path);
}
return false;
}
private static void LoadGitIgnore(string rootPath)
{
gitIgnorePatterns = new List<string>();
var gitIgnorePath = Path.Combine(rootPath, ".gitignore");
if (File.Exists(gitIgnorePath))
gitIgnorePatterns.AddRange(File.ReadAllLines(gitIgnorePath)
.Where(line => !string.IsNullOrWhiteSpace(line) && !line.StartsWith('#')));
}
private static bool IsIgnoredByGitIgnore(string filePath, string rootPath)
{
var relativePath = Path.GetRelativePath(rootPath, filePath);
return gitIgnorePatterns.Any(pattern => IsMatch(relativePath, pattern));
}
private static bool IsMatch(string path, string pattern)
{
pattern = pattern.Replace(".", "\\.").Replace("*", ".*").Replace("?", ".");
return Regex.IsMatch(path, $"^{pattern}$", RegexOptions.IgnoreCase);
}
}