From d79556259bf775aaf1e5d757284f1e20ea3b2a05 Mon Sep 17 00:00:00 2001 From: comp500 Date: Fri, 24 Mar 2023 02:18:13 +0000 Subject: [PATCH] Improve directory traversal performance --- core/index.go | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/core/index.go b/core/index.go index 5e5c967..0faecc9 100644 --- a/core/index.go +++ b/core/index.go @@ -4,6 +4,7 @@ import ( "errors" "fmt" "io" + "io/fs" "os" "path" "path/filepath" @@ -214,21 +215,25 @@ func (in *Index) Refresh() error { ignore, ignoreExists := readGitignore(pathIgnore) var fileList []string - err := filepath.Walk(packRoot, func(path string, info os.FileInfo, err error) error { + err := filepath.WalkDir(packRoot, func(path string, info os.DirEntry, err error) error { if err != nil { // TODO: Handle errors on individual files properly return err } + if info.IsDir() { + // Don't traverse ignored directories (consistent with Git handling of ignored dirs) + if ignore.MatchesPath(path) { + return fs.SkipDir + } + // Don't add directories to the file list + return nil + } // Exit if the files are the same as the pack/index files absPath, _ := filepath.Abs(path) if absPath == pathPF || absPath == pathIndex { return nil } - // Exit if this is a directory - if info.IsDir() { - return nil - } if ignoreExists { if absPath == pathIgnore { return nil