diff options
Diffstat (limited to 'hugofs/glob')
-rw-r--r-- | hugofs/glob/filename_filter.go | 8 | ||||
-rw-r--r-- | hugofs/glob/filename_filter_test.go | 5 | ||||
-rw-r--r-- | hugofs/glob/glob.go | 42 | ||||
-rw-r--r-- | hugofs/glob/glob_test.go | 6 |
4 files changed, 16 insertions, 45 deletions
diff --git a/hugofs/glob/filename_filter.go b/hugofs/glob/filename_filter.go index c4b582bd5..8e8af554b 100644 --- a/hugofs/glob/filename_filter.go +++ b/hugofs/glob/filename_filter.go @@ -49,7 +49,7 @@ func NewFilenameFilter(inclusions, exclusions []string) (*FilenameFilter, error) for _, include := range inclusions { include = normalizeFilenameGlobPattern(include) - g, err := filenamesGlobCache.GetGlob(include) + g, err := GetGlob(include) if err != nil { return nil, err } @@ -60,9 +60,9 @@ func NewFilenameFilter(inclusions, exclusions []string) (*FilenameFilter, error) // gets included. dir := path.Dir(include) parts := strings.Split(dir, "/") - for i, _ := range parts { + for i := range parts { pattern := "/" + filepath.Join(parts[:i+1]...) - g, err := filenamesGlobCache.GetGlob(pattern) + g, err := GetGlob(pattern) if err != nil { return nil, err } @@ -72,7 +72,7 @@ func NewFilenameFilter(inclusions, exclusions []string) (*FilenameFilter, error) for _, exclude := range exclusions { exclude = normalizeFilenameGlobPattern(exclude) - g, err := filenamesGlobCache.GetGlob(exclude) + g, err := GetGlob(exclude) if err != nil { return nil, err } diff --git a/hugofs/glob/filename_filter_test.go b/hugofs/glob/filename_filter_test.go index 1fce5b135..b74982ef3 100644 --- a/hugofs/glob/filename_filter_test.go +++ b/hugofs/glob/filename_filter_test.go @@ -42,6 +42,11 @@ func TestFilenameFilter(t *testing.T) { c.Assert(excludeAllButFooJSON.Match(filepath.FromSlash("/a/b/"), true), qt.Equals, true) c.Assert(excludeAllButFooJSON.Match(filepath.FromSlash("/"), true), qt.Equals, true) c.Assert(excludeAllButFooJSON.Match(filepath.FromSlash("/b"), true), qt.Equals, false) + + excludeAllButFooJSONMixedCasePattern, err := NewFilenameFilter([]string{"/**/Foo.json"}, nil) + c.Assert(excludeAllButFooJSONMixedCasePattern.Match(filepath.FromSlash("/a/b/c/d/e/foo.json"), false), qt.Equals, true) + c.Assert(excludeAllButFooJSONMixedCasePattern.Match(filepath.FromSlash("/a/b/c/d/e/FOO.json"), false), qt.Equals, true) + c.Assert(err, qt.IsNil) nopFilter, err := NewFilenameFilter(nil, nil) diff --git a/hugofs/glob/glob.go b/hugofs/glob/glob.go index 87619802e..841824bcc 100644 --- a/hugofs/glob/glob.go +++ b/hugofs/glob/glob.go @@ -33,11 +33,6 @@ var ( isWindows: isWindows, cache: make(map[string]globErr), } - - filenamesGlobCache = &globCache{ - isWindows: isWindows, - cache: make(map[string]globErr), - } ) type globErr struct { @@ -86,10 +81,6 @@ func (gc *globCache) GetGlob(pattern string) (glob.Glob, error) { } type globDecorator struct { - // Whether both pattern and the strings to match will be matched - // by their original case. - isCaseSensitive bool - // On Windows we may get filenames with Windows slashes to match, // which wee need to normalize. isWindows bool @@ -101,14 +92,12 @@ func (g globDecorator) Match(s string) bool { if g.isWindows { s = filepath.ToSlash(s) } - if !g.isCaseSensitive { - s = strings.ToLower(s) - } + s = strings.ToLower(s) return g.g.Match(s) } type globDecoratorDouble struct { - lowerCase glob.Glob + lowerCase glob.Glob originalCase glob.Glob } @@ -120,34 +109,11 @@ func GetGlob(pattern string) (glob.Glob, error) { return defaultGlobCache.GetGlob(pattern) } -func GetFilenamesGlob(pattern string) (glob.Glob, error) { - lowered := strings.ToLower(pattern) - hasUpperCase := pattern != lowered - gLowered, err := filenamesGlobCache.GetGlob(lowered) - if err != nil { - return nil, err - } - - if !hasUpperCase { - return gLowered, nil - } - - gSensitive, err := filenamesGlobCache.GetGlob(pattern) - if err != nil { - return nil, err - } - return globDecoratorDouble{ - lowerCase: gLowered, - originalCase: gSensitive, - }, nil - -} - func NormalizePath(p string) string { - return strings.Trim(path.Clean(filepath.ToSlash(strings.ToLower(p))), "/.") + return strings.ToLower(NormalizePathNoLower(p)) } -func NormalizePathCaseSensitive(p string) string { +func NormalizePathNoLower(p string) string { return strings.Trim(path.Clean(filepath.ToSlash(p)), "/.") } diff --git a/hugofs/glob/glob_test.go b/hugofs/glob/glob_test.go index 66efe9e53..d40e16edc 100644 --- a/hugofs/glob/glob_test.go +++ b/hugofs/glob/glob_test.go @@ -66,7 +66,7 @@ func TestNormalizePath(t *testing.T) { } func TestGetGlob(t *testing.T) { - for _, cache := range []*globCache{defaultGlobCache, filenamesGlobCache} { + for _, cache := range []*globCache{defaultGlobCache} { c := qt.New(t) g, err := cache.GetGlob("**.JSON") c.Assert(err, qt.IsNil) @@ -89,8 +89,8 @@ func BenchmarkGetGlob(b *testing.B) { } runBench("Default cache", defaultGlobCache, "abcde") - runBench("Filenames cache, lowercase searchs", filenamesGlobCache, "abcde") - runBench("Filenames cache, mixed case searchs", filenamesGlobCache, "abCDe") + runBench("Filenames cache, lowercase searchs", defaultGlobCache, "abcde") + runBench("Filenames cache, mixed case searchs", defaultGlobCache, "abCDe") b.Run("GetGlob", func(b *testing.B) { for i := 0; i < b.N; i++ { |