Skip to content

Commit 4a20bb7

Browse files
committed
refactor: switch Globber/GitIgnore to use fs.FS
1 parent 0ac2aeb commit 4a20bb7

File tree

4 files changed

+30
-21
lines changed

4 files changed

+30
-21
lines changed

.github/workflows/ci.yml

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ jobs:
1515
uses: cashapp/activate-hermit@v1
1616
- name: Test
1717
run: go test ./...
18-
lint:
19-
name: Lint
18+
sum-types:
19+
name: Check Exhaustive Type Switch
2020
runs-on: ubuntu-latest
2121
steps:
2222
- name: Checkout code
@@ -25,3 +25,13 @@ jobs:
2525
uses: cashapp/activate-hermit@v1
2626
- name: golangci-lint
2727
run: golangci-lint run
28+
lint:
29+
name: Lint
30+
runs-on: ubuntu-latest
31+
steps:
32+
- name: Checkout code
33+
uses: actions/checkout@v2
34+
- name: Init Hermit
35+
uses: cashapp/activate-hermit@v1
36+
- name: go-check-sumtype
37+
run: go-check-sumtype ./...

engine/engine.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ func Compile(logger *logging.Logger, bitfile *parser.Bitfile) (*Engine, error) {
8888
"CWD": {Pos: bitfile.Pos, Body: cwd},
8989
},
9090
}
91-
engine.globber, err = internal.NewGlobber(cwd, engine.Outputs)
91+
engine.globber, err = internal.NewGlobber(os.DirFS(cwd), engine.Outputs)
9292
if err != nil {
9393
return nil, err
9494
}

engine/internal/gitignore.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,17 @@ package internal
22

33
import (
44
"bufio"
5-
"os"
5+
"io/fs"
66
"path"
7-
"path/filepath"
87
"strings"
98
)
109

11-
func LoadGitIgnore(dir string) []string {
10+
func LoadGitIgnore(root fs.FS, dir string) []string {
1211
ignore := []string{
1312
"**/.*",
1413
"**/.*/**",
1514
}
16-
r, err := os.Open(filepath.Join(dir, ".gitignore"))
15+
r, err := root.Open(path.Join(dir, ".gitignore"))
1716
if err != nil {
1817
return nil
1918
}

engine/internal/glob.go

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,35 +11,36 @@ import (
1111

1212
// Globber is a file globber that respects .gitingore files.
1313
type Globber struct {
14-
dir string
1514
ignore []string
1615
files []string
1716
// Doesn't cache outputs
1817
cache map[string][]string
1918
outputs func() []string
2019
}
2120

22-
// NewGlobber creates a new Globber for the given directory.
21+
// NewGlobber creates a new Globber for the given fs.FS.
2322
//
2423
// The "outputs" function is called to provide additional files to be
2524
// considered when globbing. This is used for files output by the
2625
// build process.
27-
func NewGlobber(root string, outputs func() []string) (*Globber, error) {
28-
ignore := LoadGitIgnore(root)
26+
func NewGlobber(root fs.FS, outputs func() []string) (*Globber, error) {
27+
ignores := LoadGitIgnore(root, ".")
2928
var files []string
30-
err := filepath.WalkDir(root, func(path string, d fs.DirEntry, err error) error {
31-
if path == root {
29+
err := fs.WalkDir(root, ".", func(path string, d fs.DirEntry, err error) error {
30+
if err != nil {
31+
return err
32+
}
33+
if path == "." {
3234
return nil
3335
}
34-
if d.IsDir() && path != root {
35-
extraIgnores := LoadGitIgnore(path)
36+
if d.IsDir() && path != "." {
37+
extraIgnores := LoadGitIgnore(root, path)
3638
for _, extraIgnore := range extraIgnores {
37-
extraIgnore = strings.TrimPrefix(strings.TrimPrefix(filepath.Join(path, extraIgnore), root), "/")
38-
ignore = append(ignore, extraIgnore)
39+
extraIgnore = filepath.Join(path, extraIgnore)
40+
ignores = append(ignores, extraIgnore)
3941
}
4042
}
41-
path = strings.TrimPrefix(path, root+"/")
42-
for _, ignore := range ignore {
43+
for _, ignore := range ignores {
4344
if ok, err := doublestar.Match(ignore, path); ok || err != nil {
4445
if d.IsDir() {
4546
return filepath.SkipDir
@@ -55,8 +56,7 @@ func NewGlobber(root string, outputs func() []string) (*Globber, error) {
5556
}
5657
sort.Strings(files)
5758
return &Globber{
58-
dir: root,
59-
ignore: ignore,
59+
ignore: ignores,
6060
files: files,
6161
outputs: outputs,
6262
cache: map[string][]string{},

0 commit comments

Comments
 (0)