-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxfg_process.go
281 lines (244 loc) · 6.43 KB
/
xfg_process.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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
package main
import (
"errors"
"fmt"
"io/fs"
"os"
"path/filepath"
"strings"
"golang.org/x/sync/errgroup"
"github.com/bayashi/xfg/internal/xfgignore"
"github.com/bayashi/xfg/internal/xfglangxt"
"github.com/monochromegane/go-gitignore"
)
func (x *xfg) process() error {
if err := x.preWalkDir(); err != nil {
return fmt.Errorf("preWalkDir() : %w", err)
}
if x.options.Stats {
x.cli.stats.Mark("preWalkDir")
}
eg := new(errgroup.Group)
for _, startDir := range x.options.SearchStart {
startDir := startDir
eg.Go(func() error {
ms := x.initIgnoreMatchers(startDir)
var maxDepth uint32 = 1
x.walkDir(eg, startDir, ms, maxDepth)
return nil
})
}
if err := eg.Wait(); err != nil {
return fmt.Errorf("walkDir Wait : %w", err)
}
return nil
}
func (x *xfg) walkDir(eg *errgroup.Group, dirPath string, ms xfgignore.Matchers, maxDepth uint32) {
eg.Go(func() error {
if maxDepth > x.options.MaxDepth {
return nil
} else {
maxDepth++
}
if !x.options.SkipGitIgnore {
if matcher, err := gitignore.NewGitIgnore(filepath.Join(dirPath, xfgignore.GITIGNORE_FILE_NAME)); err == nil {
ms = append(ms, matcher)
}
}
if x.options.Quiet && x.hasMatchedAny() {
return nil // already match. skip after all
}
stuff, err := os.ReadDir(dirPath)
if err != nil {
if errors.Is(err, fs.ErrPermission) {
if !x.options.IgnorePermissionError {
x.cli.putErr(err)
}
return nil
}
return err
}
x.walkStuff(stuff, eg, dirPath, ms, maxDepth)
return nil
})
}
func (x *xfg) walkStuff(stuff []fs.DirEntry, eg *errgroup.Group, dirPath string, ms xfgignore.Matchers, maxDepth uint32) {
for _, s := range stuff {
if x.options.Quiet && x.hasMatchedAny() {
break // already match. skip after all
}
if !x.options.SearchAll && !x.options.SearchDefaultSkipStuff {
if (!x.options.NoDefaultSkip && isDefaultSkipDir(s)) ||
(s.IsDir() && !x.options.Hidden && strings.HasPrefix(s.Name(), ".")) {
continue // skip all stuff in this dir
}
}
if s.IsDir() {
p := filepath.Join(dirPath, s.Name())
if !x.options.SearchAll && x.isSkippableByIgnoreFile(p, ms) {
continue // skip all stuff in this dir
}
x.walkDir(eg, p, ms, maxDepth) // recursively
}
x.walkFile(filepath.Join(dirPath, s.Name()), s, ms)
}
}
func (x *xfg) walkFile(fPath string, fInfo fs.DirEntry, ms xfgignore.Matchers) error {
if x.options.Stats {
x.cli.stats.IncrWalkedPaths()
}
if x.isSkippablePath(fPath, fInfo, ms) {
return nil
}
if x.options.Stats {
x.cli.stats.IncrWalkedContents()
}
return x.postMatchPath(fPath, fInfo)
}
func (x *xfg) isMatchExt(fInfo fs.DirEntry, extensions []string) bool {
for _, ext := range extensions {
if !strings.HasPrefix(ext, ".") {
ext = "." + ext
}
if strings.HasSuffix(fInfo.Name(), ext) {
return true
}
}
return false
}
func (x *xfg) isLangFile(fInfo fs.DirEntry) bool {
for _, l := range x.options.Lang {
if xfglangxt.IsLangFile(l, fInfo.Name()) {
return true
}
}
return false
}
func (x *xfg) isMatchFileType(fPath string, fInfo fs.DirEntry) bool {
switch x.options.Type {
case "d", "directory":
return fInfo.IsDir()
case "l", "symlink":
return (fInfo.Type() & fs.ModeSymlink) == fs.ModeSymlink
case "x", "executable":
if fInfo.IsDir() || (fInfo.Type()&fs.ModeSymlink) == fs.ModeSymlink {
return false
}
i, err := fInfo.Info()
if err != nil {
return false // trap error
}
return (i.Mode() & 0111) == 0111
case "e", "empty":
if fInfo.IsDir() {
d, err := os.ReadDir(fPath)
if err != nil {
return false // trap error
}
return len(d) == 0
} else {
i, err := fInfo.Info()
if err != nil {
return false // trap error
}
return i.Size() == 0
}
case "s", "socket":
return (fInfo.Type() & fs.ModeSocket) == fs.ModeSocket
case "p", "pipe":
return (fInfo.Type() & fs.ModeNamedPipe) == fs.ModeNamedPipe
case "b", "block-device":
return (fInfo.Type() & fs.ModeDevice) == fs.ModeDevice
case "c", "char-device":
return (fInfo.Type() & fs.ModeCharDevice) == fs.ModeCharDevice
default:
panic("not support type") // unreachable here though
}
}
func (x *xfg) isSkippablePath(fPath string, fInfo fs.DirEntry, ms xfgignore.Matchers) bool {
if !x.options.SearchAll {
if (len(x.options.Ext) > 0 && !x.isMatchExt(fInfo, x.options.Ext)) ||
(len(x.options.Lang) > 0 && !x.isLangFile(fInfo)) ||
(x.options.Type != "" && !x.isMatchFileType(fPath, fInfo)) {
return true
}
}
if fInfo.IsDir() && x.options.extra.onlyMatchContent {
return true // Just not pick up only this dir path. It will be searched files and directories in this dir.
}
if x.isIgnorePath(fPath) {
return true
}
if !x.options.SearchAll && !x.options.SearchDefaultSkipStuff {
if (!x.options.NoDefaultSkip && isDefaultSkipFile(fInfo)) ||
(!x.options.Hidden && strings.HasPrefix(fInfo.Name(), ".")) ||
x.isSkippableByIgnoreFile(fPath, ms) {
return true
}
}
return x.canSkipPath(fPath, fInfo)
}
func (x *xfg) isSkippableByIgnoreFile(fPath string, ms xfgignore.Matchers) bool {
if len(ms) > 0 {
for _, im := range ms {
if im.Match(fPath, false) {
return true
}
}
}
return false
}
func (x *xfg) isIgnorePath(fPath string) bool {
if x.options.IgnoreCase {
for _, re := range x.extra.ignoreOptionRe {
if isMatchRegexp(fPath, re) {
return true // ignore
}
}
} else {
for _, i := range x.options.Ignore {
if isMatch(fPath, i) {
return true // ignore
}
}
}
return false
}
func (x *xfg) canSkipPath(fPath string, fInfo fs.DirEntry) bool {
if x.options.SearchOnlyName {
return x._canSkipPath(fInfo.Name())
}
return x._canSkipPath(fPath)
}
func (x *xfg) _canSkipPath(fPath string) bool {
if x.options.IgnoreCase && len(x.extra.searchPathi) > 0 {
for _, spr := range x.extra.searchPathi {
if !isMatchRegexp(fPath, spr) {
return true // OK, skip
}
}
} else if len(x.options.SearchPath) > 0 {
for _, sp := range x.options.SearchPath {
if !isMatch(fPath, sp) {
return true // OK, skip
}
}
}
if len(x.extra.searchPathRe) > 0 {
for _, re := range x.extra.searchPathRe {
if !isMatchRegexp(fPath, re) {
return true // OK, skip
}
}
}
return false // match all, cannot skip
}
func (x *xfg) hasMatchedAny() bool {
x.result.mu.RLock()
defer x.result.mu.RUnlock()
if (len(x.options.SearchGrep) == 0 && len(x.result.paths) > 0) ||
(len(x.options.SearchGrep) > 0 && len(x.result.paths) > 0 && x.result.alreadyMatchContent) {
return true // already match
}
return false
}