| 1 |
// Copyright 2021 The Hugo Authors. All rights reserved. |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
// you may not use this file except in compliance with the License. |
| 5 |
// You may obtain a copy of the License at |
| 6 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 7 |
// |
| 8 |
// Unless required by applicable law or agreed to in writing, software |
| 9 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 10 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 |
// See the License for the specific language governing permissions and |
| 12 |
// limitations under the License. |
| 13 |
|
| 14 |
package hglob |
| 15 |
|
| 16 |
import ( |
| 17 |
"path" |
| 18 |
"path/filepath" |
| 19 |
"slices" |
| 20 |
"strings" |
| 21 |
|
| 22 |
"github.com/gohugoio/hugo/common/hstrings" |
| 23 |
) |
| 24 |
|
| 25 |
type FilenameFilter struct { |
| 26 |
shouldInclude func(filename string) bool |
| 27 |
|
| 28 |
entries []globFilenameFilterEntry |
| 29 |
|
| 30 |
isWindows bool |
| 31 |
|
| 32 |
nested []*FilenameFilter |
| 33 |
} |
| 34 |
|
| 35 |
type globFilenameFilterEntryType int |
| 36 |
|
| 37 |
const ( |
| 38 |
globFilenameFilterEntryTypeInclusion globFilenameFilterEntryType = iota |
| 39 |
globFilenameFilterEntryTypeInclusionDir |
| 40 |
globFilenameFilterEntryTypeExclusion |
| 41 |
) |
| 42 |
|
| 43 |
// NegationPrefix is the prefix that makes a pattern an exclusion. |
| 44 |
const NegationPrefix = "! " |
| 45 |
|
| 46 |
type globFilenameFilterEntry struct { |
| 47 |
g hstrings.Matcher |
| 48 |
t globFilenameFilterEntryType |
| 49 |
} |
| 50 |
|
| 51 |
func normalizeFilenameGlobPattern(s string) string { |
| 52 |
// Use Unix separators even on Windows. |
| 53 |
s = filepath.ToSlash(s) |
| 54 |
if !strings.HasPrefix(s, "/") { |
| 55 |
s = "/" + s |
| 56 |
} |
| 57 |
return s |
| 58 |
} |
| 59 |
|
| 60 |
func NewFilenameFilterV2(patterns []string) (*FilenameFilter, error) { |
| 61 |
if len(patterns) == 0 { |
| 62 |
return nil, nil |
| 63 |
} |
| 64 |
filter := &FilenameFilter{isWindows: isWindows} |
| 65 |
for _, p := range patterns { |
| 66 |
var t globFilenameFilterEntryType |
| 67 |
if strings.HasPrefix(p, NegationPrefix) { |
| 68 |
t = globFilenameFilterEntryTypeExclusion |
| 69 |
p = strings.TrimPrefix(p, NegationPrefix) |
| 70 |
} else { |
| 71 |
t = globFilenameFilterEntryTypeInclusion |
| 72 |
} |
| 73 |
p = normalizeFilenameGlobPattern(p) |
| 74 |
g, err := GetGlob(p) |
| 75 |
if err != nil { |
| 76 |
return nil, err |
| 77 |
} |
| 78 |
filter.entries = append(filter.entries, globFilenameFilterEntry{t: t, g: g}) |
| 79 |
if t == globFilenameFilterEntryTypeInclusion { |
| 80 |
// For mounts that do directory walking (e.g. content) we |
| 81 |
// must make sure that all directories up to this inclusion also |
| 82 |
// gets included. |
| 83 |
dir := path.Dir(p) |
| 84 |
parts := strings.Split(dir, "/") |
| 85 |
for i := range parts { |
| 86 |
pattern := "/" + filepath.Join(parts[:i+1]...) |
| 87 |
g, err := GetGlob(pattern) |
| 88 |
if err != nil { |
| 89 |
return nil, err |
| 90 |
} |
| 91 |
filter.entries = append(filter.entries, globFilenameFilterEntry{t: globFilenameFilterEntryTypeInclusionDir, g: g}) |
| 92 |
} |
| 93 |
} |
| 94 |
|
| 95 |
} |
| 96 |
|
| 97 |
return filter, nil |
| 98 |
} |
| 99 |
|
| 100 |
// NewFilenameFilter creates a new Glob where the Match method will |
| 101 |
// return true if the file should be included. |
| 102 |
// Note that the exclusions will be checked first. |
| 103 |
// Deprecated: Use NewFilenameFilterV2. |
| 104 |
func NewFilenameFilter(inclusions, exclusions []string) (*FilenameFilter, error) { |
| 105 |
for i, p := range exclusions { |
| 106 |
if !strings.HasPrefix(p, NegationPrefix) { |
| 107 |
exclusions[i] = NegationPrefix + p |
| 108 |
} |
| 109 |
} |
| 110 |
all := slices.Concat(inclusions, exclusions) |
| 111 |
return NewFilenameFilterV2(all) |
| 112 |
} |
| 113 |
|
| 114 |
// MustNewFilenameFilter invokes NewFilenameFilter and panics on error. |
| 115 |
// Deprecated: Use NewFilenameFilterV2. |
| 116 |
func MustNewFilenameFilter(inclusions, exclusions []string) *FilenameFilter { |
| 117 |
filter, err := NewFilenameFilter(inclusions, exclusions) |
| 118 |
if err != nil { |
| 119 |
panic(err) |
| 120 |
} |
| 121 |
return filter |
| 122 |
} |
| 123 |
|
| 124 |
// NewFilenameFilterForInclusionFunc create a new filter using the provided inclusion func. |
| 125 |
func NewFilenameFilterForInclusionFunc(shouldInclude func(filename string) bool) *FilenameFilter { |
| 126 |
return &FilenameFilter{shouldInclude: shouldInclude, isWindows: isWindows} |
| 127 |
} |
| 128 |
|
| 129 |
// Match returns whether filename should be included. |
| 130 |
func (f *FilenameFilter) Match(filename string, isDir bool) bool { |
| 131 |
if f == nil { |
| 132 |
return true |
| 133 |
} |
| 134 |
if !f.doMatch(filename, isDir) { |
| 135 |
return false |
| 136 |
} |
| 137 |
|
| 138 |
for _, nested := range f.nested { |
| 139 |
if !nested.Match(filename, isDir) { |
| 140 |
return false |
| 141 |
} |
| 142 |
} |
| 143 |
|
| 144 |
return true |
| 145 |
} |
| 146 |
|
| 147 |
// Append appends a filter to the chain. The receiver will be copied if needed. |
| 148 |
func (f *FilenameFilter) Append(other *FilenameFilter) *FilenameFilter { |
| 149 |
if f == nil { |
| 150 |
return other |
| 151 |
} |
| 152 |
|
| 153 |
clone := *f |
| 154 |
nested := make([]*FilenameFilter, len(clone.nested)+1) |
| 155 |
copy(nested, clone.nested) |
| 156 |
nested[len(nested)-1] = other |
| 157 |
clone.nested = nested |
| 158 |
|
| 159 |
return &clone |
| 160 |
} |
| 161 |
|
| 162 |
func (f *FilenameFilter) doMatch(filename string, isDir bool) bool { |
| 163 |
if f == nil { |
| 164 |
return true |
| 165 |
} |
| 166 |
|
| 167 |
if !strings.HasPrefix(filename, filepathSeparator) { |
| 168 |
filename = filepathSeparator + filename |
| 169 |
} |
| 170 |
|
| 171 |
if f.shouldInclude != nil { |
| 172 |
if f.shouldInclude(filename) { |
| 173 |
return true |
| 174 |
} |
| 175 |
if f.isWindows { |
| 176 |
// The Glob matchers below handles this by themselves, |
| 177 |
// for the shouldInclude we need to take some extra steps |
| 178 |
// to make this robust. |
| 179 |
winFilename := filepath.FromSlash(filename) |
| 180 |
if filename != winFilename { |
| 181 |
if f.shouldInclude(winFilename) { |
| 182 |
return true |
| 183 |
} |
| 184 |
} |
| 185 |
} |
| 186 |
} |
| 187 |
|
| 188 |
var hasInclude bool |
| 189 |
for _, entry := range f.entries { |
| 190 |
switch entry.t { |
| 191 |
case globFilenameFilterEntryTypeExclusion: |
| 192 |
if entry.g.Match(filename) { |
| 193 |
return false |
| 194 |
} |
| 195 |
case globFilenameFilterEntryTypeInclusion: |
| 196 |
if entry.g.Match(filename) { |
| 197 |
return true |
| 198 |
} |
| 199 |
hasInclude = true |
| 200 |
case globFilenameFilterEntryTypeInclusionDir: |
| 201 |
if isDir && entry.g.Match(filename) { |
| 202 |
return true |
| 203 |
} |
| 204 |
} |
| 205 |
} |
| 206 |
|
| 207 |
return !hasInclude && f.shouldInclude == nil |
| 208 |
} |