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)
| 220 | // only: list of extensions to include (empty = all) |
| 221 | // exclude: list of patterns to exclude |
| 222 | func 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, |