Skip to content

Commit

Permalink
cmd/go/internal/get: simplify scheme lookup
Browse files Browse the repository at this point in the history
Fixes #26123

Change-Id: If0dad65a3885d2146624f2aac42099e9eca9670e
Reviewed-on: https://go-review.googlesource.com/c/go/+/200758
Run-TryBot: Bryan C. Mills <[email protected]>
TryBot-Result: Gobot Gobot <[email protected]>
Reviewed-by: Jay Conrod <[email protected]>
  • Loading branch information
Bryan C. Mills committed Oct 11, 2019
1 parent 78d6716 commit 0e3e46f
Showing 1 changed file with 26 additions and 25 deletions.
51 changes: 26 additions & 25 deletions src/cmd/go/internal/get/vcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -531,12 +531,12 @@ func (v *vcsCmd) tagSync(dir, tag string) error {
// A vcsPath describes how to convert an import path into a
// version control system and repository name.
type vcsPath struct {
prefix string // prefix this description applies to
regexp *lazyregexp.Regexp // compiled pattern for import path
repo string // repository 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
ping bool // ping for scheme to use to download repo
prefix string // prefix this description applies to
regexp *lazyregexp.Regexp // compiled pattern for import path
repo string // repository 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
schemelessRepo bool // if true, the repo pattern lacks a scheme
}

// vcsFromDir inspects dir and its parents to determine the
Expand Down Expand Up @@ -657,15 +657,15 @@ const (
// RepoRootForImportPath analyzes importPath to determine the
// version control system, and code repository to use.
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 {
rr, err = repoRootForImportDynamic(importPath, mod, security)
if err != nil {
err = fmt.Errorf("unrecognized import path %q: %v", importPath, err)
}
}
if err != nil {
rr1, err1 := repoRootFromVCSPaths(importPath, "", security, vcsPathsAfterDynamic)
rr1, err1 := repoRootFromVCSPaths(importPath, security, vcsPathsAfterDynamic)
if err1 == nil {
rr = rr1
err = nil
Expand All @@ -685,8 +685,7 @@ var errUnknownSite = errors.New("dynamic lookup required to find mapping")

// repoRootFromVCSPaths attempts to map importPath to a repoRoot
// using the mappings defined in vcsPaths.
// If scheme is non-empty, that scheme is forced.
func repoRootFromVCSPaths(importPath, scheme string, security web.SecurityMode, vcsPaths []*vcsPath) (*RepoRoot, error) {
func repoRootFromVCSPaths(importPath string, security web.SecurityMode, vcsPaths []*vcsPath) (*RepoRoot, error) {
// A common error is to use https://packagepath because that's what
// hg and git require. Diagnose this helpfully.
if prefix := httpPrefix(importPath); prefix != "" {
Expand Down Expand Up @@ -731,26 +730,28 @@ func repoRootFromVCSPaths(importPath, scheme string, security web.SecurityMode,
if vcs == nil {
return nil, fmt.Errorf("unknown version control system %q", match["vcs"])
}
if srv.ping {
if scheme != "" {
match["repo"] = scheme + ":https://" + match["repo"]
} else {
for _, scheme := range vcs.scheme {
if security == web.SecureOnly && !vcs.isSecureScheme(scheme) {
var repoURL string
if !srv.schemelessRepo {
repoURL = match["repo"]
} else {
scheme := vcs.scheme[0] // default to first 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
}
if vcs.pingCmd != "" && vcs.ping(scheme, match["repo"]) == nil {
match["repo"] = scheme + ":https://" + match["repo"]
goto Found
if vcs.ping(s, repo) == nil {
scheme = s
break
}
}
// No scheme found. Fall back to the first one.
match["repo"] = vcs.scheme[0] + ":https://" + match["repo"]
Found:
}
repoURL = scheme + ":https://" + repo
}
rr := &RepoRoot{
Repo: match["repo"],
Repo: repoURL,
Root: match["root"],
VCS: vcs.cmd,
vcs: vcs,
Expand Down Expand Up @@ -1075,8 +1076,8 @@ var vcsPaths = []*vcsPath{
// General syntax for any server.
// 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_.\-]+)*$`),
ping: true,
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_.\-]+)*$`),
schemelessRepo: true,
},
}

Expand Down

0 comments on commit 0e3e46f

Please sign in to comment.