Skip to content

Commit 51b6e52

Browse files
committed
zoekt: implement mode which has same behaviour as attribution search
This is to help reproduce slow attribution searches we have on sourcegraph.com.
1 parent 008a775 commit 51b6e52

1 file changed

Lines changed: 34 additions & 10 deletions

File tree

cmd/zoekt/main.go

Lines changed: 34 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818
"context"
1919
"flag"
2020
"fmt"
21+
"io"
2122
"log"
2223
"os"
2324
"path/filepath"
@@ -85,6 +86,7 @@ func main() {
8586
verbose := flag.Bool("v", false, "print some background data")
8687
withRepo := flag.Bool("r", false, "print the repo before the file name")
8788
list := flag.Bool("l", false, "print matching filenames only")
89+
exact := flag.Bool("exact_stdin", false, "look for exact matches on STDIN")
8890

8991
flag.Usage = func() {
9092
name := os.Args[0]
@@ -95,12 +97,39 @@ func main() {
9597
}
9698
flag.Parse()
9799

98-
if len(flag.Args()) == 0 {
100+
var pat string
101+
var q query.Q
102+
var sOpts zoekt.SearchOptions
103+
if *exact {
104+
needle, err := io.ReadAll(os.Stdin)
105+
if err != nil {
106+
log.Fatal(err)
107+
}
108+
pat = string(needle)
109+
q = &query.Substring{
110+
Pattern: pat,
111+
CaseSensitive: true,
112+
Content: true,
113+
}
114+
sOpts = zoekt.SearchOptions{
115+
ShardMaxMatchCount: 10_000,
116+
ShardRepoMaxMatchCount: 1,
117+
TotalMaxMatchCount: 100_000,
118+
MaxWallTime: 20 * time.Second,
119+
MaxDocDisplayCount: 5,
120+
}
121+
} else if len(flag.Args()) == 0 {
99122
fmt.Fprintf(os.Stderr, "Pattern is missing.\n")
100123
flag.Usage()
101124
os.Exit(2)
125+
} else {
126+
var err error
127+
pat = flag.Arg(0)
128+
q, err = query.Parse(pat)
129+
if err != nil {
130+
log.Fatal(err)
131+
}
102132
}
103-
pat := flag.Arg(0)
104133

105134
var searcher zoekt.Searcher
106135
var err error
@@ -114,16 +143,11 @@ func main() {
114143
log.Fatal(err)
115144
}
116145

117-
query, err := query.Parse(pat)
118-
if err != nil {
119-
log.Fatal(err)
120-
}
121146
if *verbose {
122-
log.Println("query:", query)
147+
log.Println("query:", q)
123148
}
124149

125-
var sOpts zoekt.SearchOptions
126-
sres, err := searcher.Search(context.Background(), query, &sOpts)
150+
sres, err := searcher.Search(context.Background(), q, &sOpts)
127151
if *cpuProfile != "" {
128152
// If profiling, do it another time so we measure with
129153
// warm caches.
@@ -141,7 +165,7 @@ func main() {
141165
log.Fatal(err)
142166
}
143167
for {
144-
sres, _ = searcher.Search(context.Background(), query, &sOpts)
168+
sres, _ = searcher.Search(context.Background(), q, &sOpts)
145169
if time.Since(t) > *profileTime {
146170
break
147171
}

0 commit comments

Comments
 (0)