| 1 |
// Copyright 2019 The go-git authors |
| 2 |
// |
| 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 |
// |
| 7 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
// |
| 9 |
// Unless required by applicable law or agreed to in writing, software |
| 10 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 |
// See the License for the specific language governing permissions and |
| 13 |
// limitations under the License. |
| 14 |
// |
| 15 |
// This file was originally part of https://github.com/go-git/go-git/blob/main/plumbing/format/gitattributes |
| 16 |
|
| 17 |
package gitignore |
| 18 |
|
| 19 |
import ( |
| 20 |
"path/filepath" |
| 21 |
"strings" |
| 22 |
) |
| 23 |
|
| 24 |
// MatchResult defines outcomes of a match, no match, exclusion or inclusion. |
| 25 |
type MatchResult int |
| 26 |
|
| 27 |
const ( |
| 28 |
// NoMatch defines the no match outcome of a match check |
| 29 |
NoMatch MatchResult = iota |
| 30 |
// Exclude defines an exclusion of a file as a result of a match check |
| 31 |
Exclude |
| 32 |
// Include defines an explicit inclusion of a file as a result of a match check |
| 33 |
Include |
| 34 |
) |
| 35 |
|
| 36 |
const ( |
| 37 |
inclusionPrefix = "!" |
| 38 |
zeroToManyDirs = "**" |
| 39 |
patternDirSep = "/" |
| 40 |
) |
| 41 |
|
| 42 |
// Pattern defines a single gitignore pattern. |
| 43 |
type Pattern interface { |
| 44 |
// Match matches the given path to the pattern. |
| 45 |
Match(path []string, isDir bool) MatchResult |
| 46 |
} |
| 47 |
|
| 48 |
type pattern struct { |
| 49 |
domain []string |
| 50 |
pattern []string |
| 51 |
inclusion bool |
| 52 |
dirOnly bool |
| 53 |
isGlob bool |
| 54 |
} |
| 55 |
|
| 56 |
// ParsePattern parses a gitignore pattern string into the Pattern structure. |
| 57 |
func ParsePattern(p string, domain []string) Pattern { |
| 58 |
// storing domain, copy it to ensure it isn't changed externally |
| 59 |
domain = append([]string(nil), domain...) |
| 60 |
res := pattern{domain: domain} |
| 61 |
|
| 62 |
if strings.HasPrefix(p, inclusionPrefix) { |
| 63 |
res.inclusion = true |
| 64 |
p = p[1:] |
| 65 |
} |
| 66 |
|
| 67 |
if !strings.HasSuffix(p, "\\ ") { |
| 68 |
p = strings.TrimRight(p, " ") |
| 69 |
} |
| 70 |
|
| 71 |
if strings.HasSuffix(p, patternDirSep) { |
| 72 |
res.dirOnly = true |
| 73 |
p = p[:len(p)-1] |
| 74 |
} |
| 75 |
|
| 76 |
if strings.Contains(p, patternDirSep) { |
| 77 |
res.isGlob = true |
| 78 |
} |
| 79 |
|
| 80 |
res.pattern = strings.Split(p, patternDirSep) |
| 81 |
return &res |
| 82 |
} |
| 83 |
|
| 84 |
func (p *pattern) Match(path []string, isDir bool) MatchResult { |
| 85 |
if len(path) <= len(p.domain) { |
| 86 |
return NoMatch |
| 87 |
} |
| 88 |
for i, e := range p.domain { |
| 89 |
if path[i] != e { |
| 90 |
return NoMatch |
| 91 |
} |
| 92 |
} |
| 93 |
|
| 94 |
path = path[len(p.domain):] |
| 95 |
if p.isGlob && !p.globMatch(path, isDir) { |
| 96 |
return NoMatch |
| 97 |
} else if !p.isGlob && !p.simpleNameMatch(path, isDir) { |
| 98 |
return NoMatch |
| 99 |
} |
| 100 |
|
| 101 |
if p.inclusion { |
| 102 |
return Include |
| 103 |
} else { |
| 104 |
return Exclude |
| 105 |
} |
| 106 |
} |
| 107 |
|
| 108 |
func (p *pattern) simpleNameMatch(path []string, isDir bool) bool { |
| 109 |
for i, name := range path { |
| 110 |
if match, err := filepath.Match(p.pattern[0], name); err != nil { |
| 111 |
return false |
| 112 |
} else if !match { |
| 113 |
continue |
| 114 |
} |
| 115 |
if p.dirOnly && !isDir && i == len(path)-1 { |
| 116 |
return false |
| 117 |
} |
| 118 |
return true |
| 119 |
} |
| 120 |
return false |
| 121 |
} |
| 122 |
|
| 123 |
func (p *pattern) globMatch(path []string, isDir bool) bool { |
| 124 |
matched := false |
| 125 |
canTraverse := false |
| 126 |
for i, pattern := range p.pattern { |
| 127 |
if pattern == "" { |
| 128 |
canTraverse = false |
| 129 |
continue |
| 130 |
} |
| 131 |
if pattern == zeroToManyDirs { |
| 132 |
if i == len(p.pattern)-1 { |
| 133 |
break |
| 134 |
} |
| 135 |
canTraverse = true |
| 136 |
continue |
| 137 |
} |
| 138 |
if strings.Contains(pattern, zeroToManyDirs) { |
| 139 |
return false |
| 140 |
} |
| 141 |
if len(path) == 0 { |
| 142 |
return false |
| 143 |
} |
| 144 |
if canTraverse { |
| 145 |
canTraverse = false |
| 146 |
for len(path) > 0 { |
| 147 |
e := path[0] |
| 148 |
path = path[1:] |
| 149 |
if match, err := filepath.Match(pattern, e); err != nil { |
| 150 |
return false |
| 151 |
} else if match { |
| 152 |
matched = true |
| 153 |
break |
| 154 |
} else if len(path) == 0 { |
| 155 |
// if nothing left then fail |
| 156 |
matched = false |
| 157 |
} |
| 158 |
} |
| 159 |
} else { |
| 160 |
if match, err := filepath.Match(pattern, path[0]); err != nil || !match { |
| 161 |
return false |
| 162 |
} |
| 163 |
matched = true |
| 164 |
path = path[1:] |
| 165 |
// files matching dir globs, don't match |
| 166 |
if len(path) == 0 && i < len(p.pattern)-1 { |
| 167 |
matched = false |
| 168 |
} |
| 169 |
} |
| 170 |
} |
| 171 |
if matched && p.dirOnly && !isDir && len(path) == 0 { |
| 172 |
matched = false |
| 173 |
} |
| 174 |
return matched |
| 175 |
} |