| 1 | // Copyright 2019 The Kubernetes Authors. |
| 2 | // SPDX-License-Identifier: Apache-2.0 |
| 3 | |
| 4 | package kio |
| 5 | |
| 6 | import ( |
| 7 | "errors" |
| 8 | "os" |
| 9 | "path/filepath" |
| 10 | "strings" |
| 11 | |
| 12 | gitignore "github.com/monochromegane/go-gitignore" |
| 13 | "sigs.k8s.io/kustomize/kyaml/ext" |
| 14 | "sigs.k8s.io/kustomize/kyaml/filesys" |
| 15 | ) |
| 16 | |
| 17 | // ignoreFilesMatcher handles `.krmignore` files, which allows for ignoring |
| 18 | // files or folders in a package. The format of this file is a subset of the |
| 19 | // gitignore format, with recursive patterns (like a/**/c) not supported. If a |
| 20 | // file or folder matches any of the patterns in the .krmignore file for the |
| 21 | // package, it will be excluded. |
| 22 | // |
| 23 | // It works as follows: |
| 24 | // |
| 25 | // * It will look for .krmignore file in the top folder and on the top level |
| 26 | // of any subpackages. Subpackages are defined by the presence of a Krmfile |
| 27 | // in the folder. |
| 28 | // * `.krmignore` files only cover files and folders for the package in which |
| 29 | // it is defined. So ignore patterns defined in a parent package does not |
| 30 | // affect which files are ignored from a subpackage. |
| 31 | // * An ignore pattern can not ignore a subpackage. So even if the parent |
| 32 | // package contains a pattern that ignores the directory foo, if foo is a |
| 33 | // subpackage, it will still be included if the IncludeSubpackages property |
| 34 | // is set to true |
| 35 | type ignoreFilesMatcher struct { |
| 36 | matchers []matcher |
| 37 | fs filesys.FileSystemOrOnDisk |
| 38 | } |
| 39 | |
| 40 | // readIgnoreFile checks whether there is a .krmignore file in the path, and |
| 41 | // if it is, reads it in and turns it into a matcher. If we can't find a file, |
| 42 | // we just add a matcher that match nothing. |
| 43 | func (i *ignoreFilesMatcher) readIgnoreFile(path string) error { |
| 44 | i.verifyPath(path) |
| 45 | f, err := i.fs.Open(filepath.Join(path, ext.IgnoreFileName())) |
| 46 | if err != nil { |
| 47 | if errors.Is(err, os.ErrNotExist) { |
| 48 | i.matchers = append(i.matchers, matcher{ |
| 49 | matcher: gitignore.DummyIgnoreMatcher(false), |
| 50 | basePath: path, |
| 51 | }) |
| 52 | return nil |
| 53 | } |
| 54 | return err |
| 55 | } |
| 56 | defer f.Close() |
| 57 | |
| 58 | i.matchers = append(i.matchers, matcher{ |
| 59 | matcher: gitignore.NewGitIgnoreFromReader(path, f), |
| 60 | basePath: path, |
| 61 | }) |
| 62 | return nil |
| 63 | } |
| 64 | |
| 65 | // verifyPath checks whether the top matcher on the stack |
| 66 | // is correct for the provided filepath. Matchers are removed once |
| 67 | // we encounter a filepath that is not a subpath of the basepath for |
| 68 | // the matcher. |
| 69 | func (i *ignoreFilesMatcher) verifyPath(path string) { |
| 70 | for j := len(i.matchers) - 1; j >= 0; j-- { |
| 71 | matcher := i.matchers[j] |
| 72 | if strings.HasPrefix(path, matcher.basePath) || path == matcher.basePath { |
| 73 | i.matchers = i.matchers[:j+1] |
| 74 | return |
| 75 | } |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | // matchFile checks whether the file given by the provided path matches |
| 80 | // any of the patterns in the .krmignore file for the package. |
| 81 | func (i *ignoreFilesMatcher) matchFile(path string) bool { |
| 82 | if len(i.matchers) == 0 { |
| 83 | return false |
| 84 | } |
| 85 | i.verifyPath(filepath.Dir(path)) |
| 86 | return i.matchers[len(i.matchers)-1].matcher.Match(path, false) |
| 87 | } |
| 88 | |
| 89 | // matchDir checks whether the directory given by the provided path matches |
| 90 | // any of the patterns in the .krmignore file for the package. |
| 91 | func (i *ignoreFilesMatcher) matchDir(path string) bool { |
| 92 | if len(i.matchers) == 0 { |
| 93 | return false |
| 94 | } |
| 95 | i.verifyPath(path) |
| 96 | return i.matchers[len(i.matchers)-1].matcher.Match(path, true) |
| 97 | } |
| 98 | |
| 99 | // matcher wraps the gitignore matcher and the path to the folder |
| 100 | // where the file was found. |
| 101 | type matcher struct { |
| 102 | matcher gitignore.IgnoreMatcher |
| 103 | |
| 104 | basePath string |
| 105 | } |