MCPcopy Create free account
hub / github.com/JordanCoin/codemap / ScanFiles

Function ScanFiles

scanner/walker.go:222–288  ·  view source on GitHub ↗

ScanFiles walks the directory tree and returns all files. Supports nested .gitignore files via GitIgnoreCache. only: list of extensions to include (empty = all) exclude: list of patterns to exclude

(root string, cache *GitIgnoreCache, only []string, exclude []string)

Source from the content-addressed store, hash-verified

220// only: list of extensions to include (empty = all)
221// exclude: list of patterns to exclude
222func ScanFiles(root string, cache *GitIgnoreCache, only []string, exclude []string) ([]FileInfo, error) {
223 var files []FileInfo
224 absRoot, _ := filepath.Abs(root)
225
226 err := filepath.Walk(root, func(path string, info os.FileInfo, err error) error {
227 if err != nil {
228 return err
229 }
230
231 name := info.Name()
232
233 // Fast path: skip hardcoded ignored dirs/files
234 if IgnoredDirs[name] {
235 if info.IsDir() {
236 return filepath.SkipDir
237 }
238 return nil
239 }
240
241 // Compute absolute path once for gitignore checks and relative path calculation
242 absPath, _ := filepath.Abs(path)
243
244 // For directories: load any .gitignore, then check if dir itself should be skipped
245 if info.IsDir() {
246 if cache != nil {
247 cache.tryLoadGitignore(absPath)
248 if cache.ShouldIgnore(absPath) {
249 return filepath.SkipDir
250 }
251 }
252 // Check if directory matches any exclude pattern
253 relPath, _ := filepath.Rel(absRoot, absPath)
254 if relPath != "." {
255 for _, pattern := range exclude {
256 pattern = strings.TrimSpace(pattern)
257 if pattern != "" && matchesPattern(relPath, pattern) {
258 return filepath.SkipDir
259 }
260 }
261 }
262 return nil
263 }
264
265 // For files: check gitignore
266 if cache != nil && cache.ShouldIgnore(absPath) {
267 return nil
268 }
269
270 relPath, _ := filepath.Rel(absRoot, absPath)
271 ext := filepath.Ext(path)
272
273 // Apply user filters (--only and --exclude)
274 if !shouldIncludeFile(relPath, ext, only, exclude) {
275 return nil
276 }
277
278 files = append(files, FileInfo{
279 Path: relPath,

Callers 15

buildBlastRadiusBundleFunction · 0.92
mainFunction · 0.92
resolveRepoFileCountFunction · 0.92
initProjectConfigFunction · 0.92
detectLanguagesFromFilesFunction · 0.92
countSourceFilesFunction · 0.92
handleGetStructureFunction · 0.92
handleGetDiffFunction · 0.92
handleFindFileFunction · 0.92
getProjectStatsFunction · 0.92
fullScanMethod · 0.92
TestScanFilesFunction · 0.85

Calls 5

matchesPatternFunction · 0.85
shouldIncludeFileFunction · 0.85
tryLoadGitignoreMethod · 0.80
ShouldIgnoreMethod · 0.80
SizeMethod · 0.80

Tested by 9

TestScanFilesFunction · 0.68
TestScanFilesIgnoresDirsFunction · 0.68
TestScanFilesExtensionsFunction · 0.68
TestNestedGitignoreFunction · 0.68
TestGitIgnoreCacheNilFunction · 0.68