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

Configure Feed

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

indexserver: allow specifying branches to index for sourcegraphFake (#393)

Allows specifying multiple branch names using the Git config key
zoekt.branch to instruct sourcegraphFake which branches to index

+44 -9
+44 -9
cmd/zoekt-sourcegraph-indexserver/sg.go
··· 20 20 "strings" 21 21 "time" 22 22 23 + "github.com/go-git/go-git/v5" 23 24 retryablehttp "github.com/hashicorp/go-retryablehttp" 24 25 "golang.org/x/net/trace" 25 26 ··· 463 464 Priority: float("SG_PRIORITY"), 464 465 } 465 466 466 - cmd := exec.Command("git", "rev-parse", "HEAD") 467 - cmd.Dir = dir 468 - if b, err := cmd.Output(); err != nil { 467 + branches, err := sf.getBranches(name) 468 + if err != nil { 469 469 return opts, err 470 - } else { 471 - head := string(bytes.TrimSpace(b)) 472 - opts.Branches = []zoekt.RepositoryBranch{{ 473 - Name: "HEAD", 474 - Version: head, 475 - }} 476 470 } 471 + opts.Branches = branches 477 472 478 473 return opts, nil 474 + } 475 + 476 + func (sf sourcegraphFake) getBranches(name string) ([]zoekt.RepositoryBranch, error) { 477 + dir := filepath.Join(sf.RootDir, filepath.FromSlash(name)) 478 + repo, err := git.PlainOpen(dir) 479 + if err != nil { 480 + return nil, err 481 + } 482 + 483 + cfg, err := repo.Config() 484 + if err != nil { 485 + return nil, err 486 + } 487 + 488 + sec := cfg.Raw.Section("zoekt") 489 + branches := sec.Options.GetAll("branch") 490 + if len(branches) == 0 { 491 + branches = append(branches, "HEAD") 492 + } 493 + 494 + rBranches := make([]zoekt.RepositoryBranch, 0, len(branches)) 495 + for _, branch := range branches { 496 + cmd := exec.Command("git", "rev-parse", branch) 497 + cmd.Dir = dir 498 + if b, err := cmd.Output(); err != nil { 499 + sf.Log.Printf("WARN: Could not get branch %s/%s", name, branch) 500 + } else { 501 + version := string(bytes.TrimSpace(b)) 502 + rBranches = append(rBranches, zoekt.RepositoryBranch{ 503 + Name: branch, 504 + Version: version, 505 + }) 506 + } 507 + } 508 + 509 + if len(rBranches) == 0 { 510 + return nil, fmt.Errorf("WARN: Could not get any branch revisions for repo %s", name) 511 + } 512 + 513 + return rBranches, nil 479 514 } 480 515 481 516 func (sf sourcegraphFake) id(name string) uint32 {