Commit 0e3e46f0 authored by Bryan C. Mills's avatar Bryan C. Mills

cmd/go/internal/get: simplify scheme lookup

Fixes #26123

Change-Id: If0dad65a3885d2146624f2aac42099e9eca9670e
Reviewed-on: https://go-review.googlesource.com/c/go/+/200758
Run-TryBot: Bryan C. Mills <bcmills@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: default avatarJay Conrod <jayconrod@google.com>
parent 78d67164
...@@ -531,12 +531,12 @@ func (v *vcsCmd) tagSync(dir, tag string) error { ...@@ -531,12 +531,12 @@ func (v *vcsCmd) tagSync(dir, tag string) error {
// A vcsPath describes how to convert an import path into a // A vcsPath describes how to convert an import path into a
// version control system and repository name. // version control system and repository name.
type vcsPath struct { type vcsPath struct {
prefix string // prefix this description applies to prefix string // prefix this description applies to
regexp *lazyregexp.Regexp // compiled pattern for import path regexp *lazyregexp.Regexp // compiled pattern for import path
repo string // repository to use (expand with match of re) repo string // repository to use (expand with match of re)
vcs string // version control system to use (expand with match of re) vcs string // version control system to use (expand with match of re)
check func(match map[string]string) error // additional checks check func(match map[string]string) error // additional checks
ping bool // ping for scheme to use to download repo schemelessRepo bool // if true, the repo pattern lacks a scheme
} }
// vcsFromDir inspects dir and its parents to determine the // vcsFromDir inspects dir and its parents to determine the
...@@ -657,7 +657,7 @@ const ( ...@@ -657,7 +657,7 @@ const (
// RepoRootForImportPath analyzes importPath to determine the // RepoRootForImportPath analyzes importPath to determine the
// version control system, and code repository to use. // version control system, and code repository to use.
func RepoRootForImportPath(importPath string, mod ModuleMode, security web.SecurityMode) (*RepoRoot, error) { func RepoRootForImportPath(importPath string, mod ModuleMode, security web.SecurityMode) (*RepoRoot, error) {
rr, err := repoRootFromVCSPaths(importPath, "", security, vcsPaths) rr, err := repoRootFromVCSPaths(importPath, security, vcsPaths)
if err == errUnknownSite { if err == errUnknownSite {
rr, err = repoRootForImportDynamic(importPath, mod, security) rr, err = repoRootForImportDynamic(importPath, mod, security)
if err != nil { if err != nil {
...@@ -665,7 +665,7 @@ func RepoRootForImportPath(importPath string, mod ModuleMode, security web.Secur ...@@ -665,7 +665,7 @@ func RepoRootForImportPath(importPath string, mod ModuleMode, security web.Secur
} }
} }
if err != nil { if err != nil {
rr1, err1 := repoRootFromVCSPaths(importPath, "", security, vcsPathsAfterDynamic) rr1, err1 := repoRootFromVCSPaths(importPath, security, vcsPathsAfterDynamic)
if err1 == nil { if err1 == nil {
rr = rr1 rr = rr1
err = nil err = nil
...@@ -685,8 +685,7 @@ var errUnknownSite = errors.New("dynamic lookup required to find mapping") ...@@ -685,8 +685,7 @@ var errUnknownSite = errors.New("dynamic lookup required to find mapping")
// repoRootFromVCSPaths attempts to map importPath to a repoRoot // repoRootFromVCSPaths attempts to map importPath to a repoRoot
// using the mappings defined in vcsPaths. // using the mappings defined in vcsPaths.
// If scheme is non-empty, that scheme is forced. func repoRootFromVCSPaths(importPath string, security web.SecurityMode, vcsPaths []*vcsPath) (*RepoRoot, error) {
func repoRootFromVCSPaths(importPath, scheme string, security web.SecurityMode, vcsPaths []*vcsPath) (*RepoRoot, error) {
// A common error is to use https://packagepath because that's what // A common error is to use https://packagepath because that's what
// hg and git require. Diagnose this helpfully. // hg and git require. Diagnose this helpfully.
if prefix := httpPrefix(importPath); prefix != "" { if prefix := httpPrefix(importPath); prefix != "" {
...@@ -731,26 +730,28 @@ func repoRootFromVCSPaths(importPath, scheme string, security web.SecurityMode, ...@@ -731,26 +730,28 @@ func repoRootFromVCSPaths(importPath, scheme string, security web.SecurityMode,
if vcs == nil { if vcs == nil {
return nil, fmt.Errorf("unknown version control system %q", match["vcs"]) return nil, fmt.Errorf("unknown version control system %q", match["vcs"])
} }
if srv.ping { var repoURL string
if scheme != "" { if !srv.schemelessRepo {
match["repo"] = scheme + "://" + match["repo"] repoURL = match["repo"]
} else { } else {
for _, scheme := range vcs.scheme { scheme := vcs.scheme[0] // default to first scheme
if security == web.SecureOnly && !vcs.isSecureScheme(scheme) { repo := match["repo"]
if vcs.pingCmd != "" {
// If we know how to test schemes, scan to find one.
for _, s := range vcs.scheme {
if security == web.SecureOnly && !vcs.isSecureScheme(s) {
continue continue
} }
if vcs.pingCmd != "" && vcs.ping(scheme, match["repo"]) == nil { if vcs.ping(s, repo) == nil {
match["repo"] = scheme + "://" + match["repo"] scheme = s
goto Found break
} }
} }
// No scheme found. Fall back to the first one.
match["repo"] = vcs.scheme[0] + "://" + match["repo"]
Found:
} }
repoURL = scheme + "://" + repo
} }
rr := &RepoRoot{ rr := &RepoRoot{
Repo: match["repo"], Repo: repoURL,
Root: match["root"], Root: match["root"],
VCS: vcs.cmd, VCS: vcs.cmd,
vcs: vcs, vcs: vcs,
...@@ -1075,8 +1076,8 @@ var vcsPaths = []*vcsPath{ ...@@ -1075,8 +1076,8 @@ var vcsPaths = []*vcsPath{
// General syntax for any server. // General syntax for any server.
// Must be last. // Must be last.
{ {
regexp: lazyregexp.New(`(?P<root>(?P<repo>([a-z0-9.\-]+\.)+[a-z0-9.\-]+(:[0-9]+)?(/~?[A-Za-z0-9_.\-]+)+?)\.(?P<vcs>bzr|fossil|git|hg|svn))(/~?[A-Za-z0-9_.\-]+)*$`), regexp: lazyregexp.New(`(?P<root>(?P<repo>([a-z0-9.\-]+\.)+[a-z0-9.\-]+(:[0-9]+)?(/~?[A-Za-z0-9_.\-]+)+?)\.(?P<vcs>bzr|fossil|git|hg|svn))(/~?[A-Za-z0-9_.\-]+)*$`),
ping: true, schemelessRepo: true,
}, },
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment