Skip to content

Commit 2da37f7

Browse files
committed
feat: Use https://github.com/Azuyamat/globber for glob pattern of watcher
1 parent 18970a1 commit 2da37f7

4 files changed

Lines changed: 16 additions & 84 deletions

File tree

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ toolchain go1.24.10
66

77
require (
88
github.com/azuyamat/gear v0.0.0-20251118045405-f2133a4d3c3a
9+
github.com/azuyamat/globber v0.0.0-20251126020500-7fa19a3402a2
910
github.com/fsnotify/fsnotify v1.9.0
1011
)
1112

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
github.com/azuyamat/gear v0.0.0-20251118045405-f2133a4d3c3a h1:lsdZXJX9/D6xsvmDhBsmt0O4JndCvbwyAnURauI35TA=
22
github.com/azuyamat/gear v0.0.0-20251118045405-f2133a4d3c3a/go.mod h1:r+5DrbDCyAmVC3AOtSxvZivpSoIxwV6/KsaA0IUExB8=
3+
github.com/azuyamat/globber v0.0.0-20251126020500-7fa19a3402a2 h1:uPR9bpHS0tmHde8FD0200Swu9qdjZcN+zOGypEl27Hw=
4+
github.com/azuyamat/globber v0.0.0-20251126020500-7fa19a3402a2/go.mod h1:p4AYsIvRO0SFjr0MH2hyEgO3085qZkIa3ymrrj/bxvw=
35
github.com/fsnotify/fsnotify v1.9.0 h1:2Ml+OJNzbYCTzsxtv8vKSFD9PbJjmhYF14k/jKC7S9k=
46
github.com/fsnotify/fsnotify v1.9.0/go.mod h1:8jBTzvmWwFyi3Pb8djgCCO5IBqzKJ/Jwo8TRcHyHii0=
57
golang.org/x/sys v0.38.0 h1:3yZWxaJjBmCWXqhN1qh02AkOnCQ1poK6oF+a7xWL6Gc=

internal/runner/glob.go

Lines changed: 11 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,18 @@
11
package runner
22

33
import (
4-
"fmt"
5-
"os"
4+
"io/fs"
65
"path/filepath"
7-
"strings"
6+
7+
"github.com/azuyamat/globber/glob"
88
)
99

1010
func expandGlobPattern(pattern string) ([]string, error) {
11-
if !containsDoublestar(pattern) {
12-
return filepath.Glob(pattern)
13-
}
14-
15-
return expandDoublestar(pattern)
16-
}
17-
18-
func containsDoublestar(pattern string) bool {
19-
return len(pattern) >= 2 && (pattern[:2] == "**" ||
20-
(len(pattern) >= 3 && pattern[len(pattern)-3:] == "/**") ||
21-
strings.Contains(pattern, "/**/") || strings.Contains(pattern, "\\**\\"))
22-
}
23-
24-
func expandDoublestar(pattern string) ([]string, error) {
2511
var matches []string
2612

27-
parts := splitPattern(pattern)
28-
if len(parts) == 0 {
29-
return nil, fmt.Errorf("invalid pattern")
30-
}
31-
32-
baseDir := "."
33-
filePattern := parts[len(parts)-1]
34-
35-
err := filepath.Walk(baseDir, func(path string, info os.FileInfo, err error) error {
36-
if err != nil {
37-
return nil
38-
}
39-
40-
if !info.IsDir() {
41-
if matched, _ := filepath.Match(filePattern, info.Name()); matched {
42-
matches = append(matches, path)
43-
}
44-
}
45-
13+
fsMatcher := glob.FSMatcher(pattern)
14+
err := fsMatcher.WalkDirFS(".", func(path string, entry fs.DirEntry) error {
15+
matches = append(matches, path)
4616
return nil
4717
})
4818

@@ -53,24 +23,9 @@ func expandDoublestar(pattern string) ([]string, error) {
5323
return matches, nil
5424
}
5525

56-
func splitPattern(pattern string) []string {
57-
var parts []string
58-
var current strings.Builder
59-
60-
for i := 0; i < len(pattern); i++ {
61-
if pattern[i] == '/' || pattern[i] == '\\' {
62-
if current.Len() > 0 {
63-
parts = append(parts, current.String())
64-
current.Reset()
65-
}
66-
} else {
67-
current.WriteByte(pattern[i])
68-
}
69-
}
70-
71-
if current.Len() > 0 {
72-
parts = append(parts, current.String())
73-
}
74-
75-
return parts
26+
func matchesGlobPattern(pattern, filePath string) bool {
27+
matcher := glob.Matcher(pattern)
28+
normalizedPath := filepath.ToSlash(filePath)
29+
matches, _ := matcher.Matches(normalizedPath)
30+
return matches
7631
}

internal/runner/watcher.go

Lines changed: 2 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ func (w *Watcher) setupWatchPaths(watcher *fsnotify.Watcher) error {
5555
dirs := make(map[string]bool)
5656

5757
for _, pattern := range w.patterns {
58-
matches, err := w.expandPattern(pattern)
58+
matches, err := expandGlobPattern(pattern)
5959
if err != nil {
6060
w.log.Warning("invalid pattern %q: %v", pattern, err)
6161
continue
@@ -86,10 +86,6 @@ func (w *Watcher) setupWatchPaths(watcher *fsnotify.Watcher) error {
8686
return nil
8787
}
8888

89-
func (w *Watcher) expandPattern(pattern string) ([]string, error) {
90-
return expandGlobPattern(pattern)
91-
}
92-
9389
func (w *Watcher) eventLoop(watcher *fsnotify.Watcher) error {
9490
sigChan := make(chan os.Signal, 1)
9591
signal.Notify(sigChan, syscall.SIGINT, syscall.SIGTERM)
@@ -150,31 +146,9 @@ func (w *Watcher) isRelevantEvent(event fsnotify.Event) bool {
150146

151147
func (w *Watcher) matchesPattern(filePath string) bool {
152148
for _, pattern := range w.patterns {
153-
if containsDoublestar(pattern) {
154-
filePattern := filepath.Base(pattern)
155-
if matched, err := filepath.Match(filePattern, filepath.Base(filePath)); err == nil && matched {
156-
return true
157-
}
158-
continue
159-
}
160-
161-
matched, err := filepath.Match(pattern, filePath)
162-
if err == nil && matched {
149+
if matchesGlobPattern(pattern, filePath) {
163150
return true
164151
}
165-
166-
normalizedPattern := filepath.FromSlash(pattern)
167-
if matched, err := filepath.Match(normalizedPattern, filePath); err == nil && matched {
168-
return true
169-
}
170-
171-
if matched, err := filepath.Match(filepath.Base(pattern), filepath.Base(filePath)); err == nil && matched {
172-
patternDir := filepath.Dir(pattern)
173-
fileDir := filepath.Dir(filePath)
174-
if patternDir == fileDir || patternDir == "." {
175-
return true
176-
}
177-
}
178152
}
179153
return false
180154
}

0 commit comments

Comments
 (0)