|
| 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 | +) |
| 27 | + |
| 28 | +type dockerPathMatcher struct { |
| 29 | + repoRoot string |
| 30 | + matcher *PatternMatcher |
| 31 | +} |
| 32 | + |
| 33 | +func (i dockerPathMatcher) Matches(f string) (bool, error) { |
| 34 | + if !filepath.IsAbs(f) { |
| 35 | + f = filepath.Join(i.repoRoot, f) |
| 36 | + } |
| 37 | + return i.matcher.Matches(f) |
| 38 | +} |
| 39 | + |
| 40 | +func (i dockerPathMatcher) MatchesEntireDir(f string) (bool, error) { |
| 41 | + matches, err := i.Matches(f) |
| 42 | + if !matches || err != nil { |
| 43 | + return matches, err |
| 44 | + } |
| 45 | + |
| 46 | + // We match the dir, but we might exclude files underneath it. |
| 47 | + if i.matcher.Exclusions() { |
| 48 | + for _, pattern := range i.matcher.Patterns() { |
| 49 | + if !pattern.Exclusion() { |
| 50 | + continue |
| 51 | + } |
| 52 | + if IsChild(f, pattern.String()) { |
| 53 | + // Found an exclusion match -- we don't match this whole dir |
| 54 | + return false, nil |
| 55 | + } |
| 56 | + } |
| 57 | + return true, nil |
| 58 | + } |
| 59 | + return true, nil |
| 60 | +} |
| 61 | + |
| 62 | +func NewDockerIgnoreTester(repoRoot string) (*dockerPathMatcher, error) { |
| 63 | + absRoot, err := filepath.Abs(repoRoot) |
| 64 | + if err != nil { |
| 65 | + return nil, err |
| 66 | + } |
| 67 | + |
| 68 | + patterns, err := readDockerignorePatterns(absRoot) |
| 69 | + if err != nil { |
| 70 | + return nil, err |
| 71 | + } |
| 72 | + |
| 73 | + return NewDockerPatternMatcher(absRoot, patterns) |
| 74 | +} |
| 75 | + |
| 76 | +// Make all the patterns use absolute paths. |
| 77 | +func absPatterns(absRoot string, patterns []string) []string { |
| 78 | + absPatterns := make([]string, 0, len(patterns)) |
| 79 | + for _, p := range patterns { |
| 80 | + // The pattern parsing here is loosely adapted from fileutils' NewPatternMatcher |
| 81 | + p = strings.TrimSpace(p) |
| 82 | + if p == "" { |
| 83 | + continue |
| 84 | + } |
| 85 | + p = filepath.Clean(p) |
| 86 | + |
| 87 | + pPath := p |
| 88 | + isExclusion := false |
| 89 | + if p[0] == '!' { |
| 90 | + pPath = p[1:] |
| 91 | + isExclusion = true |
| 92 | + } |
| 93 | + |
| 94 | + if !filepath.IsAbs(pPath) { |
| 95 | + pPath = filepath.Join(absRoot, pPath) |
| 96 | + } |
| 97 | + absPattern := pPath |
| 98 | + if isExclusion { |
| 99 | + absPattern = fmt.Sprintf("!%s", pPath) |
| 100 | + } |
| 101 | + absPatterns = append(absPatterns, absPattern) |
| 102 | + } |
| 103 | + return absPatterns |
| 104 | +} |
| 105 | + |
| 106 | +func NewDockerPatternMatcher(repoRoot string, patterns []string) (*dockerPathMatcher, error) { |
| 107 | + absRoot, err := filepath.Abs(repoRoot) |
| 108 | + if err != nil { |
| 109 | + return nil, err |
| 110 | + } |
| 111 | + |
| 112 | + pm, err := NewPatternMatcher(absPatterns(absRoot, patterns)) |
| 113 | + if err != nil { |
| 114 | + return nil, err |
| 115 | + } |
| 116 | + |
| 117 | + return &dockerPathMatcher{ |
| 118 | + repoRoot: absRoot, |
| 119 | + matcher: pm, |
| 120 | + }, nil |
| 121 | +} |
| 122 | + |
| 123 | +func readDockerignorePatterns(repoRoot string) ([]string, error) { |
| 124 | + var excludes []string |
| 125 | + |
| 126 | + f, err := os.Open(filepath.Join(repoRoot, ".dockerignore")) |
| 127 | + switch { |
| 128 | + case os.IsNotExist(err): |
| 129 | + return excludes, nil |
| 130 | + case err != nil: |
| 131 | + return nil, err |
| 132 | + } |
| 133 | + defer func() { _ = f.Close() }() |
| 134 | + |
| 135 | + return dockerignore.ReadAll(f) |
| 136 | +} |
| 137 | + |
| 138 | +func DockerIgnoreTesterFromContents(repoRoot string, contents string) (*dockerPathMatcher, error) { |
| 139 | + patterns, err := dockerignore.ReadAll(strings.NewReader(contents)) |
| 140 | + if err != nil { |
| 141 | + return nil, err |
| 142 | + } |
| 143 | + |
| 144 | + return NewDockerPatternMatcher(repoRoot, patterns) |
| 145 | +} |
0 commit comments