forked from boumenot/gocover-cobertura
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathignore.go
61 lines (50 loc) · 1.14 KB
/
ignore.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
52
53
54
55
56
57
58
59
60
61
package main
import (
"path/filepath"
"regexp"
)
// As golint-ci referencing https://golang.org/s/generatedcode, be laxer
var genCodeRe = regexp.MustCompile(`(?im)^//.*(?:code generated|do not edit|autogenerated file)`)
type Ignore struct {
Dirs *regexp.Regexp
Files *regexp.Regexp
GeneratedFiles bool
cache map[string]bool
}
func (i *Ignore) Match(fileName string, data []byte) (ret bool) {
if i.cache == nil {
i.cache = map[string]bool{}
} else if match, exists := i.cache[fileName]; exists {
return match
}
dir := filepath.Dir(fileName)
if i.dirMatch(dir) ||
(i.Files != nil && i.Files.MatchString(fileName)) {
ret = true
} else if i.GeneratedFiles {
if data == nil {
return false // no cache if no content provided
}
if len(data) > 256 {
data = data[:256]
}
ret = genCodeRe.Match(data)
}
i.cache[fileName] = ret
return ret
}
func (i *Ignore) dirMatch(dir string) bool {
if i.Dirs == nil {
return false
}
for {
if i.Dirs.MatchString(dir) {
return true
}
dir, _ = filepath.Split(dir)
if dir == "" {
return false
}
dir = dir[:len(dir)-1] // without last separator
}
}