From 0a8ffe0c63c7f214ac93cd740992b23f9187c181 Mon Sep 17 00:00:00 2001 From: Brian McGee Date: Tue, 9 Jul 2024 19:21:09 +0100 Subject: [PATCH] feat: move filtering of directories and symlinks into walker implementations Signed-off-by: Brian McGee --- cache/cache.go | 5 ----- cli/format.go | 9 +++------ walk/filesystem.go | 6 ++++++ walk/filesystem_test.go | 19 ------------------- walk/git.go | 6 +++++- 5 files changed, 14 insertions(+), 31 deletions(-) diff --git a/cache/cache.go b/cache/cache.go index a6c743f7..fe1c1ac2 100644 --- a/cache/cache.go +++ b/cache/cache.go @@ -218,11 +218,6 @@ func ChangeSet(ctx context.Context, walker walk.Walker, filesCh chan<- *walk.Fil } } - // ignore symlinks - if file.Info.Mode()&os.ModeSymlink == os.ModeSymlink { - return nil - } - // open a new read tx if there isn't one in progress // we have to periodically open a new read tx to prevent writes from being blocked if tx == nil { diff --git a/cli/format.go b/cli/format.go index 7218a956..95ff37bc 100644 --- a/cli/format.go +++ b/cli/format.go @@ -237,12 +237,9 @@ func (f *Format) walkFilesystem(ctx context.Context) func() error { case <-ctx.Done(): return ctx.Err() default: - // ignore symlinks and directories - if !(file.Info.IsDir() || file.Info.Mode()&os.ModeSymlink == os.ModeSymlink) { - stats.Add(stats.Traversed, 1) - stats.Add(stats.Emitted, 1) - f.filesCh <- file - } + stats.Add(stats.Traversed, 1) + stats.Add(stats.Emitted, 1) + f.filesCh <- file return nil } }) diff --git a/walk/filesystem.go b/walk/filesystem.go index 35aec7ce..729a4972 100644 --- a/walk/filesystem.go +++ b/walk/filesystem.go @@ -4,6 +4,7 @@ import ( "context" "fmt" "io/fs" + "os" "path/filepath" ) @@ -32,6 +33,11 @@ func (f filesystemWalker) Walk(_ context.Context, fn WalkFunc) error { return fmt.Errorf("no such file or directory '%s'", path) } + // ignore directories and symlinks + if info.IsDir() || info.Mode()&os.ModeSymlink == os.ModeSymlink { + return nil + } + relPath, err := f.relPath(path) if err != nil { return fmt.Errorf("failed to determine a relative path for %s: %w", path, err) diff --git a/walk/filesystem_test.go b/walk/filesystem_test.go index 202f0595..503ec0d3 100644 --- a/walk/filesystem_test.go +++ b/walk/filesystem_test.go @@ -10,56 +10,37 @@ import ( ) var examplesPaths = []string{ - ".", - "elm", "elm/elm.json", - "elm/src", "elm/src/Main.elm", - "go", "go/go.mod", "go/main.go", - "haskell", "haskell/CHANGELOG.md", "haskell/Foo.hs", "haskell/Main.hs", - "haskell/Nested", "haskell/Nested/Foo.hs", "haskell/Setup.hs", "haskell/haskell.cabal", "haskell/treefmt.toml", - "haskell-frontend", "haskell-frontend/CHANGELOG.md", "haskell-frontend/Main.hs", "haskell-frontend/Setup.hs", "haskell-frontend/haskell-frontend.cabal", - "html", "html/index.html", - "html/scripts", "html/scripts/.gitkeep", - "javascript", - "javascript/source", "javascript/source/hello.js", - "nix", "nix/sources.nix", "nixpkgs.toml", - "python", "python/main.py", "python/requirements.txt", "python/virtualenv_proxy.py", - "ruby", "ruby/bundler.rb", - "rust", "rust/Cargo.toml", - "rust/src", "rust/src/main.rs", - "shell", "shell/foo.sh", - "terraform", "terraform/main.tf", "terraform/two.tf", "touch.toml", "treefmt.toml", - "yaml", "yaml/test.yaml", } diff --git a/walk/git.go b/walk/git.go index 58afa4e8..cfe5f12f 100644 --- a/walk/git.go +++ b/walk/git.go @@ -50,9 +50,13 @@ func (g *gitWalker) Walk(ctx context.Context, fn WalkFunc) error { case <-ctx.Done(): return ctx.Err() default: - path := filepath.Join(g.root, entry.Name) + // we only want regular files, not directories or symlinks + if !entry.Mode.IsRegular() { + continue + } // stat the file + path := filepath.Join(g.root, entry.Name) info, err := os.Lstat(path) file := File{