2023-01-27 15:04:58 +00:00
|
|
|
package fileset
|
|
|
|
|
2023-10-10 08:45:15 +00:00
|
|
|
import (
|
|
|
|
ignore "github.com/sabhiram/go-gitignore"
|
|
|
|
)
|
|
|
|
|
2023-01-27 15:04:58 +00:00
|
|
|
// Ignorer is the interface for what determines if a path
|
|
|
|
// in the [FileSet] must be ignored or not.
|
|
|
|
type Ignorer interface {
|
2023-01-31 17:34:36 +00:00
|
|
|
IgnoreFile(path string) (bool, error)
|
|
|
|
IgnoreDirectory(path string) (bool, error)
|
2023-01-27 15:04:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// nopIgnorer implements an [Ignorer] that doesn't ignore anything.
|
|
|
|
type nopIgnorer struct{}
|
|
|
|
|
2023-01-31 17:34:36 +00:00
|
|
|
func (nopIgnorer) IgnoreFile(path string) (bool, error) {
|
|
|
|
return false, nil
|
2023-01-27 15:04:58 +00:00
|
|
|
}
|
|
|
|
|
2023-01-31 17:34:36 +00:00
|
|
|
func (nopIgnorer) IgnoreDirectory(path string) (bool, error) {
|
|
|
|
return false, nil
|
2023-01-27 15:04:58 +00:00
|
|
|
}
|
2023-10-10 08:45:15 +00:00
|
|
|
|
|
|
|
type includer struct {
|
|
|
|
matcher *ignore.GitIgnore
|
|
|
|
}
|
|
|
|
|
|
|
|
func newIncluder(includes []string) *includer {
|
|
|
|
matcher := ignore.CompileIgnoreLines(includes...)
|
|
|
|
return &includer{
|
|
|
|
matcher,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i *includer) IgnoreFile(path string) (bool, error) {
|
|
|
|
return i.ignore(path), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// In the context of 'include' functionality, the Ignorer logic appears to be reversed:
|
|
|
|
// For patterns like 'foo/bar/' which intends to match directories only, we still need to traverse into the directory for potential file matches.
|
|
|
|
// Ignoring the directory entirely isn't an option, especially when dealing with patterns like 'foo/bar/*.go'.
|
|
|
|
// While this pattern doesn't target directories, it does match all Go files within them and ignoring directories not matching the pattern
|
|
|
|
// Will result in missing file matches.
|
|
|
|
// During the tree traversal process, we call 'IgnoreDirectory' on ".", "./foo", and "./foo/bar",
|
|
|
|
// all while applying the 'foo/bar/*.go' pattern. To handle this situation effectively, it requires to make the code more complex.
|
|
|
|
// This could mean generating various prefix patterns to facilitate the exclusion of directories from traversal.
|
|
|
|
// It's worth noting that, in this particular case, opting for a simpler logic results in a performance trade-off.
|
|
|
|
func (i *includer) IgnoreDirectory(path string) (bool, error) {
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i *includer) ignore(path string) bool {
|
|
|
|
matched := i.matcher.MatchesPath(path)
|
|
|
|
// If matched, do not ignore the file because we want to include it
|
|
|
|
return !matched
|
|
|
|
}
|