github.com/rakutentech/cli@v6.12.5-0.20151006231303-24468b65536e+incompatible/cf/app_files/cf_ignore.go (about)

     1  package app_files
     2  
     3  import (
     4  	"path"
     5  	"strings"
     6  
     7  	"github.com/cloudfoundry/cli/glob"
     8  )
     9  
    10  type CfIgnore interface {
    11  	FileShouldBeIgnored(path string) bool
    12  }
    13  
    14  func NewCfIgnore(text string) CfIgnore {
    15  	patterns := []ignorePattern{}
    16  	lines := strings.Split(text, "\n")
    17  	lines = append(defaultIgnoreLines, lines...)
    18  
    19  	for _, line := range lines {
    20  		line = strings.TrimSpace(line)
    21  		if line == "" {
    22  			continue
    23  		}
    24  
    25  		ignore := true
    26  		if strings.HasPrefix(line, "!") {
    27  			line = line[1:]
    28  			ignore = false
    29  		}
    30  
    31  		for _, p := range globsForPattern(path.Clean(line)) {
    32  			patterns = append(patterns, ignorePattern{ignore, p})
    33  		}
    34  	}
    35  
    36  	return cfIgnore(patterns)
    37  }
    38  
    39  func (ignore cfIgnore) FileShouldBeIgnored(path string) bool {
    40  	result := false
    41  
    42  	for _, pattern := range ignore {
    43  		if strings.HasPrefix(pattern.glob.String(), "/") && !strings.HasPrefix(path, "/") {
    44  			path = "/" + path
    45  		}
    46  
    47  		if pattern.glob.Match(path) {
    48  			result = pattern.exclude
    49  		}
    50  	}
    51  
    52  	return result
    53  }
    54  
    55  func globsForPattern(pattern string) (globs []glob.Glob) {
    56  	globs = append(globs, glob.MustCompileGlob(pattern))
    57  	globs = append(globs, glob.MustCompileGlob(path.Join(pattern, "*")))
    58  	globs = append(globs, glob.MustCompileGlob(path.Join(pattern, "**", "*")))
    59  
    60  	if !strings.HasPrefix(pattern, "/") {
    61  		globs = append(globs, glob.MustCompileGlob(path.Join("**", pattern)))
    62  		globs = append(globs, glob.MustCompileGlob(path.Join("**", pattern, "*")))
    63  		globs = append(globs, glob.MustCompileGlob(path.Join("**", pattern, "**", "*")))
    64  	}
    65  
    66  	return
    67  }
    68  
    69  type ignorePattern struct {
    70  	exclude bool
    71  	glob    glob.Glob
    72  }
    73  
    74  type cfIgnore []ignorePattern
    75  
    76  var defaultIgnoreLines = []string{
    77  	".cfignore",
    78  	"/manifest.yml",
    79  	".gitignore",
    80  	".git",
    81  	".hg",
    82  	".svn",
    83  	"_darcs",
    84  	".DS_Store",
    85  }