|
| 1 | +/* |
| 2 | + Copyright 2020 Docker Compose CLI authors |
| 3 | +
|
| 4 | + Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + you may not use this file except in compliance with the License. |
| 6 | + You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | + Unless required by applicable law or agreed to in writing, software |
| 11 | + distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + See the License for the specific language governing permissions and |
| 14 | + limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package watch |
| 18 | + |
| 19 | +import ( |
| 20 | + "fmt" |
| 21 | + "os" |
| 22 | + "path/filepath" |
| 23 | + "strings" |
| 24 | + |
| 25 | + "github.com/moby/buildkit/frontend/dockerfile/dockerignore" |
| 26 | + "github.com/moby/patternmatcher" |
| 27 | +) |
| 28 | + |
| 29 | +type dockerPathMatcher struct { |
| 30 | + repoRoot string |
| 31 | + matcher *patternmatcher.PatternMatcher |
| 32 | +} |
| 33 | + |
| 34 | +func (i dockerPathMatcher) Matches(f string) (bool, error) { |
| 35 | + if !filepath.IsAbs(f) { |
| 36 | + f = filepath.Join(i.repoRoot, f) |
| 37 | + } |
| 38 | + return i.matcher.Matches(f) |
| 39 | +} |
| 40 | + |
| 41 | +func (i dockerPathMatcher) MatchesEntireDir(f string) (bool, error) { |
| 42 | + matches, err := i.Matches(f) |
| 43 | + if !matches || err != nil { |
| 44 | + return matches, err |
| 45 | + } |
| 46 | + |
| 47 | + // We match the dir, but we might exclude files underneath it. |
| 48 | + if i.matcher.Exclusions() { |
| 49 | + for _, pattern := range i.matcher.Patterns() { |
| 50 | + if !pattern.Exclusion() { |
| 51 | + continue |
| 52 | + } |
| 53 | + if IsChild(f, pattern.String()) { |
| 54 | + // Found an exclusion match -- we don't match this whole dir |
| 55 | + return false, nil |
| 56 | + } |
| 57 | + } |
| 58 | + return true, nil |
| 59 | + } |
| 60 | + return true, nil |
| 61 | +} |
| 62 | + |
| 63 | +func NewDockerIgnoreTester(repoRoot string) (*dockerPathMatcher, error) { |
| 64 | + absRoot, err := filepath.Abs(repoRoot) |
| 65 | + if err != nil { |
| 66 | + return nil, err |
| 67 | + } |
| 68 | + |
| 69 | + patterns, err := readDockerignorePatterns(absRoot) |
| 70 | + if err != nil { |
| 71 | + return nil, err |
| 72 | + } |
| 73 | + |
| 74 | + return NewDockerPatternMatcher(absRoot, patterns) |
| 75 | +} |
| 76 | + |
| 77 | +// Make all the patterns use absolute paths. |
| 78 | +func absPatterns(absRoot string, patterns []string) []string { |
| 79 | + absPatterns := make([]string, 0, len(patterns)) |
| 80 | + for _, p := range patterns { |
| 81 | + // The pattern parsing here is loosely adapted from fileutils' NewPatternMatcher |
| 82 | + p = strings.TrimSpace(p) |
| 83 | + if p == "" { |
| 84 | + continue |
| 85 | + } |
| 86 | + p = filepath.Clean(p) |
| 87 | + |
| 88 | + pPath := p |
| 89 | + isExclusion := false |
| 90 | + if p[0] == '!' { |
| 91 | + pPath = p[1:] |
| 92 | + isExclusion = true |
| 93 | + } |
| 94 | + |
| 95 | + if !filepath.IsAbs(pPath) { |
| 96 | + pPath = filepath.Join(absRoot, pPath) |
| 97 | + } |
| 98 | + absPattern := pPath |
| 99 | + if isExclusion { |
| 100 | + absPattern = fmt.Sprintf("!%s", pPath) |
| 101 | + } |
| 102 | + absPatterns = append(absPatterns, absPattern) |
| 103 | + } |
| 104 | + return absPatterns |
| 105 | +} |
| 106 | + |
| 107 | +func NewDockerPatternMatcher(repoRoot string, patterns []string) (*dockerPathMatcher, error) { |
| 108 | + absRoot, err := filepath.Abs(repoRoot) |
| 109 | + if err != nil { |
| 110 | + return nil, err |
| 111 | + } |
| 112 | + |
| 113 | + pm, err := patternmatcher.New(absPatterns(absRoot, patterns)) |
| 114 | + if err != nil { |
| 115 | + return nil, err |
| 116 | + } |
| 117 | + |
| 118 | + return &dockerPathMatcher{ |
| 119 | + repoRoot: absRoot, |
| 120 | + matcher: pm, |
| 121 | + }, nil |
| 122 | +} |
| 123 | + |
| 124 | +func readDockerignorePatterns(repoRoot string) ([]string, error) { |
| 125 | + var excludes []string |
| 126 | + |
| 127 | + f, err := os.Open(filepath.Join(repoRoot, ".dockerignore")) |
| 128 | + switch { |
| 129 | + case os.IsNotExist(err): |
| 130 | + return excludes, nil |
| 131 | + case err != nil: |
| 132 | + return nil, err |
| 133 | + } |
| 134 | + defer func() { _ = f.Close() }() |
| 135 | + |
| 136 | + return dockerignore.ReadAll(f) |
| 137 | +} |
| 138 | + |
| 139 | +func DockerIgnoreTesterFromContents(repoRoot string, contents string) (*dockerPathMatcher, error) { |
| 140 | + patterns, err := dockerignore.ReadAll(strings.NewReader(contents)) |
| 141 | + if err != nil { |
| 142 | + return nil, err |
| 143 | + } |
| 144 | + |
| 145 | + return NewDockerPatternMatcher(repoRoot, patterns) |
| 146 | +} |
0 commit comments