-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfilter.go
92 lines (78 loc) · 1.97 KB
/
filter.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
package main
import (
"log"
"os"
"regexp"
)
var ignoredFiles = make(map[string]bool)
var notIgnoredFiles = make(map[string]bool)
func IgnoreFile(filePath string) bool {
if !setIgnoreRegexp {
return false
}
if _, exist := notIgnoredFiles[filePath]; exist {
return false
}
if _, exist := ignoredFiles[filePath]; exist {
return true
}
if IgnoreFileByPath(filePath) || IgnoreFileByContent(filePath) {
ignoredFiles[filePath] = true
return true
} else {
notIgnoredFiles[filePath] = true
return false
}
}
var ignoreFileContentRegexp *regexp.Regexp
var ignoreFilePathRegexp *regexp.Regexp
var setIgnoreRegexp bool
func InitIgnoreRegexBeforeAnalyze() {
if *verbose {
log.Printf("--Ignore file path regex is: %s\n", defaultVal(*ignoreFilePathExpr, "<not_set>"))
log.Printf("--Ignore file content regex is: %s\n", defaultVal(*ignoreFileContentExpr, "<not_set>"))
}
ignoreFilePathRegexp = prepareRegexp(*ignoreFilePathExpr)
ignoreFileContentRegexp = prepareRegexp(*ignoreFileContentExpr)
if ignoreFileContentRegexp != nil || ignoreFilePathRegexp != nil {
setIgnoreRegexp = true
}
}
func IgnoreFileByPath(filePath string) bool {
ignore := ignoreFilePathRegexp != nil && ignoreFilePathRegexp.MatchString(filePath)
if *verbose && ignore {
log.Printf("IgnoreFileByPath: %s\n", filePath)
}
return ignore
}
func IgnoreFileByContent(filePath string) bool {
if ignoreFileContentRegexp == nil {
return false
}
contentBytes, err := os.ReadFile(filePath)
if err != nil {
log.Printf("读取文件时出错:%s, %v\n", filePath, err)
return false
}
ignore := ignoreFileContentRegexp.MatchString(string(contentBytes))
if *verbose && ignore {
log.Printf("IgnoreFileByContent: %s\n", filePath)
}
return ignore
}
func defaultVal(val, def string) string {
if val != "" {
return val
}
return def
}
func prepareRegexp(expr string) *regexp.Regexp {
if expr == "" {
return nil
}
ret, err := regexp.Compile(expr)
if err != nil {
log.Fatal(err)
}
return ret
}