Skip to content

Commit

Permalink
feat: move filtering of directories and symlinks into walker implemen…
Browse files Browse the repository at this point in the history
…tations

Signed-off-by: Brian McGee <[email protected]>
  • Loading branch information
brianmcgee committed Jul 9, 2024
1 parent ff8b1ed commit 0a8ffe0
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 31 deletions.
5 changes: 0 additions & 5 deletions cache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
9 changes: 3 additions & 6 deletions cli/format.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
})
Expand Down
6 changes: 6 additions & 0 deletions walk/filesystem.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"fmt"
"io/fs"
"os"
"path/filepath"
)

Expand Down Expand Up @@ -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)
Expand Down
19 changes: 0 additions & 19 deletions walk/filesystem_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
}

Expand Down
6 changes: 5 additions & 1 deletion walk/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down

0 comments on commit 0a8ffe0

Please sign in to comment.