Files
task/internal/fingerprint/glob.go

71 lines
1.4 KiB
Go

package fingerprint
import (
"os"
"path/filepath"
"slices"
"github.com/go-task/task/v3/internal/execext"
"github.com/go-task/task/v3/internal/filepathext"
"github.com/go-task/task/v3/internal/fsext"
"github.com/go-task/task/v3/taskfile/ast"
)
func Globs(dir string, globs []*ast.Glob, useGitignore bool) ([]string, error) {
resultMap := make(map[string]bool)
for _, g := range globs {
matches, err := glob(dir, g.Glob)
if err != nil {
continue
}
for _, match := range matches {
resultMap[match] = !g.Negate
}
}
if useGitignore {
resultMap = filterGitignored(resultMap, dir)
}
return collectKeys(resultMap), nil
}
func glob(dir string, g string) ([]string, error) {
g = filepathext.SmartJoin(dir, g)
if results, ok, err := fsext.FastRecursiveGlob(g); ok {
return results, err
}
fs, err := execext.ExpandFields(g)
if err != nil {
return nil, err
}
results := make(map[string]bool, len(fs))
for _, f := range fs {
info, err := os.Stat(f)
if err != nil {
return nil, err
}
if info.IsDir() {
continue
}
results[f] = true
}
return collectKeys(results), nil
}
func collectKeys(m map[string]bool) []string {
keys := make([]string, 0, len(m))
for k, v := range m {
if v {
// Normalize path separators for consistent sorting across platforms
keys = append(keys, filepath.ToSlash(k))
}
}
slices.Sort(keys)
return keys
}