Improve directory traversal performance

This commit is contained in:
comp500 2023-03-24 02:18:13 +00:00
parent fa5460a187
commit d79556259b

View File

@ -4,6 +4,7 @@ import (
"errors" "errors"
"fmt" "fmt"
"io" "io"
"io/fs"
"os" "os"
"path" "path"
"path/filepath" "path/filepath"
@ -214,21 +215,25 @@ func (in *Index) Refresh() error {
ignore, ignoreExists := readGitignore(pathIgnore) ignore, ignoreExists := readGitignore(pathIgnore)
var fileList []string 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 { if err != nil {
// TODO: Handle errors on individual files properly // TODO: Handle errors on individual files properly
return err 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 // Exit if the files are the same as the pack/index files
absPath, _ := filepath.Abs(path) absPath, _ := filepath.Abs(path)
if absPath == pathPF || absPath == pathIndex { if absPath == pathPF || absPath == pathIndex {
return nil return nil
} }
// Exit if this is a directory
if info.IsDir() {
return nil
}
if ignoreExists { if ignoreExists {
if absPath == pathIgnore { if absPath == pathIgnore {
return nil return nil