-
Notifications
You must be signed in to change notification settings - Fork 81
/
utils.go
166 lines (145 loc) · 3.59 KB
/
utils.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
package gocloc
import (
"crypto/md5"
"fmt"
"os"
"path/filepath"
"strings"
)
func trimBOM(line string) string {
l := len(line)
if l >= 3 {
if line[0] == 0xef && line[1] == 0xbb && line[2] == 0xbf {
trimLine := line[3:]
return trimLine
}
}
return line
}
func containsComment(line string, multiLines [][]string) bool {
for _, comments := range multiLines {
for _, comm := range comments {
if strings.Contains(line, comm) {
return true
}
}
}
return false
}
func nextRune(s string) rune {
for _, r := range s {
return r
}
return 0
}
func checkMD5Sum(path string, fileCache map[string]struct{}) (ignore bool) {
content, err := os.ReadFile(path)
if err != nil {
return true
}
// calc md5sum
hash := md5.Sum(content)
c := fmt.Sprintf("%x", hash)
if _, ok := fileCache[c]; ok {
return true
}
fileCache[c] = struct{}{}
return false
}
func isVCSDir(path string) bool {
if len(path) > 1 && path[0] == os.PathSeparator {
path = path[1:]
}
vcsDirs := []string{".bzr", ".cvs", ".hg", ".git", ".svn"}
for _, dir := range vcsDirs {
if strings.Contains(path, dir) {
return true
}
}
return false
}
func checkDefaultIgnore(path string, info os.FileInfo, isVCS bool) bool {
if info.IsDir() {
// directory is ignored
return true
}
if !isVCS && isVCSDir(path) {
// vcs file or directory is ignored
return true
}
return false
}
func checkOptionMatch(path string, info os.FileInfo, opts *ClocOptions) bool {
// check match directory & file options
if opts.ReNotMatch != nil && opts.ReNotMatch.MatchString(info.Name()) {
return false
}
if opts.ReMatch != nil && !opts.ReMatch.MatchString(info.Name()) {
return false
}
dir := filepath.Dir(path)
if opts.ReNotMatchDir != nil && opts.ReNotMatchDir.MatchString(dir) {
return false
}
if opts.ReMatchDir != nil && !opts.ReMatchDir.MatchString(dir) {
return false
}
return true
}
// getAllFiles return all the files to be analyzed in paths.
func getAllFiles(paths []string, languages *DefinedLanguages, opts *ClocOptions) (result map[string]*Language, err error) {
result = make(map[string]*Language, 0)
fileCache := make(map[string]struct{})
for _, root := range paths {
vcsInRoot := isVCSDir(root)
err = filepath.Walk(root, func(path string, info os.FileInfo, err error) error {
if err != nil {
fmt.Fprintf(os.Stderr, "%s\n", err)
return nil
}
if ignore := checkDefaultIgnore(path, info, vcsInRoot); ignore {
return nil
}
// check match & not-match directory
if match := checkOptionMatch(path, info, opts); !match {
return nil
}
if ext, ok := getFileType(path, opts); ok {
if targetExt, ok := Exts[ext]; ok {
// check exclude extension
if _, ok := opts.ExcludeExts[targetExt]; ok {
return nil
}
if len(opts.IncludeLangs) != 0 {
if _, ok = opts.IncludeLangs[targetExt]; !ok {
return nil
}
}
if !opts.SkipDuplicated {
ignore := checkMD5Sum(path, fileCache)
if ignore {
if opts.Debug {
fmt.Printf("[ignore=%v] find same md5\n", path)
}
return nil
}
}
if _, ok := result[targetExt]; !ok {
definedLang := NewLanguage(
languages.Langs[targetExt].Name,
languages.Langs[targetExt].lineComments,
languages.Langs[targetExt].multiLines,
)
if len(languages.Langs[targetExt].regexLineComments) > 0 {
definedLang.regexLineComments = languages.Langs[targetExt].regexLineComments
}
result[targetExt] = definedLang
}
result[targetExt].Files = append(result[targetExt].Files, path)
}
}
return nil
})
}
return
}