-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
522 lines (487 loc) · 12.8 KB
/
Copy pathmain.go
File metadata and controls
522 lines (487 loc) · 12.8 KB
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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
// Copyright 2026 Bjørn Erik Pedersen
// SPDX-License-Identifier: Apache-2.0
package main
import (
"bufio"
"bytes"
"context"
"errors"
"flag"
"fmt"
"io"
"io/fs"
"os"
"path/filepath"
"runtime"
"runtime/pprof"
"strings"
"sync"
"github.com/bep/grrep/internal"
"github.com/charlievieth/fastwalk"
"golang.org/x/sync/errgroup"
)
const (
peekSize = 8000
readerBufSize = 1 << 20 // 1 MiB; bufio fallback for files exceeding scanBufSize.
scanBufSize = 1 << 20 // 1 MiB; whole-file pool buffer.
)
var readerPool = sync.Pool{
New: func() any {
return bufio.NewReaderSize(nil, readerBufSize)
},
}
var bufPool = sync.Pool{
New: func() any {
b := make([]byte, scanBufSize)
return &b
},
}
func main() {
found, err := run()
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(2)
}
if !found {
os.Exit(1)
}
}
// trimCR strips a trailing carriage return from line. Combined with the
// per-scan-path \n stripping, this normalizes both LF and CRLF line endings
// so output and regex matches don't see a stray \r at the end.
func trimCR(line []byte) []byte {
if n := len(line); n > 0 && line[n-1] == '\r' {
return line[:n-1]
}
return line
}
func writeProfile(name, path string) {
f, err := os.Create(path)
if err != nil {
fmt.Fprintf(os.Stderr, "create %s profile: %v\n", name, err)
return
}
defer f.Close()
if err := pprof.Lookup(name).WriteTo(f, 0); err != nil {
fmt.Fprintf(os.Stderr, "write %s profile: %v\n", name, err)
}
}
type grepper struct {
m *internal.Matcher
root string
quiet bool
invert bool // -v: emit non-matching lines instead
hidden bool // --hidden: descend into dot-dirs/files (.git is always skipped)
maxDepth int // 0 = unlimited; passed through to fastwalk.Config
ctx context.Context
paths chan string
results chan []byte
ignores *internal.IgnoreSet // nil if --no-ignore
numWorkersDirWalker int
numWorkersFileScanner int
}
func run() (bool, error) {
var (
quiet bool
noIgnore bool
opts internal.MatchOpts
invert bool
hidden bool
maxDepth int
cpuProfile string
memProfile string
mutexProfile string
)
flag.BoolVar(&quiet, "q", false, "quiet: suppress match output")
flag.BoolVar(&noIgnore, "no-ignore", false, "do not respect .gitignore/.ignore files")
flag.BoolVar(&opts.FixedString, "F", false, "treat PATTERN as a fixed string, not a regex")
flag.BoolVar(&opts.CaseInsensitive, "i", false, "case-insensitive match")
flag.BoolVar(&opts.WordBoundary, "w", false, "match only at word boundaries")
flag.BoolVar(&invert, "v", false, "select non-matching lines")
flag.BoolVar(&hidden, "hidden", false, "search hidden files and directories (.git is always skipped)")
flag.IntVar(&maxDepth, "max-depth", -1, "search at most N directory levels (1 = root only, 0 = nothing)")
flag.IntVar(&maxDepth, "d", -1, "") // alias for --max-depth; suppressed in -h, paired with it below
// Hidden profiling flags — registered but suppressed in -h via flag.Usage below.
flag.StringVar(&cpuProfile, "profile-cpu", "", "")
flag.StringVar(&memProfile, "profile-mem", "", "")
flag.StringVar(&mutexProfile, "profile-mutex", "", "")
flag.Usage = func() {
out := flag.CommandLine.Output()
fmt.Fprintln(out, "usage: grrep [-q] [-F] [-i] [-w] [-v] [-d N] [--hidden] [--no-ignore] PATTERN [PATH]")
fmt.Fprintln(out)
fmt.Fprintln(out, "Flags:")
flag.VisitAll(func(f *flag.Flag) {
if strings.HasPrefix(f.Name, "profile-") || f.Usage == "" {
return
}
// Single-character flags get -x, multi-character get --xxx (ripgrep-style).
name := "-" + f.Name
if len(f.Name) > 1 {
name = "--" + f.Name
}
// Pair known short aliases with their long form.
if f.Name == "max-depth" {
name = "-d, " + name + "=N"
}
fmt.Fprintf(out, " %-18s %s\n", name, f.Usage)
})
}
flag.Parse()
args := flag.Args()
if len(args) < 1 {
return false, fmt.Errorf("usage: grrep [-q] [-F] [-i] [-w] [-v] [-d N] [--hidden] [--no-ignore] PATTERN [PATH]")
}
root := "."
if len(args) >= 2 {
root = args[1]
}
if cpuProfile != "" {
f, err := os.Create(cpuProfile)
if err != nil {
return false, fmt.Errorf("create cpu profile: %w", err)
}
defer f.Close()
if err := pprof.StartCPUProfile(f); err != nil {
return false, fmt.Errorf("start cpu profile: %w", err)
}
defer pprof.StopCPUProfile()
}
if mutexProfile != "" {
runtime.SetMutexProfileFraction(1)
defer writeProfile("mutex", mutexProfile)
}
if memProfile != "" {
defer func() {
f, err := os.Create(memProfile)
if err != nil {
fmt.Fprintf(os.Stderr, "create mem profile: %v\n", err)
return
}
defer f.Close()
runtime.GC() // make in-use heap accurate
if err := pprof.WriteHeapProfile(f); err != nil {
fmt.Fprintf(os.Stderr, "write mem profile: %v\n", err)
}
}()
}
m, err := internal.CompileMatcher(args[0], opts)
if err != nil {
return false, err
}
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
eg, gCtx := errgroup.WithContext(ctx)
g := &grepper{
m: m,
root: root,
quiet: quiet,
invert: invert,
hidden: hidden,
maxDepth: maxDepth,
ctx: gCtx,
paths: make(chan string, 256),
results: make(chan []byte, 64),
numWorkersDirWalker: max(runtime.NumCPU()/2, 2),
numWorkersFileScanner: max(runtime.NumCPU()/3, 2),
}
if !noIgnore {
g.ignores = internal.NewIgnoreSet(root)
}
eg.Go(g.walk)
eg.Go(func() error {
var wg sync.WaitGroup
for i := 0; i < g.numWorkersFileScanner; i++ {
wg.Add(1)
go func() {
defer wg.Done()
g.worker()
}()
}
wg.Wait()
close(g.results)
return nil
})
found := false
for buf := range g.results {
found = true
if len(buf) > 0 {
os.Stdout.Write(buf)
}
if g.quiet {
cancel()
break
}
}
return found, eg.Wait()
}
func (g *grepper) walk() error {
defer close(g.paths)
// -d 0 means "search nothing" (matches ripgrep). fastwalk's MaxDepth=0 is the
// unlimited sentinel, so we can't express that through fastwalk — short-circuit.
if g.maxDepth == 0 {
return nil
}
// For N >= 1, fastwalk's MaxDepth=N already aligns with ripgrep's -d N
// (root dir counts as one level in both). Negative means unset → unlimited.
fwMaxDepth := 0
if g.maxDepth > 0 {
fwMaxDepth = g.maxDepth
}
cfg := &fastwalk.Config{NumWorkers: g.numWorkersDirWalker, MaxDepth: fwMaxDepth}
err := fastwalk.Walk(cfg, g.root, func(path string, d fs.DirEntry, err error) error {
if g.ctx.Err() != nil {
return fs.SkipAll
}
if err != nil {
return nil
}
name := d.Name()
if d.IsDir() {
if path != g.root {
// .git is always skipped, even with --hidden: it's a VCS
// internals directory, not something users want to grep.
if name == ".git" {
return fs.SkipDir
}
if !g.hidden && strings.HasPrefix(name, ".") {
return fs.SkipDir
}
}
if g.ignores != nil && path != g.root {
rel, err := filepath.Rel(g.root, path)
if err == nil {
if g.ignores.Match(rel, true) {
return fs.SkipDir
}
// Eager-build this dir's ignoreNode now so that every
// child's match() call below is a cache hit, no recursion.
g.ignores.EnsureNode(rel)
}
}
return nil
}
if !g.hidden && strings.HasPrefix(name, ".") {
return nil
}
if !d.Type().IsRegular() {
return nil
}
if g.ignores != nil {
if rel, err := filepath.Rel(g.root, path); err == nil && g.ignores.Match(rel, false) {
return nil
}
}
select {
case g.paths <- path:
case <-g.ctx.Done():
return fs.SkipAll
}
return nil
})
if errors.Is(err, fs.SkipAll) {
return nil
}
return err
}
func (g *grepper) worker() {
for p := range g.paths {
if buf := g.scanFile(p); buf != nil {
select {
case g.results <- buf:
case <-g.ctx.Done():
return
}
}
}
}
func (g *grepper) scanFile(path string) []byte {
f, err := os.Open(path)
if err != nil {
return nil
}
defer f.Close()
// Output paths are slash-separated regardless of host OS, so cross-platform
// consumers (and tests) see consistent paths. Native `path` is still used
// for the os.Open above; this normalization only affects emission.
displayPath := filepath.ToSlash(path)
bufp := bufPool.Get().(*[]byte)
defer bufPool.Put(bufp)
buf := *bufp
n, err := io.ReadFull(f, buf)
switch {
case errors.Is(err, io.ErrUnexpectedEOF) || errors.Is(err, io.EOF):
// File fit in the buffer (possibly empty).
return g.scanWholeBody(displayPath, buf[:n])
case err == nil:
// Buffer filled exactly; probe for extra bytes.
var probe [1]byte
if m, _ := f.Read(probe[:]); m == 0 {
// File was exactly scanBufSize.
return g.scanWholeBody(displayPath, buf)
}
// File is larger than the pool buffer; rewind and stream.
if _, e := f.Seek(0, io.SeekStart); e != nil {
return nil
}
return g.scanFileStream(displayPath, f)
default:
return nil
}
}
// scanWholeBody finds matches by sliding bytes.Index over data. Cheap when a
// file has no matches at all — one bytes.Index call returns -1 and we're done.
func (g *grepper) scanWholeBody(path string, data []byte) []byte {
headLimit := len(data)
if headLimit > peekSize {
headLimit = peekSize
}
if bytes.IndexByte(data[:headLimit], 0) >= 0 {
return nil
}
// -v needs to look at every line; the bytes.Index fast path doesn't apply.
if g.invert {
return g.scanInverted(path, data)
}
// Pure-regex (no extracted literal): one FindAllIndex over the whole body.
if g.m.Re != nil && len(g.m.Literal) == 0 {
return g.scanWholeRegex(path, data)
}
// Literal or literal pre-filter: slide bytes.Index, validate with re if present.
lit := g.m.Literal
var out bytes.Buffer
lineNum := 1
cursor := 0
for {
idx := bytes.Index(data[cursor:], lit)
if idx < 0 {
break
}
matchPos := cursor + idx
lineNum += bytes.Count(data[cursor:matchPos], []byte{'\n'})
lineStart := 0
if i := bytes.LastIndexByte(data[:matchPos], '\n'); i >= 0 {
lineStart = i + 1
}
lineEnd := len(data)
if i := bytes.IndexByte(data[matchPos:], '\n'); i >= 0 {
lineEnd = matchPos + i
}
line := trimCR(data[lineStart:lineEnd])
if g.m.Re == nil || g.m.Re.Match(line) {
if g.quiet {
return []byte{}
}
fmt.Fprintf(&out, "%s:%d:%s\n", path, lineNum, line)
}
// Advance past this line so we don't re-match on it.
cursor = lineEnd
if cursor < len(data) {
cursor++ // skip the '\n'
lineNum++
}
}
if out.Len() == 0 {
return nil
}
return out.Bytes()
}
func (g *grepper) scanWholeRegex(path string, data []byte) []byte {
hits := g.m.Re.FindAllIndex(data, -1)
if len(hits) == 0 {
return nil
}
if g.quiet {
return []byte{}
}
var out bytes.Buffer
lineNum := 1
cursor := 0
prevLineEnd := -1
for _, h := range hits {
matchPos := h[0]
lineNum += bytes.Count(data[cursor:matchPos], []byte{'\n'})
lineStart := 0
if i := bytes.LastIndexByte(data[:matchPos], '\n'); i >= 0 {
lineStart = i + 1
}
lineEnd := len(data)
if i := bytes.IndexByte(data[matchPos:], '\n'); i >= 0 {
lineEnd = matchPos + i
}
// Multiple regex hits can land on the same line — emit the line once.
if lineEnd != prevLineEnd {
line := trimCR(data[lineStart:lineEnd])
fmt.Fprintf(&out, "%s:%d:%s\n", path, lineNum, line)
prevLineEnd = lineEnd
}
cursor = matchPos
}
return out.Bytes()
}
// scanInverted iterates every line and emits the ones that DON'T match.
// Used for -v; we lose the bytes.Index fast path because a non-match
// requires checking every line.
func (g *grepper) scanInverted(path string, data []byte) []byte {
var out bytes.Buffer
lineNum := 1
start := 0
for start < len(data) {
end := len(data)
if i := bytes.IndexByte(data[start:], '\n'); i >= 0 {
end = start + i
}
line := trimCR(data[start:end])
if !g.m.Match(line) {
if g.quiet {
return []byte{}
}
fmt.Fprintf(&out, "%s:%d:%s\n", path, lineNum, line)
}
start = end + 1
lineNum++
}
if out.Len() == 0 {
return nil
}
return out.Bytes()
}
// scanFileStream is the existing bufio fallback used for files larger than
// scanBufSize.
func (g *grepper) scanFileStream(path string, f *os.File) []byte {
br := readerPool.Get().(*bufio.Reader)
defer readerPool.Put(br)
br.Reset(f)
head, _ := br.Peek(peekSize)
if bytes.IndexByte(head, 0) >= 0 {
return nil
}
var out bytes.Buffer
lineNum := 0
for {
line, err := br.ReadSlice('\n')
if err == bufio.ErrBufferFull {
return nil
}
if len(line) > 0 || err == nil {
lineNum++
if n := len(line); n > 0 && line[n-1] == '\n' {
line = line[:n-1]
}
line = trimCR(line)
matched := g.m.Match(line)
if matched != g.invert {
if g.quiet {
return []byte{}
}
fmt.Fprintf(&out, "%s:%d:%s\n", path, lineNum, line)
}
}
if err != nil {
break
}
}
if out.Len() == 0 {
return nil
}
return out.Bytes()
}