fork of https://github.com/sourcegraph/zoekt
0

Configure Feed

Select the types of activity you want to include in your feed.

eval: fix gatherBranches (#562)

fixes a bug where we wouldn't display branches correctly if the query contained one or more branch filters.

author
Stefan Hengl
committer
GitHub
date (Mar 23, 2023, 11:02 AM +0100) commit cf9500b1 parent 27effbad
+74 -17
+24 -16
eval.go
··· 563 563 return -1 564 564 } 565 565 566 - // gatherBranches returns a list of branch names. 566 + // gatherBranches returns a list of branch names taking into account any branch 567 + // filters in the query. If the query contains a branch filter, it returns all 568 + // branches containing the docID and matching the branch filter. Otherwise, it 569 + // returns all branches containing docID. 567 570 func (d *indexData) gatherBranches(docID uint32, mt matchTree, known map[matchTree]bool) []string { 568 - foundBranchQuery := false 569 - var branches []string 570 571 repoIdx := d.repos[docID] 572 + 573 + var mask uint64 571 574 visitMatches(mt, known, func(mt matchTree) { 572 575 bq, ok := mt.(*branchQueryMatchTree) 573 - if ok { 574 - foundBranchQuery = true 575 - branches = append(branches, 576 - d.branchNames[repoIdx][uint(bq.masks[repoIdx])]) 576 + if !ok { 577 + return 577 578 } 579 + 580 + // bq.masks[repoIdx]: the branches we are filtering on 581 + // bq.branchMask(): the branches of the current file 582 + mask = mask | (bq.masks[repoIdx] & bq.branchMask()) 578 583 }) 579 584 580 - if !foundBranchQuery { 581 - mask := d.fileBranchMasks[docID] 582 - id := uint32(1) 583 - for mask != 0 { 584 - if mask&0x1 != 0 { 585 - branches = append(branches, d.branchNames[repoIdx][uint(id)]) 586 - } 587 - id <<= 1 588 - mask >>= 1 585 + if mask == 0 { 586 + mask = d.fileBranchMasks[docID] 587 + } 588 + 589 + var branches []string 590 + id := uint32(1) 591 + for mask != 0 { 592 + if mask&0x1 != 0 { 593 + branches = append(branches, d.branchNames[repoIdx][uint(id)]) 589 594 } 595 + id <<= 1 596 + mask >>= 1 590 597 } 598 + 591 599 return branches 592 600 } 593 601
+45
eval_test.go
··· 25 25 "github.com/RoaringBitmap/roaring" 26 26 "github.com/google/go-cmp/cmp" 27 27 "github.com/grafana/regexp" 28 + 28 29 "github.com/sourcegraph/zoekt/query" 29 30 ) 30 31 ··· 339 340 h.Write([]byte(name)) 340 341 return h.Sum32() 341 342 } 343 + 344 + func TestGatherBranches(t *testing.T) { 345 + content := []byte("dummy") 346 + b := testIndexBuilder(t, &Repository{ 347 + Branches: []RepositoryBranch{ 348 + {"foo", "v1"}, 349 + {"foo-2", "v1"}, 350 + {"main", "v1"}, 351 + {"bar", "v1"}, 352 + {"quz", "v1"}, 353 + }}, 354 + Document{Name: "f1", Content: content, Branches: []string{"foo", "bar", "quz"}}, 355 + Document{Name: "f2", Content: content, Branches: []string{"foo", "foo-2"}}, 356 + Document{Name: "f3", Content: content, Branches: []string{"main"}}) 357 + 358 + d := searcherForTest(t, b).(*indexData) 359 + 360 + sr, err := d.Search( 361 + context.Background(), 362 + &query.Or{Children: []query.Q{ 363 + &query.Branch{Pattern: "foo"}, 364 + &query.Branch{Pattern: "quz"}, 365 + }}, 366 + &SearchOptions{}, 367 + ) 368 + if err != nil { 369 + t.Fatal(err) 370 + } 371 + 372 + want := map[string][]string{ 373 + "f1": []string{"foo", "quz"}, 374 + "f2": []string{"foo", "foo-2"}, 375 + } 376 + 377 + if len(sr.Files) != 2 { 378 + t.Fatalf("len(sr.Files): want %d, got %d", 2, len(sr.Files)) 379 + } 380 + 381 + for _, f := range sr.Files { 382 + if d := cmp.Diff(want[f.FileName], f.Branches); d != "" { 383 + t.Fatalf("-want,+got:\n%s", d) 384 + } 385 + } 386 + }
+5 -1
matchtree.go
··· 181 181 docID uint32 182 182 } 183 183 184 + func (t *branchQueryMatchTree) branchMask() uint64 { 185 + return t.fileMasks[t.docID] 186 + } 187 + 184 188 type symbolRegexpMatchTree struct { 185 189 matchTree 186 190 regexp *regexp.Regexp ··· 672 676 } 673 677 674 678 func (t *branchQueryMatchTree) matches(cp *contentProvider, cost int, known map[matchTree]bool) (bool, bool) { 675 - return t.fileMasks[t.docID]&t.masks[t.repos[t.docID]] != 0, true 679 + return t.branchMask()&t.masks[t.repos[t.docID]] != 0, true 676 680 } 677 681 678 682 func (t *regexpMatchTree) matches(cp *contentProvider, cost int, known map[matchTree]bool) (bool, bool) {