servegit: Walk has logic for using the root (#48603)

Now that we don't have the tricky post processing we can move all the
important logic of discovering repos into the Walk function. I imagine
in the future we will have external callers to Walk so making this
correct will help.

Test Plan: go test
This commit is contained in:
Keegan Carruthers-Smith 2023-03-03 16:08:27 +02:00 committed by GitHub
parent d5cefc1aed
commit 8ead93db58
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 15 additions and 17 deletions

View File

@ -4,7 +4,6 @@ import (
"flag"
"fmt"
"os"
"path/filepath"
"strings"
"github.com/sourcegraph/log"
@ -66,7 +65,7 @@ func main() {
repoC := make(chan servegit.Repo, 4)
go func() {
defer close(repoC)
err := srv.Walk(filepath.Clean(*root), repoC)
err := srv.Walk(repoC)
if err != nil {
fmt.Fprintf(os.Stderr, "Walk returned error: %v\n", err)
os.Exit(1)

View File

@ -178,23 +178,17 @@ func isGitRepo(path string) bool {
return string(out) == ".git\n"
}
// Repos returns a slice of all the git repositories it finds.
// Repos returns a slice of all the git repositories it finds. It is a wrapper
// around Walk which removes the need to deal with channels and sorts the
// response.
func (s *Serve) Repos() ([]Repo, error) {
root, err := filepath.EvalSymlinks(s.Root)
if err != nil {
s.Logger.Warn("ignoring error searching", log.String("path", root), log.Error(err))
return nil, nil
}
root = filepath.Clean(root)
var (
repoC = make(chan Repo, 4) // 4 is the same buffer size used in fastwalk
walkErr error
)
go func() {
defer close(repoC)
walkErr = s.Walk(root, repoC)
walkErr = s.Walk(repoC)
}()
var repos []Repo
@ -215,10 +209,15 @@ func (s *Serve) Repos() ([]Repo, error) {
return repos, nil
}
// Walk is the core repos finding routine. This is only exported for use in
// app-discover-repos, normally you should use Repos instead which does
// additional work.
func (s *Serve) Walk(root string, repoC chan<- Repo) error {
// Walk is the core repos finding routine.
func (s *Serve) Walk(repoC chan<- Repo) error {
root, err := filepath.EvalSymlinks(s.Root)
if err != nil {
s.Logger.Warn("ignoring error searching", log.String("path", root), log.Error(err))
return nil
}
root = filepath.Clean(root)
if repo, ok, err := rootIsRepo(root); err != nil {
return err
} else if ok {
@ -242,7 +241,7 @@ func (s *Serve) Walk(root string, repoC chan<- Repo) error {
// - you can return fastwalk.ErrSkipFiles to avoid calling func on
// files (so will only get dirs)
// - filepath.SkipDir has the same meaning
err := fastwalk.Walk(root, func(path string, typ os.FileMode) error {
err = fastwalk.Walk(root, func(path string, typ os.FileMode) error {
if ctx.Err() != nil {
return ctx.Err()
}