github.com/tilt-dev/tilt@v0.33.15-0.20240515162809-0a22ed45d8a0/internal/ignore/path_matcher.go (about) 1 package ignore 2 3 import ( 4 "path/filepath" 5 6 "github.com/pkg/errors" 7 8 "github.com/tilt-dev/tilt/internal/dockerignore" 9 "github.com/tilt-dev/tilt/internal/ospath" 10 "github.com/tilt-dev/tilt/pkg/apis/core/v1alpha1" 11 "github.com/tilt-dev/tilt/pkg/model" 12 ) 13 14 // Filter out files that should not be included in the build context. 15 func CreateBuildContextFilter(ignores []v1alpha1.IgnoreDef) model.PathMatcher { 16 return model.NewCompositeMatcher(ToMatchersBestEffort(ignores)) 17 } 18 19 // Filter out files that should not trigger new builds. 20 func CreateFileChangeFilter(ignores []v1alpha1.IgnoreDef) model.PathMatcher { 21 return model.NewCompositeMatcher( 22 append(ToMatchersBestEffort(ignores), EphemeralPathMatcher)) 23 } 24 25 // Interpret ignores as a PathMatcher, skipping ignores that are ill-formed. 26 func ToMatchersBestEffort(ignores []v1alpha1.IgnoreDef) []model.PathMatcher { 27 var ignoreMatchers []model.PathMatcher 28 for _, ignoreDef := range ignores { 29 if len(ignoreDef.Patterns) != 0 { 30 m, err := dockerignore.NewDockerPatternMatcher( 31 ignoreDef.BasePath, 32 append([]string{}, ignoreDef.Patterns...)) 33 if err == nil { 34 ignoreMatchers = append(ignoreMatchers, m) 35 } 36 } else { 37 m, err := NewDirectoryMatcher(ignoreDef.BasePath) 38 if err == nil { 39 ignoreMatchers = append(ignoreMatchers, m) 40 } 41 } 42 } 43 return ignoreMatchers 44 } 45 46 type DirectoryMatcher struct { 47 dir string 48 } 49 50 var _ model.PathMatcher = DirectoryMatcher{} 51 52 func NewDirectoryMatcher(dir string) (DirectoryMatcher, error) { 53 dir, err := filepath.Abs(dir) 54 if err != nil { 55 return DirectoryMatcher{}, errors.Wrapf(err, "failed to get abs path of '%s'", dir) 56 } 57 return DirectoryMatcher{dir}, nil 58 } 59 60 func (d DirectoryMatcher) Dir() string { 61 return d.dir 62 } 63 64 func (d DirectoryMatcher) Matches(p string) (bool, error) { 65 return ospath.IsChild(d.dir, p), nil 66 } 67 68 func (d DirectoryMatcher) MatchesEntireDir(p string) (bool, error) { 69 return d.Matches(p) 70 }