@@ -1,8 +1,12 @@
1package fsext
1package fsext
5 "path/filepath"
8 "path/filepath"
7 "github.com/charlievieth/fastwalk"
11 "github.com/charlievieth/fastwalk"
8 ignore "github.com/sabhiram/go-gitignore"
12 ignore "github.com/sabhiram/go-gitignore"
@@ -67,10 +71,8 @@ var CommonIgnorePatterns = []string{
69type DirectoryLister struct {
73type DirectoryLister struct {
70− gitignore *ignore.GitIgnore
71− crushignore *ignore.GitIgnore
72− commonIgnore *ignore.GitIgnore
74+ ignores *ignore.GitIgnore
73− rootPath string
75+ rootPath string
76func NewDirectoryLister(rootPath string) *DirectoryLister {
78func NewDirectoryLister(rootPath string) *DirectoryLister {
@@ -78,26 +80,29 @@ func NewDirectoryLister(rootPath string) *DirectoryLister {
78 rootPath: rootPath,
80 rootPath: rootPath,
81− // Load gitignore if it exists
83+ dl.ignores = ignore.CompileIgnoreLines(append(CommonIgnorePatterns, strings.Split(parseIgnores(rootPath), "\n")...)...)
82− gitignorePath := filepath.Join(rootPath, ".gitignore")
84+
83− if _, err := os.Stat(gitignorePath); err == nil {
85+ return dl
84− if gi, err := ignore.CompileIgnoreFile(gitignorePath); err == nil {
89− // Load crushignore if it exists
88+func parseIgnores(path string) string {
90− crushignorePath := filepath.Join(rootPath, ".crushignore")
89+ var b bytes.Buffer
90+ for _, ign := range []string{".crushignore", ".gitignore"} {
91+ p := filepath.Join(path, ign)
91− if _, err := os.Stat(crushignorePath); err == nil {
92+ if _, err := os.Stat(p); err == nil {
92− if ci, err := ignore.CompileIgnoreFile(crushignorePath); err == nil {
93+ f, err := os.Open(p)
93− dl.crushignore = ci
94+ if err != nil {
96+ slog.Error("Failed to open ignore file", "path", p, "error", err)
99+ if _, err := io.Copy(&b, f); err != nil {
100+ slog.Error("Failed to read ignore file", "path", p, "error", err)
96−
105+ return b.String()
97− // Create common ignore patterns
98− dl.commonIgnore = ignore.CompileIgnoreLines(CommonIgnorePatterns...)
103func (dl *DirectoryLister) shouldIgnore(path string, ignorePatterns []string) bool {
108func (dl *DirectoryLister) shouldIgnore(path string, ignorePatterns []string) bool {
@@ -106,18 +111,7 @@ func (dl *DirectoryLister) shouldIgnore(path string, ignorePatterns []string) bo
106 relPath = path
111 relPath = path
109− // Check common ignore patterns
110− if dl.commonIgnore.MatchesPath(relPath) {
114− // Check gitignore patterns if available
115− if dl.gitignore != nil && dl.gitignore.MatchesPath(relPath) {
119− // Check crushignore patterns if available
120− if dl.crushignore != nil && dl.crushignore.MatchesPath(relPath) {
114+ if dl.ignores.MatchesPath(relPath) {
121 return true
115 return true
@@ -144,6 +138,7 @@ func ListDirectory(initialPath string, ignorePatterns []string, limit int) ([]st
144 ToSlash: fastwalk.DefaultToSlash(),
138 ToSlash: fastwalk.DefaultToSlash(),
145 Sort: fastwalk.SortDirsFirst,
139 Sort: fastwalk.SortDirsFirst,
147 err := fastwalk.Walk(&conf, initialPath, func(path string, d os.DirEntry, err error) error {
142 err := fastwalk.Walk(&conf, initialPath, func(path string, d os.DirEntry, err error) error {
148 if err != nil {
143 if err != nil {
149 return nil // Skip files we don't have permission to access
144 return nil // Skip files we don't have permission to access