dockerignore.go 3.4 KB

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