/
sg.go
802 lines (662 loc) · 21.7 KB
/
sg.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
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
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
package main
import (
"bufio"
"bytes"
"context"
"errors"
"fmt"
"hash/crc32"
"log"
"math/rand"
"net/url"
"os"
"os/exec"
"path"
"path/filepath"
"strconv"
"strings"
"time"
"github.com/go-git/go-git/v5"
proto "github.com/sourcegraph/zoekt/cmd/zoekt-sourcegraph-indexserver/protos/sourcegraph/zoekt/configuration/v1"
"github.com/sourcegraph/zoekt/ctags"
"golang.org/x/net/trace"
"github.com/sourcegraph/zoekt"
)
// SourcegraphListResult is the return value of Sourcegraph.List. It is its
// own type since internally we batch the calculation of index options. This
// is exposed via IterateIndexOptions.
//
// This type has state and is coupled to the Sourcegraph implementation.
type SourcegraphListResult struct {
// IDs is the set of Sourcegraph repository IDs that this replica needs
// to index.
IDs []uint32
// IterateIndexOptions best effort resolves the IndexOptions for RepoIDs. If
// any repository fails it internally logs. It uses the "config fingerprint"
// to reduce the amount of work done. This means we only resolve options for
// repositories which have been mutated since the last Sourcegraph.List
// call.
//
// Note: this has a side-effect of setting a the "config fingerprint". The
// config fingerprint means we only calculate index options for repositories
// that have changed since the last call to IterateIndexOptions. If you want
// to force calculation of index options use
// Sourcegraph.ForceIterateIndexOptions.
//
// Note: This should not be called concurrently with the Sourcegraph client.
IterateIndexOptions func(func(IndexOptions))
}
// Sourcegraph represents the Sourcegraph service. It informs the indexserver
// what to index and which options to use.
type Sourcegraph interface {
// List returns a list of repository IDs to index as well as a facility to
// fetch the indexing options.
//
// Note: The return value is not safe to use concurrently with future calls
// to List.
List(ctx context.Context, indexed []uint32) (*SourcegraphListResult, error)
// ForceIterateIndexOptions will best-effort calculate the index options for
// all repos. For each repo it will call either onSuccess or onError. This
// is the forced version of IterateIndexOptions, so will always calculate
// options for each id in repos.
ForceIterateIndexOptions(onSuccess func(IndexOptions), onError func(uint32, error), repos ...uint32)
// GetDocumentRanks returns a map from paths within the given repo to their
// rank vectors. Paths are assumed to be ordered by each pairwise component of
// the resulting vector, higher ranks coming earlier
GetDocumentRanks(ctx context.Context, repoName string) (RepoPathRanks, error)
// UpdateIndexStatus sends a request to Sourcegraph to confirm that the
// given repositories have been indexed.
UpdateIndexStatus(repositories []indexStatus) error
}
type SourcegraphClientOption func(*sourcegraphClient)
// WithBatchSize controls how many repository configurations we request a time.
// If BatchSize is 0, we default to requesting 10,000 repositories at once.
func WithBatchSize(batchSize int) SourcegraphClientOption {
return func(c *sourcegraphClient) {
c.BatchSize = batchSize
}
}
func newSourcegraphClient(rootURL *url.URL, hostname string, grpcClient proto.ZoektConfigurationServiceClient, opts ...SourcegraphClientOption) *sourcegraphClient {
client := &sourcegraphClient{
Root: rootURL,
Hostname: hostname,
BatchSize: 0,
grpcClient: grpcClient,
}
for _, opt := range opts {
opt(client)
}
return client
}
// sourcegraphClient contains methods which interact with the sourcegraph API.
type sourcegraphClient struct {
// Root is the base URL for the Sourcegraph instance to index. Normally
// http://sourcegraph-frontend-internal or http://localhost:3090.
Root *url.URL
// Hostname is the name we advertise to Sourcegraph when asking for the
// list of repositories to index.
Hostname string
// BatchSize is how many repository configurations we request at once. If
// zero a value of 10000 is used.
BatchSize int
// grpcClient is used to make requests to the Sourcegraph instance if gRPC is enabled.
grpcClient proto.ZoektConfigurationServiceClient
// configFingerprintProto is the last config fingerprint (as GRPC) returned from
// Sourcegraph. It can be used for future calls to the configuration
// endpoint.
//
// configFingerprintProto is mutually exclusive with configFingerprint - this field
// will only be used if gRPC is enabled.
configFingerprintProto *proto.Fingerprint
// configFingerprintReset tracks when we should zero out the
// configFingerprint. We want to periodically do this just in case our
// configFingerprint logic is faulty. When it is cleared out, we fallback to
// calculating everything.
configFingerprintReset time.Time
}
// GetDocumentRanks asks Sourcegraph for a mapping of file paths to rank
// vectors.
func (s *sourcegraphClient) GetDocumentRanks(ctx context.Context, repoName string) (RepoPathRanks, error) {
resp, err := s.grpcClient.DocumentRanks(ctx, &proto.DocumentRanksRequest{Repository: repoName})
if err != nil {
return RepoPathRanks{}, err
}
var out RepoPathRanks
out.FromProto(resp)
return out, nil
}
func (s *sourcegraphClient) List(ctx context.Context, indexed []uint32) (*SourcegraphListResult, error) {
repos, err := s.listRepoIDs(ctx, indexed)
if err != nil {
return nil, fmt.Errorf("listRepoIDs: %w", err)
}
batchSize := s.BatchSize
if batchSize == 0 {
batchSize = 10_000
}
// Check if we should recalculate everything.
if time.Now().After(s.configFingerprintReset) {
// for every 500 repos we wait a minute. 2021-12-15 on sourcegraph.com
// this works out to every 100 minutes.
next := time.Duration(len(indexed)) * time.Minute / 500
if min := 5 * time.Minute; next < min {
next = min
}
next += time.Duration(rand.Int63n(int64(next) / 4)) // jitter
s.configFingerprintReset = time.Now().Add(next)
s.configFingerprintProto = nil
}
// getIndexOptionsFunc is a function that can be used to get the index
// options for a set of repos (while properly handling any configuration fingerprint
// changes).
//
// In general, this function provides a consistent fingerprint for each batch call,
// and updates the server state with the new fingerprint. If any of the batch calls
// fail, the old fingerprint is restored.
type getIndexOptionsFunc func(repos ...uint32) ([]indexOptionsItem, error)
mkGetIndexOptionsFunc := func(tr trace.Trace) getIndexOptionsFunc {
startingFingerPrint := s.configFingerprintProto
tr.LazyPrintf("fingerprint: %s", startingFingerPrint.String())
first := true
return func(repos ...uint32) ([]indexOptionsItem, error) {
options, nextFingerPrint, err := s.getIndexOptions(ctx, startingFingerPrint, repos)
if err != nil {
first = false
s.configFingerprintProto = startingFingerPrint
return nil, err
}
if first {
first = false
s.configFingerprintProto = nextFingerPrint
tr.LazyPrintf("new fingerprint: %s", nextFingerPrint.String())
}
return options, nil
}
}
iterate := func(f func(IndexOptions)) {
start := time.Now()
tr := trace.New("getIndexOptions", "")
tr.LazyPrintf("getting index options for %d repos", len(repos))
defer func() {
metricResolveRevisionsDuration.Observe(time.Since(start).Seconds())
tr.Finish()
}()
getIndexOptions := mkGetIndexOptionsFunc(tr)
// We ask the frontend to get index options in batches.
for repos := range batched(repos, batchSize) {
start := time.Now()
options, err := getIndexOptions(repos...)
duration := time.Since(start)
if err != nil {
metricResolveRevisionDuration.WithLabelValues("false").Observe(duration.Seconds())
tr.LazyPrintf("failed fetching options batch: %v", err)
tr.SetError()
continue
}
metricResolveRevisionDuration.WithLabelValues("true").Observe(duration.Seconds())
for _, o := range options {
metricGetIndexOptions.Inc()
if o.Error != "" {
metricGetIndexOptionsError.Inc()
tr.LazyPrintf("failed fetching options for %v: %v", o.Name, o.Error)
tr.SetError()
continue
}
f(o.IndexOptions)
}
}
}
return &SourcegraphListResult{
IDs: repos,
IterateIndexOptions: iterate,
}, nil
}
func (s *sourcegraphClient) ForceIterateIndexOptions(onSuccess func(IndexOptions), onError func(uint32, error), repos ...uint32) {
batchSize := s.BatchSize
if batchSize == 0 {
batchSize = 10_000
}
for repos := range batched(repos, batchSize) {
opts, _, err := s.getIndexOptions(context.Background(), nil, repos)
if err != nil {
for _, id := range repos {
onError(id, err)
}
continue
}
for _, o := range opts {
if o.RepoID > 0 && o.Error != "" {
onError(o.RepoID, errors.New(o.Error))
}
if o.Error == "" {
onSuccess(o.IndexOptions)
}
}
}
}
// indexOptionsItem wraps IndexOptions to also include an error returned by
// the API.
type indexOptionsItem struct {
IndexOptions
Error string
}
func (o *indexOptionsItem) FromProto(x *proto.ZoektIndexOptions) {
branches := make([]zoekt.RepositoryBranch, 0, len(x.Branches))
for _, b := range x.GetBranches() {
branches = append(branches, zoekt.RepositoryBranch{
Name: b.GetName(),
Version: b.GetVersion(),
})
}
item := indexOptionsItem{}
languageMap := make(map[string]ctags.CTagsParserType)
for _, lang := range x.GetLanguageMap() {
languageMap[lang.GetLanguage()] = ctags.CTagsParserType(lang.GetCtags().Number())
}
item.IndexOptions = IndexOptions{
RepoID: uint32(x.GetRepoId()),
LargeFiles: x.GetLargeFiles(),
Symbols: x.GetSymbols(),
Branches: branches,
Name: x.GetName(),
Priority: x.GetPriority(),
DocumentRanksVersion: x.GetDocumentRanksVersion(),
Public: x.GetPublic(),
Fork: x.GetFork(),
Archived: x.GetArchived(),
LanguageMap: languageMap,
ShardConcurrency: x.GetShardConcurrency(),
}
item.Error = x.GetError()
*o = item
}
func (o *indexOptionsItem) ToProto() *proto.ZoektIndexOptions {
branches := make([]*proto.ZoektRepositoryBranch, 0, len(o.Branches))
for _, b := range o.Branches {
branches = append(branches, &proto.ZoektRepositoryBranch{
Name: b.Name,
Version: b.Version,
})
}
languageMap := make([]*proto.LanguageMapping, 0, len(o.LanguageMap))
for lang, parser := range o.LanguageMap {
languageMap = append(languageMap, &proto.LanguageMapping{
Language: lang,
Ctags: proto.CTagsParserType(parser),
})
}
return &proto.ZoektIndexOptions{
RepoId: int32(o.RepoID),
LargeFiles: o.LargeFiles,
Symbols: o.Symbols,
Branches: branches,
Name: o.Name,
Priority: o.Priority,
DocumentRanksVersion: o.DocumentRanksVersion,
Public: o.Public,
Fork: o.Fork,
Archived: o.Archived,
Error: o.Error,
LanguageMap: languageMap,
ShardConcurrency: o.ShardConcurrency,
}
}
func (s *sourcegraphClient) getIndexOptions(ctx context.Context, fingerprint *proto.Fingerprint, repos []uint32) ([]indexOptionsItem, *proto.Fingerprint, error) {
repoIDs := make([]int32, 0, len(repos))
for _, id := range repos {
repoIDs = append(repoIDs, int32(id))
}
req := proto.SearchConfigurationRequest{
RepoIds: repoIDs,
Fingerprint: fingerprint,
}
response, err := s.grpcClient.SearchConfiguration(ctx, &req)
if err != nil {
return nil, nil, err
}
protoItems := response.GetUpdatedOptions()
items := make([]indexOptionsItem, 0, len(protoItems))
for _, x := range protoItems {
var item indexOptionsItem
item.FromProto(x)
item.IndexOptions.CloneURL = s.getCloneURL(item.Name)
items = append(items, item)
}
return items, response.GetFingerprint(), nil
}
func (s *sourcegraphClient) getCloneURL(name string) string {
return s.Root.ResolveReference(&url.URL{Path: path.Join("/.internal/git", name)}).String()
}
func (s *sourcegraphClient) listRepoIDs(ctx context.Context, indexed []uint32) ([]uint32, error) {
var request proto.ListRequest
request.Hostname = s.Hostname
request.IndexedIds = make([]int32, 0, len(indexed))
for _, id := range indexed {
request.IndexedIds = append(request.IndexedIds, int32(id))
}
response, err := s.grpcClient.List(ctx, &request)
if err != nil {
return nil, err
}
repoIDs := make([]uint32, 0, len(response.RepoIds))
for _, id := range response.RepoIds {
repoIDs = append(repoIDs, uint32(id))
}
return repoIDs, nil
}
type indexStatus struct {
RepoID uint32
Branches []zoekt.RepositoryBranch
IndexTimeUnix int64
}
type updateIndexStatusRequest struct {
Repositories []indexStatus
}
func (u *updateIndexStatusRequest) ToProto() *proto.UpdateIndexStatusRequest {
repositories := make([]*proto.UpdateIndexStatusRequest_Repository, 0, len(u.Repositories))
for _, repo := range u.Repositories {
branches := make([]*proto.ZoektRepositoryBranch, 0, len(repo.Branches))
for _, branch := range repo.Branches {
branches = append(branches, &proto.ZoektRepositoryBranch{
Name: branch.Name,
Version: branch.Version,
})
}
repositories = append(repositories, &proto.UpdateIndexStatusRequest_Repository{
RepoId: repo.RepoID,
Branches: branches,
IndexTimeUnix: repo.IndexTimeUnix,
})
}
return &proto.UpdateIndexStatusRequest{
Repositories: repositories,
}
}
func (u *updateIndexStatusRequest) FromProto(x *proto.UpdateIndexStatusRequest) {
protoRepositories := x.GetRepositories()
repositories := make([]indexStatus, 0, len(protoRepositories))
for _, repo := range x.GetRepositories() {
protoBranches := repo.GetBranches()
branches := make([]zoekt.RepositoryBranch, 0, len(protoBranches))
for _, branch := range repo.GetBranches() {
branches = append(branches, zoekt.RepositoryBranch{
Name: branch.GetName(),
Version: branch.GetVersion(),
})
}
repositories = append(repositories, indexStatus{
RepoID: repo.GetRepoId(),
Branches: branches,
IndexTimeUnix: repo.GetIndexTimeUnix(),
})
}
*u = updateIndexStatusRequest{
Repositories: repositories,
}
}
// UpdateIndexStatus sends a request to Sourcegraph to confirm that the given
// repositories have been indexed.
func (s *sourcegraphClient) UpdateIndexStatus(repositories []indexStatus) error {
r := updateIndexStatusRequest{Repositories: repositories}
request := r.ToProto()
_, err := s.grpcClient.UpdateIndexStatus(context.Background(), request)
if err != nil {
return fmt.Errorf("failed to update index status: %w", err)
}
return nil
}
type sourcegraphFake struct {
RootDir string
Log *log.Logger
}
// GetDocumentRanks expects a file where each line has the following format:
// path<tab>rank... where rank is a float64.
func (sf sourcegraphFake) GetDocumentRanks(ctx context.Context, repoName string) (RepoPathRanks, error) {
dir := filepath.Join(sf.RootDir, filepath.FromSlash(repoName))
fd, err := os.Open(filepath.Join(dir, "SG_DOCUMENT_RANKS"))
if err != nil {
return RepoPathRanks{}, err
}
ranks := RepoPathRanks{}
sum := 0.0
count := 0
scanner := bufio.NewScanner(fd)
for scanner.Scan() {
s := scanner.Text()
pathRanks := strings.Split(s, "\t")
if rank, err := strconv.ParseFloat(pathRanks[1], 64); err == nil {
ranks.Paths[pathRanks[0]] = rank
sum += rank
count++
}
}
if err := scanner.Err(); err != nil {
return RepoPathRanks{}, err
}
ranks.MeanRank = sum / float64(count)
return ranks, nil
}
func (sf sourcegraphFake) List(ctx context.Context, indexed []uint32) (*SourcegraphListResult, error) {
repos, err := sf.ListRepoIDs(ctx, indexed)
if err != nil {
return nil, err
}
iterate := func(f func(IndexOptions)) {
opts, err := sf.GetIndexOptions(repos...)
if err != nil {
sf.Log.Printf("WARN: ignoring GetIndexOptions error: %v", err)
}
for _, opt := range opts {
if opt.Error != "" {
sf.Log.Printf("WARN: ignoring GetIndexOptions error for %s: %v", opt.Name, opt.Error)
continue
}
f(opt.IndexOptions)
}
}
return &SourcegraphListResult{
IDs: repos,
IterateIndexOptions: iterate,
}, nil
}
func (sf sourcegraphFake) ForceIterateIndexOptions(onSuccess func(IndexOptions), onError func(uint32, error), repos ...uint32) {
opts, err := sf.GetIndexOptions(repos...)
if err != nil {
for _, id := range repos {
onError(id, err)
}
return
}
for _, o := range opts {
if o.RepoID > 0 && o.Error != "" {
onError(o.RepoID, errors.New(o.Error))
}
if o.Error == "" {
onSuccess(o.IndexOptions)
}
}
}
func (sf sourcegraphFake) GetIndexOptions(repos ...uint32) ([]indexOptionsItem, error) {
reposIdx := map[uint32]int{}
for i, id := range repos {
reposIdx[id] = i
}
items := make([]indexOptionsItem, len(repos))
err := sf.visitRepos(func(name string) {
idx, ok := reposIdx[sf.id(name)]
if !ok {
return
}
opts, err := sf.getIndexOptions(name)
if err != nil {
items[idx] = indexOptionsItem{Error: err.Error()}
} else {
items[idx] = indexOptionsItem{IndexOptions: opts}
}
})
if err != nil {
return nil, err
}
for i := range items {
if items[i].Error == "" && items[i].RepoID == 0 {
items[i].Error = "not found"
}
}
return items, nil
}
func (sf sourcegraphFake) getIndexOptions(name string) (IndexOptions, error) {
dir := filepath.Join(sf.RootDir, filepath.FromSlash(name))
exists := func(p string) bool {
_, err := os.Stat(filepath.Join(dir, p))
return err == nil
}
float := func(p string) float64 {
b, _ := os.ReadFile(filepath.Join(dir, p))
f, _ := strconv.ParseFloat(string(bytes.TrimSpace(b)), 64)
return f
}
opts := IndexOptions{
RepoID: sf.id(name),
Name: name,
CloneURL: sf.getCloneURL(name),
Symbols: true,
Public: !exists("SG_PRIVATE"),
Fork: exists("SG_FORK"),
Archived: exists("SG_ARCHIVED"),
Priority: float("SG_PRIORITY"),
}
if stat, err := os.Stat(filepath.Join(dir, "SG_DOCUMENT_RANKS")); err == nil {
opts.DocumentRanksVersion = stat.ModTime().String()
}
branches, err := sf.getBranches(name)
if err != nil {
return opts, err
}
opts.Branches = branches
return opts, nil
}
func (sf sourcegraphFake) getBranches(name string) ([]zoekt.RepositoryBranch, error) {
dir := filepath.Join(sf.RootDir, filepath.FromSlash(name))
repo, err := git.PlainOpen(dir)
if err != nil {
return nil, err
}
cfg, err := repo.Config()
if err != nil {
return nil, err
}
sec := cfg.Raw.Section("zoekt")
branches := sec.Options.GetAll("branch")
if len(branches) == 0 {
branches = append(branches, "HEAD")
}
rBranches := make([]zoekt.RepositoryBranch, 0, len(branches))
for _, branch := range branches {
cmd := exec.Command("git", "rev-parse", branch)
cmd.Dir = dir
if b, err := cmd.Output(); err != nil {
sf.Log.Printf("WARN: Could not get branch %s/%s", name, branch)
} else {
version := string(bytes.TrimSpace(b))
rBranches = append(rBranches, zoekt.RepositoryBranch{
Name: branch,
Version: version,
})
}
}
if len(rBranches) == 0 {
return nil, fmt.Errorf("WARN: Could not get any branch revisions for repo %s", name)
}
return rBranches, nil
}
func (sf sourcegraphFake) id(name string) uint32 {
// allow overriding the ID.
idPath := filepath.Join(sf.RootDir, filepath.FromSlash(name), "SG_ID")
if b, _ := os.ReadFile(idPath); len(b) > 0 {
id, err := strconv.Atoi(strings.TrimSpace(string(b)))
if err == nil {
return uint32(id)
}
}
return fakeID(name)
}
func (sf sourcegraphFake) getCloneURL(name string) string {
return filepath.Join(sf.RootDir, filepath.FromSlash(name))
}
func (sf sourcegraphFake) ListRepoIDs(ctx context.Context, indexed []uint32) ([]uint32, error) {
var repos []uint32
err := sf.visitRepos(func(name string) {
repos = append(repos, sf.id(name))
})
return repos, err
}
func (sf sourcegraphFake) visitRepos(visit func(name string)) error {
return filepath.Walk(sf.RootDir, func(path string, fi os.FileInfo, fileErr error) error {
if fileErr != nil {
sf.Log.Printf("WARN: ignoring error searching %s: %v", path, fileErr)
return nil
}
if !fi.IsDir() {
return nil
}
gitdir := filepath.Join(path, ".git")
if fi, err := os.Stat(gitdir); err != nil || !fi.IsDir() {
return nil
}
subpath, err := filepath.Rel(sf.RootDir, path)
if err != nil {
// According to WalkFunc docs, path is always filepath.Join(root,
// subpath). So Rel should always work.
return fmt.Errorf("filepath.Walk returned %s which is not relative to %s: %w", path, sf.RootDir, err)
}
name := filepath.ToSlash(subpath)
visit(name)
return filepath.SkipDir
})
}
func (s sourcegraphFake) UpdateIndexStatus(repositories []indexStatus) error {
// noop
return nil
}
// fakeID returns a deterministic ID based on name. Used for fakes and tests.
func fakeID(name string) uint32 {
// magic at the end is to ensure we get a positive number when casting.
return uint32(crc32.ChecksumIEEE([]byte(name))%(1<<31-1) + 1)
}
type sourcegraphNop struct{}
func (s sourcegraphNop) List(ctx context.Context, indexed []uint32) (*SourcegraphListResult, error) {
return nil, nil
}
func (s sourcegraphNop) ForceIterateIndexOptions(onSuccess func(IndexOptions), onError func(uint32, error), repos ...uint32) {
}
func (s sourcegraphNop) GetDocumentRanks(ctx context.Context, repoName string) (RepoPathRanks, error) {
return RepoPathRanks{}, nil
}
func (s sourcegraphNop) UpdateIndexStatus(repositories []indexStatus) error {
return nil
}
type RepoPathRanks struct {
MeanRank float64 `json:"mean_reference_count"`
Paths map[string]float64 `json:"paths"`
}
func (r *RepoPathRanks) FromProto(x *proto.DocumentRanksResponse) {
protoPaths := x.GetPaths()
ranks := make(map[string]float64, len(protoPaths))
for filePath, rank := range protoPaths {
ranks[filePath] = rank
}
*r = RepoPathRanks{
MeanRank: x.GetMeanRank(),
Paths: ranks,
}
}
func (r *RepoPathRanks) ToProto() *proto.DocumentRanksResponse {
paths := make(map[string]float64, len(r.Paths))
for filePath, rank := range r.Paths {
paths[filePath] = rank
}
return &proto.DocumentRanksResponse{
MeanRank: r.MeanRank,
Paths: paths,
}
}