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