Documentation
¶
Index ¶
- func Walk(root string, fn func(path string, d fs.DirEntry) error, opts ...Option) error
- func WalkFrom(root, start string, fn func(path string, d fs.DirEntry) error, opts ...Option) error
- type IgnoreFileSizeError
- 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 Option
- 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.
With MaxIgnoreFileSize set, an oversized ignore file stops the walk and is returned as an *IgnoreFileSizeError.
func WalkFrom ¶ added in v1.2.0
WalkFrom walks the directory tree starting at a subdirectory of root, calling fn for each file and directory that is not ignored by gitignore rules. Unlike Walk, it separates the repository root (used to find .git/info/exclude and the root .gitignore) from the directory where the walk begins. Any .gitignore files between root and start are loaded before the walk begins, so their patterns apply correctly.
The start parameter is a path relative to root (e.g. "src/pkg"), using either forward slashes or the OS path separator. Paths passed to fn are relative to root (not to start) and use the OS path separator. The start directory itself is passed to fn.
With MaxIgnoreFileSize set, an oversized ignore file stops the walk and is returned as an *IgnoreFileSizeError.
Types ¶
type IgnoreFileSizeError ¶ added in v1.3.0
IgnoreFileSizeError reports an ignore file that exceeded its byte limit.
func (*IgnoreFileSizeError) Error ¶ added in v1.3.0
func (e *IgnoreFileSizeError) Error() string
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/). If root is empty, no filesystem patterns are loaded and the returned Matcher is empty. Use AddPatterns or AddFromFile to add patterns programmatically.
Options such as MaxIgnoreFileSize apply to files loaded here and to later AddFromFile calls; oversized files are skipped and recorded in Errors with Line set to zero.
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. Oversized files under MaxIgnoreFileSize are skipped and recorded in Errors.
func (*Matcher) AddFromFile ¶
AddFromFile reads a .gitignore file at the given absolute path and scopes its patterns to the given relative directory. It uses the matcher's file-size limit, if set, and records oversized files in Errors without applying any rules.
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 pattern compilation errors and skipped oversized files. File-size errors have a source path and a zero line number.
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/"). An ignored parent directory makes its descendants ignored. Otherwise, the last matching rule determines whether the path is ignored.
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 Option ¶ added in v1.3.0
type Option func(*Matcher)
Option configures a Matcher at construction time.
func MaxIgnoreFileSize ¶ added in v1.3.0
MaxIgnoreFileSize limits the bytes read from each ignore file. Nonpositive values are unlimited. AddPatterns is unaffected because its data is already in memory.
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; zero for a file-size error
Message string
}
PatternError records a pattern compilation error or a skipped oversized file.
func (PatternError) Error ¶ added in v1.1.0
func (e PatternError) Error() string