github.com/peggyl/go@v0.0.0-20151008231540-ae315999c2d5/src/cmd/go/vcs.go (about)

     1  // Copyright 2012 The Go Authors.  All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package main
     6  
     7  import (
     8  	"bytes"
     9  	"encoding/json"
    10  	"errors"
    11  	"fmt"
    12  	"internal/singleflight"
    13  	"log"
    14  	"net/url"
    15  	"os"
    16  	"os/exec"
    17  	"path/filepath"
    18  	"regexp"
    19  	"strings"
    20  	"sync"
    21  )
    22  
    23  // A vcsCmd describes how to use a version control system
    24  // like Mercurial, Git, or Subversion.
    25  type vcsCmd struct {
    26  	name string
    27  	cmd  string // name of binary to invoke command
    28  
    29  	createCmd   []string // commands to download a fresh copy of a repository
    30  	downloadCmd []string // commands to download updates into an existing repository
    31  
    32  	tagCmd         []tagCmd // commands to list tags
    33  	tagLookupCmd   []tagCmd // commands to lookup tags before running tagSyncCmd
    34  	tagSyncCmd     []string // commands to sync to specific tag
    35  	tagSyncDefault []string // commands to sync to default tag
    36  
    37  	scheme  []string
    38  	pingCmd string
    39  
    40  	remoteRepo  func(v *vcsCmd, rootDir string) (remoteRepo string, err error)
    41  	resolveRepo func(v *vcsCmd, rootDir, remoteRepo string) (realRepo string, err error)
    42  }
    43  
    44  var isSecureScheme = map[string]bool{
    45  	"https":   true,
    46  	"git+ssh": true,
    47  	"bzr+ssh": true,
    48  	"svn+ssh": true,
    49  	"ssh":     true,
    50  }
    51  
    52  func (v *vcsCmd) isSecure(repo string) bool {
    53  	u, err := url.Parse(repo)
    54  	if err != nil {
    55  		// If repo is not a URL, it's not secure.
    56  		return false
    57  	}
    58  	return isSecureScheme[u.Scheme]
    59  }
    60  
    61  // A tagCmd describes a command to list available tags
    62  // that can be passed to tagSyncCmd.
    63  type tagCmd struct {
    64  	cmd     string // command to list tags
    65  	pattern string // regexp to extract tags from list
    66  }
    67  
    68  // vcsList lists the known version control systems
    69  var vcsList = []*vcsCmd{
    70  	vcsHg,
    71  	vcsGit,
    72  	vcsSvn,
    73  	vcsBzr,
    74  }
    75  
    76  // vcsByCmd returns the version control system for the given
    77  // command name (hg, git, svn, bzr).
    78  func vcsByCmd(cmd string) *vcsCmd {
    79  	for _, vcs := range vcsList {
    80  		if vcs.cmd == cmd {
    81  			return vcs
    82  		}
    83  	}
    84  	return nil
    85  }
    86  
    87  // vcsHg describes how to use Mercurial.
    88  var vcsHg = &vcsCmd{
    89  	name: "Mercurial",
    90  	cmd:  "hg",
    91  
    92  	createCmd:   []string{"clone -U {repo} {dir}"},
    93  	downloadCmd: []string{"pull"},
    94  
    95  	// We allow both tag and branch names as 'tags'
    96  	// for selecting a version.  This lets people have
    97  	// a go.release.r60 branch and a go1 branch
    98  	// and make changes in both, without constantly
    99  	// editing .hgtags.
   100  	tagCmd: []tagCmd{
   101  		{"tags", `^(\S+)`},
   102  		{"branches", `^(\S+)`},
   103  	},
   104  	tagSyncCmd:     []string{"update -r {tag}"},
   105  	tagSyncDefault: []string{"update default"},
   106  
   107  	scheme:     []string{"https", "http", "ssh"},
   108  	pingCmd:    "identify {scheme}://{repo}",
   109  	remoteRepo: hgRemoteRepo,
   110  }
   111  
   112  func hgRemoteRepo(vcsHg *vcsCmd, rootDir string) (remoteRepo string, err error) {
   113  	out, err := vcsHg.runOutput(rootDir, "paths default")
   114  	if err != nil {
   115  		return "", err
   116  	}
   117  	return strings.TrimSpace(string(out)), nil
   118  }
   119  
   120  // vcsGit describes how to use Git.
   121  var vcsGit = &vcsCmd{
   122  	name: "Git",
   123  	cmd:  "git",
   124  
   125  	createCmd:   []string{"clone {repo} {dir}", "--git-dir={dir}/.git submodule update --init --recursive"},
   126  	downloadCmd: []string{"pull --ff-only", "submodule update --init --recursive"},
   127  
   128  	tagCmd: []tagCmd{
   129  		// tags/xxx matches a git tag named xxx
   130  		// origin/xxx matches a git branch named xxx on the default remote repository
   131  		{"show-ref", `(?:tags|origin)/(\S+)$`},
   132  	},
   133  	tagLookupCmd: []tagCmd{
   134  		{"show-ref tags/{tag} origin/{tag}", `((?:tags|origin)/\S+)$`},
   135  	},
   136  	tagSyncCmd: []string{"checkout {tag}", "submodule update --init --recursive"},
   137  	// both createCmd and downloadCmd update the working dir.
   138  	// No need to do more here. We used to 'checkout master'
   139  	// but that doesn't work if the default branch is not named master.
   140  	// See golang.org/issue/9032.
   141  	tagSyncDefault: []string{"checkout master", "submodule update --init --recursive"},
   142  
   143  	scheme:     []string{"git", "https", "http", "git+ssh", "ssh"},
   144  	pingCmd:    "ls-remote {scheme}://{repo}",
   145  	remoteRepo: gitRemoteRepo,
   146  }
   147  
   148  // scpSyntaxRe matches the SCP-like addresses used by Git to access
   149  // repositories by SSH.
   150  var scpSyntaxRe = regexp.MustCompile(`^([a-zA-Z0-9_]+)@([a-zA-Z0-9._-]+):(.*)$`)
   151  
   152  func gitRemoteRepo(vcsGit *vcsCmd, rootDir string) (remoteRepo string, err error) {
   153  	cmd := "config remote.origin.url"
   154  	errParse := errors.New("unable to parse output of git " + cmd)
   155  	errRemoteOriginNotFound := errors.New("remote origin not found")
   156  	outb, err := vcsGit.run1(rootDir, cmd, nil, false)
   157  	if err != nil {
   158  		// if it doesn't output any message, it means the config argument is correct,
   159  		// but the config value itself doesn't exist
   160  		if outb != nil && len(outb) == 0 {
   161  			return "", errRemoteOriginNotFound
   162  		}
   163  		return "", err
   164  	}
   165  	out := strings.TrimSpace(string(outb))
   166  
   167  	var repoURL *url.URL
   168  	if m := scpSyntaxRe.FindStringSubmatch(out); m != nil {
   169  		// Match SCP-like syntax and convert it to a URL.
   170  		// Eg, "git@github.com:user/repo" becomes
   171  		// "ssh://git@github.com/user/repo".
   172  		repoURL = &url.URL{
   173  			Scheme:  "ssh",
   174  			User:    url.User(m[1]),
   175  			Host:    m[2],
   176  			RawPath: m[3],
   177  		}
   178  	} else {
   179  		repoURL, err = url.Parse(out)
   180  		if err != nil {
   181  			return "", err
   182  		}
   183  	}
   184  
   185  	// Iterate over insecure schemes too, because this function simply
   186  	// reports the state of the repo. If we can't see insecure schemes then
   187  	// we can't report the actual repo URL.
   188  	for _, s := range vcsGit.scheme {
   189  		if repoURL.Scheme == s {
   190  			return repoURL.String(), nil
   191  		}
   192  	}
   193  	return "", errParse
   194  }
   195  
   196  // vcsBzr describes how to use Bazaar.
   197  var vcsBzr = &vcsCmd{
   198  	name: "Bazaar",
   199  	cmd:  "bzr",
   200  
   201  	createCmd: []string{"branch {repo} {dir}"},
   202  
   203  	// Without --overwrite bzr will not pull tags that changed.
   204  	// Replace by --overwrite-tags after http://pad.lv/681792 goes in.
   205  	downloadCmd: []string{"pull --overwrite"},
   206  
   207  	tagCmd:         []tagCmd{{"tags", `^(\S+)`}},
   208  	tagSyncCmd:     []string{"update -r {tag}"},
   209  	tagSyncDefault: []string{"update -r revno:-1"},
   210  
   211  	scheme:      []string{"https", "http", "bzr", "bzr+ssh"},
   212  	pingCmd:     "info {scheme}://{repo}",
   213  	remoteRepo:  bzrRemoteRepo,
   214  	resolveRepo: bzrResolveRepo,
   215  }
   216  
   217  func bzrRemoteRepo(vcsBzr *vcsCmd, rootDir string) (remoteRepo string, err error) {
   218  	outb, err := vcsBzr.runOutput(rootDir, "config parent_location")
   219  	if err != nil {
   220  		return "", err
   221  	}
   222  	return strings.TrimSpace(string(outb)), nil
   223  }
   224  
   225  func bzrResolveRepo(vcsBzr *vcsCmd, rootDir, remoteRepo string) (realRepo string, err error) {
   226  	outb, err := vcsBzr.runOutput(rootDir, "info "+remoteRepo)
   227  	if err != nil {
   228  		return "", err
   229  	}
   230  	out := string(outb)
   231  
   232  	// Expect:
   233  	// ...
   234  	//   (branch root|repository branch): <URL>
   235  	// ...
   236  
   237  	found := false
   238  	for _, prefix := range []string{"\n  branch root: ", "\n  repository branch: "} {
   239  		i := strings.Index(out, prefix)
   240  		if i >= 0 {
   241  			out = out[i+len(prefix):]
   242  			found = true
   243  			break
   244  		}
   245  	}
   246  	if !found {
   247  		return "", fmt.Errorf("unable to parse output of bzr info")
   248  	}
   249  
   250  	i := strings.Index(out, "\n")
   251  	if i < 0 {
   252  		return "", fmt.Errorf("unable to parse output of bzr info")
   253  	}
   254  	out = out[:i]
   255  	return strings.TrimSpace(string(out)), nil
   256  }
   257  
   258  // vcsSvn describes how to use Subversion.
   259  var vcsSvn = &vcsCmd{
   260  	name: "Subversion",
   261  	cmd:  "svn",
   262  
   263  	createCmd:   []string{"checkout {repo} {dir}"},
   264  	downloadCmd: []string{"update"},
   265  
   266  	// There is no tag command in subversion.
   267  	// The branch information is all in the path names.
   268  
   269  	scheme:     []string{"https", "http", "svn", "svn+ssh"},
   270  	pingCmd:    "info {scheme}://{repo}",
   271  	remoteRepo: svnRemoteRepo,
   272  }
   273  
   274  func svnRemoteRepo(vcsSvn *vcsCmd, rootDir string) (remoteRepo string, err error) {
   275  	outb, err := vcsSvn.runOutput(rootDir, "info")
   276  	if err != nil {
   277  		return "", err
   278  	}
   279  	out := string(outb)
   280  
   281  	// Expect:
   282  	// ...
   283  	// Repository Root: <URL>
   284  	// ...
   285  
   286  	i := strings.Index(out, "\nRepository Root: ")
   287  	if i < 0 {
   288  		return "", fmt.Errorf("unable to parse output of svn info")
   289  	}
   290  	out = out[i+len("\nRepository Root: "):]
   291  	i = strings.Index(out, "\n")
   292  	if i < 0 {
   293  		return "", fmt.Errorf("unable to parse output of svn info")
   294  	}
   295  	out = out[:i]
   296  	return strings.TrimSpace(string(out)), nil
   297  }
   298  
   299  func (v *vcsCmd) String() string {
   300  	return v.name
   301  }
   302  
   303  // run runs the command line cmd in the given directory.
   304  // keyval is a list of key, value pairs.  run expands
   305  // instances of {key} in cmd into value, but only after
   306  // splitting cmd into individual arguments.
   307  // If an error occurs, run prints the command line and the
   308  // command's combined stdout+stderr to standard error.
   309  // Otherwise run discards the command's output.
   310  func (v *vcsCmd) run(dir string, cmd string, keyval ...string) error {
   311  	_, err := v.run1(dir, cmd, keyval, true)
   312  	return err
   313  }
   314  
   315  // runVerboseOnly is like run but only generates error output to standard error in verbose mode.
   316  func (v *vcsCmd) runVerboseOnly(dir string, cmd string, keyval ...string) error {
   317  	_, err := v.run1(dir, cmd, keyval, false)
   318  	return err
   319  }
   320  
   321  // runOutput is like run but returns the output of the command.
   322  func (v *vcsCmd) runOutput(dir string, cmd string, keyval ...string) ([]byte, error) {
   323  	return v.run1(dir, cmd, keyval, true)
   324  }
   325  
   326  // run1 is the generalized implementation of run and runOutput.
   327  func (v *vcsCmd) run1(dir string, cmdline string, keyval []string, verbose bool) ([]byte, error) {
   328  	m := make(map[string]string)
   329  	for i := 0; i < len(keyval); i += 2 {
   330  		m[keyval[i]] = keyval[i+1]
   331  	}
   332  	args := strings.Fields(cmdline)
   333  	for i, arg := range args {
   334  		args[i] = expand(m, arg)
   335  	}
   336  
   337  	_, err := exec.LookPath(v.cmd)
   338  	if err != nil {
   339  		fmt.Fprintf(os.Stderr,
   340  			"go: missing %s command. See https://golang.org/s/gogetcmd\n",
   341  			v.name)
   342  		return nil, err
   343  	}
   344  
   345  	cmd := exec.Command(v.cmd, args...)
   346  	cmd.Dir = dir
   347  	cmd.Env = envForDir(cmd.Dir, os.Environ())
   348  	if buildX {
   349  		fmt.Printf("cd %s\n", dir)
   350  		fmt.Printf("%s %s\n", v.cmd, strings.Join(args, " "))
   351  	}
   352  	var buf bytes.Buffer
   353  	cmd.Stdout = &buf
   354  	cmd.Stderr = &buf
   355  	err = cmd.Run()
   356  	out := buf.Bytes()
   357  	if err != nil {
   358  		if verbose || buildV {
   359  			fmt.Fprintf(os.Stderr, "# cd %s; %s %s\n", dir, v.cmd, strings.Join(args, " "))
   360  			os.Stderr.Write(out)
   361  		}
   362  		return out, err
   363  	}
   364  	return out, nil
   365  }
   366  
   367  // ping pings to determine scheme to use.
   368  func (v *vcsCmd) ping(scheme, repo string) error {
   369  	return v.runVerboseOnly(".", v.pingCmd, "scheme", scheme, "repo", repo)
   370  }
   371  
   372  // create creates a new copy of repo in dir.
   373  // The parent of dir must exist; dir must not.
   374  func (v *vcsCmd) create(dir, repo string) error {
   375  	for _, cmd := range v.createCmd {
   376  		if !go15VendorExperiment && strings.Contains(cmd, "submodule") {
   377  			continue
   378  		}
   379  		if err := v.run(".", cmd, "dir", dir, "repo", repo); err != nil {
   380  			return err
   381  		}
   382  	}
   383  	return nil
   384  }
   385  
   386  // download downloads any new changes for the repo in dir.
   387  func (v *vcsCmd) download(dir string) error {
   388  	if err := v.fixDetachedHead(dir); err != nil {
   389  		return err
   390  	}
   391  	for _, cmd := range v.downloadCmd {
   392  		if !go15VendorExperiment && strings.Contains(cmd, "submodule") {
   393  			continue
   394  		}
   395  		if err := v.run(dir, cmd); err != nil {
   396  			return err
   397  		}
   398  	}
   399  	return nil
   400  }
   401  
   402  // fixDetachedHead switches a Git repository in dir from a detached head to the master branch.
   403  // Go versions before 1.2 downloaded Git repositories in an unfortunate way
   404  // that resulted in the working tree state being on a detached head.
   405  // That meant the repository was not usable for normal Git operations.
   406  // Go 1.2 fixed that, but we can't pull into a detached head, so if this is
   407  // a Git repository we check for being on a detached head and switch to the
   408  // real branch, almost always called "master".
   409  // TODO(dsymonds): Consider removing this for Go 1.3.
   410  func (v *vcsCmd) fixDetachedHead(dir string) error {
   411  	if v != vcsGit {
   412  		return nil
   413  	}
   414  
   415  	// "git symbolic-ref HEAD" succeeds iff we are not on a detached head.
   416  	if err := v.runVerboseOnly(dir, "symbolic-ref HEAD"); err == nil {
   417  		// not on a detached head
   418  		return nil
   419  	}
   420  	if buildV {
   421  		log.Printf("%s on detached head; repairing", dir)
   422  	}
   423  	return v.run(dir, "checkout master")
   424  }
   425  
   426  // tags returns the list of available tags for the repo in dir.
   427  func (v *vcsCmd) tags(dir string) ([]string, error) {
   428  	var tags []string
   429  	for _, tc := range v.tagCmd {
   430  		out, err := v.runOutput(dir, tc.cmd)
   431  		if err != nil {
   432  			return nil, err
   433  		}
   434  		re := regexp.MustCompile(`(?m-s)` + tc.pattern)
   435  		for _, m := range re.FindAllStringSubmatch(string(out), -1) {
   436  			tags = append(tags, m[1])
   437  		}
   438  	}
   439  	return tags, nil
   440  }
   441  
   442  // tagSync syncs the repo in dir to the named tag,
   443  // which either is a tag returned by tags or is v.tagDefault.
   444  func (v *vcsCmd) tagSync(dir, tag string) error {
   445  	if v.tagSyncCmd == nil {
   446  		return nil
   447  	}
   448  	if tag != "" {
   449  		for _, tc := range v.tagLookupCmd {
   450  			out, err := v.runOutput(dir, tc.cmd, "tag", tag)
   451  			if err != nil {
   452  				return err
   453  			}
   454  			re := regexp.MustCompile(`(?m-s)` + tc.pattern)
   455  			m := re.FindStringSubmatch(string(out))
   456  			if len(m) > 1 {
   457  				tag = m[1]
   458  				break
   459  			}
   460  		}
   461  	}
   462  
   463  	if tag == "" && v.tagSyncDefault != nil {
   464  		for _, cmd := range v.tagSyncDefault {
   465  			if !go15VendorExperiment && strings.Contains(cmd, "submodule") {
   466  				continue
   467  			}
   468  			if err := v.run(dir, cmd); err != nil {
   469  				return err
   470  			}
   471  		}
   472  		return nil
   473  	}
   474  
   475  	for _, cmd := range v.tagSyncCmd {
   476  		if !go15VendorExperiment && strings.Contains(cmd, "submodule") {
   477  			continue
   478  		}
   479  		if err := v.run(dir, cmd, "tag", tag); err != nil {
   480  			return err
   481  		}
   482  	}
   483  	return nil
   484  }
   485  
   486  // A vcsPath describes how to convert an import path into a
   487  // version control system and repository name.
   488  type vcsPath struct {
   489  	prefix string                              // prefix this description applies to
   490  	re     string                              // pattern for import path
   491  	repo   string                              // repository to use (expand with match of re)
   492  	vcs    string                              // version control system to use (expand with match of re)
   493  	check  func(match map[string]string) error // additional checks
   494  	ping   bool                                // ping for scheme to use to download repo
   495  
   496  	regexp *regexp.Regexp // cached compiled form of re
   497  }
   498  
   499  // vcsForDir inspects dir and its parents to determine the
   500  // version control system and code repository to use.
   501  // On return, root is the import path
   502  // corresponding to the root of the repository
   503  // (thus root is a prefix of importPath).
   504  func vcsForDir(p *Package) (vcs *vcsCmd, root string, err error) {
   505  	// Clean and double-check that dir is in (a subdirectory of) srcRoot.
   506  	dir := filepath.Clean(p.Dir)
   507  	srcRoot := filepath.Clean(p.build.SrcRoot)
   508  	if len(dir) <= len(srcRoot) || dir[len(srcRoot)] != filepath.Separator {
   509  		return nil, "", fmt.Errorf("directory %q is outside source root %q", dir, srcRoot)
   510  	}
   511  
   512  	origDir := dir
   513  	for len(dir) > len(srcRoot) {
   514  		for _, vcs := range vcsList {
   515  			if fi, err := os.Stat(filepath.Join(dir, "."+vcs.cmd)); err == nil && fi.IsDir() {
   516  				return vcs, dir[len(srcRoot)+1:], nil
   517  			}
   518  		}
   519  
   520  		// Move to parent.
   521  		ndir := filepath.Dir(dir)
   522  		if len(ndir) >= len(dir) {
   523  			// Shouldn't happen, but just in case, stop.
   524  			break
   525  		}
   526  		dir = ndir
   527  	}
   528  
   529  	return nil, "", fmt.Errorf("directory %q is not using a known version control system", origDir)
   530  }
   531  
   532  // repoRoot represents a version control system, a repo, and a root of
   533  // where to put it on disk.
   534  type repoRoot struct {
   535  	vcs *vcsCmd
   536  
   537  	// repo is the repository URL, including scheme
   538  	repo string
   539  
   540  	// root is the import path corresponding to the root of the
   541  	// repository
   542  	root string
   543  }
   544  
   545  var httpPrefixRE = regexp.MustCompile(`^https?:`)
   546  
   547  // securityMode specifies whether a function should make network
   548  // calls using insecure transports (eg, plain text HTTP).
   549  // The zero value is "secure".
   550  type securityMode int
   551  
   552  const (
   553  	secure securityMode = iota
   554  	insecure
   555  )
   556  
   557  // repoRootForImportPath analyzes importPath to determine the
   558  // version control system, and code repository to use.
   559  func repoRootForImportPath(importPath string, security securityMode) (*repoRoot, error) {
   560  	rr, err := repoRootFromVCSPaths(importPath, "", security, vcsPaths)
   561  	if err == errUnknownSite {
   562  		// If there are wildcards, look up the thing before the wildcard,
   563  		// hoping it applies to the wildcarded parts too.
   564  		// This makes 'go get rsc.io/pdf/...' work in a fresh GOPATH.
   565  		lookup := strings.TrimSuffix(importPath, "/...")
   566  		if i := strings.Index(lookup, "/.../"); i >= 0 {
   567  			lookup = lookup[:i]
   568  		}
   569  		rr, err = repoRootForImportDynamic(lookup, security)
   570  
   571  		// repoRootForImportDynamic returns error detail
   572  		// that is irrelevant if the user didn't intend to use a
   573  		// dynamic import in the first place.
   574  		// Squelch it.
   575  		if err != nil {
   576  			if buildV {
   577  				log.Printf("import %q: %v", importPath, err)
   578  			}
   579  			err = fmt.Errorf("unrecognized import path %q", importPath)
   580  		}
   581  	}
   582  	if err != nil {
   583  		rr1, err1 := repoRootFromVCSPaths(importPath, "", security, vcsPathsAfterDynamic)
   584  		if err1 == nil {
   585  			rr = rr1
   586  			err = nil
   587  		}
   588  	}
   589  
   590  	if err == nil && strings.Contains(importPath, "...") && strings.Contains(rr.root, "...") {
   591  		// Do not allow wildcards in the repo root.
   592  		rr = nil
   593  		err = fmt.Errorf("cannot expand ... in %q", importPath)
   594  	}
   595  	return rr, err
   596  }
   597  
   598  var errUnknownSite = errors.New("dynamic lookup required to find mapping")
   599  
   600  // repoRootFromVCSPaths attempts to map importPath to a repoRoot
   601  // using the mappings defined in vcsPaths.
   602  // If scheme is non-empty, that scheme is forced.
   603  func repoRootFromVCSPaths(importPath, scheme string, security securityMode, vcsPaths []*vcsPath) (*repoRoot, error) {
   604  	// A common error is to use https://packagepath because that's what
   605  	// hg and git require. Diagnose this helpfully.
   606  	if loc := httpPrefixRE.FindStringIndex(importPath); loc != nil {
   607  		// The importPath has been cleaned, so has only one slash. The pattern
   608  		// ignores the slashes; the error message puts them back on the RHS at least.
   609  		return nil, fmt.Errorf("%q not allowed in import path", importPath[loc[0]:loc[1]]+"//")
   610  	}
   611  	for _, srv := range vcsPaths {
   612  		if !strings.HasPrefix(importPath, srv.prefix) {
   613  			continue
   614  		}
   615  		m := srv.regexp.FindStringSubmatch(importPath)
   616  		if m == nil {
   617  			if srv.prefix != "" {
   618  				return nil, fmt.Errorf("invalid %s import path %q", srv.prefix, importPath)
   619  			}
   620  			continue
   621  		}
   622  
   623  		// Build map of named subexpression matches for expand.
   624  		match := map[string]string{
   625  			"prefix": srv.prefix,
   626  			"import": importPath,
   627  		}
   628  		for i, name := range srv.regexp.SubexpNames() {
   629  			if name != "" && match[name] == "" {
   630  				match[name] = m[i]
   631  			}
   632  		}
   633  		if srv.vcs != "" {
   634  			match["vcs"] = expand(match, srv.vcs)
   635  		}
   636  		if srv.repo != "" {
   637  			match["repo"] = expand(match, srv.repo)
   638  		}
   639  		if srv.check != nil {
   640  			if err := srv.check(match); err != nil {
   641  				return nil, err
   642  			}
   643  		}
   644  		vcs := vcsByCmd(match["vcs"])
   645  		if vcs == nil {
   646  			return nil, fmt.Errorf("unknown version control system %q", match["vcs"])
   647  		}
   648  		if srv.ping {
   649  			if scheme != "" {
   650  				match["repo"] = scheme + "://" + match["repo"]
   651  			} else {
   652  				for _, scheme := range vcs.scheme {
   653  					if security == secure && !isSecureScheme[scheme] {
   654  						continue
   655  					}
   656  					if vcs.ping(scheme, match["repo"]) == nil {
   657  						match["repo"] = scheme + "://" + match["repo"]
   658  						break
   659  					}
   660  				}
   661  			}
   662  		}
   663  		rr := &repoRoot{
   664  			vcs:  vcs,
   665  			repo: match["repo"],
   666  			root: match["root"],
   667  		}
   668  		return rr, nil
   669  	}
   670  	return nil, errUnknownSite
   671  }
   672  
   673  // repoRootForImportDynamic finds a *repoRoot for a custom domain that's not
   674  // statically known by repoRootForImportPathStatic.
   675  //
   676  // This handles custom import paths like "name.tld/pkg/foo" or just "name.tld".
   677  func repoRootForImportDynamic(importPath string, security securityMode) (*repoRoot, error) {
   678  	slash := strings.Index(importPath, "/")
   679  	if slash < 0 {
   680  		slash = len(importPath)
   681  	}
   682  	host := importPath[:slash]
   683  	if !strings.Contains(host, ".") {
   684  		return nil, errors.New("import path does not begin with hostname")
   685  	}
   686  	urlStr, body, err := httpsOrHTTP(importPath, security)
   687  	if err != nil {
   688  		msg := "https fetch: %v"
   689  		if security == insecure {
   690  			msg = "http/" + msg
   691  		}
   692  		return nil, fmt.Errorf(msg, err)
   693  	}
   694  	defer body.Close()
   695  	imports, err := parseMetaGoImports(body)
   696  	if err != nil {
   697  		return nil, fmt.Errorf("parsing %s: %v", importPath, err)
   698  	}
   699  	// Find the matched meta import.
   700  	mmi, err := matchGoImport(imports, importPath)
   701  	if err != nil {
   702  		if err != errNoMatch {
   703  			return nil, fmt.Errorf("parse %s: %v", urlStr, err)
   704  		}
   705  		return nil, fmt.Errorf("parse %s: no go-import meta tags", urlStr)
   706  	}
   707  	if buildV {
   708  		log.Printf("get %q: found meta tag %#v at %s", importPath, mmi, urlStr)
   709  	}
   710  	// If the import was "uni.edu/bob/project", which said the
   711  	// prefix was "uni.edu" and the RepoRoot was "evilroot.com",
   712  	// make sure we don't trust Bob and check out evilroot.com to
   713  	// "uni.edu" yet (possibly overwriting/preempting another
   714  	// non-evil student).  Instead, first verify the root and see
   715  	// if it matches Bob's claim.
   716  	if mmi.Prefix != importPath {
   717  		if buildV {
   718  			log.Printf("get %q: verifying non-authoritative meta tag", importPath)
   719  		}
   720  		urlStr0 := urlStr
   721  		var imports []metaImport
   722  		urlStr, imports, err = metaImportsForPrefix(mmi.Prefix, security)
   723  		if err != nil {
   724  			return nil, err
   725  		}
   726  		metaImport2, err := matchGoImport(imports, importPath)
   727  		if err != nil || mmi != metaImport2 {
   728  			return nil, fmt.Errorf("%s and %s disagree about go-import for %s", urlStr0, urlStr, mmi.Prefix)
   729  		}
   730  	}
   731  
   732  	if !strings.Contains(mmi.RepoRoot, "://") {
   733  		return nil, fmt.Errorf("%s: invalid repo root %q; no scheme", urlStr, mmi.RepoRoot)
   734  	}
   735  	rr := &repoRoot{
   736  		vcs:  vcsByCmd(mmi.VCS),
   737  		repo: mmi.RepoRoot,
   738  		root: mmi.Prefix,
   739  	}
   740  	if rr.vcs == nil {
   741  		return nil, fmt.Errorf("%s: unknown vcs %q", urlStr, mmi.VCS)
   742  	}
   743  	return rr, nil
   744  }
   745  
   746  var fetchGroup singleflight.Group
   747  var (
   748  	fetchCacheMu sync.Mutex
   749  	fetchCache   = map[string]fetchResult{} // key is metaImportsForPrefix's importPrefix
   750  )
   751  
   752  // metaImportsForPrefix takes a package's root import path as declared in a <meta> tag
   753  // and returns its HTML discovery URL and the parsed metaImport lines
   754  // found on the page.
   755  //
   756  // The importPath is of the form "golang.org/x/tools".
   757  // It is an error if no imports are found.
   758  // urlStr will still be valid if err != nil.
   759  // The returned urlStr will be of the form "https://golang.org/x/tools?go-get=1"
   760  func metaImportsForPrefix(importPrefix string, security securityMode) (urlStr string, imports []metaImport, err error) {
   761  	setCache := func(res fetchResult) (fetchResult, error) {
   762  		fetchCacheMu.Lock()
   763  		defer fetchCacheMu.Unlock()
   764  		fetchCache[importPrefix] = res
   765  		return res, nil
   766  	}
   767  
   768  	resi, _, _ := fetchGroup.Do(importPrefix, func() (resi interface{}, err error) {
   769  		fetchCacheMu.Lock()
   770  		if res, ok := fetchCache[importPrefix]; ok {
   771  			fetchCacheMu.Unlock()
   772  			return res, nil
   773  		}
   774  		fetchCacheMu.Unlock()
   775  
   776  		urlStr, body, err := httpsOrHTTP(importPrefix, security)
   777  		if err != nil {
   778  			return setCache(fetchResult{urlStr: urlStr, err: fmt.Errorf("fetch %s: %v", urlStr, err)})
   779  		}
   780  		imports, err := parseMetaGoImports(body)
   781  		if err != nil {
   782  			return setCache(fetchResult{urlStr: urlStr, err: fmt.Errorf("parsing %s: %v", urlStr, err)})
   783  		}
   784  		if len(imports) == 0 {
   785  			err = fmt.Errorf("fetch %s: no go-import meta tag", urlStr)
   786  		}
   787  		return setCache(fetchResult{urlStr: urlStr, imports: imports, err: err})
   788  	})
   789  	res := resi.(fetchResult)
   790  	return res.urlStr, res.imports, res.err
   791  }
   792  
   793  type fetchResult struct {
   794  	urlStr  string // e.g. "https://foo.com/x/bar?go-get=1"
   795  	imports []metaImport
   796  	err     error
   797  }
   798  
   799  // metaImport represents the parsed <meta name="go-import"
   800  // content="prefix vcs reporoot" /> tags from HTML files.
   801  type metaImport struct {
   802  	Prefix, VCS, RepoRoot string
   803  }
   804  
   805  // errNoMatch is returned from matchGoImport when there's no applicable match.
   806  var errNoMatch = errors.New("no import match")
   807  
   808  // matchGoImport returns the metaImport from imports matching importPath.
   809  // An error is returned if there are multiple matches.
   810  // errNoMatch is returned if none match.
   811  func matchGoImport(imports []metaImport, importPath string) (_ metaImport, err error) {
   812  	match := -1
   813  	for i, im := range imports {
   814  		if !strings.HasPrefix(importPath, im.Prefix) {
   815  			continue
   816  		}
   817  		if match != -1 {
   818  			err = fmt.Errorf("multiple meta tags match import path %q", importPath)
   819  			return
   820  		}
   821  		match = i
   822  	}
   823  	if match == -1 {
   824  		err = errNoMatch
   825  		return
   826  	}
   827  	return imports[match], nil
   828  }
   829  
   830  // expand rewrites s to replace {k} with match[k] for each key k in match.
   831  func expand(match map[string]string, s string) string {
   832  	for k, v := range match {
   833  		s = strings.Replace(s, "{"+k+"}", v, -1)
   834  	}
   835  	return s
   836  }
   837  
   838  // vcsPaths defines the meaning of import paths referring to
   839  // commonly-used VCS hosting sites (github.com/user/dir)
   840  // and import paths referring to a fully-qualified importPath
   841  // containing a VCS type (foo.com/repo.git/dir)
   842  var vcsPaths = []*vcsPath{
   843  	// Google Code - new syntax
   844  	{
   845  		prefix: "code.google.com/",
   846  		re:     `^(?P<root>code\.google\.com/p/(?P<project>[a-z0-9\-]+)(\.(?P<subrepo>[a-z0-9\-]+))?)(/[A-Za-z0-9_.\-]+)*$`,
   847  		repo:   "https://{root}",
   848  		check:  googleCodeVCS,
   849  	},
   850  
   851  	// Google Code - old syntax
   852  	{
   853  		re:    `^(?P<project>[a-z0-9_\-.]+)\.googlecode\.com/(git|hg|svn)(?P<path>/.*)?$`,
   854  		check: oldGoogleCode,
   855  	},
   856  
   857  	// Github
   858  	{
   859  		prefix: "github.com/",
   860  		re:     `^(?P<root>github\.com/[A-Za-z0-9_.\-]+/[A-Za-z0-9_.\-]+)(/[A-Za-z0-9_.\-]+)*$`,
   861  		vcs:    "git",
   862  		repo:   "https://{root}",
   863  		check:  noVCSSuffix,
   864  	},
   865  
   866  	// Bitbucket
   867  	{
   868  		prefix: "bitbucket.org/",
   869  		re:     `^(?P<root>bitbucket\.org/(?P<bitname>[A-Za-z0-9_.\-]+/[A-Za-z0-9_.\-]+))(/[A-Za-z0-9_.\-]+)*$`,
   870  		repo:   "https://{root}",
   871  		check:  bitbucketVCS,
   872  	},
   873  
   874  	// IBM DevOps Services (JazzHub)
   875  	{
   876  		prefix: "hub.jazz.net/git",
   877  		re:     `^(?P<root>hub.jazz.net/git/[a-z0-9]+/[A-Za-z0-9_.\-]+)(/[A-Za-z0-9_.\-]+)*$`,
   878  		vcs:    "git",
   879  		repo:   "https://{root}",
   880  		check:  noVCSSuffix,
   881  	},
   882  
   883  	// Git at Apache
   884  	{
   885  		prefix: "git.apache.org",
   886  		re:     `^(?P<root>git.apache.org/[a-z0-9_.\-]+\.git)(/[A-Za-z0-9_.\-]+)*$`,
   887  		vcs:    "git",
   888  		repo:   "https://{root}",
   889  	},
   890  
   891  	// General syntax for any server.
   892  	// Must be last.
   893  	{
   894  		re:   `^(?P<root>(?P<repo>([a-z0-9.\-]+\.)+[a-z0-9.\-]+(:[0-9]+)?/[A-Za-z0-9_.\-/]*?)\.(?P<vcs>bzr|git|hg|svn))(/[A-Za-z0-9_.\-]+)*$`,
   895  		ping: true,
   896  	},
   897  }
   898  
   899  // vcsPathsAfterDynamic gives additional vcsPaths entries
   900  // to try after the dynamic HTML check.
   901  // This gives those sites a chance to introduce <meta> tags
   902  // as part of a graceful transition away from the hard-coded logic.
   903  var vcsPathsAfterDynamic = []*vcsPath{
   904  	// Launchpad. See golang.org/issue/11436.
   905  	{
   906  		prefix: "launchpad.net/",
   907  		re:     `^(?P<root>launchpad\.net/((?P<project>[A-Za-z0-9_.\-]+)(?P<series>/[A-Za-z0-9_.\-]+)?|~[A-Za-z0-9_.\-]+/(\+junk|[A-Za-z0-9_.\-]+)/[A-Za-z0-9_.\-]+))(/[A-Za-z0-9_.\-]+)*$`,
   908  		vcs:    "bzr",
   909  		repo:   "https://{root}",
   910  		check:  launchpadVCS,
   911  	},
   912  }
   913  
   914  func init() {
   915  	// fill in cached regexps.
   916  	// Doing this eagerly discovers invalid regexp syntax
   917  	// without having to run a command that needs that regexp.
   918  	for _, srv := range vcsPaths {
   919  		srv.regexp = regexp.MustCompile(srv.re)
   920  	}
   921  	for _, srv := range vcsPathsAfterDynamic {
   922  		srv.regexp = regexp.MustCompile(srv.re)
   923  	}
   924  }
   925  
   926  // noVCSSuffix checks that the repository name does not
   927  // end in .foo for any version control system foo.
   928  // The usual culprit is ".git".
   929  func noVCSSuffix(match map[string]string) error {
   930  	repo := match["repo"]
   931  	for _, vcs := range vcsList {
   932  		if strings.HasSuffix(repo, "."+vcs.cmd) {
   933  			return fmt.Errorf("invalid version control suffix in %s path", match["prefix"])
   934  		}
   935  	}
   936  	return nil
   937  }
   938  
   939  var googleCheckout = regexp.MustCompile(`id="checkoutcmd">(hg|git|svn)`)
   940  
   941  // googleCodeVCS determines the version control system for
   942  // a code.google.com repository, by scraping the project's
   943  // /source/checkout page.
   944  func googleCodeVCS(match map[string]string) error {
   945  	if err := noVCSSuffix(match); err != nil {
   946  		return err
   947  	}
   948  	data, err := httpGET(expand(match, "https://code.google.com/p/{project}/source/checkout?repo={subrepo}"))
   949  	if err != nil {
   950  		return err
   951  	}
   952  
   953  	if m := googleCheckout.FindSubmatch(data); m != nil {
   954  		if vcs := vcsByCmd(string(m[1])); vcs != nil {
   955  			// Subversion requires the old URLs.
   956  			// TODO: Test.
   957  			if vcs == vcsSvn {
   958  				if match["subrepo"] != "" {
   959  					return fmt.Errorf("sub-repositories not supported in Google Code Subversion projects")
   960  				}
   961  				match["repo"] = expand(match, "https://{project}.googlecode.com/svn")
   962  			}
   963  			match["vcs"] = vcs.cmd
   964  			return nil
   965  		}
   966  	}
   967  
   968  	return fmt.Errorf("unable to detect version control system for code.google.com/ path")
   969  }
   970  
   971  // oldGoogleCode is invoked for old-style foo.googlecode.com paths.
   972  // It prints an error giving the equivalent new path.
   973  func oldGoogleCode(match map[string]string) error {
   974  	return fmt.Errorf("invalid Google Code import path: use %s instead",
   975  		expand(match, "code.google.com/p/{project}{path}"))
   976  }
   977  
   978  // bitbucketVCS determines the version control system for a
   979  // Bitbucket repository, by using the Bitbucket API.
   980  func bitbucketVCS(match map[string]string) error {
   981  	if err := noVCSSuffix(match); err != nil {
   982  		return err
   983  	}
   984  
   985  	var resp struct {
   986  		SCM string `json:"scm"`
   987  	}
   988  	url := expand(match, "https://api.bitbucket.org/1.0/repositories/{bitname}")
   989  	data, err := httpGET(url)
   990  	if err != nil {
   991  		if httpErr, ok := err.(*httpError); ok && httpErr.statusCode == 403 {
   992  			// this may be a private repository. If so, attempt to determine which
   993  			// VCS it uses. See issue 5375.
   994  			root := match["root"]
   995  			for _, vcs := range []string{"git", "hg"} {
   996  				if vcsByCmd(vcs).ping("https", root) == nil {
   997  					resp.SCM = vcs
   998  					break
   999  				}
  1000  			}
  1001  		}
  1002  
  1003  		if resp.SCM == "" {
  1004  			return err
  1005  		}
  1006  	} else {
  1007  		if err := json.Unmarshal(data, &resp); err != nil {
  1008  			return fmt.Errorf("decoding %s: %v", url, err)
  1009  		}
  1010  	}
  1011  
  1012  	if vcsByCmd(resp.SCM) != nil {
  1013  		match["vcs"] = resp.SCM
  1014  		if resp.SCM == "git" {
  1015  			match["repo"] += ".git"
  1016  		}
  1017  		return nil
  1018  	}
  1019  
  1020  	return fmt.Errorf("unable to detect version control system for bitbucket.org/ path")
  1021  }
  1022  
  1023  // launchpadVCS solves the ambiguity for "lp.net/project/foo". In this case,
  1024  // "foo" could be a series name registered in Launchpad with its own branch,
  1025  // and it could also be the name of a directory within the main project
  1026  // branch one level up.
  1027  func launchpadVCS(match map[string]string) error {
  1028  	if match["project"] == "" || match["series"] == "" {
  1029  		return nil
  1030  	}
  1031  	_, err := httpGET(expand(match, "https://code.launchpad.net/{project}{series}/.bzr/branch-format"))
  1032  	if err != nil {
  1033  		match["root"] = expand(match, "launchpad.net/{project}")
  1034  		match["repo"] = expand(match, "https://{root}")
  1035  	}
  1036  	return nil
  1037  }