-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpattern.go
51 lines (39 loc) · 1.04 KB
/
pattern.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package gitlabcodeowners
import (
"fmt"
"regexp"
"strings"
doublestar "github.com/bmatcuk/doublestar/v4"
)
type pattern struct {
value string
normalized string
}
func (p pattern) match(path string) bool {
matched, err := doublestar.Match(p.normalized, path)
return err == nil && matched
}
func newPattern(value string) pattern {
return pattern{
value: value,
normalized: normalizePattern(value),
}
}
func normalizePattern(pattern string) string {
if pattern == "*" {
return "/**/*"
}
// remove `\` when escaping `\#`
pattern = regexp.MustCompile(`^\\#`).ReplaceAllString(pattern, "#")
// replace all whitespace preceded by a `\` with a regular whitespace
pattern = regexp.MustCompile(`\\\s+`).ReplaceAllString(pattern, " ")
// add `/**/` before pattern if it is a relative pattern
if !strings.HasPrefix(pattern, "/") {
pattern = fmt.Sprintf("/**/%s", pattern)
}
// add `**/*` after pattern if it is a directory
if strings.HasSuffix(pattern, "/") {
pattern = fmt.Sprintf("%s**/*", pattern)
}
return pattern
}