-
Notifications
You must be signed in to change notification settings - Fork 70
/
main.go
150 lines (131 loc) · 3.43 KB
/
main.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
// Copyright 2016 Google Inc. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package main
import (
"flag"
"fmt"
"log"
"os"
"path/filepath"
"runtime/pprof"
"strings"
"github.com/sourcegraph/zoekt"
"github.com/sourcegraph/zoekt/build"
"github.com/sourcegraph/zoekt/cmd"
"go.uber.org/automaxprocs/maxprocs"
)
type fileInfo struct {
name string
size int64
}
type fileAggregator struct {
ignoreDirs map[string]struct{}
sizeMax int64
sink chan fileInfo
}
func (a *fileAggregator) add(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if info.IsDir() {
base := filepath.Base(path)
if _, ok := a.ignoreDirs[base]; ok {
return filepath.SkipDir
}
}
if info.Mode().IsRegular() {
a.sink <- fileInfo{path, info.Size()}
}
return nil
}
func main() {
cpuProfile := flag.String("cpu_profile", "", "write cpu profile to file")
ignoreDirs := flag.String("ignore_dirs", ".git,.hg,.svn", "comma separated list of directories to ignore.")
flag.Parse()
// Tune GOMAXPROCS to match Linux container CPU quota.
_, _ = maxprocs.Set()
opts := cmd.OptionsFromFlags()
if *cpuProfile != "" {
f, err := os.Create(*cpuProfile)
if err != nil {
log.Fatal(err)
}
if err := pprof.StartCPUProfile(f); err != nil {
log.Fatal(err)
}
defer pprof.StopCPUProfile()
}
ignoreDirMap := map[string]struct{}{}
if *ignoreDirs != "" {
dirs := strings.Split(*ignoreDirs, ",")
for _, d := range dirs {
d = strings.TrimSpace(d)
if d != "" {
ignoreDirMap[d] = struct{}{}
}
}
}
for _, arg := range flag.Args() {
opts.RepositoryDescription.Source = arg
if err := indexArg(arg, *opts, ignoreDirMap); err != nil {
log.Fatal(err)
}
}
}
func indexArg(arg string, opts build.Options, ignore map[string]struct{}) error {
dir, err := filepath.Abs(filepath.Clean(arg))
if err != nil {
return err
}
opts.RepositoryDescription.Name = filepath.Base(dir)
builder, err := build.NewBuilder(opts)
if err != nil {
return err
}
// we don't need to check error, since we either already have an error, or
// we returning the first call to builder.Finish.
defer builder.Finish() // nolint:errcheck
comm := make(chan fileInfo, 100)
agg := fileAggregator{
ignoreDirs: ignore,
sink: comm,
sizeMax: int64(opts.SizeMax),
}
go func() {
if err := filepath.Walk(dir, agg.add); err != nil {
log.Fatal(err)
}
close(comm)
}()
for f := range comm {
displayName := strings.TrimPrefix(f.name, dir+"/")
if f.size > int64(opts.SizeMax) && !opts.IgnoreSizeMax(displayName) {
if err := builder.Add(zoekt.Document{
Name: displayName,
SkipReason: fmt.Sprintf("document size %d larger than limit %d", f.size, opts.SizeMax),
}); err != nil {
return err
}
continue
}
content, err := os.ReadFile(f.name)
if err != nil {
return err
}
if err := builder.AddFile(displayName, content); err != nil {
return err
}
}
return builder.Finish()
}