feat: changed hidden matching from regex to prefix.

This commit is contained in:
Tiger Nie 2020-11-18 18:21:02 +08:00
parent 4d618f1f18
commit 0aa85e09f0
3 changed files with 29 additions and 4 deletions

View File

@ -27,7 +27,7 @@ type data struct {
// Check implements rules.Checker.
func (d *data) Check(path string) bool {
if d.user.HideDotfiles && rules.MatchHidden.Matches(path) {
if d.user.HideDotfiles && rules.MatchHidden(path) {
return false
}

View File

@ -1,6 +1,7 @@
package rules
import (
"path/filepath"
"regexp"
"strings"
)
@ -18,9 +19,10 @@ type Rule struct {
Regexp *Regexp `json:"regexp"`
}
var MatchHidden = Rule{
Regex: true,
Regexp: &Regexp{Raw: `\/\..*$`},
// MatchHidden matches paths with a basename
// that begins with a dot.
func MatchHidden(path string) bool {
return filepath.Base(path)[0] == '.'
}
// Matches matches a path against a rule.

23
rules/rules_test.go Normal file
View File

@ -0,0 +1,23 @@
package rules
import "testing"
func TestMatchHidden(t *testing.T) {
cases := map[string]bool{
"/": false,
"/src": false,
"/src/": false,
"/.circleci": true,
"/a/b/c/.docker.json": true,
".docker.json": true,
"Dockerfile": false,
"/Dockerfile": false,
}
for path, truth := range cases {
got := MatchHidden(path)
if got != truth {
t.Errorf("MatchHidden(%s)=%v; want %v", path, got, truth)
}
}
}