github.com/xushiwei/go@v0.0.0-20130601165731-2b9d83f45bc9/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  	"log"
    13  	"os"
    14  	"os/exec"
    15  	"path/filepath"
    16  	"regexp"
    17  	"strings"
    18  )
    19  
    20  // A vcsCmd describes how to use a version control system
    21  // like Mercurial, Git, or Subversion.
    22  type vcsCmd struct {
    23  	name string
    24  	cmd  string // name of binary to invoke command
    25  
    26  	createCmd   string // command to download a fresh copy of a repository
    27  	downloadCmd string // command to download updates into an existing repository
    28  
    29  	tagCmd         []tagCmd // commands to list tags
    30  	tagLookupCmd   []tagCmd // commands to lookup tags before running tagSyncCmd
    31  	tagSyncCmd     string   // command to sync to specific tag
    32  	tagSyncDefault string   // command to sync to default tag
    33  
    34  	scheme  []string
    35  	pingCmd string
    36  }
    37  
    38  // A tagCmd describes a command to list available tags
    39  // that can be passed to tagSyncCmd.
    40  type tagCmd struct {
    41  	cmd     string // command to list tags
    42  	pattern string // regexp to extract tags from list
    43  }
    44  
    45  // vcsList lists the known version control systems
    46  var vcsList = []*vcsCmd{
    47  	vcsHg,
    48  	vcsGit,
    49  	vcsSvn,
    50  	vcsBzr,
    51  }
    52  
    53  // vcsByCmd returns the version control system for the given
    54  // command name (hg, git, svn, bzr).
    55  func vcsByCmd(cmd string) *vcsCmd {
    56  	for _, vcs := range vcsList {
    57  		if vcs.cmd == cmd {
    58  			return vcs
    59  		}
    60  	}
    61  	return nil
    62  }
    63  
    64  // vcsHg describes how to use Mercurial.
    65  var vcsHg = &vcsCmd{
    66  	name: "Mercurial",
    67  	cmd:  "hg",
    68  
    69  	createCmd:   "clone -U {repo} {dir}",
    70  	downloadCmd: "pull",
    71  
    72  	// We allow both tag and branch names as 'tags'
    73  	// for selecting a version.  This lets people have
    74  	// a go.release.r60 branch and a go1 branch
    75  	// and make changes in both, without constantly
    76  	// editing .hgtags.
    77  	tagCmd: []tagCmd{
    78  		{"tags", `^(\S+)`},
    79  		{"branches", `^(\S+)`},
    80  	},
    81  	tagSyncCmd:     "update -r {tag}",
    82  	tagSyncDefault: "update default",
    83  
    84  	scheme:  []string{"https", "http", "ssh"},
    85  	pingCmd: "identify {scheme}://{repo}",
    86  }
    87  
    88  // vcsGit describes how to use Git.
    89  var vcsGit = &vcsCmd{
    90  	name: "Git",
    91  	cmd:  "git",
    92  
    93  	createCmd:   "clone {repo} {dir}",
    94  	downloadCmd: "fetch",
    95  
    96  	tagCmd: []tagCmd{
    97  		// tags/xxx matches a git tag named xxx
    98  		// origin/xxx matches a git branch named xxx on the default remote repository
    99  		{"show-ref", `(?:tags|origin)/(\S+)$`},
   100  	},
   101  	tagLookupCmd: []tagCmd{
   102  		{"show-ref tags/{tag} origin/{tag}", `((?:tags|origin)/\S+)$`},
   103  	},
   104  	tagSyncCmd:     "checkout {tag}",
   105  	tagSyncDefault: "checkout origin/master",
   106  
   107  	scheme:  []string{"git", "https", "http", "git+ssh"},
   108  	pingCmd: "ls-remote {scheme}://{repo}",
   109  }
   110  
   111  // vcsBzr describes how to use Bazaar.
   112  var vcsBzr = &vcsCmd{
   113  	name: "Bazaar",
   114  	cmd:  "bzr",
   115  
   116  	createCmd: "branch {repo} {dir}",
   117  
   118  	// Without --overwrite bzr will not pull tags that changed.
   119  	// Replace by --overwrite-tags after http://pad.lv/681792 goes in.
   120  	downloadCmd: "pull --overwrite",
   121  
   122  	tagCmd:         []tagCmd{{"tags", `^(\S+)`}},
   123  	tagSyncCmd:     "update -r {tag}",
   124  	tagSyncDefault: "update -r revno:-1",
   125  
   126  	scheme:  []string{"https", "http", "bzr", "bzr+ssh"},
   127  	pingCmd: "info {scheme}://{repo}",
   128  }
   129  
   130  // vcsSvn describes how to use Subversion.
   131  var vcsSvn = &vcsCmd{
   132  	name: "Subversion",
   133  	cmd:  "svn",
   134  
   135  	createCmd:   "checkout {repo} {dir}",
   136  	downloadCmd: "update",
   137  
   138  	// There is no tag command in subversion.
   139  	// The branch information is all in the path names.
   140  
   141  	scheme:  []string{"https", "http", "svn", "svn+ssh"},
   142  	pingCmd: "info {scheme}://{repo}",
   143  }
   144  
   145  func (v *vcsCmd) String() string {
   146  	return v.name
   147  }
   148  
   149  // run runs the command line cmd in the given directory.
   150  // keyval is a list of key, value pairs.  run expands
   151  // instances of {key} in cmd into value, but only after
   152  // splitting cmd into individual arguments.
   153  // If an error occurs, run prints the command line and the
   154  // command's combined stdout+stderr to standard error.
   155  // Otherwise run discards the command's output.
   156  func (v *vcsCmd) run(dir string, cmd string, keyval ...string) error {
   157  	_, err := v.run1(dir, cmd, keyval, true)
   158  	return err
   159  }
   160  
   161  // runVerboseOnly is like run but only generates error output to standard error in verbose mode.
   162  func (v *vcsCmd) runVerboseOnly(dir string, cmd string, keyval ...string) error {
   163  	_, err := v.run1(dir, cmd, keyval, false)
   164  	return err
   165  }
   166  
   167  // runOutput is like run but returns the output of the command.
   168  func (v *vcsCmd) runOutput(dir string, cmd string, keyval ...string) ([]byte, error) {
   169  	return v.run1(dir, cmd, keyval, true)
   170  }
   171  
   172  // run1 is the generalized implementation of run and runOutput.
   173  func (v *vcsCmd) run1(dir string, cmdline string, keyval []string, verbose bool) ([]byte, error) {
   174  	m := make(map[string]string)
   175  	for i := 0; i < len(keyval); i += 2 {
   176  		m[keyval[i]] = keyval[i+1]
   177  	}
   178  	args := strings.Fields(cmdline)
   179  	for i, arg := range args {
   180  		args[i] = expand(m, arg)
   181  	}
   182  
   183  	_, err := exec.LookPath(v.cmd)
   184  	if err != nil {
   185  		fmt.Fprintf(os.Stderr,
   186  			"go: missing %s command. See http://golang.org/s/gogetcmd\n",
   187  			v.name)
   188  		return nil, err
   189  	}
   190  
   191  	cmd := exec.Command(v.cmd, args...)
   192  	cmd.Dir = dir
   193  	cmd.Env = envForDir(cmd.Dir)
   194  	if buildX {
   195  		fmt.Printf("cd %s\n", dir)
   196  		fmt.Printf("%s %s\n", v.cmd, strings.Join(args, " "))
   197  	}
   198  	var buf bytes.Buffer
   199  	cmd.Stdout = &buf
   200  	cmd.Stderr = &buf
   201  	err = cmd.Run()
   202  	out := buf.Bytes()
   203  	if err != nil {
   204  		if verbose || buildV {
   205  			fmt.Fprintf(os.Stderr, "# cd %s; %s %s\n", dir, v.cmd, strings.Join(args, " "))
   206  			os.Stderr.Write(out)
   207  		}
   208  		return nil, err
   209  	}
   210  	return out, nil
   211  }
   212  
   213  // ping pings to determine scheme to use.
   214  func (v *vcsCmd) ping(scheme, repo string) error {
   215  	return v.runVerboseOnly(".", v.pingCmd, "scheme", scheme, "repo", repo)
   216  }
   217  
   218  // create creates a new copy of repo in dir.
   219  // The parent of dir must exist; dir must not.
   220  func (v *vcsCmd) create(dir, repo string) error {
   221  	return v.run(".", v.createCmd, "dir", dir, "repo", repo)
   222  }
   223  
   224  // download downloads any new changes for the repo in dir.
   225  func (v *vcsCmd) download(dir string) error {
   226  	return v.run(dir, v.downloadCmd)
   227  }
   228  
   229  // tags returns the list of available tags for the repo in dir.
   230  func (v *vcsCmd) tags(dir string) ([]string, error) {
   231  	var tags []string
   232  	for _, tc := range v.tagCmd {
   233  		out, err := v.runOutput(dir, tc.cmd)
   234  		if err != nil {
   235  			return nil, err
   236  		}
   237  		re := regexp.MustCompile(`(?m-s)` + tc.pattern)
   238  		for _, m := range re.FindAllStringSubmatch(string(out), -1) {
   239  			tags = append(tags, m[1])
   240  		}
   241  	}
   242  	return tags, nil
   243  }
   244  
   245  // tagSync syncs the repo in dir to the named tag,
   246  // which either is a tag returned by tags or is v.tagDefault.
   247  func (v *vcsCmd) tagSync(dir, tag string) error {
   248  	if v.tagSyncCmd == "" {
   249  		return nil
   250  	}
   251  	if tag != "" {
   252  		for _, tc := range v.tagLookupCmd {
   253  			out, err := v.runOutput(dir, tc.cmd, "tag", tag)
   254  			if err != nil {
   255  				return err
   256  			}
   257  			re := regexp.MustCompile(`(?m-s)` + tc.pattern)
   258  			m := re.FindStringSubmatch(string(out))
   259  			if len(m) > 1 {
   260  				tag = m[1]
   261  				break
   262  			}
   263  		}
   264  	}
   265  	if tag == "" && v.tagSyncDefault != "" {
   266  		return v.run(dir, v.tagSyncDefault)
   267  	}
   268  	return v.run(dir, v.tagSyncCmd, "tag", tag)
   269  }
   270  
   271  // A vcsPath describes how to convert an import path into a
   272  // version control system and repository name.
   273  type vcsPath struct {
   274  	prefix string                              // prefix this description applies to
   275  	re     string                              // pattern for import path
   276  	repo   string                              // repository to use (expand with match of re)
   277  	vcs    string                              // version control system to use (expand with match of re)
   278  	check  func(match map[string]string) error // additional checks
   279  	ping   bool                                // ping for scheme to use to download repo
   280  
   281  	regexp *regexp.Regexp // cached compiled form of re
   282  }
   283  
   284  // vcsForDir inspects dir and its parents to determine the
   285  // version control system and code repository to use.
   286  // On return, root is the import path
   287  // corresponding to the root of the repository
   288  // (thus root is a prefix of importPath).
   289  func vcsForDir(p *Package) (vcs *vcsCmd, root string, err error) {
   290  	// Clean and double-check that dir is in (a subdirectory of) srcRoot.
   291  	dir := filepath.Clean(p.Dir)
   292  	srcRoot := filepath.Clean(p.build.SrcRoot)
   293  	if len(dir) <= len(srcRoot) || dir[len(srcRoot)] != filepath.Separator {
   294  		return nil, "", fmt.Errorf("directory %q is outside source root %q", dir, srcRoot)
   295  	}
   296  
   297  	for len(dir) > len(srcRoot) {
   298  		for _, vcs := range vcsList {
   299  			if fi, err := os.Stat(filepath.Join(dir, "."+vcs.cmd)); err == nil && fi.IsDir() {
   300  				return vcs, dir[len(srcRoot)+1:], nil
   301  			}
   302  		}
   303  
   304  		// Move to parent.
   305  		ndir := filepath.Dir(dir)
   306  		if len(ndir) >= len(dir) {
   307  			// Shouldn't happen, but just in case, stop.
   308  			break
   309  		}
   310  		dir = ndir
   311  	}
   312  
   313  	return nil, "", fmt.Errorf("directory %q is not using a known version control system", dir)
   314  }
   315  
   316  // repoRoot represents a version control system, a repo, and a root of
   317  // where to put it on disk.
   318  type repoRoot struct {
   319  	vcs *vcsCmd
   320  
   321  	// repo is the repository URL, including scheme
   322  	repo string
   323  
   324  	// root is the import path corresponding to the root of the
   325  	// repository
   326  	root string
   327  }
   328  
   329  // repoRootForImportPath analyzes importPath to determine the
   330  // version control system, and code repository to use.
   331  func repoRootForImportPath(importPath string) (*repoRoot, error) {
   332  	rr, err := repoRootForImportPathStatic(importPath, "")
   333  	if err == errUnknownSite {
   334  		rr, err = repoRootForImportDynamic(importPath)
   335  
   336  		// repoRootForImportDynamic returns error detail
   337  		// that is irrelevant if the user didn't intend to use a
   338  		// dynamic import in the first place.
   339  		// Squelch it.
   340  		if err != nil {
   341  			if buildV {
   342  				log.Printf("import %q: %v", importPath, err)
   343  			}
   344  			err = fmt.Errorf("unrecognized import path %q", importPath)
   345  		}
   346  	}
   347  
   348  	if err == nil && strings.Contains(importPath, "...") && strings.Contains(rr.root, "...") {
   349  		// Do not allow wildcards in the repo root.
   350  		rr = nil
   351  		err = fmt.Errorf("cannot expand ... in %q", importPath)
   352  	}
   353  	return rr, err
   354  }
   355  
   356  var errUnknownSite = errors.New("dynamic lookup required to find mapping")
   357  
   358  // repoRootForImportPathStatic attempts to map importPath to a
   359  // repoRoot using the commonly-used VCS hosting sites in vcsPaths
   360  // (github.com/user/dir), or from a fully-qualified importPath already
   361  // containing its VCS type (foo.com/repo.git/dir)
   362  //
   363  // If scheme is non-empty, that scheme is forced.
   364  func repoRootForImportPathStatic(importPath, scheme string) (*repoRoot, error) {
   365  	if strings.Contains(importPath, "://") {
   366  		return nil, fmt.Errorf("invalid import path %q", importPath)
   367  	}
   368  	for _, srv := range vcsPaths {
   369  		if !strings.HasPrefix(importPath, srv.prefix) {
   370  			continue
   371  		}
   372  		m := srv.regexp.FindStringSubmatch(importPath)
   373  		if m == nil {
   374  			if srv.prefix != "" {
   375  				return nil, fmt.Errorf("invalid %s import path %q", srv.prefix, importPath)
   376  			}
   377  			continue
   378  		}
   379  
   380  		// Build map of named subexpression matches for expand.
   381  		match := map[string]string{
   382  			"prefix": srv.prefix,
   383  			"import": importPath,
   384  		}
   385  		for i, name := range srv.regexp.SubexpNames() {
   386  			if name != "" && match[name] == "" {
   387  				match[name] = m[i]
   388  			}
   389  		}
   390  		if srv.vcs != "" {
   391  			match["vcs"] = expand(match, srv.vcs)
   392  		}
   393  		if srv.repo != "" {
   394  			match["repo"] = expand(match, srv.repo)
   395  		}
   396  		if srv.check != nil {
   397  			if err := srv.check(match); err != nil {
   398  				return nil, err
   399  			}
   400  		}
   401  		vcs := vcsByCmd(match["vcs"])
   402  		if vcs == nil {
   403  			return nil, fmt.Errorf("unknown version control system %q", match["vcs"])
   404  		}
   405  		if srv.ping {
   406  			if scheme != "" {
   407  				match["repo"] = scheme + "://" + match["repo"]
   408  			} else {
   409  				for _, scheme := range vcs.scheme {
   410  					if vcs.ping(scheme, match["repo"]) == nil {
   411  						match["repo"] = scheme + "://" + match["repo"]
   412  						break
   413  					}
   414  				}
   415  			}
   416  		}
   417  		rr := &repoRoot{
   418  			vcs:  vcs,
   419  			repo: match["repo"],
   420  			root: match["root"],
   421  		}
   422  		return rr, nil
   423  	}
   424  	return nil, errUnknownSite
   425  }
   426  
   427  // repoRootForImportDynamic finds a *repoRoot for a custom domain that's not
   428  // statically known by repoRootForImportPathStatic.
   429  //
   430  // This handles "vanity import paths" like "name.tld/pkg/foo".
   431  func repoRootForImportDynamic(importPath string) (*repoRoot, error) {
   432  	slash := strings.Index(importPath, "/")
   433  	if slash < 0 {
   434  		return nil, errors.New("import path doesn't contain a slash")
   435  	}
   436  	host := importPath[:slash]
   437  	if !strings.Contains(host, ".") {
   438  		return nil, errors.New("import path doesn't contain a hostname")
   439  	}
   440  	urlStr, body, err := httpsOrHTTP(importPath)
   441  	if err != nil {
   442  		return nil, fmt.Errorf("http/https fetch: %v", err)
   443  	}
   444  	defer body.Close()
   445  	metaImport, err := matchGoImport(parseMetaGoImports(body), importPath)
   446  	if err != nil {
   447  		if err != errNoMatch {
   448  			return nil, fmt.Errorf("parse %s: %v", urlStr, err)
   449  		}
   450  		return nil, fmt.Errorf("parse %s: no go-import meta tags", urlStr)
   451  	}
   452  	if buildV {
   453  		log.Printf("get %q: found meta tag %#v at %s", importPath, metaImport, urlStr)
   454  	}
   455  	// If the import was "uni.edu/bob/project", which said the
   456  	// prefix was "uni.edu" and the RepoRoot was "evilroot.com",
   457  	// make sure we don't trust Bob and check out evilroot.com to
   458  	// "uni.edu" yet (possibly overwriting/preempting another
   459  	// non-evil student).  Instead, first verify the root and see
   460  	// if it matches Bob's claim.
   461  	if metaImport.Prefix != importPath {
   462  		if buildV {
   463  			log.Printf("get %q: verifying non-authoritative meta tag", importPath)
   464  		}
   465  		urlStr0 := urlStr
   466  		urlStr, body, err = httpsOrHTTP(metaImport.Prefix)
   467  		if err != nil {
   468  			return nil, fmt.Errorf("fetch %s: %v", urlStr, err)
   469  		}
   470  		imports := parseMetaGoImports(body)
   471  		if len(imports) == 0 {
   472  			return nil, fmt.Errorf("fetch %s: no go-import meta tag", urlStr)
   473  		}
   474  		metaImport2, err := matchGoImport(imports, importPath)
   475  		if err != nil || metaImport != metaImport2 {
   476  			return nil, fmt.Errorf("%s and %s disagree about go-import for %s", urlStr0, urlStr, metaImport.Prefix)
   477  		}
   478  	}
   479  
   480  	if !strings.Contains(metaImport.RepoRoot, "://") {
   481  		return nil, fmt.Errorf("%s: invalid repo root %q; no scheme", urlStr, metaImport.RepoRoot)
   482  	}
   483  	rr := &repoRoot{
   484  		vcs:  vcsByCmd(metaImport.VCS),
   485  		repo: metaImport.RepoRoot,
   486  		root: metaImport.Prefix,
   487  	}
   488  	if rr.vcs == nil {
   489  		return nil, fmt.Errorf("%s: unknown vcs %q", urlStr, metaImport.VCS)
   490  	}
   491  	return rr, nil
   492  }
   493  
   494  // metaImport represents the parsed <meta name="go-import"
   495  // content="prefix vcs reporoot" /> tags from HTML files.
   496  type metaImport struct {
   497  	Prefix, VCS, RepoRoot string
   498  }
   499  
   500  // errNoMatch is returned from matchGoImport when there's no applicable match.
   501  var errNoMatch = errors.New("no import match")
   502  
   503  // matchGoImport returns the metaImport from imports matching importPath.
   504  // An error is returned if there are multiple matches.
   505  // errNoMatch is returned if none match.
   506  func matchGoImport(imports []metaImport, importPath string) (_ metaImport, err error) {
   507  	match := -1
   508  	for i, im := range imports {
   509  		if !strings.HasPrefix(importPath, im.Prefix) {
   510  			continue
   511  		}
   512  		if match != -1 {
   513  			err = fmt.Errorf("multiple meta tags match import path %q", importPath)
   514  			return
   515  		}
   516  		match = i
   517  	}
   518  	if match == -1 {
   519  		err = errNoMatch
   520  		return
   521  	}
   522  	return imports[match], nil
   523  }
   524  
   525  // expand rewrites s to replace {k} with match[k] for each key k in match.
   526  func expand(match map[string]string, s string) string {
   527  	for k, v := range match {
   528  		s = strings.Replace(s, "{"+k+"}", v, -1)
   529  	}
   530  	return s
   531  }
   532  
   533  // vcsPaths lists the known vcs paths.
   534  var vcsPaths = []*vcsPath{
   535  	// Google Code - new syntax
   536  	{
   537  		prefix: "code.google.com/",
   538  		re:     `^(?P<root>code\.google\.com/p/(?P<project>[a-z0-9\-]+)(\.(?P<subrepo>[a-z0-9\-]+))?)(/[A-Za-z0-9_.\-]+)*$`,
   539  		repo:   "https://{root}",
   540  		check:  googleCodeVCS,
   541  	},
   542  
   543  	// Google Code - old syntax
   544  	{
   545  		re:    `^(?P<project>[a-z0-9_\-.]+)\.googlecode\.com/(git|hg|svn)(?P<path>/.*)?$`,
   546  		check: oldGoogleCode,
   547  	},
   548  
   549  	// Github
   550  	{
   551  		prefix: "github.com/",
   552  		re:     `^(?P<root>github\.com/[A-Za-z0-9_.\-]+/[A-Za-z0-9_.\-]+)(/[A-Za-z0-9_.\-]+)*$`,
   553  		vcs:    "git",
   554  		repo:   "https://{root}",
   555  		check:  noVCSSuffix,
   556  	},
   557  
   558  	// Bitbucket
   559  	{
   560  		prefix: "bitbucket.org/",
   561  		re:     `^(?P<root>bitbucket\.org/(?P<bitname>[A-Za-z0-9_.\-]+/[A-Za-z0-9_.\-]+))(/[A-Za-z0-9_.\-]+)*$`,
   562  		repo:   "https://{root}",
   563  		check:  bitbucketVCS,
   564  	},
   565  
   566  	// Launchpad
   567  	{
   568  		prefix: "launchpad.net/",
   569  		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_.\-]+)*$`,
   570  		vcs:    "bzr",
   571  		repo:   "https://{root}",
   572  		check:  launchpadVCS,
   573  	},
   574  
   575  	// General syntax for any server.
   576  	{
   577  		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_.\-]+)*$`,
   578  		ping: true,
   579  	},
   580  }
   581  
   582  func init() {
   583  	// fill in cached regexps.
   584  	// Doing this eagerly discovers invalid regexp syntax
   585  	// without having to run a command that needs that regexp.
   586  	for _, srv := range vcsPaths {
   587  		srv.regexp = regexp.MustCompile(srv.re)
   588  	}
   589  }
   590  
   591  // noVCSSuffix checks that the repository name does not
   592  // end in .foo for any version control system foo.
   593  // The usual culprit is ".git".
   594  func noVCSSuffix(match map[string]string) error {
   595  	repo := match["repo"]
   596  	for _, vcs := range vcsList {
   597  		if strings.HasSuffix(repo, "."+vcs.cmd) {
   598  			return fmt.Errorf("invalid version control suffix in %s path", match["prefix"])
   599  		}
   600  	}
   601  	return nil
   602  }
   603  
   604  var googleCheckout = regexp.MustCompile(`id="checkoutcmd">(hg|git|svn)`)
   605  
   606  // googleCodeVCS determines the version control system for
   607  // a code.google.com repository, by scraping the project's
   608  // /source/checkout page.
   609  func googleCodeVCS(match map[string]string) error {
   610  	if err := noVCSSuffix(match); err != nil {
   611  		return err
   612  	}
   613  	data, err := httpGET(expand(match, "https://code.google.com/p/{project}/source/checkout?repo={subrepo}"))
   614  	if err != nil {
   615  		return err
   616  	}
   617  
   618  	if m := googleCheckout.FindSubmatch(data); m != nil {
   619  		if vcs := vcsByCmd(string(m[1])); vcs != nil {
   620  			// Subversion requires the old URLs.
   621  			// TODO: Test.
   622  			if vcs == vcsSvn {
   623  				if match["subrepo"] != "" {
   624  					return fmt.Errorf("sub-repositories not supported in Google Code Subversion projects")
   625  				}
   626  				match["repo"] = expand(match, "https://{project}.googlecode.com/svn")
   627  			}
   628  			match["vcs"] = vcs.cmd
   629  			return nil
   630  		}
   631  	}
   632  
   633  	return fmt.Errorf("unable to detect version control system for code.google.com/ path")
   634  }
   635  
   636  // oldGoogleCode is invoked for old-style foo.googlecode.com paths.
   637  // It prints an error giving the equivalent new path.
   638  func oldGoogleCode(match map[string]string) error {
   639  	return fmt.Errorf("invalid Google Code import path: use %s instead",
   640  		expand(match, "code.google.com/p/{project}{path}"))
   641  }
   642  
   643  // bitbucketVCS determines the version control system for a
   644  // Bitbucket repository, by using the Bitbucket API.
   645  func bitbucketVCS(match map[string]string) error {
   646  	if err := noVCSSuffix(match); err != nil {
   647  		return err
   648  	}
   649  
   650  	var resp struct {
   651  		SCM string `json:"scm"`
   652  	}
   653  	url := expand(match, "https://api.bitbucket.org/1.0/repositories/{bitname}")
   654  	data, err := httpGET(url)
   655  	if err != nil {
   656  		return err
   657  	}
   658  	if err := json.Unmarshal(data, &resp); err != nil {
   659  		return fmt.Errorf("decoding %s: %v", url, err)
   660  	}
   661  
   662  	if vcsByCmd(resp.SCM) != nil {
   663  		match["vcs"] = resp.SCM
   664  		if resp.SCM == "git" {
   665  			match["repo"] += ".git"
   666  		}
   667  		return nil
   668  	}
   669  
   670  	return fmt.Errorf("unable to detect version control system for bitbucket.org/ path")
   671  }
   672  
   673  // launchpadVCS solves the ambiguity for "lp.net/project/foo". In this case,
   674  // "foo" could be a series name registered in Launchpad with its own branch,
   675  // and it could also be the name of a directory within the main project
   676  // branch one level up.
   677  func launchpadVCS(match map[string]string) error {
   678  	if match["project"] == "" || match["series"] == "" {
   679  		return nil
   680  	}
   681  	_, err := httpGET(expand(match, "https://code.launchpad.net/{project}{series}/.bzr/branch-format"))
   682  	if err != nil {
   683  		match["root"] = expand(match, "launchpad.net/{project}")
   684  		match["repo"] = expand(match, "https://{root}")
   685  	}
   686  	return nil
   687  }