Documentation
¶
Index ¶
- func Walk(root string, fn func(path string, d fs.DirEntry) error) error
- type MatchResult
- type Matcher
- func (m *Matcher) AddFromFile(absPath, relDir string)
- func (m *Matcher) AddPatterns(data []byte, dir string)
- func (m *Matcher) Errors() []PatternError
- func (m *Matcher) Match(relPath string) bool
- func (m *Matcher) MatchDetail(relPath string) MatchResult
- func (m *Matcher) MatchPath(relPath string, isDir bool) bool
- type PatternError
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Walk ¶ added in v1.1.0
Walk walks the directory tree rooted at root, calling fn for each file and directory that is not ignored by gitignore rules. It loads .gitignore files as it descends, so patterns from deeper directories take effect for their subtrees. The .git directory is always skipped.
Paths passed to fn are relative to root and use the OS path separator. The root directory itself is not passed to fn.
Types ¶
type MatchResult ¶ added in v1.1.0
type MatchResult struct {
Ignored bool // true if the path should be ignored
Matched bool // true if any pattern matched (false means no pattern applied)
Pattern string // original pattern text (empty if no match)
Source string // file the pattern came from (empty for programmatic patterns)
Line int // 1-based line number in Source (0 if no match)
Negate bool // true if the matching pattern was a negation (!)
}
MatchResult describes which pattern matched a path and whether the path is ignored.
type Matcher ¶
type Matcher struct {
// contains filtered or unexported fields
}
Matcher checks paths against gitignore rules collected from .gitignore files, .git/info/exclude, and any additional patterns. Patterns from subdirectory .gitignore files are scoped to paths within that directory.
Paths passed to Match should use forward slashes. Directory paths must have a trailing slash (e.g. "vendor/") so that directory-only patterns (those written with a trailing slash in .gitignore) match correctly.
A Matcher is safe for concurrent use by multiple goroutines once construction is complete (after New, NewFromDirectory, or the last AddPatterns/AddFromFile call). Do not call AddPatterns or AddFromFile concurrently with Match.
func New ¶
New creates a Matcher that reads patterns from the user's global excludes file (core.excludesfile), the repository's .git/info/exclude, and the root .gitignore. Patterns are loaded in priority order: global excludes first (lowest priority), then .git/info/exclude, then .gitignore (highest priority). Last-match-wins semantics means later patterns override earlier ones.
The root parameter should be the repository working directory (containing .git/).
func NewFromDirectory ¶ added in v1.1.0
NewFromDirectory creates a Matcher by walking the directory tree rooted at root, loading every .gitignore file found along the way. Each nested .gitignore is scoped to its containing directory. The .git directory is skipped.
func (*Matcher) AddFromFile ¶
AddFromFile reads a .gitignore file at the given absolute path and scopes its patterns to the given relative directory.
func (*Matcher) AddPatterns ¶
AddPatterns parses gitignore pattern lines from data and scopes them to the given relative directory. Pass an empty dir for root-level patterns.
func (*Matcher) Errors ¶ added in v1.1.0
func (m *Matcher) Errors() []PatternError
Errors returns any pattern compilation errors encountered while loading patterns. Invalid patterns are silently skipped during matching; this method lets callers detect and report them.
func (*Matcher) Match ¶
Match returns true if the given path should be ignored. The path should be slash-separated and relative to the repository root. For directories, append a trailing slash (e.g. "vendor/"). Uses last-match-wins semantics: iterates patterns in reverse and returns on the first match.
func (*Matcher) MatchDetail ¶ added in v1.1.0
func (m *Matcher) MatchDetail(relPath string) MatchResult
MatchDetail returns detailed information about which pattern matched the given path. If no pattern matches, Matched is false and Ignored is false. The path uses the same trailing-slash convention as Match.
func (*Matcher) MatchPath ¶ added in v1.1.0
MatchPath returns true if the given path should be ignored. Unlike Match, it takes an explicit isDir flag instead of requiring a trailing slash convention. The path should be slash-separated, relative to the repository root, and should not have a trailing slash.
type PatternError ¶ added in v1.1.0
type PatternError struct {
Pattern string // the original pattern text
Source string // file path, empty for programmatic patterns
Line int // 1-based line number
Message string
}
PatternError records a pattern that could not be compiled.
func (PatternError) Error ¶ added in v1.1.0
func (e PatternError) Error() string