Skip to content

Commit 2223066

Browse files
committed
improve pattern matching with current directory
1 parent afe4af4 commit 2223066

File tree

2 files changed

+49
-9
lines changed

2 files changed

+49
-9
lines changed

arelo.go

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,8 @@ func main() {
6161
if *patterns == nil {
6262
*patterns = []string{"**"}
6363
}
64+
*patterns = removeCurDirPrefix(*patterns)
65+
*ignores = removeCurDirPrefix(*ignores)
6466
sig, sigstr := parseSignalOption(*sigopt)
6567
filtOp, err := parseFilters(*filters)
6668
if err != nil {
@@ -147,6 +149,15 @@ func versionstr() string {
147149
return info.Main.Version
148150
}
149151

152+
func removeCurDirPrefix(arr []string) []string {
153+
for i, s := range arr {
154+
if strings.HasPrefix(s, "./") {
155+
arr[i] = s[2:]
156+
}
157+
}
158+
return arr
159+
}
160+
150161
func parseFilters(filters []string) (fsnotify.Op, error) {
151162
var op fsnotify.Op
152163
for _, f := range filters {
@@ -243,6 +254,9 @@ func watcher(targets, patterns, ignores []string, filtOp fsnotify.Op, interval t
243254
}
244255

245256
func matchPatterns(t string, pats []string) (bool, error) {
257+
if strings.HasPrefix(t, "./") {
258+
t = t[2:]
259+
}
246260
for _, p := range pats {
247261
m, err := doublestar.Match(p, t)
248262
if err != nil {
@@ -251,15 +265,6 @@ func matchPatterns(t string, pats []string) (bool, error) {
251265
if m {
252266
return true, nil
253267
}
254-
if strings.HasPrefix(t, "./") {
255-
m, err = doublestar.Match(p, t[2:])
256-
if err != nil {
257-
return false, xerrors.Errorf("match(%v, %v): %w", p, t[2:], err)
258-
}
259-
if m {
260-
return true, nil
261-
}
262-
}
263268
}
264269
return false, nil
265270
}

arelo_test.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package main
33
import (
44
"os"
55
"path"
6+
"reflect"
67
"testing"
78
"time"
89
)
@@ -107,6 +108,9 @@ func TestMatchPatterns(t *testing.T) {
107108
{"./abc.efg", "*.efg", true},
108109
{"./.abc", "**/.*", true},
109110
{"./.abc", ".*", true},
111+
{"./", "", true},
112+
{"./", "*", true},
113+
{"./", "**", true},
110114
}
111115

112116
for _, test := range tests {
@@ -119,3 +123,34 @@ func TestMatchPatterns(t *testing.T) {
119123
}
120124
}
121125
}
126+
127+
func TestRemoveCurDirPrefix(t *testing.T) {
128+
arr := []string{
129+
".a",
130+
".aa",
131+
"./.*",
132+
"./abc",
133+
"../a",
134+
".",
135+
"./",
136+
"abc",
137+
"a./",
138+
"a/./b",
139+
}
140+
exp := []string{
141+
".a",
142+
".aa",
143+
".*",
144+
"abc",
145+
"../a",
146+
".",
147+
"",
148+
"abc",
149+
"a./",
150+
"a/./b",
151+
}
152+
r := removeCurDirPrefix(arr)
153+
if !reflect.DeepEqual(r, exp) {
154+
t.Fatalf("removeCurDirPrefix: %#v wants %#v", r, exp)
155+
}
156+
}

0 commit comments

Comments
 (0)