summaryrefslogtreecommitdiffstats
path: root/hugofs
diff options
context:
space:
mode:
authorBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>2019-08-13 12:35:04 +0200
committerBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>2019-08-13 18:09:46 +0200
commitcd575023af846aa18ffa709f37bc70277e98cad3 (patch)
treeb3eb6fcd3ab7b9073699df0486210cec00f877a1 /hugofs
parent6315098104ff80f8be6d5ae812835b4b4079582e (diff)
Improve the server assets cache invalidation logic
Fixes #6199
Diffstat (limited to 'hugofs')
-rw-r--r--hugofs/glob/glob.go33
-rw-r--r--hugofs/glob/glob_test.go26
2 files changed, 44 insertions, 15 deletions
diff --git a/hugofs/glob/glob.go b/hugofs/glob/glob.go
index 18d8d44cb..124a3d50e 100644
--- a/hugofs/glob/glob.go
+++ b/hugofs/glob/glob.go
@@ -51,7 +51,7 @@ func GetGlob(pattern string) (glob.Glob, error) {
}
func NormalizePath(p string) string {
- return strings.Trim(filepath.ToSlash(strings.ToLower(p)), "/.")
+ return strings.Trim(path.Clean(filepath.ToSlash(strings.ToLower(p))), "/.")
}
// ResolveRootDir takes a normalized path on the form "assets/**.json" and
@@ -60,14 +60,7 @@ func ResolveRootDir(p string) string {
parts := strings.Split(path.Dir(p), "/")
var roots []string
for _, part := range parts {
- isSpecial := false
- for i := 0; i < len(part); i++ {
- if syntax.Special(part[i]) {
- isSpecial = true
- break
- }
- }
- if isSpecial {
+ if HasGlobChar(part) {
break
}
roots = append(roots, part)
@@ -79,3 +72,25 @@ func ResolveRootDir(p string) string {
return strings.Join(roots, "/")
}
+
+// FilterGlobParts removes any string with glob wildcard.
+func FilterGlobParts(a []string) []string {
+ b := a[:0]
+ for _, x := range a {
+ if !HasGlobChar(x) {
+ b = append(b, x)
+ }
+ }
+ return b
+}
+
+// HasGlobChar returns whether s contains any glob wildcards.
+func HasGlobChar(s string) bool {
+ for i := 0; i < len(s); i++ {
+ if syntax.Special(s[i]) {
+ return true
+ }
+ }
+ return false
+
+}
diff --git a/hugofs/glob/glob_test.go b/hugofs/glob/glob_test.go
index 2b1c74107..cca8e4e0f 100644
--- a/hugofs/glob/glob_test.go
+++ b/hugofs/glob/glob_test.go
@@ -24,8 +24,8 @@ func TestResolveRootDir(t *testing.T) {
c := qt.New(t)
for _, test := range []struct {
- in string
- expect string
+ input string
+ expected string
}{
{"data/foo.json", "data"},
{"a/b/**/foo.json", "a/b"},
@@ -33,7 +33,21 @@ func TestResolveRootDir(t *testing.T) {
{"a/b[a-c]/foo.json", "a"},
} {
- c.Assert(ResolveRootDir(test.in), qt.Equals, test.expect)
+ c.Assert(ResolveRootDir(test.input), qt.Equals, test.expected)
+ }
+}
+
+func TestFilterGlobParts(t *testing.T) {
+ c := qt.New(t)
+
+ for _, test := range []struct {
+ input []string
+ expected []string
+ }{
+ {[]string{"a", "*", "c"}, []string{"a", "c"}},
+ } {
+
+ c.Assert(FilterGlobParts(test.input), qt.DeepEquals, test.expected)
}
}
@@ -41,8 +55,8 @@ func TestNormalizePath(t *testing.T) {
c := qt.New(t)
for _, test := range []struct {
- in string
- expect string
+ input string
+ expected string
}{
{filepath.FromSlash("data/FOO.json"), "data/foo.json"},
{filepath.FromSlash("/data/FOO.json"), "data/foo.json"},
@@ -50,7 +64,7 @@ func TestNormalizePath(t *testing.T) {
{"//", ""},
} {
- c.Assert(NormalizePath(test.in), qt.Equals, test.expect)
+ c.Assert(NormalizePath(test.input), qt.Equals, test.expected)
}
}