func loadPackage(fs vfs.FileSystem, dir, importPath string) (*Package, error) {
	for _, env := range [...]struct{ GOOS, GOARCH string }{
		{"linux", "amd64"},
		{"darwin", "amd64"},
	} {
		bctx := &build.Context{
			GOOS:        env.GOOS,
			GOARCH:      env.GOARCH,
			CgoEnabled:  true,
			Compiler:    build.Default.Compiler,
			ReleaseTags: build.Default.ReleaseTags,
			JoinPath:      path.Join,
			SplitPathList: splitPathList,
			IsAbsPath:     path.IsAbs,
			IsDir: func(path string) bool {
				fi, err := fs.Stat(path)
				return err == nil && fi.IsDir()
			},
			HasSubdir: hasSubdir,
			ReadDir:   func(dir string) ([]os.FileInfo, error) { return fs.ReadDir(dir) },
			OpenFile:  func(path string) (io.ReadCloser, error) { return fs.Open(path) },
		}
		p, err := bctx.ImportDir(dir, 0)
		if _, ok := err.(*build.NoGoError); ok {
			if buildConstraintsExcludeAll(p) {
				continue
			}
			break
		} else if err != nil {
			return nil, err
		}
		dpkg, err := computeDoc(bctx, p)
		if err != nil {
			return nil, fmt.Errorf("can't get godoc of package %q: %v", importPath, err)
		}
		return &Package{
			Name:     p.Name,
			Synopsis: p.Doc,
			DocHTML:  docHTML(dpkg.Doc),
		}, nil
	}
	return nil, nil
}
loadPackage loads a Go package with import path importPath
from filesystem fs in directory dir.
It returns a nil Package if the directory doesn't contain a Go package.
package code
import (
	"bytes"
	"fmt"
	"go/ast"
	"go/build"
	"go/doc"
	"go/parser"
	"go/token"
	"io"
	"log"
	"os"
	"path"
	"path/filepath"
	"strings"
	"github.com/shurcooL/go/vfs/godocfs/vfsutil"
	"golang.org/x/tools/go/buildutil"
	"golang.org/x/tools/godoc/vfs"
	"sourcegraph.com/sourcegraph/go-vcs/vcs"
	"sourcegraph.com/sourcegraph/go-vcs/vcs/git"
)
func discover(reposDir string) ([]*Directory, map[string]*Directory, error) {
	dirs, err := walkRepositoryStore(reposDir)
	if err != nil {
		return nil, nil, err
	}
	var byImportPath = make(map[string]*Directory)
	for _, d := range dirs {
		byImportPath[d.ImportPath] = d
	}
	populateLicenseRoot(dirs, byImportPath)
	return dirs, byImportPath, nil
}
func populateLicenseRoot(dirs []*Directory, byImportPath map[string]*Directory) {
	for _, dir := range dirs {
		if dir.HasLicenseFile() {
			continue
		}
		elems := strings.Split(dir.ImportPath, "/")
		for i := len(elems) - 1; i >= 1; i-- {  
			p, ok := byImportPath[path.Join(elems[:i]...)]
			if ok && p.HasLicenseFile() {
				dir.LicenseRoot = p.ImportPath
				break
			}
		}
	}
}
func walkRepositoryStore(reposDir string) ([]*Directory, error) {
	var dirs []*Directory
	err := filepath.Walk(reposDir, func(path string, fi os.FileInfo, err error) error {
		if err != nil {
			return err
		}
		if !fi.IsDir() {
			return nil
		}
		if strings.HasPrefix(fi.Name(), ".") || strings.HasPrefix(fi.Name(), "_") || fi.Name() == "testdata" {
			return filepath.SkipDir
		}
		ok, err := isBareGitRepository(path)
		if err != nil {
			return err
		} else if !ok {
			return nil
		}
		ds, err := walkRepository(path, path[len(reposDir)+1:])
		if err != nil {
			return fmt.Errorf("walkRepository(%q): %v", path, err)
		}
		dirs = append(dirs, ds...)
		return filepath.SkipDir
	})
	return dirs, err
}
func isBareGitRepository(dir string) (bool, error) {
	head, err := os.Stat(filepath.Join(dir, "HEAD"))
	if os.IsNotExist(err) {
		return false, nil
	} else if err != nil {
		return false, err
	}
	return !head.IsDir(), nil
}
func walkRepository(gitDir, repoRoot string) ([]*Directory, error) {
	r, err := git.Open(gitDir)
	if err != nil {
		return nil, err
	}
	defer func() {
		err := r.Close()
		if err != nil {
			log.Println("walkRepository: r.Close:", err)
		}
	}()
	master, err := r.ResolveBranch("master")
	if err == vcs.ErrBranchNotFound {
		return []*Directory{{
			ImportPath: repoRoot,
			RepoRoot:   repoRoot,
		}}, nil
	} else if err != nil {
		return nil, err
	}
	fs, err := r.FileSystem(master)
	if err != nil {
		return nil, err
	}
	var (
		dirs         []*Directory
		repoPackages int
	)
	err = vfsutil.Walk(fs, "/", func(dir string, fi os.FileInfo, err error) error {
		if err != nil {
			return err
		}
		if !fi.IsDir() {
			return nil
		}
		if strings.HasPrefix(fi.Name(), ".") || strings.HasPrefix(fi.Name(), "_") || fi.Name() == "testdata" {
			return filepath.SkipDir
		}
		importPath := path.Join(repoRoot, dir)
		var licenseRoot string
		if ok, err := hasLicenseFile(fs, dir); err == nil && ok {
			licenseRoot = importPath
		} else if err != nil {
			return err
		}
		pkg, err := loadPackage(fs, dir, importPath)
		if err != nil {
			return err
		}
		if pkg != nil {
			repoPackages++
		}
		dirs = append(dirs, &Directory{
			ImportPath:  importPath,
			RepoRoot:    repoRoot,
			LicenseRoot: licenseRoot,
			Package:     pkg,
		})
		return nil
	})
	if err != nil {
		return nil, err
	}
	for _, d := range dirs {
		d.RepoPackages = repoPackages
	}
	return dirs, nil
}