Source file src/cmd/go/internal/vcs/vcs.go

     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 vcs
     6  
     7  import (
     8  	"bytes"
     9  	"errors"
    10  	"fmt"
    11  	"internal/godebug"
    12  	"internal/lazyregexp"
    13  	"internal/singleflight"
    14  	"log"
    15  	urlpkg "net/url"
    16  	"os"
    17  	"os/exec"
    18  	"path/filepath"
    19  	"strconv"
    20  	"strings"
    21  	"sync"
    22  	"time"
    23  
    24  	"cmd/go/internal/base"
    25  	"cmd/go/internal/cfg"
    26  	"cmd/go/internal/search"
    27  	"cmd/go/internal/str"
    28  	"cmd/go/internal/web"
    29  	"cmd/internal/pathcache"
    30  	"cmd/internal/telemetry/counter"
    31  
    32  	"golang.org/x/mod/module"
    33  )
    34  
    35  // A Cmd describes how to use a version control system
    36  // like Mercurial, Git, or Subversion.
    37  type Cmd struct {
    38  	Name  string
    39  	Cmd   string      // name of binary to invoke command
    40  	Env   []string    // any environment values to set/override
    41  	Roots []isVCSRoot // filters to identify repository root directories
    42  
    43  	Scheme  []string
    44  	PingCmd string
    45  
    46  	Status func(v *Cmd, rootDir string) (Status, error)
    47  }
    48  
    49  // Status is the current state of a local repository.
    50  type Status struct {
    51  	Revision    string    // Optional.
    52  	CommitTime  time.Time // Optional.
    53  	Uncommitted bool      // Required.
    54  }
    55  
    56  var (
    57  	// VCSTestRepoURL is the URL of the HTTP server that serves the repos for
    58  	// vcs-test.golang.org.
    59  	//
    60  	// In tests, this is set to the URL of an httptest.Server hosting a
    61  	// cmd/go/internal/vcweb.Server.
    62  	VCSTestRepoURL string
    63  
    64  	// VCSTestHosts is the set of hosts supported by the vcs-test server.
    65  	VCSTestHosts []string
    66  
    67  	// VCSTestIsLocalHost reports whether the given URL refers to a local
    68  	// (loopback) host, such as "localhost" or "127.0.0.1:8080".
    69  	VCSTestIsLocalHost func(*urlpkg.URL) bool
    70  )
    71  
    72  var defaultSecureScheme = map[string]bool{
    73  	"https":   true,
    74  	"git+ssh": true,
    75  	"bzr+ssh": true,
    76  	"svn+ssh": true,
    77  	"ssh":     true,
    78  }
    79  
    80  func (v *Cmd) IsSecure(repo string) bool {
    81  	u, err := urlpkg.Parse(repo)
    82  	if err != nil {
    83  		// If repo is not a URL, it's not secure.
    84  		return false
    85  	}
    86  	if VCSTestRepoURL != "" && web.IsLocalHost(u) {
    87  		// If the vcstest server is in use, it may redirect to other local ports for
    88  		// other protocols (such as svn). Assume that all loopback addresses are
    89  		// secure during testing.
    90  		return true
    91  	}
    92  	return v.isSecureScheme(u.Scheme)
    93  }
    94  
    95  func (v *Cmd) isSecureScheme(scheme string) bool {
    96  	switch v.Cmd {
    97  	case "git":
    98  		// GIT_ALLOW_PROTOCOL is an environment variable defined by Git. It is a
    99  		// colon-separated list of schemes that are allowed to be used with git
   100  		// fetch/clone. Any scheme not mentioned will be considered insecure.
   101  		if allow := os.Getenv("GIT_ALLOW_PROTOCOL"); allow != "" {
   102  			for s := range strings.SplitSeq(allow, ":") {
   103  				if s == scheme {
   104  					return true
   105  				}
   106  			}
   107  			return false
   108  		}
   109  	}
   110  	return defaultSecureScheme[scheme]
   111  }
   112  
   113  // A tagCmd describes a command to list available tags
   114  // that can be passed to tagSyncCmd.
   115  type tagCmd struct {
   116  	cmd     string // command to list tags
   117  	pattern string // regexp to extract tags from list
   118  }
   119  
   120  // vcsList lists the known version control systems
   121  var vcsList = []*Cmd{
   122  	vcsHg,
   123  	vcsGit,
   124  	vcsSvn,
   125  	vcsBzr,
   126  	vcsFossil,
   127  }
   128  
   129  // vcsMod is a stub for the "mod" scheme. It's returned by
   130  // repoRootForImportPathDynamic, but is otherwise not treated as a VCS command.
   131  var vcsMod = &Cmd{Name: "mod"}
   132  
   133  // vcsByCmd returns the version control system for the given
   134  // command name (hg, git, svn, bzr).
   135  func vcsByCmd(cmd string) *Cmd {
   136  	for _, vcs := range vcsList {
   137  		if vcs.Cmd == cmd {
   138  			return vcs
   139  		}
   140  	}
   141  	return nil
   142  }
   143  
   144  // vcsHg describes how to use Mercurial.
   145  var vcsHg = &Cmd{
   146  	Name: "Mercurial",
   147  	Cmd:  "hg",
   148  
   149  	// HGPLAIN=+strictflags turns off additional output that a user may have
   150  	// enabled via config options or certain extensions.
   151  	Env: []string{"HGPLAIN=+strictflags"},
   152  	Roots: []isVCSRoot{
   153  		vcsDirRoot(".hg"),
   154  	},
   155  
   156  	Scheme:  []string{"https", "http", "ssh"},
   157  	PingCmd: "identify -- {scheme}://{repo}",
   158  	Status:  hgStatus,
   159  }
   160  
   161  func hgStatus(vcsHg *Cmd, rootDir string) (Status, error) {
   162  	// Output changeset ID and seconds since epoch.
   163  	out, err := vcsHg.runOutputVerboseOnly(rootDir, `log -r. -T {node}:{date|hgdate}`)
   164  	if err != nil {
   165  		return Status{}, err
   166  	}
   167  
   168  	var rev string
   169  	var commitTime time.Time
   170  	if len(out) > 0 {
   171  		// Strip trailing timezone offset.
   172  		if i := bytes.IndexByte(out, ' '); i > 0 {
   173  			out = out[:i]
   174  		}
   175  		rev, commitTime, err = parseRevTime(out)
   176  		if err != nil {
   177  			return Status{}, err
   178  		}
   179  	}
   180  
   181  	// Also look for untracked files.
   182  	out, err = vcsHg.runOutputVerboseOnly(rootDir, "status -S")
   183  	if err != nil {
   184  		return Status{}, err
   185  	}
   186  	uncommitted := len(out) > 0
   187  
   188  	return Status{
   189  		Revision:    rev,
   190  		CommitTime:  commitTime,
   191  		Uncommitted: uncommitted,
   192  	}, nil
   193  }
   194  
   195  // parseRevTime parses commit details in "revision:seconds" format.
   196  func parseRevTime(out []byte) (string, time.Time, error) {
   197  	buf := string(bytes.TrimSpace(out))
   198  
   199  	i := strings.IndexByte(buf, ':')
   200  	if i < 1 {
   201  		return "", time.Time{}, errors.New("unrecognized VCS tool output")
   202  	}
   203  	rev := buf[:i]
   204  
   205  	secs, err := strconv.ParseInt(buf[i+1:], 10, 64)
   206  	if err != nil {
   207  		return "", time.Time{}, fmt.Errorf("unrecognized VCS tool output: %v", err)
   208  	}
   209  
   210  	return rev, time.Unix(secs, 0), nil
   211  }
   212  
   213  // vcsGit describes how to use Git.
   214  var vcsGit = &Cmd{
   215  	Name: "Git",
   216  	Cmd:  "git",
   217  	Roots: []isVCSRoot{
   218  		vcsGitRoot{},
   219  	},
   220  
   221  	Scheme: []string{"git", "https", "http", "git+ssh", "ssh"},
   222  
   223  	// Leave out the '--' separator in the ls-remote command: git 2.7.4 does not
   224  	// support such a separator for that command, and this use should be safe
   225  	// without it because the {scheme} value comes from the predefined list above.
   226  	// See golang.org/issue/33836.
   227  	PingCmd: "ls-remote {scheme}://{repo}",
   228  
   229  	Status: gitStatus,
   230  }
   231  
   232  func gitStatus(vcsGit *Cmd, rootDir string) (Status, error) {
   233  	out, err := vcsGit.runOutputVerboseOnly(rootDir, "status --porcelain")
   234  	if err != nil {
   235  		return Status{}, err
   236  	}
   237  	uncommitted := len(out) > 0
   238  
   239  	// "git status" works for empty repositories, but "git log" does not.
   240  	// Assume there are no commits in the repo when "git log" fails with
   241  	// uncommitted files and skip tagging revision / committime.
   242  	var rev string
   243  	var commitTime time.Time
   244  	out, err = vcsGit.runOutputVerboseOnly(rootDir, "-c log.showsignature=false log -1 --format=%H:%ct")
   245  	if err != nil && !uncommitted {
   246  		return Status{}, err
   247  	} else if err == nil {
   248  		rev, commitTime, err = parseRevTime(out)
   249  		if err != nil {
   250  			return Status{}, err
   251  		}
   252  	}
   253  
   254  	return Status{
   255  		Revision:    rev,
   256  		CommitTime:  commitTime,
   257  		Uncommitted: uncommitted,
   258  	}, nil
   259  }
   260  
   261  // vcsBzr describes how to use Bazaar.
   262  var vcsBzr = &Cmd{
   263  	Name: "Bazaar",
   264  	Cmd:  "bzr",
   265  	Roots: []isVCSRoot{
   266  		vcsDirRoot(".bzr"),
   267  	},
   268  
   269  	Scheme:  []string{"https", "http", "bzr", "bzr+ssh"},
   270  	PingCmd: "info -- {scheme}://{repo}",
   271  	Status:  bzrStatus,
   272  }
   273  
   274  func bzrStatus(vcsBzr *Cmd, rootDir string) (Status, error) {
   275  	outb, err := vcsBzr.runOutputVerboseOnly(rootDir, "version-info")
   276  	if err != nil {
   277  		return Status{}, err
   278  	}
   279  	out := string(outb)
   280  
   281  	// Expect (non-empty repositories only):
   282  	//
   283  	// revision-id: gopher@gopher.net-20211021072330-qshok76wfypw9lpm
   284  	// date: 2021-09-21 12:00:00 +1000
   285  	// ...
   286  	var rev string
   287  	var commitTime time.Time
   288  
   289  	for line := range strings.SplitSeq(out, "\n") {
   290  		i := strings.IndexByte(line, ':')
   291  		if i < 0 {
   292  			continue
   293  		}
   294  		key := line[:i]
   295  		value := strings.TrimSpace(line[i+1:])
   296  
   297  		switch key {
   298  		case "revision-id":
   299  			rev = value
   300  		case "date":
   301  			var err error
   302  			commitTime, err = time.Parse("2006-01-02 15:04:05 -0700", value)
   303  			if err != nil {
   304  				return Status{}, errors.New("unable to parse output of bzr version-info")
   305  			}
   306  		}
   307  	}
   308  
   309  	outb, err = vcsBzr.runOutputVerboseOnly(rootDir, "status")
   310  	if err != nil {
   311  		return Status{}, err
   312  	}
   313  
   314  	// Skip warning when working directory is set to an older revision.
   315  	if bytes.HasPrefix(outb, []byte("working tree is out of date")) {
   316  		i := bytes.IndexByte(outb, '\n')
   317  		if i < 0 {
   318  			i = len(outb)
   319  		}
   320  		outb = outb[:i]
   321  	}
   322  	uncommitted := len(outb) > 0
   323  
   324  	return Status{
   325  		Revision:    rev,
   326  		CommitTime:  commitTime,
   327  		Uncommitted: uncommitted,
   328  	}, nil
   329  }
   330  
   331  // vcsSvn describes how to use Subversion.
   332  var vcsSvn = &Cmd{
   333  	Name: "Subversion",
   334  	Cmd:  "svn",
   335  	Roots: []isVCSRoot{
   336  		vcsDirRoot(".svn"),
   337  	},
   338  
   339  	// There is no tag command in subversion.
   340  	// The branch information is all in the path names.
   341  
   342  	Scheme:  []string{"https", "http", "svn", "svn+ssh"},
   343  	PingCmd: "info -- {scheme}://{repo}",
   344  	Status:  svnStatus,
   345  }
   346  
   347  func svnStatus(vcsSvn *Cmd, rootDir string) (Status, error) {
   348  	out, err := vcsSvn.runOutputVerboseOnly(rootDir, "info --show-item last-changed-revision")
   349  	if err != nil {
   350  		return Status{}, err
   351  	}
   352  	rev := strings.TrimSpace(string(out))
   353  
   354  	out, err = vcsSvn.runOutputVerboseOnly(rootDir, "info --show-item last-changed-date")
   355  	if err != nil {
   356  		return Status{}, err
   357  	}
   358  	commitTime, err := time.Parse(time.RFC3339, strings.TrimSpace(string(out)))
   359  	if err != nil {
   360  		return Status{}, fmt.Errorf("unable to parse output of svn info: %v", err)
   361  	}
   362  
   363  	out, err = vcsSvn.runOutputVerboseOnly(rootDir, "status")
   364  	if err != nil {
   365  		return Status{}, err
   366  	}
   367  	uncommitted := len(out) > 0
   368  
   369  	return Status{
   370  		Revision:    rev,
   371  		CommitTime:  commitTime,
   372  		Uncommitted: uncommitted,
   373  	}, nil
   374  }
   375  
   376  // fossilRepoName is the name go get associates with a fossil repository. In the
   377  // real world the file can be named anything.
   378  const fossilRepoName = ".fossil"
   379  
   380  // vcsFossil describes how to use Fossil (fossil-scm.org)
   381  var vcsFossil = &Cmd{
   382  	Name: "Fossil",
   383  	Cmd:  "fossil",
   384  	Roots: []isVCSRoot{
   385  		vcsFileRoot(".fslckout"),
   386  		vcsFileRoot("_FOSSIL_"),
   387  	},
   388  
   389  	Scheme: []string{"https", "http"},
   390  	Status: fossilStatus,
   391  }
   392  
   393  var errFossilInfo = errors.New("unable to parse output of fossil info")
   394  
   395  func fossilStatus(vcsFossil *Cmd, rootDir string) (Status, error) {
   396  	outb, err := vcsFossil.runOutputVerboseOnly(rootDir, "info")
   397  	if err != nil {
   398  		return Status{}, err
   399  	}
   400  	out := string(outb)
   401  
   402  	// Expect:
   403  	// ...
   404  	// checkout:     91ed71f22c77be0c3e250920f47bfd4e1f9024d2 2021-09-21 12:00:00 UTC
   405  	// ...
   406  
   407  	// Extract revision and commit time.
   408  	// Ensure line ends with UTC (known timezone offset).
   409  	const prefix = "\ncheckout:"
   410  	const suffix = " UTC"
   411  	i := strings.Index(out, prefix)
   412  	if i < 0 {
   413  		return Status{}, errFossilInfo
   414  	}
   415  	checkout := out[i+len(prefix):]
   416  	i = strings.Index(checkout, suffix)
   417  	if i < 0 {
   418  		return Status{}, errFossilInfo
   419  	}
   420  	checkout = strings.TrimSpace(checkout[:i])
   421  
   422  	i = strings.IndexByte(checkout, ' ')
   423  	if i < 0 {
   424  		return Status{}, errFossilInfo
   425  	}
   426  	rev := checkout[:i]
   427  
   428  	commitTime, err := time.ParseInLocation(time.DateTime, checkout[i+1:], time.UTC)
   429  	if err != nil {
   430  		return Status{}, fmt.Errorf("%v: %v", errFossilInfo, err)
   431  	}
   432  
   433  	// Also look for untracked changes.
   434  	outb, err = vcsFossil.runOutputVerboseOnly(rootDir, "changes --differ")
   435  	if err != nil {
   436  		return Status{}, err
   437  	}
   438  	uncommitted := len(outb) > 0
   439  
   440  	return Status{
   441  		Revision:    rev,
   442  		CommitTime:  commitTime,
   443  		Uncommitted: uncommitted,
   444  	}, nil
   445  }
   446  
   447  func (v *Cmd) String() string {
   448  	return v.Name
   449  }
   450  
   451  // run runs the command line cmd in the given directory.
   452  // keyval is a list of key, value pairs. run expands
   453  // instances of {key} in cmd into value, but only after
   454  // splitting cmd into individual arguments.
   455  // If an error occurs, run prints the command line and the
   456  // command's combined stdout+stderr to standard error.
   457  // Otherwise run discards the command's output.
   458  func (v *Cmd) run(dir string, cmd string, keyval ...string) error {
   459  	_, err := v.run1(dir, cmd, keyval, true)
   460  	return err
   461  }
   462  
   463  // runVerboseOnly is like run but only generates error output to standard error in verbose mode.
   464  func (v *Cmd) runVerboseOnly(dir string, cmd string, keyval ...string) error {
   465  	_, err := v.run1(dir, cmd, keyval, false)
   466  	return err
   467  }
   468  
   469  // runOutput is like run but returns the output of the command.
   470  func (v *Cmd) runOutput(dir string, cmd string, keyval ...string) ([]byte, error) {
   471  	return v.run1(dir, cmd, keyval, true)
   472  }
   473  
   474  // runOutputVerboseOnly is like runOutput but only generates error output to
   475  // standard error in verbose mode.
   476  func (v *Cmd) runOutputVerboseOnly(dir string, cmd string, keyval ...string) ([]byte, error) {
   477  	return v.run1(dir, cmd, keyval, false)
   478  }
   479  
   480  // run1 is the generalized implementation of run and runOutput.
   481  func (v *Cmd) run1(dir string, cmdline string, keyval []string, verbose bool) ([]byte, error) {
   482  	m := make(map[string]string)
   483  	for i := 0; i < len(keyval); i += 2 {
   484  		m[keyval[i]] = keyval[i+1]
   485  	}
   486  	args := strings.Fields(cmdline)
   487  	for i, arg := range args {
   488  		args[i] = expand(m, arg)
   489  	}
   490  
   491  	_, err := pathcache.LookPath(v.Cmd)
   492  	if err != nil {
   493  		fmt.Fprintf(os.Stderr,
   494  			"go: missing %s command. See https://go.dev/s/gogetcmd\n",
   495  			v.Name)
   496  		return nil, err
   497  	}
   498  
   499  	cmd := exec.Command(v.Cmd, args...)
   500  	cmd.Dir = dir
   501  	if v.Env != nil {
   502  		cmd.Env = append(cmd.Environ(), v.Env...)
   503  	}
   504  	if cfg.BuildX {
   505  		fmt.Fprintf(os.Stderr, "cd %s\n", dir)
   506  		fmt.Fprintf(os.Stderr, "%s %s\n", v.Cmd, strings.Join(args, " "))
   507  	}
   508  	out, err := cmd.Output()
   509  	if err != nil {
   510  		if verbose || cfg.BuildV {
   511  			fmt.Fprintf(os.Stderr, "# cd %s; %s %s\n", dir, v.Cmd, strings.Join(args, " "))
   512  			if ee, ok := err.(*exec.ExitError); ok && len(ee.Stderr) > 0 {
   513  				os.Stderr.Write(ee.Stderr)
   514  			} else {
   515  				fmt.Fprintln(os.Stderr, err.Error())
   516  			}
   517  		}
   518  	}
   519  	return out, err
   520  }
   521  
   522  // Ping pings to determine scheme to use.
   523  func (v *Cmd) Ping(scheme, repo string) error {
   524  	// Run the ping command in an arbitrary working directory,
   525  	// but don't let the current working directory pollute the results.
   526  	// In module mode, we expect GOMODCACHE to exist and be a safe place for
   527  	// commands; in GOPATH mode, we expect that to be true of GOPATH/src.
   528  	dir := cfg.GOMODCACHE
   529  	if !cfg.ModulesEnabled {
   530  		dir = filepath.Join(cfg.BuildContext.GOPATH, "src")
   531  	}
   532  	os.MkdirAll(dir, 0o777) // Ignore errors — if unsuccessful, the command will likely fail.
   533  
   534  	release, err := base.AcquireNet()
   535  	if err != nil {
   536  		return err
   537  	}
   538  	defer release()
   539  
   540  	return v.runVerboseOnly(dir, v.PingCmd, "scheme", scheme, "repo", repo)
   541  }
   542  
   543  // A vcsPath describes how to convert an import path into a
   544  // version control system and repository name.
   545  type vcsPath struct {
   546  	pathPrefix     string                              // prefix this description applies to
   547  	regexp         *lazyregexp.Regexp                  // compiled pattern for import path
   548  	repo           string                              // repository to use (expand with match of re)
   549  	vcs            string                              // version control system to use (expand with match of re)
   550  	check          func(match map[string]string) error // additional checks
   551  	schemelessRepo bool                                // if true, the repo pattern lacks a scheme
   552  }
   553  
   554  var allowmultiplevcs = godebug.New("allowmultiplevcs")
   555  
   556  // FromDir inspects dir and its parents to determine the
   557  // version control system and code repository to use.
   558  // If no repository is found, FromDir returns an error
   559  // equivalent to os.ErrNotExist.
   560  func FromDir(dir, srcRoot string) (repoDir string, vcsCmd *Cmd, err error) {
   561  	// Clean and double-check that dir is in (a subdirectory of) srcRoot.
   562  	dir = filepath.Clean(dir)
   563  	if srcRoot != "" {
   564  		srcRoot = filepath.Clean(srcRoot)
   565  		if len(dir) <= len(srcRoot) || dir[len(srcRoot)] != filepath.Separator {
   566  			return "", nil, fmt.Errorf("directory %q is outside source root %q", dir, srcRoot)
   567  		}
   568  	}
   569  
   570  	origDir := dir
   571  	for len(dir) > len(srcRoot) {
   572  		for _, vcs := range vcsList {
   573  			if isVCSRootDir(dir, vcs.Roots) {
   574  				if vcsCmd == nil {
   575  					// Record first VCS we find.
   576  					vcsCmd = vcs
   577  					repoDir = dir
   578  					if allowmultiplevcs.Value() == "1" {
   579  						allowmultiplevcs.IncNonDefault()
   580  						return repoDir, vcsCmd, nil
   581  					}
   582  					// If allowmultiplevcs is not set, keep looking for
   583  					// repositories in current and parent directories and report
   584  					// an error if one is found to mitigate VCS injection
   585  					// attacks.
   586  					continue
   587  				}
   588  				if vcsCmd == vcsGit && vcs == vcsGit {
   589  					// Nested Git is allowed, as this is how things like
   590  					// submodules work. Git explicitly protects against
   591  					// injection against itself.
   592  					continue
   593  				}
   594  				return "", nil, fmt.Errorf("multiple VCS detected: %s in %q, and %s in %q",
   595  					vcsCmd.Cmd, repoDir, vcs.Cmd, dir)
   596  			}
   597  		}
   598  
   599  		// Move to parent.
   600  		ndir := filepath.Dir(dir)
   601  		if len(ndir) >= len(dir) {
   602  			break
   603  		}
   604  		dir = ndir
   605  	}
   606  	if vcsCmd == nil {
   607  		return "", nil, &vcsNotFoundError{dir: origDir}
   608  	}
   609  	return repoDir, vcsCmd, nil
   610  }
   611  
   612  // isVCSRootDir reports whether dir is a VCS root according to roots.
   613  func isVCSRootDir(dir string, roots []isVCSRoot) bool {
   614  	for _, root := range roots {
   615  		if root.isRoot(dir) {
   616  			return true
   617  		}
   618  	}
   619  	return false
   620  }
   621  
   622  type isVCSRoot interface {
   623  	isRoot(dir string) bool
   624  }
   625  
   626  // vcsFileRoot identifies a VCS root by the presence of a regular file.
   627  type vcsFileRoot string
   628  
   629  func (vfr vcsFileRoot) isRoot(dir string) bool {
   630  	fi, err := os.Stat(filepath.Join(dir, string(vfr)))
   631  	return err == nil && fi.Mode().IsRegular()
   632  }
   633  
   634  // vcsDirRoot identifies a VCS root by the presence of a directory.
   635  type vcsDirRoot string
   636  
   637  func (vdr vcsDirRoot) isRoot(dir string) bool {
   638  	fi, err := os.Stat(filepath.Join(dir, string(vdr)))
   639  	return err == nil && fi.IsDir()
   640  }
   641  
   642  // vcsGitRoot identifies a Git root by the presence of a .git directory or a .git worktree file.
   643  // See https://go.dev/issue/58218.
   644  type vcsGitRoot struct{}
   645  
   646  func (vcsGitRoot) isRoot(dir string) bool {
   647  	path := filepath.Join(dir, ".git")
   648  	fi, err := os.Stat(path)
   649  	if err != nil {
   650  		return false
   651  	}
   652  	if fi.IsDir() {
   653  		return true
   654  	}
   655  	// Is it a git worktree file?
   656  	// The format is "gitdir: <path>\n".
   657  	if !fi.Mode().IsRegular() || fi.Size() == 0 || fi.Size() > 4096 {
   658  		return false
   659  	}
   660  	raw, err := os.ReadFile(path)
   661  	if err != nil {
   662  		return false
   663  	}
   664  	rest, ok := strings.CutPrefix(string(raw), "gitdir:")
   665  	if !ok {
   666  		return false
   667  	}
   668  	gitdir := strings.TrimSpace(rest)
   669  	if gitdir == "" {
   670  		return false
   671  	}
   672  	if !filepath.IsAbs(gitdir) {
   673  		gitdir = filepath.Join(dir, gitdir)
   674  	}
   675  	fi, err = os.Stat(gitdir)
   676  	return err == nil && fi.IsDir()
   677  }
   678  
   679  type vcsNotFoundError struct {
   680  	dir string
   681  }
   682  
   683  func (e *vcsNotFoundError) Error() string {
   684  	return fmt.Sprintf("directory %q is not using a known version control system", e.dir)
   685  }
   686  
   687  func (e *vcsNotFoundError) Is(err error) bool {
   688  	return err == os.ErrNotExist
   689  }
   690  
   691  // A govcsRule is a single GOVCS rule like private:hg|svn.
   692  type govcsRule struct {
   693  	pattern string
   694  	allowed []string
   695  }
   696  
   697  // A govcsConfig is a full GOVCS configuration.
   698  type govcsConfig []govcsRule
   699  
   700  func parseGOVCS(s string) (govcsConfig, error) {
   701  	s = strings.TrimSpace(s)
   702  	if s == "" {
   703  		return nil, nil
   704  	}
   705  	var cfg govcsConfig
   706  	have := make(map[string]string)
   707  	for item := range strings.SplitSeq(s, ",") {
   708  		item = strings.TrimSpace(item)
   709  		if item == "" {
   710  			return nil, fmt.Errorf("empty entry in GOVCS")
   711  		}
   712  		pattern, list, found := strings.Cut(item, ":")
   713  		if !found {
   714  			return nil, fmt.Errorf("malformed entry in GOVCS (missing colon): %q", item)
   715  		}
   716  		pattern, list = strings.TrimSpace(pattern), strings.TrimSpace(list)
   717  		if pattern == "" {
   718  			return nil, fmt.Errorf("empty pattern in GOVCS: %q", item)
   719  		}
   720  		if list == "" {
   721  			return nil, fmt.Errorf("empty VCS list in GOVCS: %q", item)
   722  		}
   723  		if search.IsRelativePath(pattern) {
   724  			return nil, fmt.Errorf("relative pattern not allowed in GOVCS: %q", pattern)
   725  		}
   726  		if old := have[pattern]; old != "" {
   727  			return nil, fmt.Errorf("unreachable pattern in GOVCS: %q after %q", item, old)
   728  		}
   729  		have[pattern] = item
   730  		allowed := strings.Split(list, "|")
   731  		for i, a := range allowed {
   732  			a = strings.TrimSpace(a)
   733  			if a == "" {
   734  				return nil, fmt.Errorf("empty VCS name in GOVCS: %q", item)
   735  			}
   736  			allowed[i] = a
   737  		}
   738  		cfg = append(cfg, govcsRule{pattern, allowed})
   739  	}
   740  	return cfg, nil
   741  }
   742  
   743  func (c *govcsConfig) allow(path string, private bool, vcs string) bool {
   744  	for _, rule := range *c {
   745  		match := false
   746  		switch rule.pattern {
   747  		case "private":
   748  			match = private
   749  		case "public":
   750  			match = !private
   751  		default:
   752  			// Note: rule.pattern is known to be comma-free,
   753  			// so MatchPrefixPatterns is only matching a single pattern for us.
   754  			match = module.MatchPrefixPatterns(rule.pattern, path)
   755  		}
   756  		if !match {
   757  			continue
   758  		}
   759  		for _, allow := range rule.allowed {
   760  			if allow == vcs || allow == "all" {
   761  				return true
   762  			}
   763  		}
   764  		return false
   765  	}
   766  
   767  	// By default, nothing is allowed.
   768  	return false
   769  }
   770  
   771  var (
   772  	govcs     govcsConfig
   773  	govcsErr  error
   774  	govcsOnce sync.Once
   775  )
   776  
   777  // defaultGOVCS is the default setting for GOVCS.
   778  // Setting GOVCS adds entries ahead of these but does not remove them.
   779  // (They are appended to the parsed GOVCS setting.)
   780  //
   781  // The rationale behind allowing only Git and Mercurial is that
   782  // these two systems have had the most attention to issues
   783  // of being run as clients of untrusted servers. In contrast,
   784  // Bazaar, Fossil, and Subversion have primarily been used
   785  // in trusted, authenticated environments and are not as well
   786  // scrutinized as attack surfaces.
   787  //
   788  // See golang.org/issue/41730 for details.
   789  var defaultGOVCS = govcsConfig{
   790  	{"private", []string{"all"}},
   791  	{"public", []string{"git", "hg"}},
   792  }
   793  
   794  // checkGOVCS checks whether the policy defined by the environment variable
   795  // GOVCS allows the given vcs command to be used with the given repository
   796  // root path. Note that root may not be a real package or module path; it's
   797  // the same as the root path in the go-import meta tag.
   798  func checkGOVCS(vcs *Cmd, root string) error {
   799  	if vcs == vcsMod {
   800  		// Direct module (proxy protocol) fetches don't
   801  		// involve an external version control system
   802  		// and are always allowed.
   803  		return nil
   804  	}
   805  
   806  	govcsOnce.Do(func() {
   807  		govcs, govcsErr = parseGOVCS(os.Getenv("GOVCS"))
   808  		govcs = append(govcs, defaultGOVCS...)
   809  	})
   810  	if govcsErr != nil {
   811  		return govcsErr
   812  	}
   813  
   814  	private := module.MatchPrefixPatterns(cfg.GOPRIVATE, root)
   815  	if !govcs.allow(root, private, vcs.Cmd) {
   816  		what := "public"
   817  		if private {
   818  			what = "private"
   819  		}
   820  		return fmt.Errorf("GOVCS disallows using %s for %s %s; see 'go help vcs'", vcs.Cmd, what, root)
   821  	}
   822  
   823  	return nil
   824  }
   825  
   826  // RepoRoot describes the repository root for a tree of source code.
   827  type RepoRoot struct {
   828  	Repo     string // repository URL, including scheme
   829  	Root     string // import path corresponding to the SubDir
   830  	SubDir   string // subdirectory within the repo (empty for root)
   831  	IsCustom bool   // defined by served <meta> tags (as opposed to hard-coded pattern)
   832  	VCS      *Cmd
   833  }
   834  
   835  func httpPrefix(s string) string {
   836  	for _, prefix := range [...]string{"http:", "https:"} {
   837  		if strings.HasPrefix(s, prefix) {
   838  			return prefix
   839  		}
   840  	}
   841  	return ""
   842  }
   843  
   844  // ModuleMode specifies whether to prefer modules when looking up code sources.
   845  type ModuleMode int
   846  
   847  const (
   848  	IgnoreMod ModuleMode = iota
   849  	PreferMod
   850  )
   851  
   852  // RepoRootForImportPath analyzes importPath to determine the
   853  // version control system, and code repository to use.
   854  func RepoRootForImportPath(importPath string, mod ModuleMode, security web.SecurityMode) (*RepoRoot, error) {
   855  	rr, err := repoRootFromVCSPaths(importPath, security, vcsPaths)
   856  	if err == errUnknownSite {
   857  		rr, err = repoRootForImportDynamic(importPath, mod, security)
   858  		if err != nil {
   859  			err = importErrorf(importPath, "unrecognized import path %q: %v", importPath, err)
   860  		}
   861  	}
   862  	if err != nil {
   863  		rr1, err1 := repoRootFromVCSPaths(importPath, security, vcsPathsAfterDynamic)
   864  		if err1 == nil {
   865  			rr = rr1
   866  			err = nil
   867  		}
   868  	}
   869  
   870  	// Should have been taken care of above, but make sure.
   871  	if err == nil && strings.Contains(importPath, "...") && strings.Contains(rr.Root, "...") {
   872  		// Do not allow wildcards in the repo root.
   873  		rr = nil
   874  		err = importErrorf(importPath, "cannot expand ... in %q", importPath)
   875  	}
   876  
   877  	// Record telemetry about which VCS was used.
   878  	if err == nil {
   879  		counter.Inc("go/vcs:" + rr.VCS.Name)
   880  	}
   881  
   882  	return rr, err
   883  }
   884  
   885  var errUnknownSite = errors.New("dynamic lookup required to find mapping")
   886  
   887  // repoRootFromVCSPaths attempts to map importPath to a repoRoot
   888  // using the mappings defined in vcsPaths.
   889  func repoRootFromVCSPaths(importPath string, security web.SecurityMode, vcsPaths []*vcsPath) (*RepoRoot, error) {
   890  	if str.HasPathPrefix(importPath, "example.net") {
   891  		// TODO(rsc): This should not be necessary, but it's required to keep
   892  		// tests like ../../testdata/script/mod_get_extra.txt from using the network.
   893  		// That script has everything it needs in the replacement set, but it is still
   894  		// doing network calls.
   895  		return nil, fmt.Errorf("no modules on example.net")
   896  	}
   897  	if importPath == "rsc.io" {
   898  		// This special case allows tests like ../../testdata/script/govcs.txt
   899  		// to avoid making any network calls. The module lookup for a path
   900  		// like rsc.io/nonexist.svn/foo needs to not make a network call for
   901  		// a lookup on rsc.io.
   902  		return nil, fmt.Errorf("rsc.io is not a module")
   903  	}
   904  	// A common error is to use https://packagepath because that's what
   905  	// hg and git require. Diagnose this helpfully.
   906  	if prefix := httpPrefix(importPath); prefix != "" {
   907  		// The importPath has been cleaned, so has only one slash. The pattern
   908  		// ignores the slashes; the error message puts them back on the RHS at least.
   909  		return nil, fmt.Errorf("%q not allowed in import path", prefix+"//")
   910  	}
   911  	for _, srv := range vcsPaths {
   912  		if !str.HasPathPrefix(importPath, srv.pathPrefix) {
   913  			continue
   914  		}
   915  		m := srv.regexp.FindStringSubmatch(importPath)
   916  		if m == nil {
   917  			if srv.pathPrefix != "" {
   918  				return nil, importErrorf(importPath, "invalid %s import path %q", srv.pathPrefix, importPath)
   919  			}
   920  			continue
   921  		}
   922  
   923  		// Build map of named subexpression matches for expand.
   924  		match := map[string]string{
   925  			"prefix": srv.pathPrefix + "/",
   926  			"import": importPath,
   927  		}
   928  		for i, name := range srv.regexp.SubexpNames() {
   929  			if name != "" && match[name] == "" {
   930  				match[name] = m[i]
   931  			}
   932  		}
   933  		if srv.vcs != "" {
   934  			match["vcs"] = expand(match, srv.vcs)
   935  		}
   936  		if srv.repo != "" {
   937  			match["repo"] = expand(match, srv.repo)
   938  		}
   939  		if srv.check != nil {
   940  			if err := srv.check(match); err != nil {
   941  				return nil, err
   942  			}
   943  		}
   944  		vcs := vcsByCmd(match["vcs"])
   945  		if vcs == nil {
   946  			return nil, fmt.Errorf("unknown version control system %q", match["vcs"])
   947  		}
   948  		if err := checkGOVCS(vcs, match["root"]); err != nil {
   949  			return nil, err
   950  		}
   951  		var repoURL string
   952  		if !srv.schemelessRepo {
   953  			repoURL = match["repo"]
   954  		} else {
   955  			repo := match["repo"]
   956  			var ok bool
   957  			repoURL, ok = interceptVCSTest(repo, vcs, security)
   958  			if !ok {
   959  				scheme, err := func() (string, error) {
   960  					for _, s := range vcs.Scheme {
   961  						if security == web.SecureOnly && !vcs.isSecureScheme(s) {
   962  							continue
   963  						}
   964  
   965  						// If we know how to ping URL schemes for this VCS,
   966  						// check that this repo works.
   967  						// Otherwise, default to the first scheme
   968  						// that meets the requested security level.
   969  						if vcs.PingCmd == "" {
   970  							return s, nil
   971  						}
   972  						if err := vcs.Ping(s, repo); err == nil {
   973  							return s, nil
   974  						}
   975  					}
   976  					securityFrag := ""
   977  					if security == web.SecureOnly {
   978  						securityFrag = "secure "
   979  					}
   980  					return "", fmt.Errorf("no %sprotocol found for repository", securityFrag)
   981  				}()
   982  				if err != nil {
   983  					return nil, err
   984  				}
   985  				repoURL = scheme + "://" + repo
   986  			}
   987  		}
   988  		rr := &RepoRoot{
   989  			Repo: repoURL,
   990  			Root: match["root"],
   991  			VCS:  vcs,
   992  		}
   993  		return rr, nil
   994  	}
   995  	return nil, errUnknownSite
   996  }
   997  
   998  func interceptVCSTest(repo string, vcs *Cmd, security web.SecurityMode) (repoURL string, ok bool) {
   999  	if VCSTestRepoURL == "" {
  1000  		return "", false
  1001  	}
  1002  	if vcs == vcsMod {
  1003  		// Since the "mod" protocol is implemented internally,
  1004  		// requests will be intercepted at a lower level (in cmd/go/internal/web).
  1005  		return "", false
  1006  	}
  1007  
  1008  	if scheme, path, ok := strings.Cut(repo, "://"); ok {
  1009  		if security == web.SecureOnly && !vcs.isSecureScheme(scheme) {
  1010  			return "", false // Let the caller reject the original URL.
  1011  		}
  1012  		repo = path // Remove leading URL scheme if present.
  1013  	}
  1014  	for _, host := range VCSTestHosts {
  1015  		if !str.HasPathPrefix(repo, host) {
  1016  			continue
  1017  		}
  1018  
  1019  		httpURL := VCSTestRepoURL + strings.TrimPrefix(repo, host)
  1020  
  1021  		if vcs == vcsSvn {
  1022  			// Ping the vcweb HTTP server to tell it to initialize the SVN repository
  1023  			// and get the SVN server URL.
  1024  			u, err := urlpkg.Parse(httpURL + "?vcwebsvn=1")
  1025  			if err != nil {
  1026  				panic(fmt.Sprintf("invalid vcs-test repo URL: %v", err))
  1027  			}
  1028  			svnURL, err := web.GetBytes(u)
  1029  			svnURL = bytes.TrimSpace(svnURL)
  1030  			if err == nil && len(svnURL) > 0 {
  1031  				return string(svnURL) + strings.TrimPrefix(repo, host), true
  1032  			}
  1033  
  1034  			// vcs-test doesn't have a svn handler for the given path,
  1035  			// so resolve the repo to HTTPS instead.
  1036  		}
  1037  
  1038  		return httpURL, true
  1039  	}
  1040  	return "", false
  1041  }
  1042  
  1043  // urlForImportPath returns a partially-populated URL for the given Go import path.
  1044  //
  1045  // The URL leaves the Scheme field blank so that web.Get will try any scheme
  1046  // allowed by the selected security mode.
  1047  func urlForImportPath(importPath string) (*urlpkg.URL, error) {
  1048  	slash := strings.Index(importPath, "/")
  1049  	if slash < 0 {
  1050  		slash = len(importPath)
  1051  	}
  1052  	host, path := importPath[:slash], importPath[slash:]
  1053  	if !strings.Contains(host, ".") {
  1054  		return nil, errors.New("import path does not begin with hostname")
  1055  	}
  1056  	if len(path) == 0 {
  1057  		path = "/"
  1058  	}
  1059  	return &urlpkg.URL{Host: host, Path: path, RawQuery: "go-get=1"}, nil
  1060  }
  1061  
  1062  // repoRootForImportDynamic finds a *RepoRoot for a custom domain that's not
  1063  // statically known by repoRootFromVCSPaths.
  1064  //
  1065  // This handles custom import paths like "name.tld/pkg/foo" or just "name.tld".
  1066  func repoRootForImportDynamic(importPath string, mod ModuleMode, security web.SecurityMode) (*RepoRoot, error) {
  1067  	url, err := urlForImportPath(importPath)
  1068  	if err != nil {
  1069  		return nil, err
  1070  	}
  1071  	resp, err := web.Get(security, url)
  1072  	if err != nil {
  1073  		msg := "https fetch: %v"
  1074  		if security == web.Insecure {
  1075  			msg = "http/" + msg
  1076  		}
  1077  		return nil, fmt.Errorf(msg, err)
  1078  	}
  1079  	body := resp.Body
  1080  	defer body.Close()
  1081  	imports, err := parseMetaGoImports(body, mod)
  1082  	if len(imports) == 0 {
  1083  		if respErr := resp.Err(); respErr != nil {
  1084  			// If the server's status was not OK, prefer to report that instead of
  1085  			// an XML parse error.
  1086  			return nil, respErr
  1087  		}
  1088  	}
  1089  	if err != nil {
  1090  		return nil, fmt.Errorf("parsing %s: %v", importPath, err)
  1091  	}
  1092  	// Find the matched meta import.
  1093  	mmi, err := matchGoImport(imports, importPath)
  1094  	if err != nil {
  1095  		if _, ok := err.(ImportMismatchError); !ok {
  1096  			return nil, fmt.Errorf("parse %s: %v", url, err)
  1097  		}
  1098  		return nil, fmt.Errorf("parse %s: no go-import meta tags (%s)", resp.URL, err)
  1099  	}
  1100  	if cfg.BuildV {
  1101  		log.Printf("get %q: found meta tag %#v at %s", importPath, mmi, url)
  1102  	}
  1103  	// If the import was "uni.edu/bob/project", which said the
  1104  	// prefix was "uni.edu" and the RepoRoot was "evilroot.com",
  1105  	// make sure we don't trust Bob and check out evilroot.com to
  1106  	// "uni.edu" yet (possibly overwriting/preempting another
  1107  	// non-evil student). Instead, first verify the root and see
  1108  	// if it matches Bob's claim.
  1109  	if mmi.Prefix != importPath {
  1110  		if cfg.BuildV {
  1111  			log.Printf("get %q: verifying non-authoritative meta tag", importPath)
  1112  		}
  1113  		var imports []metaImport
  1114  		url, imports, err = metaImportsForPrefix(mmi.Prefix, mod, security)
  1115  		if err != nil {
  1116  			return nil, err
  1117  		}
  1118  		metaImport2, err := matchGoImport(imports, importPath)
  1119  		if err != nil || mmi != metaImport2 {
  1120  			return nil, fmt.Errorf("%s and %s disagree about go-import for %s", resp.URL, url, mmi.Prefix)
  1121  		}
  1122  	}
  1123  
  1124  	if err := validateRepoSubDir(mmi.SubDir); err != nil {
  1125  		return nil, fmt.Errorf("%s: invalid subdirectory %q: %v", resp.URL, mmi.SubDir, err)
  1126  	}
  1127  
  1128  	if err := validateRepoRoot(mmi.RepoRoot); err != nil {
  1129  		return nil, fmt.Errorf("%s: invalid repo root %q: %v", resp.URL, mmi.RepoRoot, err)
  1130  	}
  1131  	var vcs *Cmd
  1132  	if mmi.VCS == "mod" {
  1133  		vcs = vcsMod
  1134  	} else {
  1135  		vcs = vcsByCmd(mmi.VCS)
  1136  		if vcs == nil {
  1137  			return nil, fmt.Errorf("%s: unknown vcs %q", resp.URL, mmi.VCS)
  1138  		}
  1139  	}
  1140  
  1141  	if err := checkGOVCS(vcs, mmi.Prefix); err != nil {
  1142  		return nil, err
  1143  	}
  1144  
  1145  	repoURL, ok := interceptVCSTest(mmi.RepoRoot, vcs, security)
  1146  	if !ok {
  1147  		repoURL = mmi.RepoRoot
  1148  	}
  1149  	rr := &RepoRoot{
  1150  		Repo:     repoURL,
  1151  		Root:     mmi.Prefix,
  1152  		SubDir:   mmi.SubDir,
  1153  		IsCustom: true,
  1154  		VCS:      vcs,
  1155  	}
  1156  	return rr, nil
  1157  }
  1158  
  1159  // validateRepoSubDir returns an error if subdir is not a valid subdirectory path.
  1160  // We consider a subdirectory path to be valid as long as it doesn't have a leading
  1161  // slash (/) or hyphen (-).
  1162  func validateRepoSubDir(subdir string) error {
  1163  	if subdir == "" {
  1164  		return nil
  1165  	}
  1166  	if subdir[0] == '/' {
  1167  		return errors.New("leading slash")
  1168  	}
  1169  	if subdir[0] == '-' {
  1170  		return errors.New("leading hyphen")
  1171  	}
  1172  	return nil
  1173  }
  1174  
  1175  // validateRepoRoot returns an error if repoRoot does not seem to be
  1176  // a valid URL with scheme.
  1177  func validateRepoRoot(repoRoot string) error {
  1178  	url, err := urlpkg.Parse(repoRoot)
  1179  	if err != nil {
  1180  		return err
  1181  	}
  1182  	if url.Scheme == "" {
  1183  		return errors.New("no scheme")
  1184  	}
  1185  	if url.Scheme == "file" {
  1186  		return errors.New("file scheme disallowed")
  1187  	}
  1188  	return nil
  1189  }
  1190  
  1191  var fetchGroup singleflight.Group
  1192  var (
  1193  	fetchCacheMu sync.Mutex
  1194  	fetchCache   = map[string]fetchResult{} // key is metaImportsForPrefix's importPrefix
  1195  )
  1196  
  1197  // metaImportsForPrefix takes a package's root import path as declared in a <meta> tag
  1198  // and returns its HTML discovery URL and the parsed metaImport lines
  1199  // found on the page.
  1200  //
  1201  // The importPath is of the form "golang.org/x/tools".
  1202  // It is an error if no imports are found.
  1203  // url will still be valid if err != nil.
  1204  // The returned url will be of the form "https://golang.org/x/tools?go-get=1"
  1205  func metaImportsForPrefix(importPrefix string, mod ModuleMode, security web.SecurityMode) (*urlpkg.URL, []metaImport, error) {
  1206  	setCache := func(res fetchResult) (fetchResult, error) {
  1207  		fetchCacheMu.Lock()
  1208  		defer fetchCacheMu.Unlock()
  1209  		fetchCache[importPrefix] = res
  1210  		return res, nil
  1211  	}
  1212  
  1213  	resi, _, _ := fetchGroup.Do(importPrefix, func() (resi any, err error) {
  1214  		fetchCacheMu.Lock()
  1215  		if res, ok := fetchCache[importPrefix]; ok {
  1216  			fetchCacheMu.Unlock()
  1217  			return res, nil
  1218  		}
  1219  		fetchCacheMu.Unlock()
  1220  
  1221  		url, err := urlForImportPath(importPrefix)
  1222  		if err != nil {
  1223  			return setCache(fetchResult{err: err})
  1224  		}
  1225  		resp, err := web.Get(security, url)
  1226  		if err != nil {
  1227  			return setCache(fetchResult{url: url, err: fmt.Errorf("fetching %s: %v", importPrefix, err)})
  1228  		}
  1229  		body := resp.Body
  1230  		defer body.Close()
  1231  		imports, err := parseMetaGoImports(body, mod)
  1232  		if len(imports) == 0 {
  1233  			if respErr := resp.Err(); respErr != nil {
  1234  				// If the server's status was not OK, prefer to report that instead of
  1235  				// an XML parse error.
  1236  				return setCache(fetchResult{url: url, err: respErr})
  1237  			}
  1238  		}
  1239  		if err != nil {
  1240  			return setCache(fetchResult{url: url, err: fmt.Errorf("parsing %s: %v", resp.URL, err)})
  1241  		}
  1242  		if len(imports) == 0 {
  1243  			err = fmt.Errorf("fetching %s: no go-import meta tag found in %s", importPrefix, resp.URL)
  1244  		}
  1245  		return setCache(fetchResult{url: url, imports: imports, err: err})
  1246  	})
  1247  	res := resi.(fetchResult)
  1248  	return res.url, res.imports, res.err
  1249  }
  1250  
  1251  type fetchResult struct {
  1252  	url     *urlpkg.URL
  1253  	imports []metaImport
  1254  	err     error
  1255  }
  1256  
  1257  // metaImport represents the parsed <meta name="go-import"
  1258  // content="prefix vcs reporoot subdir" /> tags from HTML files.
  1259  type metaImport struct {
  1260  	Prefix, VCS, RepoRoot, SubDir string
  1261  }
  1262  
  1263  // An ImportMismatchError is returned where metaImport/s are present
  1264  // but none match our import path.
  1265  type ImportMismatchError struct {
  1266  	importPath string
  1267  	mismatches []string // the meta imports that were discarded for not matching our importPath
  1268  }
  1269  
  1270  func (m ImportMismatchError) Error() string {
  1271  	formattedStrings := make([]string, len(m.mismatches))
  1272  	for i, pre := range m.mismatches {
  1273  		formattedStrings[i] = fmt.Sprintf("meta tag %s did not match import path %s", pre, m.importPath)
  1274  	}
  1275  	return strings.Join(formattedStrings, ", ")
  1276  }
  1277  
  1278  // matchGoImport returns the metaImport from imports matching importPath.
  1279  // An error is returned if there are multiple matches.
  1280  // An ImportMismatchError is returned if none match.
  1281  func matchGoImport(imports []metaImport, importPath string) (metaImport, error) {
  1282  	match := -1
  1283  
  1284  	errImportMismatch := ImportMismatchError{importPath: importPath}
  1285  	for i, im := range imports {
  1286  		if !str.HasPathPrefix(importPath, im.Prefix) {
  1287  			errImportMismatch.mismatches = append(errImportMismatch.mismatches, im.Prefix)
  1288  			continue
  1289  		}
  1290  
  1291  		if match >= 0 {
  1292  			if imports[match].VCS == "mod" && im.VCS != "mod" {
  1293  				// All the mod entries precede all the non-mod entries.
  1294  				// We have a mod entry and don't care about the rest,
  1295  				// matching or not.
  1296  				break
  1297  			}
  1298  			return metaImport{}, fmt.Errorf("multiple meta tags match import path %q", importPath)
  1299  		}
  1300  		match = i
  1301  	}
  1302  
  1303  	if match == -1 {
  1304  		return metaImport{}, errImportMismatch
  1305  	}
  1306  	return imports[match], nil
  1307  }
  1308  
  1309  // expand rewrites s to replace {k} with match[k] for each key k in match.
  1310  func expand(match map[string]string, s string) string {
  1311  	// We want to replace each match exactly once, and the result of expansion
  1312  	// must not depend on the iteration order through the map.
  1313  	// A strings.Replacer has exactly the properties we're looking for.
  1314  	oldNew := make([]string, 0, 2*len(match))
  1315  	for k, v := range match {
  1316  		oldNew = append(oldNew, "{"+k+"}", v)
  1317  	}
  1318  	return strings.NewReplacer(oldNew...).Replace(s)
  1319  }
  1320  
  1321  // vcsPaths defines the meaning of import paths referring to
  1322  // commonly-used VCS hosting sites (github.com/user/dir)
  1323  // and import paths referring to a fully-qualified importPath
  1324  // containing a VCS type (foo.com/repo.git/dir)
  1325  var vcsPaths = []*vcsPath{
  1326  	// GitHub
  1327  	{
  1328  		pathPrefix: "github.com",
  1329  		regexp:     lazyregexp.New(`^(?P<root>github\.com/[\w.\-]+/[\w.\-]+)(/[\w.\-]+)*$`),
  1330  		vcs:        "git",
  1331  		repo:       "https://{root}",
  1332  		check:      noVCSSuffix,
  1333  	},
  1334  
  1335  	// Bitbucket
  1336  	{
  1337  		pathPrefix: "bitbucket.org",
  1338  		regexp:     lazyregexp.New(`^(?P<root>bitbucket\.org/(?P<bitname>[\w.\-]+/[\w.\-]+))(/[\w.\-]+)*$`),
  1339  		vcs:        "git",
  1340  		repo:       "https://{root}",
  1341  		check:      noVCSSuffix,
  1342  	},
  1343  
  1344  	// IBM DevOps Services (JazzHub)
  1345  	{
  1346  		pathPrefix: "hub.jazz.net/git",
  1347  		regexp:     lazyregexp.New(`^(?P<root>hub\.jazz\.net/git/[a-z0-9]+/[\w.\-]+)(/[\w.\-]+)*$`),
  1348  		vcs:        "git",
  1349  		repo:       "https://{root}",
  1350  		check:      noVCSSuffix,
  1351  	},
  1352  
  1353  	// Git at Apache
  1354  	{
  1355  		pathPrefix: "git.apache.org",
  1356  		regexp:     lazyregexp.New(`^(?P<root>git\.apache\.org/[a-z0-9_.\-]+\.git)(/[\w.\-]+)*$`),
  1357  		vcs:        "git",
  1358  		repo:       "https://{root}",
  1359  	},
  1360  
  1361  	// Git at OpenStack
  1362  	{
  1363  		pathPrefix: "git.openstack.org",
  1364  		regexp:     lazyregexp.New(`^(?P<root>git\.openstack\.org/[\w.\-]+/[\w.\-]+)(\.git)?(/[\w.\-]+)*$`),
  1365  		vcs:        "git",
  1366  		repo:       "https://{root}",
  1367  	},
  1368  
  1369  	// chiselapp.com for fossil
  1370  	{
  1371  		pathPrefix: "chiselapp.com",
  1372  		regexp:     lazyregexp.New(`^(?P<root>chiselapp\.com/user/[A-Za-z0-9]+/repository/[\w.\-]+)$`),
  1373  		vcs:        "fossil",
  1374  		repo:       "https://{root}",
  1375  	},
  1376  
  1377  	// General syntax for any server.
  1378  	// Must be last.
  1379  	{
  1380  		regexp:         lazyregexp.New(`(?P<root>(?P<repo>([a-z0-9.\-]+\.)+[a-z0-9.\-]+(:[0-9]+)?(/~?[\w.\-]+)+?)\.(?P<vcs>bzr|fossil|git|hg|svn))(/~?[\w.\-]+)*$`),
  1381  		schemelessRepo: true,
  1382  	},
  1383  }
  1384  
  1385  // vcsPathsAfterDynamic gives additional vcsPaths entries
  1386  // to try after the dynamic HTML check.
  1387  // This gives those sites a chance to introduce <meta> tags
  1388  // as part of a graceful transition away from the hard-coded logic.
  1389  var vcsPathsAfterDynamic = []*vcsPath{
  1390  	// Launchpad. See golang.org/issue/11436.
  1391  	{
  1392  		pathPrefix: "launchpad.net",
  1393  		regexp:     lazyregexp.New(`^(?P<root>launchpad\.net/((?P<project>[\w.\-]+)(?P<series>/[\w.\-]+)?|~[\w.\-]+/(\+junk|[\w.\-]+)/[\w.\-]+))(/[\w.\-]+)*$`),
  1394  		vcs:        "bzr",
  1395  		repo:       "https://{root}",
  1396  		check:      launchpadVCS,
  1397  	},
  1398  }
  1399  
  1400  // noVCSSuffix checks that the repository name does not
  1401  // end in .foo for any version control system foo.
  1402  // The usual culprit is ".git".
  1403  func noVCSSuffix(match map[string]string) error {
  1404  	repo := match["repo"]
  1405  	for _, vcs := range vcsList {
  1406  		if strings.HasSuffix(repo, "."+vcs.Cmd) {
  1407  			return fmt.Errorf("invalid version control suffix in %s path", match["prefix"])
  1408  		}
  1409  	}
  1410  	return nil
  1411  }
  1412  
  1413  // launchpadVCS solves the ambiguity for "lp.net/project/foo". In this case,
  1414  // "foo" could be a series name registered in Launchpad with its own branch,
  1415  // and it could also be the name of a directory within the main project
  1416  // branch one level up.
  1417  func launchpadVCS(match map[string]string) error {
  1418  	if match["project"] == "" || match["series"] == "" {
  1419  		return nil
  1420  	}
  1421  	url := &urlpkg.URL{
  1422  		Scheme: "https",
  1423  		Host:   "code.launchpad.net",
  1424  		Path:   expand(match, "/{project}{series}/.bzr/branch-format"),
  1425  	}
  1426  	_, err := web.GetBytes(url)
  1427  	if err != nil {
  1428  		match["root"] = expand(match, "launchpad.net/{project}")
  1429  		match["repo"] = expand(match, "https://{root}")
  1430  	}
  1431  	return nil
  1432  }
  1433  
  1434  // importError is a copy of load.importError, made to avoid a dependency cycle
  1435  // on cmd/go/internal/load. It just needs to satisfy load.ImportPathError.
  1436  type importError struct {
  1437  	importPath string
  1438  	err        error
  1439  }
  1440  
  1441  func importErrorf(path, format string, args ...any) error {
  1442  	err := &importError{importPath: path, err: fmt.Errorf(format, args...)}
  1443  	if errStr := err.Error(); !strings.Contains(errStr, path) {
  1444  		panic(fmt.Sprintf("path %q not in error %q", path, errStr))
  1445  	}
  1446  	return err
  1447  }
  1448  
  1449  func (e *importError) Error() string {
  1450  	return e.err.Error()
  1451  }
  1452  
  1453  func (e *importError) Unwrap() error {
  1454  	// Don't return e.err directly, since we're only wrapping an error if %w
  1455  	// was passed to ImportErrorf.
  1456  	return errors.Unwrap(e.err)
  1457  }
  1458  
  1459  func (e *importError) ImportPath() string {
  1460  	return e.importPath
  1461  }
  1462  

View as plain text