Skip to content

Commit 0a8ffe0

Browse files
committed
feat: move filtering of directories and symlinks into walker implementations
Signed-off-by: Brian McGee <[email protected]>
1 parent ff8b1ed commit 0a8ffe0

File tree

5 files changed

+14
-31
lines changed

5 files changed

+14
-31
lines changed

cache/cache.go

-5
Original file line numberDiff line numberDiff line change
@@ -218,11 +218,6 @@ func ChangeSet(ctx context.Context, walker walk.Walker, filesCh chan<- *walk.Fil
218218
}
219219
}
220220

221-
// ignore symlinks
222-
if file.Info.Mode()&os.ModeSymlink == os.ModeSymlink {
223-
return nil
224-
}
225-
226221
// open a new read tx if there isn't one in progress
227222
// we have to periodically open a new read tx to prevent writes from being blocked
228223
if tx == nil {

cli/format.go

+3-6
Original file line numberDiff line numberDiff line change
@@ -237,12 +237,9 @@ func (f *Format) walkFilesystem(ctx context.Context) func() error {
237237
case <-ctx.Done():
238238
return ctx.Err()
239239
default:
240-
// ignore symlinks and directories
241-
if !(file.Info.IsDir() || file.Info.Mode()&os.ModeSymlink == os.ModeSymlink) {
242-
stats.Add(stats.Traversed, 1)
243-
stats.Add(stats.Emitted, 1)
244-
f.filesCh <- file
245-
}
240+
stats.Add(stats.Traversed, 1)
241+
stats.Add(stats.Emitted, 1)
242+
f.filesCh <- file
246243
return nil
247244
}
248245
})

walk/filesystem.go

+6
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"fmt"
66
"io/fs"
7+
"os"
78
"path/filepath"
89
)
910

@@ -32,6 +33,11 @@ func (f filesystemWalker) Walk(_ context.Context, fn WalkFunc) error {
3233
return fmt.Errorf("no such file or directory '%s'", path)
3334
}
3435

36+
// ignore directories and symlinks
37+
if info.IsDir() || info.Mode()&os.ModeSymlink == os.ModeSymlink {
38+
return nil
39+
}
40+
3541
relPath, err := f.relPath(path)
3642
if err != nil {
3743
return fmt.Errorf("failed to determine a relative path for %s: %w", path, err)

walk/filesystem_test.go

-19
Original file line numberDiff line numberDiff line change
@@ -10,56 +10,37 @@ import (
1010
)
1111

1212
var examplesPaths = []string{
13-
".",
14-
"elm",
1513
"elm/elm.json",
16-
"elm/src",
1714
"elm/src/Main.elm",
18-
"go",
1915
"go/go.mod",
2016
"go/main.go",
21-
"haskell",
2217
"haskell/CHANGELOG.md",
2318
"haskell/Foo.hs",
2419
"haskell/Main.hs",
25-
"haskell/Nested",
2620
"haskell/Nested/Foo.hs",
2721
"haskell/Setup.hs",
2822
"haskell/haskell.cabal",
2923
"haskell/treefmt.toml",
30-
"haskell-frontend",
3124
"haskell-frontend/CHANGELOG.md",
3225
"haskell-frontend/Main.hs",
3326
"haskell-frontend/Setup.hs",
3427
"haskell-frontend/haskell-frontend.cabal",
35-
"html",
3628
"html/index.html",
37-
"html/scripts",
3829
"html/scripts/.gitkeep",
39-
"javascript",
40-
"javascript/source",
4130
"javascript/source/hello.js",
42-
"nix",
4331
"nix/sources.nix",
4432
"nixpkgs.toml",
45-
"python",
4633
"python/main.py",
4734
"python/requirements.txt",
4835
"python/virtualenv_proxy.py",
49-
"ruby",
5036
"ruby/bundler.rb",
51-
"rust",
5237
"rust/Cargo.toml",
53-
"rust/src",
5438
"rust/src/main.rs",
55-
"shell",
5639
"shell/foo.sh",
57-
"terraform",
5840
"terraform/main.tf",
5941
"terraform/two.tf",
6042
"touch.toml",
6143
"treefmt.toml",
62-
"yaml",
6344
"yaml/test.yaml",
6445
}
6546

walk/git.go

+5-1
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,13 @@ func (g *gitWalker) Walk(ctx context.Context, fn WalkFunc) error {
5050
case <-ctx.Done():
5151
return ctx.Err()
5252
default:
53-
path := filepath.Join(g.root, entry.Name)
53+
// we only want regular files, not directories or symlinks
54+
if !entry.Mode.IsRegular() {
55+
continue
56+
}
5457

5558
// stat the file
59+
path := filepath.Join(g.root, entry.Name)
5660
info, err := os.Lstat(path)
5761

5862
file := File{

0 commit comments

Comments
 (0)