| 1 |
package gitignore |
| 2 |
|
| 3 |
import ( |
| 4 |
"path/filepath" |
| 5 |
"strings" |
| 6 |
) |
| 7 |
|
| 8 |
// MatchResult defines outcomes of a match, no match, exclusion or inclusion. |
| 9 |
type MatchResult int |
| 10 |
|
| 11 |
const ( |
| 12 |
// NoMatch defines the no match outcome of a match check |
| 13 |
NoMatch MatchResult = iota |
| 14 |
// Exclude defines an exclusion of a file as a result of a match check |
| 15 |
Exclude |
| 16 |
// Include defines an explicit inclusion of a file as a result of a match check |
| 17 |
Include |
| 18 |
) |
| 19 |
|
| 20 |
const ( |
| 21 |
inclusionPrefix = "!" |
| 22 |
zeroToManyDirs = "**" |
| 23 |
patternDirSep = "/" |
| 24 |
) |
| 25 |
|
| 26 |
// Pattern defines a single gitignore pattern. |
| 27 |
type Pattern interface { |
| 28 |
// Match matches the given path to the pattern. |
| 29 |
Match(path []string, isDir bool) MatchResult |
| 30 |
} |
| 31 |
|
| 32 |
type pattern struct { |
| 33 |
domain []string |
| 34 |
pattern []string |
| 35 |
inclusion bool |
| 36 |
dirOnly bool |
| 37 |
isGlob bool |
| 38 |
} |
| 39 |
|
| 40 |
// ParsePattern parses a gitignore pattern string into the Pattern structure. |
| 41 |
func ParsePattern(p string, domain []string) Pattern { |
| 42 |
// storing domain, copy it to ensure it isn't changed externally |
| 43 |
domain = append([]string(nil), domain...) |
| 44 |
res := pattern{domain: domain} |
| 45 |
|
| 46 |
if strings.HasPrefix(p, inclusionPrefix) { |
| 47 |
res.inclusion = true |
| 48 |
p = p[1:] |
| 49 |
} |
| 50 |
|
| 51 |
if !strings.HasSuffix(p, "\\ ") { |
| 52 |
p = strings.TrimRight(p, " ") |
| 53 |
} |
| 54 |
|
| 55 |
if strings.HasSuffix(p, patternDirSep) { |
| 56 |
res.dirOnly = true |
| 57 |
p = p[:len(p)-1] |
| 58 |
} |
| 59 |
|
| 60 |
if strings.Contains(p, patternDirSep) { |
| 61 |
res.isGlob = true |
| 62 |
} |
| 63 |
|
| 64 |
res.pattern = strings.Split(p, patternDirSep) |
| 65 |
return &res |
| 66 |
} |
| 67 |
|
| 68 |
func (p *pattern) Match(path []string, isDir bool) MatchResult { |
| 69 |
if len(path) <= len(p.domain) { |
| 70 |
return NoMatch |
| 71 |
} |
| 72 |
for i, e := range p.domain { |
| 73 |
if path[i] != e { |
| 74 |
return NoMatch |
| 75 |
} |
| 76 |
} |
| 77 |
|
| 78 |
path = path[len(p.domain):] |
| 79 |
if p.isGlob && !p.globMatch(path, isDir) { |
| 80 |
return NoMatch |
| 81 |
} else if !p.isGlob && !p.simpleNameMatch(path, isDir) { |
| 82 |
return NoMatch |
| 83 |
} |
| 84 |
|
| 85 |
if p.inclusion { |
| 86 |
return Include |
| 87 |
} else { |
| 88 |
return Exclude |
| 89 |
} |
| 90 |
} |
| 91 |
|
| 92 |
func (p *pattern) simpleNameMatch(path []string, isDir bool) bool { |
| 93 |
for i, name := range path { |
| 94 |
if match, err := filepath.Match(p.pattern[0], name); err != nil { |
| 95 |
return false |
| 96 |
} else if !match { |
| 97 |
continue |
| 98 |
} |
| 99 |
if p.dirOnly && !isDir && i == len(path)-1 { |
| 100 |
return false |
| 101 |
} |
| 102 |
return true |
| 103 |
} |
| 104 |
return false |
| 105 |
} |
| 106 |
|
| 107 |
func (p *pattern) globMatch(path []string, isDir bool) bool { |
| 108 |
matched := false |
| 109 |
canTraverse := false |
| 110 |
for i, pattern := range p.pattern { |
| 111 |
if pattern == "" { |
| 112 |
canTraverse = false |
| 113 |
continue |
| 114 |
} |
| 115 |
if pattern == zeroToManyDirs { |
| 116 |
if i == len(p.pattern)-1 { |
| 117 |
break |
| 118 |
} |
| 119 |
canTraverse = true |
| 120 |
continue |
| 121 |
} |
| 122 |
if strings.Contains(pattern, zeroToManyDirs) { |
| 123 |
return false |
| 124 |
} |
| 125 |
if len(path) == 0 { |
| 126 |
return false |
| 127 |
} |
| 128 |
if canTraverse { |
| 129 |
canTraverse = false |
| 130 |
for len(path) > 0 { |
| 131 |
e := path[0] |
| 132 |
path = path[1:] |
| 133 |
if match, err := filepath.Match(pattern, e); err != nil { |
| 134 |
return false |
| 135 |
} else if match { |
| 136 |
matched = true |
| 137 |
break |
| 138 |
} else if len(path) == 0 { |
| 139 |
// if nothing left then fail |
| 140 |
matched = false |
| 141 |
} |
| 142 |
} |
| 143 |
} else { |
| 144 |
if match, err := filepath.Match(pattern, path[0]); err != nil || !match { |
| 145 |
return false |
| 146 |
} |
| 147 |
matched = true |
| 148 |
path = path[1:] |
| 149 |
} |
| 150 |
} |
| 151 |
if matched && p.dirOnly && !isDir && len(path) == 0 { |
| 152 |
matched = false |
| 153 |
} |
| 154 |
return matched |
| 155 |
} |