ShouldIgnore checks if a path should be ignored based on all applicable .gitignore files. Git evaluates rules from root to leaf, with later rules overriding earlier ones.
(absPath string)
| 75 | // ShouldIgnore checks if a path should be ignored based on all applicable .gitignore files. |
| 76 | // Git evaluates rules from root to leaf, with later rules overriding earlier ones. |
| 77 | func (c *GitIgnoreCache) ShouldIgnore(absPath string) bool { |
| 78 | if len(c.cache) == 0 { |
| 79 | return false |
| 80 | } |
| 81 | |
| 82 | // Collect directories from leaf to root |
| 83 | var dirs []string |
| 84 | for dir := filepath.Dir(absPath); ; dir = filepath.Dir(dir) { |
| 85 | dirs = append(dirs, dir) |
| 86 | if dir == c.root || dir == filepath.Dir(dir) { |
| 87 | break |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | // Combine all patterns from root to leaf into one gitignore. |
| 92 | // This allows negation patterns in child .gitignore to override parent rules. |
| 93 | var allPatterns []string |
| 94 | for i := len(dirs) - 1; i >= 0; i-- { |
| 95 | if patterns, ok := c.patterns[dirs[i]]; ok { |
| 96 | allPatterns = append(allPatterns, patterns...) |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | if len(allPatterns) == 0 { |
| 101 | return false |
| 102 | } |
| 103 | |
| 104 | combined := ignore.CompileIgnoreLines(allPatterns...) |
| 105 | relPath, _ := filepath.Rel(c.root, absPath) |
| 106 | return combined.MatchesPath(relPath) |
| 107 | } |
| 108 | |
| 109 | // IgnoredDirs are directories to skip during scanning |
| 110 | var IgnoredDirs = map[string]bool{ |
no outgoing calls